UNPKG

@lightbend/akkaserverless-javascript-sdk

Version:
232 lines 7.37 kB
"use strict"; /* * Copyright 2021 Lightbend Inc. * * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.JwtClaims = void 0; const JwtClaimPrefix = '_akkasls-jwt-claim-'; /** * JWT claims that were part of the bearer token with this request. */ class JwtClaims { constructor(metadata) { this.metadata = metadata; } /** * Get the issuer, that is, the <tt>iss</tt> claim, as described in RFC 7519 section 4.1.1. * * @return the issuer, if present. * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1 |RFC 7519 section 4.1.1} */ get issuer() { return this.getString('iss'); } /** * Get the subject, that is, the <tt>sub</tt> claim, as described in RFC 7519 section 4.1.2. * * @return the subject, if present. * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2 |RFC 7519 section 4.1.2} */ get subject() { return this.getString('sub'); } /** * Get the audience, that is, the <tt>aud</tt> claim, as described in RFC 7519 section 4.1.3. * * @return the audience, if present. * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3 |RFC 7519 section 4.1.3} */ get audience() { return this.getString('aud'); } /** * Get the expiration time, that is, the <tt>exp</tt> claim, as described in RFC 7519 section 4.1.4. * * @return the expiration time, if present. * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4 |RFC 7519 section 4.1.4} */ get expirationTime() { return this.getNumericDate('exp'); } /** * Get the not before, that is, the <tt>nbf</tt> claim, as described in RFC 7519 section 4.1.5. * * @return the not before, if present. * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5 |RFC 7519 section 4.1.5} */ get notBefore() { return this.getNumericDate('nbf'); } /** * Get the issued at, that is, the <tt>iat</tt> claim, as described in RFC 7519 section 4.1.6. * * @return the issued at, if present. * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6 |RFC 7519 section 4.1.6} */ get issuedAt() { return this.getNumericDate('iat'); } /** * Get the JWT ID, that is, the <tt>jti</tt> claim, as described in RFC 7519 section 4.1.7. * * @return the JWT ID, if present. * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.7 |RFC 7519 section 4.1.7} */ get jwtId() { return this.getString('jti'); } /** * Get the string claim with the given name. * * @param name The name of the claim. */ getString(name) { const value = this.metadata.asMap[JwtClaimPrefix + name]; if (typeof value === 'string') { return value; } return undefined; } /** * Get the number claim with the given name. * * @param name The name of the claim. */ getNumber(name) { const value = this.getString(name); if (typeof value === 'string') { const n = parseFloat(value); if (!isNaN(n)) { return n; } } return undefined; } /** * Get the numeric date claim with the given name. * * Numeric dates are expressed as a number of seconds since epoch, as described in RFC 7519 section 2. * * @param name The name of the claim. * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-2 |RFC 7519 section 2} */ getNumericDate(name) { const value = this.getNumber(name); if (typeof value === 'number') { return new Date(value * 1000); } return undefined; } /** * Get the boolean claim with the given name. * * @param name The name of the claim. */ getBoolean(name) { const value = this.getString(name); if (value === 'true') { return true; } if (value === 'false') { return false; } return undefined; } /** * Get the object claim with the given name. * * @param name The name of the claim. */ getObject(name) { const value = this.getString(name); if (typeof value === 'string') { try { const parsed = JSON.parse(value); if (typeof parsed === 'object') { return parsed; } } catch (e) { } } return undefined; } /** * Get the string array claim with the given name. * * @param name The name of the claim. */ getStringArray(name) { return this.getArray(name, (item) => typeof item === 'string'); } /** * Get the number array claim with the given name. * * @param name The name of the claim. */ getNumberArray(name) { return this.getArray(name, (item) => typeof item === 'number'); } /** * Get the boolean array claim with the given name. * * @param name The name of the claim. */ getBooleanArray(name) { return this.getArray(name, (item) => typeof item === 'boolean'); } /** * Get the object array claim with the given name. * * @param name The name of the claim. */ getObjectArray(name) { return this.getArray(name, (item) => typeof item === 'object'); } /** * Get the numeric date array claim with the given name. * * Numeric dates are expressed as a number of seconds since epoch, as described in RFC 7519 section 2. * * @param name The name of the claim. * @see {@link https://datatracker.ietf.org/doc/html/rfc7519#section-2 |RFC 7519 section 2} */ getNumericDateArray(name) { const numbers = this.getNumberArray(name); if (numbers !== undefined) { return numbers.map((number) => new Date(number * 1000)); } return undefined; } getArray(name, isT) { const value = this.getString(name); if (typeof value === 'string') { try { const parsed = JSON.parse(value); if (Array.isArray(parsed)) { for (const item of parsed) { if (!isT(item)) { return undefined; } } return parsed; } } catch (e) { } } return undefined; } } exports.JwtClaims = JwtClaims; //# sourceMappingURL=jwt-claims.js.map