-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding basic authentication to cid-verifier (#128)
- [x] Set secret in wrangler for cid-verifier - [x] Set secret in wrangler for edge-gateway Closes #66
- Loading branch information
Showing
12 changed files
with
151 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { NoTokenError, ExpectedBasicStringError, NoValidTokenError } from './errors.js' | ||
|
||
/** | ||
* Middleware: verify the request is authenticated using Basic Authentication. | ||
* | ||
* @param {import('itty-router').RouteHandler<Request>} handler | ||
*/ | ||
export function withAuthToken (handler) { | ||
/** | ||
* @param {Request} request | ||
* @param {import('./env').Env} env | ||
* @returns {Promise<Response>} | ||
*/ | ||
return async (request, env) => { | ||
const token = getTokenFromRequest(request) | ||
if (env.BASIC_AUTH_TOKENS.indexOf(token) === -1) { | ||
throw new NoValidTokenError() | ||
} | ||
return await handler(request, env) | ||
} | ||
} | ||
|
||
/** | ||
* @param {Request} request | ||
*/ | ||
function getTokenFromRequest (request) { | ||
const authHeader = request.headers.get('Authorization') || '' | ||
if (!authHeader) { | ||
throw new NoTokenError() | ||
} | ||
|
||
const token = parseAuthorizationHeader(authHeader) | ||
if (!token) { | ||
throw new NoTokenError() | ||
} | ||
return token | ||
} | ||
|
||
/** | ||
* @param {string} header | ||
*/ | ||
function parseAuthorizationHeader (header) { | ||
if (!header.toLowerCase().startsWith('basic ')) { | ||
throw new ExpectedBasicStringError() | ||
} | ||
|
||
return header.substring(6) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { test, getMiniflare } from './utils/setup.js' | ||
|
||
test.before(async (t) => { | ||
const mf = getMiniflare() | ||
t.context = { | ||
mf | ||
} | ||
}) | ||
|
||
test('GET /denylist fails with no Authorization header', async (t) => { | ||
const { mf } = t.context | ||
const response = await mf.dispatchFetch('http://localhost:8787/denylist?cid=never') | ||
t.is(response.status, 401) | ||
}) | ||
|
||
test('GET /denylist fails with invalid Authorization header', async (t) => { | ||
const { mf } = t.context | ||
const response = await mf.dispatchFetch('http://localhost:8787/denylist?cid=never', { headers: { Authorization: '' } }) | ||
t.is(response.status, 401) | ||
}) | ||
|
||
test('GET /denylist fails with unknown Authorization credentials', async (t) => { | ||
const { mf } = t.context | ||
const response = await mf.dispatchFetch('http://localhost:8787/denylist?cid=never', { headers: { Authorization: 'basic asdfasdf' } }) | ||
t.is(response.status, 401) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters