Skip to content

Commit

Permalink
Add AuthBearerMiddleware::getAuthInfo method
Browse files Browse the repository at this point in the history
  • Loading branch information
modscleo4 committed May 21, 2024
1 parent e0faee1 commit 69cbd5a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@modscleo4/midori",
"version": "0.2.0",
"version": "0.2.1",
"description": "Midori is a Node.js web API framework with minimal dependencies and based on PSR ideas.",
"type": "module",
"keywords": [
Expand Down Expand Up @@ -101,7 +101,7 @@
},
"devDependencies": {
"@types/mime-types": "^2.1.4",
"@types/node": "^20.12.7",
"@types/node": "^20.12.12",
"typescript": "^5.4.5"
},
"dependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/http/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ export default class Response<T = any> {
} else {
this.#headers.set('Set-Cookie', [setCookie as string, cookie]);
}
} else {
this.#headers.set('Set-Cookie', [cookie]);
}

return this;
Expand Down
30 changes: 27 additions & 3 deletions src/middlewares/AuthBearerMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ export default class AuthBearerMiddleware extends Middleware {
}

override async process(req: Request, next: (req: Request) => Promise<Response>): Promise<Response> {
if (!req.headers['authorization']) {
return Response.problem('Invalid Authorization header.', 'No Authorization header provided.', EStatusCode.UNAUTHORIZED)
const authInfo = this.getAuthInfo(req);
if (!authInfo) {
return Response.problem('Invalid Authorization header.', 'No Authorization info provided.', EStatusCode.UNAUTHORIZED)
.withHeader('WWW-Authenticate', 'Bearer');
}

const [scheme, credentials] = req.headers['authorization'].split(' ', 2);
const { scheme, credentials } = authInfo;

if (scheme !== 'Bearer') {
return Response.problem('Invalid Authorization header.', 'Only Bearer scheme is supported.', EStatusCode.UNAUTHORIZED)
Expand All @@ -74,6 +75,29 @@ export default class AuthBearerMiddleware extends Middleware {
return await next(req);
}

/**
* Extracts the Authorization header from the Request.
*
* @param req Request object.
* @returns An object with the scheme and credentials or null if the header is not present.
*/
getAuthInfo(req: Request): { scheme: string, credentials: string } | null {
if (!req.headers['authorization']) {
return null;
}

const [scheme, credentials] = req.headers['authorization'].split(' ', 2);

return { scheme, credentials };
}

/**
* Validates the token payload.
*
* @param req Request object.
* @param payload Token payload.
* @returns True if the token is valid, false otherwise.
*/
async validateToken(req: Request, payload: Payload): Promise<boolean> {
if (
typeof payload !== 'object'
Expand Down
3 changes: 2 additions & 1 deletion src/router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { HandlerConstructor, HandlerFunction } from "../http/Handler.js";
import { MiddlewareConstructor, MiddlewareFunction } from "../http/Middleware.js";
import { split } from "../util/strings.js";
import { validateUUID } from "../util/uuid.js";
import Route from "./Route.js";

Expand Down Expand Up @@ -137,7 +138,7 @@ export default class Router {
if (parts[i] !== routeParts[i]) {
if (paramRegex.test(routeParts[i])) {
const [, before, param, after] = routeParts[i].match(paramRegex)!;
const [paramName, paramType] = param.split(':');
const [paramName, paramType] = split(param, ':', 2);

if (!parts[i].startsWith(before) || !parts[i].endsWith(after)) {
return false;
Expand Down

0 comments on commit 69cbd5a

Please sign in to comment.