|
1 |
| -const jwt = require("jsonwebtoken"); |
| 1 | +const jwt = require('jsonwebtoken'); |
2 | 2 |
|
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-----`; |
7 | 4 |
|
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 = ''; |
14 | 6 |
|
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 | +}; |
26 | 20 |
|
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 | +}; |
37 | 27 |
|
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 | + }); |
39 | 36 |
|
40 |
| - // TODO gets client by national id, creates it if no national id matches. |
| 37 | + return response.data.id; |
| 38 | +}; |
41 | 39 |
|
42 |
| - //////////////////////////////////////////////////////////////////////// |
| 40 | +/** |
| 41 | + * @param {AWSLambda.APIGatewayEvent} event |
| 42 | + * @returns {Promise<AWSLambda.APIGatewayProxyResult} |
| 43 | + */ |
| 44 | +exports.handler = async (event) => { |
| 45 | + const nationalId = await getNationalIdFromEvent(event); |
43 | 46 |
|
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 | + } |
57 | 50 |
|
58 | 51 | try {
|
59 |
| - const token = jwt.sign( |
60 |
| - jwtSettings.payload, |
61 |
| - jwtSettings.privateKey, |
62 |
| - jwtSettings.options |
63 |
| - ); |
| 52 | + const clientId = await getClientIdByNationalId(nationalId); |
64 | 53 |
|
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 | + } |
72 | 65 | } catch (error) {
|
73 |
| - return { |
74 |
| - statusCode: HTTP_INTERNAL_SERVER_ERROR_STATUS_CODE, |
75 |
| - }; |
| 66 | + return result(500, { message: 'Error while verifying national ID' }); |
76 | 67 | }
|
77 | 68 | };
|
0 commit comments