UNPKG

tickethead-sdk

Version:

SDK for the Tickethead API

187 lines 7.16 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.AuthService = void 0; const certificate_1 = require("../utils/certificate"); /** * Service class for account API calls. */ class AuthService { constructor(client, version) { this.client = client; this.version = version; } /** * Returns a JWT and its refresh token. * * @param refreshToken refresh token object * @param credentials specify if you want to use non-default credentials * @returns JWT and refresh token * @throws `NotFoundError`, `UnauthorizedError` */ refreshToken(refreshToken, credentials) { return __awaiter(this, void 0, void 0, function* () { let res; if (credentials) { res = yield this.client.post(`account/${this.version}/auth/refresh`, refreshToken, { headers: { Authorization: `Bearer ${credentials.token}`, }, }); } else { res = yield this.client.post(`account/${this.version}/auth/refresh`, refreshToken); } return res.data.data; }); } /** * Returns a JWT and its refresh token. * Enables accessing secured BAM endpoints. * Username can also be the user's email. * * @param credentials username/email and password * @returns JWT and refresh token * @throws `NotFoundError`, `UnauthorizedError` */ login(credentials) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.post(`account/${this.version}/auth/login`, credentials); return res.data.data; }); } /** * Returns a JWT for a newly-created anonymous user. * This user has no permissions. * * @returns JWT for an generated user */ guestLogin() { return __awaiter(this, void 0, void 0, function* () { // Empty body because there has to be one for application/json const res = yield this.client.post(`account/${this.version}/guest/login`, {}); return res.data.data; }); } /** * Returns a nonce which has to be signed with the user's wallet to obtain a JWT. * Enables accessing secured BAM endpoints. * After signing you need to call walletLogin. * * @param req enroll * @returns nonce to sign * @throws `BadRequestError` */ getWalletChallenge(req) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.post(`account/${this.version}/auth/challenge`, req); return res.data.data; }); } /** * Returns a JWT and its refresh token. * Enables accessing secured BAM endpoints. * Requires the device ID header to be set. * * @param signedChallenge contains a nonce signed by the users wallet * @returns JWT and refresh token * @throws `NotFoundError`, `BadRequestError` */ walletLogin(signedChallenge) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.post(`account/${this.version}/auth/wallet`, signedChallenge); return res.data.data; }); } /** * Returns a JWT and its refresh token. * Enables accessing secured BAM endpoints. * * @param keyPair contains a nonce signed by the users wallet * @returns JWT and refresh token * @throws `NotFoundError`, `BadRequestError` */ loginWithCertificate(wallet, organizerId) { return __awaiter(this, void 0, void 0, function* () { // Get a challenge for the user const challengeRequest = { clientNonce: Date.now().toString(), enrollmentId: (0, certificate_1.extractEnrollment)(wallet.certificate), }; const challenge = yield this.getWalletChallenge(challengeRequest); const signedNonce = yield (0, certificate_1.signWithFallback)(challenge.nonce, wallet.privateKey); // Sign the challenge const signedChallenge = { clientNonce: challengeRequest.clientNonce, nonce: challenge.nonce, signedNonce, organizerId, }; // Login return this.walletLogin(signedChallenge); }); } /** * Returns a JWT for a service. * Enables accessing secured BAM endpoints. * * @param credentials service name, org and password * @returns JWT * @throws `NotFoundError`, `UnauthorizedError` */ serviceLogin(credentials) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.post(`account/${this.version}/auth/service`, credentials); return res.data.data; }); } /** * Returns a JWT for a service. * Enables external login via OAuth. * Token is provided by the third party. * * @param req User data for login * @returns JWT and refresh token */ externalLogin(req) { return __awaiter(this, void 0, void 0, function* () { const res = yield this.client.post(`account/${this.version}/auth/external`, req); return res.data.data; }); } /** * Returns an impersonated JWT, if you have the permissions to get it. * If you specify an organizerId, the requested JWT will contain the permissions * that the userId user has for that organizerId * * @param req Impersonated user data * @param adminCredentials if provided, this token will be used for the request * @returns Impersonation JWT */ impersonate(req, adminCredentials) { return __awaiter(this, void 0, void 0, function* () { let res; if (adminCredentials) { res = yield this.client.post(`account/${this.version}/auth/impersonate`, req, { headers: { Authorization: `Bearer ${adminCredentials.token}`, }, }); } else { res = yield this.client.post(`account/${this.version}/auth/impersonate`, req); } return res.data.data; }); } } exports.AuthService = AuthService; //# sourceMappingURL=service.js.map