UNPKG

@donation-alerts/auth

Version:

Authentication provider for Donation Alerts API with ability to automatically refresh user tokens.

108 lines (107 loc) 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StaticAuthProvider = void 0; const tslib_1 = require("tslib"); const common_1 = require("@donation-alerts/common"); const shared_utils_1 = require("@stimulcross/shared-utils"); const errors_1 = require("../errors"); const helpers_1 = require("../helpers"); /** * A non-refreshable (static) authentication provider that always returns * the initially provided credentials. */ let StaticAuthProvider = class StaticAuthProvider { /** * Creates a new instance of StaticAuthProvider. * * @param clientId The client ID associated with your application. * @param scopes An optional list of required scopes to validate against. */ constructor(clientId, scopes) { this._registry = new Map(); this._clientId = clientId; this._scopes = scopes; } get clientId() { return this._clientId; } /** * Checks if the specified user is already registered in this auth provider. * * @param user The ID of the user to look for. * @returns A boolean indicating whether the user is registered. */ hasUser(user) { return this._registry.has((0, common_1.extractUserId)(user)); } /** * Adds a user and their related token data to this auth provider. * * @param user The ID of the user. * @param accessToken The access token. * @param scopes An optional list of scopes associated with the access token. * These scopes will be compared against the scopes specified in the constructor. * the scopes specified in the constructor (if any). * * @throws {@link InvalidTokenError} if the access token is empty or undefined. * @throws {@link MissingScopeError} if the token scopes miss required scopes. */ addUser(user, accessToken, scopes) { const userId = (0, common_1.extractUserId)(user); if (!accessToken) { throw new errors_1.InvalidTokenError(userId, `The access token of user "${userId}" is invalid. Make sure it's a non-empty string.`); } if (scopes) { (0, helpers_1.compareScopes)(scopes, this._scopes, userId); } this._registry.set(userId, { accessToken, refreshToken: '', expiresIn: 0, obtainmentTimestamp: Date.now(), scopes, }); } /** * Removes an existing user from this auth provider. * * @param user The ID of the user to remove. */ removeUser(user) { this._registry.delete((0, common_1.extractUserId)(user)); } getScopesForUser(user) { const userId = (0, common_1.extractUserId)(user); if (!this._registry.has(userId)) { throw new errors_1.UnregisteredUserError(userId, `User "${userId}" could not be located in the authentication provider registry. Please add the user first by using addUser method.`); } return this._registry.get(userId).scopes ?? []; } async getAccessTokenForUser(user, scopes) { const userId = (0, common_1.extractUserId)(user); if (!this._registry.has(userId)) { throw new errors_1.UnregisteredUserError(userId, `User "${userId}" could not be located in the authentication provider registry. Please add the user first by using addUser method.`); } const token = this._registry.get(userId); if (!token.accessToken) { throw new errors_1.InvalidTokenError(userId, `The access token of user "${userId}" is invalid. Make sure it's a non-empty string.`); } if (token.scopes) { (0, helpers_1.compareScopes)(token.scopes, scopes, userId); } return { ...token, userId }; } }; exports.StaticAuthProvider = StaticAuthProvider; tslib_1.__decorate([ shared_utils_1.nonenumerable ], StaticAuthProvider.prototype, "_clientId", void 0); tslib_1.__decorate([ shared_utils_1.nonenumerable ], StaticAuthProvider.prototype, "_scopes", void 0); tslib_1.__decorate([ shared_utils_1.nonenumerable ], StaticAuthProvider.prototype, "_registry", void 0); exports.StaticAuthProvider = StaticAuthProvider = tslib_1.__decorate([ (0, common_1.ReadDocumentation)('events') ], StaticAuthProvider);