UNPKG

balena-auth

Version:
116 lines (113 loc) 3.19 kB
"use strict"; /* Copyright 2016-17 Balena 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.JWT = void 0; /** * @module jwt */ const jwt_decode_1 = require("jwt-decode"); const token_1 = require("./token"); class JWT { static parse(key) { return (0, jwt_decode_1.default)(key.trim()); } static isValid(key) { try { this.parse(key); return true; } catch (err) { return false; } } constructor(key) { /** * @member type * @summary Get the type of token * @public * * @returns {TokenType} the type of token * * @example * console.log(token.type) */ this.type = token_1.TokenType.JWT; /** * @member isValid * @summary Check if a token is valid * @function * @public * * @returns {boolean} is valid * * @example * console.log(token.isValid()); */ this.isValid = () => JWT.isValid(this.key); /** * @member getAge * @summary Get the token age * @function * @public * * @returns {number | undefined} * * @example * console.log(token.getAge()); */ this.getAge = () => { const { iat } = JWT.parse(this.key); return iat ? Date.now() - iat * 1000 : undefined; }; /** * @member isExpired * @summary Check whether the token has expired * @function * @public * * @returns {boolean} * * @example * console.log(token.isExpired()); */ this.isExpired = () => { const { exp } = JWT.parse(this.key); return exp ? Date.now() > exp * 1000 : false; }; /** * @member get2FAStatus * @summary Gets whether passing a 2FA challenge is pending, passed or not required. * @function * @public * * @returns {'not_required'|'pending'|'passed'} * * @example * console.log(token.get2FAStatus()) */ this.get2FAStatus = () => { const { twoFactorRequired } = JWT.parse(this.key); if (twoFactorRequired == null) { return 'not_required'; } if (twoFactorRequired) { return 'pending'; } return 'passed'; }; this.key = key; } } exports.JWT = JWT; //# sourceMappingURL=jwt.js.map