Skip to content

Commit

Permalink
New authentication methods (1.0.14)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPlayz0 committed Dec 30, 2023
1 parent f644763 commit 7e7f0bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ashmwtech/dapir",
"version": "1.0.13",
"version": "1.0.14",
"type": "module",
"description": "An api wrapper",
"source": "src/index.ts",
Expand Down
55 changes: 27 additions & 28 deletions src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { OpenAPIV3_1 as OpenAPI } from 'openapi-types';
import Documentation from './documentation';
import { HTTPContext, RouteFile, RouteConfig, RouteAuthenticationMethodWithData } from './types/httprouter';
import { HttpStatus } from 'utils/httpStatus';
import { AuthenticationMethods, MiddlewareWhen, RouteMiddleware, ServerConfig } from './types/server';
import { AuthenticationMethods, CtxMiddlewareFunction, MiddlewareWhen, RouteMiddleware, ServerConfig } from './types/server';

interface OASchemaFile {
enabled: boolean;
Expand Down Expand Up @@ -146,29 +146,6 @@ export class Server<Context extends {}, Methods extends AuthenticationMethods<Co
runMiddleware('preroutes');
const filteredRoutes = files.filter((x) => !x.directory).filter((x) => /^(get|put|patch|post|delete|head)\.(js|ts)$/.test(x.name));
log('info', `Found ${filteredRoutes.length} route files`);
let authFunc = null;
(() => {
if (this.config.routes?.security?.authentication) {
const auth = this.config.routes.security.authentication;
if (!auth.enabled) return log('error', 'Authentication is enabled but no authentication function is defined');
// if (!auth.handle) return log('error', 'Authentication is enabled but no authentication function is defined');
// if (typeof auth.handle != 'function') return log('error', 'Authentication handle must be a function');

authFunc = (req: express.Request, res: express.Response, next: express.NextFunction) => {
const errorResponse = (status: HttpStatus, opts?: { message?: string; data?: any; code?: string }) => {
return res
.status(status)
.send({ error: true, status: status, code: opts?.code || HttpStatus[status], message: opts?.message, data: opts?.data });
};
try {
// return auth.handle({ ...this.config.routes.context, req, res, next, errorResponse });
} catch (err) {
log('error', `Middleware Error: ${(err as Error).message}`);
return errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, { message: (err as Error).message, code: 'ERROR_UNKNOWN_MIDDLEWARE' });
}
};
}
})();
for (const file of filteredRoutes) {
if (file.directory) continue;

Expand Down Expand Up @@ -196,6 +173,23 @@ export class Server<Context extends {}, Methods extends AuthenticationMethods<Co

log('debug', `Loaded route ${method.toUpperCase()} ${routePath}`);

let routeAuth: { method: string; data: object; handle: CtxMiddlewareFunction<Context> }[] = [];
if (this.config.routes.security?.authentication?.enabled)
for (let authMethod of route.configuration?.security?.authentication ?? []) {
let authName = String(typeof authMethod == 'object' ? authMethod.method : authMethod);
let authData = typeof authMethod == 'object' ? authMethod.data : undefined;
if (!authName) {
log('error', `Route ${file.name} has an invalid authentication method '${authName}'`);
continue;
}
let serverMethod = this.config.routes.security.authentication.methods[authName];
if (!serverMethod) {
log('error', `Route ${file.name} has an invalid authentication method '${authName}'`);
continue;
}
routeAuth.push({ method: authName, data: authData, handle: serverMethod });
}

if (this.documentation) this.documentation.addRoute(route.configuration?.documentation, routePath, method);

const routeFunc = async (req: express.Request, res: express.Response, next: express.NextFunction) => {
Expand All @@ -205,20 +199,25 @@ export class Server<Context extends {}, Methods extends AuthenticationMethods<Co
.send({ error: true, status: status, code: opts?.code || HttpStatus[status], message: opts?.message, data: opts?.data });
};

for (let auth of routeAuth) {
let worked = false;
let goNext = () => (worked = true);
await auth.handle({ ...this.config.routes.context, req, res, next: goNext, errorResponse }, auth.data);
if (worked == false) return;
}

try {
return route.handler({ ...this.config.routes.context, req, res, next, errorResponse });
} catch (error) {
log('error', (error as Error).message ?? 'Unknown error');
return errorResponse(HttpStatus.INTERNAL_SERVER_ERROR, {
message: (error as Error).message ?? 'Unknown error',
code: 'ERROR_UNKNOWN_ROUTE',
code: 'UNKNOWN_ROUTE_ERROR',
});
}
};

if (route.configuration.security?.authentication && authFunc != null)
this.express[method](routePath.replace(/\[([^\]]+)\]/g, ':$1'), authFunc, routeFunc);
else this.express[method](routePath.replace(/\[([^\]]+)\]/g, ':$1'), routeFunc);
this.express[method](routePath.replace(/\[([^\]]+)\]/g, ':$1'), routeFunc);
}
runMiddleware('postroutes');
}
Expand Down
9 changes: 8 additions & 1 deletion src/types/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import { ExpressErrorResponse, HTTPContext } from './httprouter';
import { APIInfoObject } from 'documentation';
import { WebSocketServer } from 'ws';

// interface MiddlewareDataDynamic {
// dynamic: true;
// where: 'body' | 'query' | 'header';
// name: string | string[];
// }
// type MiddlewareData = Record<string, any | MiddlewareDataDynamic>;

export type CtxMiddlewareFunction<Context = {}, Data = any> = (
ctx: Context & HTTPContext,
data: Data,
data?: Data,
) => (express.NextFunction | ExpressErrorResponse | void) | Promise<express.NextFunction | ExpressErrorResponse | void>;
export type MiddlewareFunction = (
req: express.Request,
Expand Down

0 comments on commit 7e7f0bb

Please sign in to comment.