Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

feat(nano-server): get user auth #111

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
],
"cSpell.language": "en,fa,fa-IR",
"cSpell.words": [
"Alwatr"
"alwatr",
"nanoservice"
],
"typescript.tsdk": ".yarn/sdks/typescript/lib",
"git.autoStash": true,
Expand Down
138 changes: 66 additions & 72 deletions packages/nano-server/src/nano-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {createServer} from 'node:http';
import {createLogger, definePackage, type AlwatrLogger} from '@alwatr/logger';
import {isNumber} from '@alwatr/math';

import type {NanoServerConfig, ConnectionConfig} from './type.js';
import type {NanoServerConfig, ConnectionConfig, UserAuth} from './type';
import type {
AlwatrServiceResponse,
AlwatrServiceResponseFailed,
Expand All @@ -16,7 +16,6 @@ import type {
QueryParameters,
Stringifyable,
StringifyableRecord,
UserAuth,
} from '@alwatr/type';
import type {IncomingMessage, ServerResponse} from 'node:http';
import type {Duplex} from 'node:stream';
Expand All @@ -33,6 +32,7 @@ export type {
AlwatrServiceResponseFailed,
AlwatrServiceResponseSuccess,
AlwatrServiceResponseSuccessWithMeta,
UserAuth,
};

definePackage('nano-server', '1.x');
Expand Down Expand Up @@ -398,19 +398,6 @@ export class AlwatrConnection {
this._logger.logMethodArgs?.('constructor', {method: incomingMessage.method, url: incomingMessage.url});
}

/**
* Get the token placed in the request header.
*/
getAuthBearer(): string | null {
const auth = this.incomingMessage.headers.authorization?.split(' ');

if (auth == null || auth[0].toLowerCase() !== 'bearer') {
return null;
}

return auth[1];
}

/**
* Get request body for POST, PUT and POST methods.
*
Expand Down Expand Up @@ -482,64 +469,86 @@ export class AlwatrConnection {
}

/**
* Parse and validate request token.
* Get the token placed in the request header.
*
* @returns Request token.
* Extract token from `Authorization` with `Bearer ` prefix.
*
* Example:
* @returns token placed in the request header without bearer.
*
* @example
* ```ts
* const token = connection.requireToken((token) => token.length > 12);
* if (token == null) return;
* const token = connection.getAuthBearer();
* ```
*/
requireToken(validator?: ((token: string) => boolean) | string[] | string): string {
const token = this.getAuthBearer();
getAuthBearer(): string | null {
const auth = this.incomingMessage.headers.authorization?.split(' ');

if (token == null) {
throw {
ok: false,
statusCode: 401,
errorCode: 'authorization_required',
};
}
else if (validator === undefined) {
return token;
}
else if (typeof validator === 'string') {
if (token === validator) return token;
}
else if (Array.isArray(validator)) {
if (validator.includes(token)) return token;
}
else if (typeof validator === 'function') {
if (validator(token) === true) return token;
if (auth == null || auth[0].toLowerCase() !== 'bearer') {
return null;
}
throw {
ok: false,
statusCode: 403,
errorCode: 'access_denied',

return auth[1];
}

/**
* Get the user authentication information from the incoming message headers.
*
* @returns An object containing the user authentication information.
*
* @example
* ```ts
* const userAuth = connection.getUserAuth();
* ```
*/
getUserAuth(): Partial<UserAuth> {
const userId = this.incomingMessage.headers['user-id'];
const userToken = this.incomingMessage.headers['user-token'];
const deviceId = this.incomingMessage.headers['device-id'];

return {
id: userId,
token: userToken,
deviceId: deviceId,
};
}

/**
* Parse and get request user auth (include id and token).
* Get and validate the user authentication.
*
* Example:
* @param validator Optional function to validate the user authentication.
* @returns The user authentication information.
* @throws {'authorization_required'} If user authentication is missing.
* @throws {'access_denied'} If user authentication is invalid.
*
*
* @example
* ```ts
* const userAuth = connection.requireUserAuth();
* function validateUserAuth(userAuth: UserAuth): boolean {
* return userAuth.id === 'public_user_id' && userAuth.token === 'secret_token';
* }
*
* await connection.requireUserAuth(validateUserAuth);
* ```
*/
getUserAuth(): UserAuth | null {
const auth = this.getAuthBearer()
?.split('/')
.filter((item) => item.trim() !== '');

return auth == null || auth.length !== 2
? null
: {
id: auth[0],
token: auth[1],
async requireUserAuth(validator?: (userAuth: UserAuth) => PromiseLike<boolean>): Promise<UserAuth> {
const userAuth = this.getUserAuth();

if (userAuth.id == null || userAuth.token == null || userAuth.deviceId == null) {
throw {
ok: false,
statusCode: 401,
errorCode: 'authorization_required',
};
}
else if (typeof validator === 'function' && await validator(userAuth as UserAuth) !== true) {
throw {
ok: false,
statusCode: 403,
errorCode: 'access_denied',
};
}

return userAuth as UserAuth;
}

/**
Expand Down Expand Up @@ -622,19 +631,4 @@ export class AlwatrConnection {
'unknown'
);
}

requireClientId(): string {
const clientId = this.incomingMessage.headers['client-id'];

if (!clientId) {
// eslint-disable-next-line no-throw-literal
throw {
ok: false,
statusCode: 401,
errorCode: 'client_denied',
};
}

return clientId;
}
}
21 changes: 19 additions & 2 deletions packages/nano-server/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
declare module 'http' {
interface IncomingHttpHeaders {
/**
* Alwatr Client UUID
* Alwatr Device UUID
*/
'client-id'?: string;
'device-id'?: string;

/**
* User id
*/
'user-id'?: string;

/**
* User token
*/
'user-token'?: string;


'x-forwarded-for'?: string;
}
Expand Down Expand Up @@ -87,3 +98,9 @@ export interface ConnectionConfig {

export type ParamKeyType = 'string' | 'number' | 'boolean';
export type ParamValueType = string | number | boolean | null;

export interface UserAuth {
id: string;
token: string;
deviceId: string;
}