feat: init and fork some code from angular-auth-oidc-client
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run

This commit is contained in:
2025-01-18 01:05:00 +08:00
parent 276d9fbda8
commit 983254164b
201 changed files with 35689 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
import { TestBed } from '@angular/core/testing';
import { mockProvider } from '../../../test/auto-mock';
import { LoggerService } from '../../logging/logger.service';
import { CryptoService } from '../../utils/crypto/crypto.service';
import { RandomService } from './random.service';
describe('RandomService Tests', () => {
let randomService: RandomService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [RandomService, mockProvider(LoggerService), CryptoService],
});
});
beforeEach(() => {
randomService = TestBed.inject(RandomService);
});
it('should create', () => {
expect(randomService).toBeTruthy();
});
it('should be not equal', () => {
const r1 = randomService.createRandom(45, { configId: 'configId1' });
const r2 = randomService.createRandom(45, { configId: 'configId1' });
expect(r1).not.toEqual(r2);
});
it('correct length with high number', () => {
const r1 = randomService.createRandom(79, { configId: 'configId1' });
const result = r1.length;
expect(result).toBe(79);
});
it('correct length with small number', () => {
const r1 = randomService.createRandom(7, { configId: 'configId1' });
const result = r1.length;
expect(result).toBe(7);
});
it('correct length with 0', () => {
const r1 = randomService.createRandom(0, { configId: 'configId1' });
const result = r1.length;
expect(result).toBe(0);
expect(r1).toBe('');
});
for (let index = 1; index < 7; index++) {
it('Giving back 10 or more characters when called with numbers less than 7', () => {
const requiredLengthSmallerThenSeven = index;
const fallbackLength = 10;
const r1 = randomService.createRandom(requiredLengthSmallerThenSeven, {
configId: 'configId1',
});
expect(r1.length).toBeGreaterThanOrEqual(fallbackLength);
});
}
});

View File

@@ -0,0 +1,60 @@
import { inject, Injectable } from 'injection-js';
import { OpenIdConfiguration } from '../../config/openid-configuration';
import { LoggerService } from '../../logging/logger.service';
import { CryptoService } from '../../utils/crypto/crypto.service';
@Injectable()
export class RandomService {
private readonly loggerService = inject(LoggerService);
private readonly cryptoService = inject(CryptoService);
createRandom(
requiredLength: number,
configuration: OpenIdConfiguration
): string {
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);
}
private toHex(dec: number): string {
return ('0' + dec.toString(16)).substr(-2);
}
private randomString(length: number): string {
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;
}
}