UNPKG

wallee

Version:
135 lines (132 loc) 6.88 kB
"use strict"; /* tslint:disable */ /* eslint-disable */ /** * Wallee AG TypeScript SDK * * This library allows to interact with the Wallee AG payment service. * * Copyright owner: Wallee AG * Website: https://en.wallee.com * Developer email: ecosystem-team@wallee.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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.WebhookEncryptionKeysService = void 0; const runtime = require("../runtime"); const EncryptionUtil_1 = require("../utils/EncryptionUtil"); const WalleeSdkException_1 = require("../models/WalleeSdkException"); const SdkExceptionErrorCodes_1 = require("../models/SdkExceptionErrorCodes"); const ServiceApiUtils_1 = require("../utils/ServiceApiUtils"); /** * */ class WebhookEncryptionKeysService extends runtime.BaseAPI { constructor(configuration) { super(configuration); } /** * Retrieve a webhook encryption key */ getWebhooksEncryptionKeysIdRaw(requestParameters, initOverrides) { return __awaiter(this, void 0, void 0, function* () { if (requestParameters['id'] == null) { throw new runtime.RequiredError('id', 'Required parameter "id" was null or undefined when calling getWebhooksEncryptionKeysId().'); } const queryParameters = {}; const headerParameters = {}; const method = 'GET'; const path = `/webhooks/encryption-keys/{id}`.replace(`{${"id"}}`, encodeURIComponent(String(requestParameters['id']))); if (this.configuration.httpBearerAuth) { yield this.configuration.httpBearerAuth.applyToRequest(path, method, queryParameters, headerParameters); } // Set per-request timeout in initOverrides: use the incoming parameter or fall back to the Configuration value const requestTimeoutInSeconds = this.configuration.requestTimeout; const updatedInitOverrides = yield ServiceApiUtils_1.ServiceApiUtils.adjustRequestSignalAsync(initOverrides, requestTimeoutInSeconds); const response = yield this.request({ path: `/webhooks/encryption-keys/{id}`.replace(`{${"id"}}`, encodeURIComponent(String(requestParameters['id']))), method: 'GET', headers: headerParameters, query: queryParameters, }, updatedInitOverrides); if (this.isJsonMime(response.headers.get('content-type'))) { return new runtime.JSONApiResponse(response); } else { return new runtime.TextApiResponse(response); } }); } /** * Retrieve a webhook encryption key */ getWebhooksEncryptionKeysId(requestParameters, initOverrides) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.getWebhooksEncryptionKeysIdRaw(requestParameters, initOverrides); return yield response.value(); }); } /** * Verifies the webhook content signature using the retrieved public key */ isContentValid(signatureHeader, contentToVerify) { return __awaiter(this, void 0, void 0, function* () { // Regular expression to extract algorithm, keyId, and signature from the signatureHeader const regex = /^.*,\s*keyId=([a-zA-Z0-9\-]+),\s*signature=([a-zA-Z0-9+/=]+)$/; const match = signatureHeader.match(regex); if (!match) { throw new WalleeSdkException_1.WalleeSdkException(SdkExceptionErrorCodes_1.SdkExceptionErrorCodes.INVALID_WEBHOOK_ENCRYPTION_HEADER_FORMAT, "Invalid webhook encryption header format. Expected format: 'algorithm=<algorithm>, keyId=<keyId>, signature=<signature>'"); } const publicKeyId = match[1]; const contentSignature = match[2]; try { let publicKey; if (WebhookEncryptionKeysService.cache.has(publicKeyId)) { publicKey = WebhookEncryptionKeysService.cache.get(publicKeyId); } else { try { publicKey = yield this.getWebhooksEncryptionKeysId({ id: publicKeyId }); } catch (e) { throw new WalleeSdkException_1.WalleeSdkException(SdkExceptionErrorCodes_1.SdkExceptionErrorCodes.UNKNOWN_WEBHOOK_ENCRYPTION_PUBLIC_KEY, `Error during retrieving public key with ID: ${publicKeyId}. Because: ${e.message}.`); } if (!publicKey) { throw new WalleeSdkException_1.WalleeSdkException(SdkExceptionErrorCodes_1.SdkExceptionErrorCodes.UNKNOWN_WEBHOOK_ENCRYPTION_PUBLIC_KEY, `Could not retrieve public key with ID: ${publicKeyId}`); } WebhookEncryptionKeysService.cache.set(publicKeyId, publicKey); } const isValid = EncryptionUtil_1.EncryptionUtil.isContentValid(contentToVerify, contentSignature, publicKey); return isValid; } catch (err) { if (err instanceof WalleeSdkException_1.WalleeSdkException) { throw err; } throw new WalleeSdkException_1.WalleeSdkException(SdkExceptionErrorCodes_1.SdkExceptionErrorCodes.WEBHOOK_ENCRYPTION_SIGNATURE_VERIFICATION_FAILED, `Webhook signature verification failed: ${err.message || err}`); } }); } } exports.WebhookEncryptionKeysService = WebhookEncryptionKeysService; WebhookEncryptionKeysService.cache = new Map();