Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fetch, nano-server): user/auth #1121

Merged
merged 2 commits into from
May 7, 2023
Merged
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
4 changes: 4 additions & 0 deletions core/fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ function _processOptions(options: FetchOptions): Required<FetchOptions> {
options.headers.Authorization = `Bearer ${options.token}`;
}

if (options.userAuth != null) {
options.headers.Authorization = `Bearer ${options.userAuth.id}/${options.userAuth.token}`;
}

return options as Required<FetchOptions>;
}

Expand Down
7 changes: 6 additions & 1 deletion core/fetch/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Methods, QueryParameters, StringifyableRecord} from '@alwatr/type';
import type {Methods, QueryParameters, StringifyableRecord, UserAuth} from '@alwatr/type';

export type CacheStrategy =
| 'network_only'
Expand Down Expand Up @@ -102,6 +102,11 @@ export interface FetchOptions extends RequestInit {
* Add token to Authentication bearer header.
*/
token?: string;

/**
* Add user id and token to Authentication bearer header.
*/
userAuth?: UserAuth;
}

export type StringifyableFetchOptions = Pick<
Expand Down
28 changes: 24 additions & 4 deletions core/nano-server/src/nano-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
ParamValueType,
QueryParameters,
StringifyableRecord,
UserAuth,
} from '@alwatr/type';
import type {IncomingMessage, ServerResponse} from 'node:http';
import type {Duplex} from 'node:stream';
Expand Down Expand Up @@ -405,7 +406,7 @@ export class AlwatrConnection {
/**
* Get the token placed in the request header.
*/
getToken(): string | null {
getAuthBearer(): string | null {
const auth = this.incomingMessage.headers.authorization?.split(' ');

if (auth == null || auth[0].toLowerCase() !== 'bearer') {
Expand Down Expand Up @@ -497,10 +498,9 @@ export class AlwatrConnection {
* ```
*/
requireToken(validator?: ((token: string) => boolean) | Array<string> | string): string {
const token = this.getToken();
const token = this.getAuthBearer();

if (token == null) {
// eslint-disable-next-line no-throw-literal
throw {
ok: false,
statusCode: 401,
Expand All @@ -519,14 +519,34 @@ export class AlwatrConnection {
else if (typeof validator === 'function') {
if (validator(token) === true) return token;
}
// eslint-disable-next-line no-throw-literal
throw {
ok: false,
statusCode: 403,
errorCode: 'access_denied',
};
}

/**
* Parse and get request user auth (include id and token).
*
* Example:
* ```ts
* const userAuth = connection.requireUserAuth();
* ```
*/
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],
};
}

/**
* Parse query param and validate with param type.
*/
Expand Down