Files
openstapps/src/app/modules/auth/default-auth.service.ts

72 lines
2.1 KiB
TypeScript

import {AuthorizationRequestHandler} from '@openid/appauth';
import {
StorageBackend,
Requestor,
AuthorizationServiceConfiguration,
LocalStorageBackend,
JQueryRequestor,
TokenRequestHandler,
} from '@openid/appauth';
import {
UserInfoHandler,
EndSessionHandler,
Browser,
DefaultBrowser,
AuthService,
AuthActionBuilder,
} from 'ionic-appauth';
import {ConfigProvider} from '../config/config.provider';
import {SCAuthorizationProvider} from '@openstapps/core';
import {getClientConfig, getEndpointsConfig} from './auth.provider.methods';
import {Injectable} from '@angular/core';
const TOKEN_RESPONSE_KEY = 'token_response';
@Injectable({
providedIn: 'root',
})
export class DefaultAuthService extends AuthService {
public localConfiguration: AuthorizationServiceConfiguration;
protected tokenHandler: TokenRequestHandler;
protected userInfoHandler: UserInfoHandler;
protected requestHandler: AuthorizationRequestHandler;
protected endSessionHandler: EndSessionHandler;
constructor(
protected browser: Browser = new DefaultBrowser(),
protected storage: StorageBackend = new LocalStorageBackend(),
protected requestor: Requestor = new JQueryRequestor(),
private readonly configProvider: ConfigProvider,
) {
super(browser, storage, requestor);
}
get configuration(): Promise<AuthorizationServiceConfiguration> {
if (!this.localConfiguration)
throw new Error('Local Configuration Not Defined');
return Promise.resolve(this.localConfiguration);
}
setupConfiguration() {
const authConfig = this.configProvider.getAnyValue('auth') as {
default: SCAuthorizationProvider;
};
this.authConfig = getClientConfig('default', authConfig);
this.localConfiguration = new AuthorizationServiceConfiguration(
getEndpointsConfig('default', authConfig),
);
}
public async signOut() {
await this.storage.removeItem(TOKEN_RESPONSE_KEY).catch(error => {
this.notifyActionListers(AuthActionBuilder.SignOutFailed(error));
});
this.notifyActionListers(AuthActionBuilder.SignOutSuccess());
}
}