UNPKG

@daystram/ratify-client

Version:
110 lines 4.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; exports.__esModule = true; exports.RatifyClient = void 0; var qs_1 = __importDefault(require("qs")); var pkce_challenge_1 = __importDefault(require("pkce-challenge")); var uuid_1 = require("uuid"); var jwt_decode_1 = __importDefault(require("jwt-decode")); var RatifyAPIClient_1 = require("./oauth/RatifyAPIClient"); var constants_1 = require("./constants"); var RatifyClient = /** @class */ (function () { function RatifyClient(opts) { this.options = opts; this.storageManager = opts.storage; this.oauth = new RatifyAPIClient_1.RatifyAPIClient(this.options.issuer); } RatifyClient.prototype.isAuthenticated = function () { return this.getToken(constants_1.ACCESS_TOKEN) !== "" && this._accessTokenValid(); }; RatifyClient.prototype._accessTokenValid = function () { return new Date(this.getToken(constants_1.EXPIRE_DATE)) > new Date(); }; RatifyClient.prototype.getToken = function (tokenKey) { return (JSON.parse(this.storageManager.getItem(constants_1.KEY_TOKEN) || "{}")[tokenKey] || ""); }; RatifyClient.prototype.getUser = function () { if (this.isAuthenticated()) return jwt_decode_1["default"](this.getToken(constants_1.ID_TOKEN)); return { sub: "", given_name: "", family_name: "", preferred_username: "", is_superuser: false, created_at: 0 }; }; RatifyClient.prototype.reset = function () { this.storageManager.removeItem(constants_1.KEY_TOKEN); }; RatifyClient.prototype.authorize = function (immediate, scopes) { window.location.href = this.options.issuer + "/authorize?" + qs_1["default"].stringify({ /* eslint-disable @typescript-eslint/camelcase */ client_id: this.options.clientId, response_type: "code", redirect_uri: this.options.redirectUri, scope: "openid profile" + (scopes || []).map(function (scope) { return " " + scope; }).join(""), state: this.getState(), code_challenge: this.getCodeChallenge(), code_challenge_method: "S256", immediate: immediate || false }); }; RatifyClient.prototype.redeemToken = function (authorizationCode) { var _this = this; return this.oauth .token({ /* eslint-disable @typescript-eslint/camelcase */ client_id: this.options.clientId, grant_type: "authorization_code", code: authorizationCode, code_verifier: this.getCodeVerifier() }) .then(function (response) { response.data[constants_1.EXPIRE_DATE] = new Date(new Date().getTime() + response.data.expires_in * 1000); _this.storageManager.setItem(constants_1.KEY_TOKEN, JSON.stringify(response.data)); return response; }); }; RatifyClient.prototype.logout = function (global) { var _this = this; return this.oauth .logout({ /* eslint-disable @typescript-eslint/camelcase */ access_token: this.getToken(constants_1.ACCESS_TOKEN), client_id: this.options.clientId, global: global || false }) .then(function () { _this.reset(); })["catch"](function () { _this.reset(); }); }; RatifyClient.prototype.getState = function () { var state = uuid_1.v4(); sessionStorage.setItem(constants_1.KEY_STATE, state); return state; }; RatifyClient.prototype.checkState = function (state) { var temp = sessionStorage.getItem(constants_1.KEY_STATE); sessionStorage.removeItem(constants_1.KEY_STATE); return temp === state; }; RatifyClient.prototype.getCodeChallenge = function () { var pkce = pkce_challenge_1["default"](); sessionStorage.setItem(constants_1.KEY_CODE, JSON.stringify(pkce)); return pkce.code_challenge; }; RatifyClient.prototype.getCodeVerifier = function () { var pkce = JSON.parse(sessionStorage.getItem(constants_1.KEY_CODE) || ""); sessionStorage.removeItem(constants_1.KEY_CODE); return pkce.code_verifier; }; return RatifyClient; }()); exports.RatifyClient = RatifyClient; //# sourceMappingURL=RatifyClient.js.map