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

OZG-4993 OZG-5146 Provide getRefreshToken in authentication service

parent c055ae14
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,8 @@ 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 { cold } from 'jest-marbles';
import faker from '@faker-js/faker';
describe('AuthenticationService', () => {
let service: AuthenticationService;
......@@ -19,7 +21,6 @@ describe('AuthenticationService', () => {
loadDiscoveryDocumentAndLogin: jest.fn().mockResolvedValue(() => Promise.resolve()),
};
environmentConfig = createEnvironment();
service = new AuthenticationService(useFromMock(oAuthService), environmentConfig);
});
......@@ -108,4 +109,32 @@ describe('AuthenticationService', () => {
expect(oAuthService.revokeTokenAndLogout).toHaveBeenCalled();
});
});
describe('get refresh token', () => {
let refreshToken1 = faker.random.alphaNumeric(40);
let refreshToken2 = faker.random.alphaNumeric(40);
beforeEach(() => {
oAuthService.getRefreshToken
.mockImplementationOnce(() => refreshToken1)
.mockImplementationOnce(() => refreshToken2);
});
it('should emit one refresh token string for each token_received event', () => {
const events = cold('-a-a-b-|', {
a: {
type: 'token_received',
},
b: {
type: 'token_error',
},
});
Object.defineProperty(oAuthService, 'events', { value: events });
const refreshTokens = cold('-a-b---|', {
a: refreshToken1,
b: refreshToken2,
});
expect(service.getRefreshToken()).toBeObservable(refreshTokens);
});
});
});
import { ENVIRONMENT_CONFIG, Environment } from '@alfa-client/environment-shared';
import { Environment, ENVIRONMENT_CONFIG } from '@alfa-client/environment-shared';
import { Inject, Injectable } from '@angular/core';
import { OAuthService, AuthConfig } from 'angular-oauth2-oidc';
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 { filter, map, Observable } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class AuthenticationService {
......@@ -14,6 +15,13 @@ export class AuthenticationService {
@Inject(ENVIRONMENT_CONFIG) private envConfig: Environment,
) {}
getRefreshToken(): Observable<string> {
return this.oAuthService.events.pipe(
filter((event) => event.type === 'token_received'),
map(() => this.oAuthService.getRefreshToken()),
);
}
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