UNPKG

propylons-client

Version:
112 lines 3.49 kB
"use strict"; /*! * Propylons * Copyright(c) 2022 Xavier Raffin * MIT Licensed */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OwnershipContext = exports.ContextType = exports.BaseToken = exports.TokenType = void 0; const assert_1 = __importDefault(require("assert")); const Utils_1 = require("./Utils"); var TokenType; (function (TokenType) { TokenType["USER"] = "USER"; TokenType["API_KEY"] = "API_KEY"; })(TokenType = exports.TokenType || (exports.TokenType = {})); class BaseToken { constructor(rawToken) { } } exports.BaseToken = BaseToken; /* to be defined by propylons user export interface CustomRawToken extends RawToken { email: string; assumedByUserEmail?: string; assumedByUserId?: string; assumedByGroup?: string; assumedBySub?: string; email_verified?: boolean; locale: string; "custom:userId": string; "custom:timezone": string; "cognito:username": string; "custom:country"?: string; "cognito:groups"?: string[]; identities?: SSOIdentity[]; } */ var ContextType; (function (ContextType) { ContextType["USER"] = "USER"; ContextType["WORKSPACE"] = "WORKSPACE"; ContextType["ORGANISATION"] = "ORGANISATION"; })(ContextType = exports.ContextType || (exports.ContextType = {})); class OwnershipContext { constructor(type, id) { this.type = type; this.id = id; } static fromUserId(id) { assert_1.default.ok((0, Utils_1.isPositiveInteger)(id, true)); return new OwnershipContext(ContextType.USER, String(id)); } static fromWorkspaceId(id) { assert_1.default.ok((0, Utils_1.isPositiveInteger)(id, true)); return new OwnershipContext(ContextType.WORKSPACE, String(id)); } static fromOrganisationId(id) { assert_1.default.ok((0, Utils_1.isPositiveInteger)(id, false)); return new OwnershipContext(ContextType.ORGANISATION, String(id)); } /** /!\ Cannot tell workspace and org ids apart, so assuming workspace id if negative */ static fromJobOwnerId(id) { const negative = String(id)[0] === "-"; return negative ? this.fromWorkspaceId(-Number(id)) : this.fromUserId(id); } isUser() { return this.type === ContextType.USER; } isWorkspace() { return this.type === ContextType.WORKSPACE; } isOrganisation() { return this.type === ContextType.ORGANISATION; } isJobOwner() { return this.isUser() || this.isWorkspace(); } isAccount() { return this.isWorkspace() || this.isOrganisation(); } ownerId() { return parseInt(this.id); } comboId() { return this.isUser() ? this.ownerId() : -this.ownerId(); } assertUserId() { assert_1.default.ok(this.isUser()); return this.ownerId(); } assertWorkspaceId() { assert_1.default.ok(this.isWorkspace()); return this.ownerId(); } assertOrganisationId() { assert_1.default.ok(this.isOrganisation()); return this.ownerId(); } assertJobOwnerId() { assert_1.default.ok(this.isJobOwner()); return this.comboId(); } assertAccountId() { assert_1.default.ok(this.isAccount()); return this.ownerId(); } } exports.OwnershipContext = OwnershipContext; //# sourceMappingURL=Token.js.map