UNPKG

oidc-client-rx

Version:

ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications

97 lines (96 loc) 4.15 kB
import { Injectable, inject } from "@outposts/injection-js"; import { DOCUMENT } from "../../dom/index.js"; import { LoggerService } from "../../logging/logger.service.js"; function _ts_decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc); else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } const PARTS_OF_TOKEN = 3; class TokenHelperService { getTokenExpirationDate(dataIdToken) { if (!Object.prototype.hasOwnProperty.call(dataIdToken, 'exp')) return new Date(new Date().toUTCString()); const date = new Date(0); date.setUTCSeconds(dataIdToken.exp); return date; } getSigningInputFromToken(token, encoded, configuration) { if (!this.tokenIsValid(token, configuration)) return ''; const header = this.getHeaderFromToken(token, encoded, configuration); const payload = this.getPayloadFromToken(token, encoded, configuration); return [ header, payload ].join('.'); } getHeaderFromToken(token, encoded, configuration) { if (!this.tokenIsValid(token, configuration)) return {}; return this.getPartOfToken(token, 0, encoded); } getPayloadFromToken(token, encoded, configuration) { if (!configuration) return {}; if (!this.tokenIsValid(token, configuration)) return {}; return this.getPartOfToken(token, 1, encoded); } getSignatureFromToken(token, encoded, configuration) { if (!this.tokenIsValid(token, configuration)) return {}; return this.getPartOfToken(token, 2, encoded); } getPartOfToken(token, index, encoded) { const partOfToken = this.extractPartOfToken(token, index); if (encoded) return partOfToken; const result = this.urlBase64Decode(partOfToken); return JSON.parse(result); } urlBase64Decode(str) { var _this_document_defaultView; let output = str.replace(/-/g, '+').replace(/_/g, '/'); switch(output.length % 4){ case 0: break; case 2: output += '=='; break; case 3: output += '='; break; default: throw new Error('Illegal base64url string!'); } const decoded = void 0 !== this.document.defaultView ? null == (_this_document_defaultView = this.document.defaultView) ? void 0 : _this_document_defaultView.atob(output) : Buffer.from(output, 'base64').toString('binary'); if (!decoded) return ''; try { return decodeURIComponent(decoded.split('').map((c)=>`%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`).join('')); } catch { return decoded; } } tokenIsValid(token, configuration) { if (!token) { this.loggerService.logError(configuration, `token '${token}' is not valid --> token falsy`); return false; } if (!token.includes('.')) { this.loggerService.logError(configuration, `token '${token}' is not valid --> no dots included`); return false; } const parts = token.split('.'); if (parts.length !== PARTS_OF_TOKEN) { this.loggerService.logError(configuration, `token '${token}' is not valid --> token has to have exactly ${PARTS_OF_TOKEN - 1} dots`); return false; } return true; } extractPartOfToken(token, index) { return token.split('.')[index]; } constructor(){ this.loggerService = inject(LoggerService); this.document = inject(DOCUMENT); } } TokenHelperService = _ts_decorate([ Injectable() ], TokenHelperService); export { TokenHelperService };