UNPKG

oidc-client-rx

Version:

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

46 lines (45 loc) 2.12 kB
import { Injectable, inject } from "injection-js"; import { LoggerService } from "../../logging/logger.service.js"; import { CryptoService } from "../../utils/crypto/crypto.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; } class RandomService { createRandom(requiredLength, configuration) { if (requiredLength <= 0) return ""; if (requiredLength > 0 && requiredLength < 7) { this.loggerService.logWarning(configuration, `RandomService called with ${requiredLength} but 7 chars is the minimum, returning 10 chars`); requiredLength = 10; } const length = requiredLength - 6; const arr = new Uint8Array(Math.floor(length / 2)); const crypto = this.cryptoService.getCrypto(); if (crypto) crypto.getRandomValues(arr); return Array.from(arr, this.toHex).join("") + this.randomString(7); } toHex(dec) { return `0${dec.toString(16)}`.substr(-2); } randomString(length) { let result = ""; const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; const values = new Uint32Array(length); const crypto = this.cryptoService.getCrypto(); if (crypto) { crypto.getRandomValues(values); for(let i = 0; i < length; i++)result += characters[values[i] % characters.length]; } return result; } constructor(){ this.loggerService = inject(LoggerService); this.cryptoService = inject(CryptoService); } } RandomService = _ts_decorate([ Injectable() ], RandomService); export { RandomService };