UNPKG

oidc-client-rx

Version:

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

61 lines (60 loc) 2.72 kB
import { Injectable, inject } from "injection-js"; import { BehaviorSubject, from } from "rxjs"; import { map } from "rxjs/operators"; import { CryptoService } from "../utils/crypto/crypto.service.js"; import { MockUtil } from "../utils/reflect/index.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; } function _ts_metadata(k, v) { if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v); } class JwtWindowCryptoService { generateCodeChallenge(codeVerifier) { return this.calcHash(codeVerifier).pipe(map((challengeRaw)=>this.base64UrlEncode(challengeRaw))); } generateAtHash(accessToken, algorithm) { return this.calcHash(accessToken, algorithm).pipe(map((tokenHash)=>{ const substr = tokenHash.substr(0, tokenHash.length / 2); const tokenHashBase64 = btoa(substr); return tokenHashBase64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, ""); })); } calcHash(valueToHash, algorithm = "SHA-256") { const msgBuffer = new TextEncoder().encode(valueToHash); return from(this.cryptoService.getCrypto().subtle.digest(algorithm, msgBuffer)).pipe(map((hashBuffer)=>{ const buffer = hashBuffer; const hashArray = Array.from(new Uint8Array(buffer)); return this.toHashString(hashArray); })); } toHashString(byteArray) { let result = ""; for (const e of byteArray)result += String.fromCharCode(e); return result; } base64UrlEncode(str) { const base64 = btoa(str); return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, ""); } constructor(){ this.cryptoService = inject(CryptoService); } } _ts_decorate([ MockUtil({ implementation: ()=>new BehaviorSubject(void 0) }), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ String ]), _ts_metadata("design:returntype", "u" < typeof Observable ? Object : Observable) ], JwtWindowCryptoService.prototype, "generateCodeChallenge", null); JwtWindowCryptoService = _ts_decorate([ Injectable() ], JwtWindowCryptoService); export { JwtWindowCryptoService };