From fda6aaf410000312998ce67fcdc9d4ddf84689c5 Mon Sep 17 00:00:00 2001 From: Thiago Bezerra Date: Sun, 5 Nov 2023 19:12:31 -0300 Subject: [PATCH] feat: first version of identification endpoint call --- index.js | 86 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/index.js b/index.js index 9b90b8b..3561246 100644 --- a/index.js +++ b/index.js @@ -2,69 +2,73 @@ const jwt = require("jsonwebtoken"); // JWT constants. const JWT_PRIVATE_KEY = process.env.JWT_PRIVATE_KEY; -const JWT_EXPIRATION_TIME_IN_HOURS = "1h"; -const JWT_RS256_ALGORITHM = "RS256"; // HTTP constants. const HTTP_SUCCESS_STATUS_CODE = 200; const HTTP_BAD_REQUEST_STATUS_CODE = 400; const HTTP_INTERNAL_SERVER_ERROR_STATUS_CODE = 500; -const HTTP_HEADER_CONTENT_TYPE_KEY = "content-type"; -const HTTP_HEADER_JSON_CONTENT_TYPE = "application/json"; -// General constants. -const STRING_TYPE = "string"; +// Endpoints. +const HOST = ""; +const CLIENTS_IDENTIFICATION_URL = `${HOST}/clients/identification`; -exports.handler = async (event) => { +// Params. +const NATIONAL_ID_QUERY_PARAMETER = "nationalId"; + +const parseEventBody = (body) => { try { - // Gets event body. - const body = - typeof event.body === STRING_TYPE ? JSON.parse(event.body) : event.body; + return typeof body === "string" ? JSON.parse(body) : body; + } catch (error) { + return null; + } +}; - // Client will be identified by national id. - const nationalId = body.nationalId; +const getNationalIdFromEvent = (event) => { + const body = parseEventBody(event.body); - if (!nationalId) { - return { - statusCode: HTTP_BAD_REQUEST_STATUS_CODE, - }; + return body?.nationalId; +}; + +const getClientIdByNationalId = async (nationalId) => { + const response = await fetch( + `${CLIENTS_IDENTIFICATION_URL}?${NATIONAL_ID_QUERY_PARAMETER}=${nationalId}`, + { + method: "POST", + headers: { + "content-type": "application/json", + }, } - } catch (error) { + ); + + return response?.data?.id; +}; + +exports.handler = async (event) => { + const nationalId = getNationalIdFromEvent(event); + + if (!nationalId) { return { statusCode: HTTP_BAD_REQUEST_STATUS_CODE, }; } - //////////////////////////////////////////////////////////////////////// - - // TODO gets client by national id, creates it if no national id matches. - - //////////////////////////////////////////////////////////////////////// - - // TODO pass client id to 'sub' instead of using this '1111111111' hardcoded value. - // Object that stores values used by 'jsonwebtoken - sign' function call. - // Sub represents the client ID. - const jwtSettings = { - privateKey: JWT_PRIVATE_KEY, - payload: { - sub: 1111111111, - }, - options: { - expiresIn: JWT_EXPIRATION_TIME_IN_HOURS, - algorithm: JWT_RS256_ALGORITHM, - }, - }; - try { + const clientId = await getClientIdByNationalId(nationalId); + const token = jwt.sign( - jwtSettings.payload, - jwtSettings.privateKey, - jwtSettings.options + { + sub: clientId, + }, + JWT_PRIVATE_KEY, + { + expiresIn: "1h", + algorithm: "RS256", + } ); return { headers: { - [HTTP_HEADER_CONTENT_TYPE_KEY]: HTTP_HEADER_JSON_CONTENT_TYPE, + "content-type": "application/json", }, statusCode: HTTP_SUCCESS_STATUS_CODE, body: JSON.stringify({ token }),