UNPKG

@ingestkorea/client-sens

Version:

INGESTKOREA SDK Naver Cloud Platform SENS Client for Node.js.

30 lines (29 loc) 1.94 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.middlewareNcpSign = void 0; const crypto_1 = require("crypto"); const util_http_handler_1 = require("@ingestkorea/util-http-handler"); const constants_js_1 = require("./constants.js"); const middlewareNcpSign = (next) => (input, context) => __awaiter(void 0, void 0, void 0, function* () { const { accessKey, secretKey } = context.credentials; const method = input.request.method; const queryString = (0, util_http_handler_1.buildQueryString)(input.request.query); const path = queryString ? `${input.request.path}?${queryString}` : input.request.path; const space = " "; const newLine = "\n"; const timestamp = new Date().getTime().toString(); const stringToSign = [method + space + path, timestamp, accessKey].join(newLine); const signature = (0, crypto_1.createHmac)("sha256", secretKey).update(stringToSign).digest("base64"); input.request.headers = Object.assign(Object.assign({}, input.request.headers), { [constants_js_1.NCP_ACCESS_KEY]: accessKey, [constants_js_1.NCP_TIMESTAMP]: timestamp, [constants_js_1.NCP_SIGNATURE]: signature }); return next(input, context); }); exports.middlewareNcpSign = middlewareNcpSign;