wallee
Version:
TypeScript/JavaScript client for wallee
74 lines (73 loc) • 3.07 kB
JavaScript
;
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.HttpBearerAuth = void 0;
/**
* 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.
*/
const jwt = require("jsonwebtoken");
const runtime_1 = require("../runtime");
class HttpBearerAuth {
constructor(userId, authenticationKey) {
this.apiPrefixPath = "/api/v2.0";
this._userId = userId;
this._authenticationKey = authenticationKey;
}
applyToRequest(path, method, queryParams, headerParameters) {
return __awaiter(this, void 0, void 0, function* () {
const timestamp = Math.trunc((new Date()).getTime() / 1000);
const resourcePath = this.getResourcePath(path, queryParams);
const payload = {
sub: this._userId.toString(),
iat: timestamp,
requestPath: resourcePath,
requestMethod: method
};
const customHeader = {
alg: "HS256",
typ: "JWT",
ver: 1
};
const signOptions = {
header: customHeader,
};
const tokenString = jwt.sign(payload, Buffer.from(this._authenticationKey, "base64"), signOptions);
if (tokenString) {
headerParameters["Authorization"] = `Bearer ${tokenString}`;
}
});
}
getResourcePath(path, queryParams) {
if (Object.keys(queryParams).length !== 0) {
path += "?" + (0, runtime_1.querystring)(queryParams);
}
return `${this.apiPrefixPath}${path}`;
}
}
exports.HttpBearerAuth = HttpBearerAuth;