Skip to content
Snippets Groups Projects
Commit fb0f46b5 authored by Jan Zickermann's avatar Jan Zickermann
Browse files

OZG-4993 Avoid using keycloak-admin-client in authentication

parent 72bbce2f
No related branches found
No related tags found
No related merge requests found
......@@ -2,33 +2,32 @@ import { TestBed } from '@angular/core/testing';
import { KeycloakRepository } from './keycloak.repository';
import KcAdminClient from '@keycloak/keycloak-admin-client';
import { Mock, mock } from '@alfa-client/test-utils';
import { AuthenticationService } from 'authentication';
import { createGroupRepresentation, createKeycloakGroup } from '../../../test/keycloak/keycloak';
import { singleCold } from '../../../../tech-shared/src/lib/resource/marbles';
import GroupRepresentation from '@keycloak/keycloak-admin-client/lib/defs/groupRepresentation';
import { KeycloakGroup } from './keycloak.model';
import { TokenProvider } from '@keycloak/keycloak-admin-client/lib/client';
import { firstValueFrom, Observable, of } from 'rxjs';
import { faker } from '@faker-js/faker';
import { OAuthService } from 'angular-oauth2-oidc';
describe('KeycloakRepository', () => {
const accessToken = faker.random.alphaNumeric(40);
let repository: KeycloakRepository;
let kcAdminClient: Mock<KcAdminClient>;
let authenticationService: Mock<AuthenticationService>;
let tokenProvider: TokenProvider;
let oAuthService: Mock<OAuthService>;
beforeEach(() => {
kcAdminClient = mock(KcAdminClient);
authenticationService = mock(AuthenticationService);
oAuthService = mock(OAuthService);
TestBed.configureTestingModule({
providers: [
{ provide: OAuthService, useValue: oAuthService },
{ provide: KcAdminClient, useValue: kcAdminClient },
{ provide: AuthenticationService, useValue: authenticationService },
],
});
tokenProvider = { getAccessToken: () => Promise.resolve('token') };
authenticationService.getTokenProvider.mockReturnValue(tokenProvider);
oAuthService.getAccessToken.mockReturnValue(accessToken);
repository = TestBed.inject(KeycloakRepository);
});
......@@ -36,8 +35,12 @@ describe('KeycloakRepository', () => {
expect(repository).toBeTruthy();
});
it('should register token provider from authentication service', () => {
expect(kcAdminClient.registerTokenProvider).toHaveBeenCalledWith(tokenProvider);
describe('registerTokenProvider', () => {
it('should register token provider from oauth service', async () => {
const tokenProvider: TokenProvider = kcAdminClient.registerTokenProvider.mock.calls[0][0];
const token: string = await tokenProvider.getAccessToken();
expect(token).toEqual(accessToken);
});
});
describe('map group representation', () => {
......
......@@ -3,21 +3,28 @@ import { KeycloakGroup } from './keycloak.model';
import { from, map, Observable } from 'rxjs';
import KcAdminClient from '@keycloak/keycloak-admin-client';
import GroupRepresentation from '@keycloak/keycloak-admin-client/lib/defs/groupRepresentation';
import { AuthenticationService } from 'authentication';
import { OAuthService } from 'angular-oauth2-oidc';
import { TokenProvider } from '@keycloak/keycloak-admin-client/lib/client';
@Injectable({
providedIn: 'root',
})
export class KeycloakRepository {
constructor(
private authenticationService: AuthenticationService,
private oAuthService: OAuthService,
private kcAdminClient: KcAdminClient,
) {
this.registerAccessTokenProvider();
}
private registerAccessTokenProvider(): void {
this.kcAdminClient.registerTokenProvider(this.authenticationService.getTokenProvider());
this.kcAdminClient.registerTokenProvider(this.getTokenProvider());
}
private getTokenProvider(): TokenProvider {
return {
getAccessToken: () => Promise.resolve(this.oAuthService.getAccessToken()),
};
}
public findGroups(): Observable<KeycloakGroup[]> {
......
/* eslint-disable */
const esModules = ['@keycloak/keycloak-admin-client', 'url-join', 'url-template', 'camelize-ts'];
export default {
displayName: 'authentication',
preset: '../../jest.preset.js',
......@@ -15,7 +14,7 @@ export default {
},
],
},
transformIgnorePatterns: [`node_modules/(?!.*\\.mjs$|${esModules.join('|')})`],
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
......
......@@ -6,17 +6,13 @@ import { AuthenticationService } from './authentication.service';
import { createAuthConfig } from '../../test/authentication';
import { createEnvironment } from '../../../environment-shared/test/environment';
import { Environment } from 'libs/environment-shared/src/lib/environment.model';
import KcAdminClient from '@keycloak/keycloak-admin-client';
import { faker } from '@faker-js/faker';
describe('AuthenticationService', () => {
let service: AuthenticationService;
let oAuthService: Mock<OAuthService>;
let environmentConfig: Environment;
let kcAdminClient: Mock<KcAdminClient>;
beforeEach(() => {
kcAdminClient = mock(KcAdminClient);
oAuthService = <any>{
...mock(OAuthService),
loadDiscoveryDocumentAndLogin: jest.fn().mockResolvedValue(() => Promise.resolve()),
......@@ -66,18 +62,6 @@ describe('AuthenticationService', () => {
});
});
describe('get token provider', () => {
it('should provide access token from oauth service', async () => {
const accessToken: string = faker.random.alphaNumeric(40);
oAuthService.getAccessToken.mockReturnValue(accessToken);
const provider = service.getTokenProvider();
const resultAccessToken = await provider.getAccessToken();
expect(resultAccessToken).toEqual(accessToken);
});
});
describe('set current user', () => {
const identityClaims: Record<string, any> = {
['given_name']: 'Marco',
......
......@@ -4,7 +4,6 @@ import { AuthConfig, OAuthService } from 'angular-oauth2-oidc';
import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks';
import { UserProfileResource } from 'libs/user-profile-shared/src/lib/user-profile.model';
import { getUserNameInitials } from 'libs/user-profile-shared/src/lib/user-profile.util';
import { TokenProvider } from '@keycloak/keycloak-admin-client/lib/client';
@Injectable({ providedIn: 'root' })
export class AuthenticationService {
......@@ -15,12 +14,6 @@ export class AuthenticationService {
@Inject(ENVIRONMENT_CONFIG) private envConfig: Environment,
) {}
public getTokenProvider(): TokenProvider {
return {
getAccessToken: () => Promise.resolve(this.oAuthService.getAccessToken()),
};
}
public async login(): Promise<void> {
this.oAuthService.configure(this.buildConfiguration());
this.oAuthService.setupAutomaticSilentRefresh();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment