Skip to content

Commit

Permalink
Fix: Basic Auth & Batch Push
Browse files Browse the repository at this point in the history
  • Loading branch information
cwxia0s committed Jan 2, 2025
1 parent 552d242 commit 21e2e18
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
14 changes: 11 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ async function handleRequest(request, env, ctx) {
}
}
if (requestBody.device_keys && typeof requestBody.device_keys === 'string') {
requestBody.device_keys = requestBody.device_keys.split(',').map(item => item.trim())
if (requestBody.device_keys.startsWith('[') || requestBody.device_keys.endsWith(']')) {
requestBody.device_keys = JSON.parse(requestBody.device_keys)
} else {
requestBody.device_keys = (decodeURIComponent(requestBody.device_keys).trim()).split(',').map(item => item.replace(/"/g, '').trim())
}

if (typeof requestBody.device_keys === 'string') {
requestBody.device_keys = [requestBody.device_keys]
}
}
} catch (error) {
return new Response(JSON.stringify({
Expand Down Expand Up @@ -164,9 +172,9 @@ async function handleRequest(request, env, ctx) {
class Handler {
constructor(env) {
this.version = "v2.1.5"
this.build = "2024-12-24 20:46:57"
this.build = "2025-01-01 18:22:43"
this.arch = "js"
this.commit = "157609b4732361a55f3bb1bb6eb7d5ac31d2a583"
this.commit = "552d242c385b24171efd7135824686e90b45bd27"

const db = new Database(env)

Expand Down
33 changes: 21 additions & 12 deletions main_kv.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ async function handleRequest(request, env, ctx) {
}
case "/info": {
if (!util.validateBasicAuth(request)) {
return new Response('I\'m a teapot', {
status: 418,
headers: {
'content-type': 'text/plain',
}
})
return new Response('Unauthorized', {
status: 401,
headers: {
'content-type': 'text/plain',
'WWW-Authenticate': 'Basic',
}
})
}

return handler.info(searchParams)
Expand All @@ -45,10 +46,11 @@ async function handleRequest(request, env, ctx) {

if (pathParts[1]) {
if (!util.validateBasicAuth(request)) {
return new Response('I\'m a teapot', {
status: 418,
return new Response('Unauthorized', {
status: 401,
headers: {
'content-type': 'text/plain',
'WWW-Authenticate': 'Basic',
}
})
}
Expand Down Expand Up @@ -77,7 +79,15 @@ async function handleRequest(request, env, ctx) {
}
}
if (requestBody.device_keys && typeof requestBody.device_keys === 'string') {
requestBody.device_keys = requestBody.device_keys.split(',').map(item => item.trim())
if (requestBody.device_keys.startsWith('[') || requestBody.device_keys.endsWith(']')) {
requestBody.device_keys = JSON.parse(requestBody.device_keys)
} else {
requestBody.device_keys = (decodeURIComponent(requestBody.device_keys).trim()).split(',').map(item => item.replace(/"/g, '').trim())
}

if (typeof requestBody.device_keys === 'string') {
requestBody.device_keys = [requestBody.device_keys]
}
}
} catch (error) {
return new Response(JSON.stringify({
Expand Down Expand Up @@ -162,9 +172,9 @@ async function handleRequest(request, env, ctx) {
class Handler {
constructor(env) {
this.version = "v2.1.5"
this.build = "2024-12-24 20:46:57"
this.build = "2025-01-01 18:22:43"
this.arch = "js"
this.commit = "157609b4732361a55f3bb1bb6eb7d5ac31d2a583"
this.commit = "552d242c385b24171efd7135824686e90b45bd27"

const db = new Database(env)

Expand Down Expand Up @@ -503,7 +513,6 @@ class Database {
}
}


/**
* Class Util
*/
Expand Down
9 changes: 9 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ curl -X "POST" "$SERVER_ADDRESS/push" \
"device_keys": "'$BATCH_PUSH_KEY_1','$BATCH_PUSH_KEY_2','$BATCH_PUSH_KEY_3'"
}'

echo ""

curl -X "POST" "$SERVER_ADDRESS/push/Body?device_keys=$BATCH_PUSH_KEY_1,$BATCH_PUSH_KEY_2,$BATCH_PUSH_KEY_3"

echo ""

#"$SERVER_ADDRESS/push/Body?device_keys=["$BATCH_PUSH_KEY_1", "$BATCH_PUSH_KEY_2", "$BATCH_PUSH_KEY_3"]"
curl -X "POST" "$SERVER_ADDRESS/push/Body?device_keys=%5B%22$BATCH_PUSH_KEY_1%22%2C%20%22$BATCH_PUSH_KEY_2%22%2C%20%22$BATCH_PUSH_KEY_3%22%5D"

echo -e "\e[1;32m"
echo ""
echo "---------------------------------------------------------------------"
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "bark-worker"
main = "main.js"
compatibility_date = "2024-12-01"
compatibility_date = "2025-01-01"

[[d1_databases]]
binding = "database" # available in your Worker on env.database
Expand Down

0 comments on commit 21e2e18

Please sign in to comment.