Skip to content

Commit

Permalink
feat(nano-server): getRemoteAddress, requireClientId (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd authored Feb 11, 2023
2 parents 57912a7 + 07f5f75 commit 68ef633
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
26 changes: 26 additions & 0 deletions core/nano-server/src/nano-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,30 @@ export class AlwatrConnection {

return parsedParams as T;
}

getRemoteAddress(): string {
return (
this.incomingMessage.headers['x-forwarded-for']
?.split(',')
.pop()
?.trim() ||
this.incomingMessage.socket.remoteAddress ||
'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;
}
}
2 changes: 2 additions & 0 deletions core/nano-server/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ declare module 'http' {
* Alwatr Client UUID
*/
'client-id'?: string;

'x-forwarded-for'?: string;
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"format": "run-s format:prettier format:eslint",
"format:eslint": "yarn lint:ts --fix",
"format:prettier": "prettier . --ignore-path .gitignore --write",
"clean": "git clean -d -x -f --exclude=node_modules --exclude=*.env",
"clean": "git clean -d -x -f --exclude=node_modules --exclude=*.env --exclude=_data",
"serve": "wds",
"watch": "run-p watch:* serve",
"watch:ts": "yarn build:ts --watch --preserveWatchOutput",
Expand Down
12 changes: 2 additions & 10 deletions services/form-registration/src/route/put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,8 @@ nanoServer.route('PUT', '/form/', async (

connection.requireToken(config.nanoServer.accessToken);
const params = connection.requireQueryParams<{formId: string}>({formId: 'string'});
const remoteAddress = connection.incomingMessage.socket.remoteAddress ?? 'unknown';
const clientId = connection.incomingMessage.headers['client-id'];

if (!clientId) {
return {
ok: false,
statusCode: 401,
errorCode: 'client_id_header_required',
};
}
const clientId = connection.requireClientId();
const remoteAddress = connection.getRemoteAddress();

if (config.formList.indexOf(params.formId) === -1) {
return {
Expand Down
12 changes: 2 additions & 10 deletions uniquely/com-api/src/route/put-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,8 @@ nanoServer.route('PUT', '/order/', async (connection) => {
connection.requireToken((token: string) => {
return tokenGenerator.verify(params.userId, token) === 'valid';
});
const remoteAddress = connection.incomingMessage.socket.remoteAddress ?? 'unknown';
const clientId = connection.incomingMessage.headers['client-id'];

if (!clientId) {
return {
ok: false,
statusCode: 401,
errorCode: 'client_id_header_required',
};
}
const remoteAddress = connection.getRemoteAddress();
const clientId = connection.requireClientId();

const order = await connection.requireJsonBody<Order>();

Expand Down

0 comments on commit 68ef633

Please sign in to comment.