Skip to content

Commit c3310ee

Browse files
committed
feat: initial v3 lambda code
1 parent d57b3fd commit c3310ee

File tree

3 files changed

+72
-63
lines changed

3 files changed

+72
-63
lines changed

index.js

Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,68 @@
1-
const jwt = require("jsonwebtoken");
1+
const jwt = require('jsonwebtoken');
22

3-
// JWT constants.
4-
const JWT_PRIVATE_KEY = process.env.JWT_PRIVATE_KEY;
5-
const JWT_EXPIRATION_TIME_IN_HOURS = "1h";
6-
const JWT_RS256_ALGORITHM = "RS256";
3+
const JWT_PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----\n${process.env.JWT_PRIVATE_KEY}\n-----END RSA PRIVATE KEY-----`;
74

8-
// HTTP constants.
9-
const HTTP_SUCCESS_STATUS_CODE = 200;
10-
const HTTP_BAD_REQUEST_STATUS_CODE = 400;
11-
const HTTP_INTERNAL_SERVER_ERROR_STATUS_CODE = 500;
12-
const HTTP_HEADER_CONTENT_TYPE_KEY = "content-type";
13-
const HTTP_HEADER_JSON_CONTENT_TYPE = "application/json";
5+
const HOST = '';
146

15-
// General constants.
16-
const STRING_TYPE = "string";
17-
18-
exports.handler = async (event) => {
19-
try {
20-
// Gets event body.
21-
const body =
22-
typeof event.body === STRING_TYPE ? JSON.parse(event.body) : event.body;
23-
24-
// Client will be identified by national id.
25-
const nationalId = body.nationalId;
7+
/**
8+
*
9+
* @param {number} statusCode
10+
* @param {any} body
11+
* @returns {Promise<AWSLambda.APIGatewayProxyResult}
12+
*/
13+
const result = (statusCode, body = null) => {
14+
return {
15+
headers: { 'content-type': 'application/json' },
16+
statusCode,
17+
body: JSON.stringify(body),
18+
};
19+
};
2620

27-
if (!nationalId) {
28-
return {
29-
statusCode: HTTP_BAD_REQUEST_STATUS_CODE,
30-
};
31-
}
32-
} catch (error) {
33-
return {
34-
statusCode: HTTP_BAD_REQUEST_STATUS_CODE,
35-
};
36-
}
21+
/**
22+
* @param {AWSLambda.APIGatewayEvent} event
23+
*/
24+
const getNationalIdFromEvent = (event) => {
25+
return event.queryStringParameters?.nationalId;
26+
};
3727

38-
////////////////////////////////////////////////////////////////////////
28+
const getClientIdByNationalId = async (nationalId) => {
29+
return 123;
30+
const response = await fetch(
31+
`${HOST}/clients/identification?nationalId=${nationalId}`,
32+
{ method: 'POST' }
33+
).catch((err) => {
34+
console.error('Error during identification request', err);
35+
});
3936

40-
// TODO gets client by national id, creates it if no national id matches.
37+
return response.data.id;
38+
};
4139

42-
////////////////////////////////////////////////////////////////////////
40+
/**
41+
* @param {AWSLambda.APIGatewayEvent} event
42+
* @returns {Promise<AWSLambda.APIGatewayProxyResult}
43+
*/
44+
exports.handler = async (event) => {
45+
const nationalId = await getNationalIdFromEvent(event);
4346

44-
// TODO pass client id to 'sub' instead of using this '1111111111' hardcoded value.
45-
// Object that stores values used by 'jsonwebtoken - sign' function call.
46-
// Sub represents the client ID.
47-
const jwtSettings = {
48-
privateKey: JWT_PRIVATE_KEY,
49-
payload: {
50-
sub: 1111111111,
51-
},
52-
options: {
53-
expiresIn: JWT_EXPIRATION_TIME_IN_HOURS,
54-
algorithm: JWT_RS256_ALGORITHM,
55-
},
56-
};
47+
if (!nationalId) {
48+
return result(400, { message: 'Missing national ID' });
49+
}
5750

5851
try {
59-
const token = jwt.sign(
60-
jwtSettings.payload,
61-
jwtSettings.privateKey,
62-
jwtSettings.options
63-
);
52+
const clientId = await getClientIdByNationalId(nationalId);
6453

65-
return {
66-
headers: {
67-
[HTTP_HEADER_CONTENT_TYPE_KEY]: HTTP_HEADER_JSON_CONTENT_TYPE,
68-
},
69-
statusCode: HTTP_SUCCESS_STATUS_CODE,
70-
body: JSON.stringify({ token }),
71-
};
54+
try {
55+
const token = jwt.sign({ sub: clientId }, JWT_PRIVATE_KEY, {
56+
expiresIn: '1h',
57+
algorithm: 'RS256',
58+
});
59+
60+
return result(200, { token });
61+
} catch (error) {
62+
console.error('Error while generating token', error);
63+
return result(500, { message: 'Error while generating token' });
64+
}
7265
} catch (error) {
73-
return {
74-
statusCode: HTTP_INTERNAL_SERVER_ERROR_STATUS_CODE,
75-
};
66+
return result(500, { message: 'Error while verifying national ID' });
7667
}
7768
};

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"dependencies": {
33
"jsonwebtoken": "^9.0.2"
4+
},
5+
"devDependencies": {
6+
"@types/aws-lambda": "^8.10.125"
47
}
58
}

0 commit comments

Comments
 (0)