Resolve "Auth providers should be ready on components init"

This commit is contained in:
Jovan Krunić
2022-05-13 09:05:33 +00:00
parent 28caaf1d21
commit b7ae2cf019
11 changed files with 77 additions and 96 deletions

View File

@@ -2,18 +2,17 @@ import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {Platform} from '@ionic/angular';
import {Requestor, StorageBackend} from '@openid/appauth';
import {authFactory, paiaAuthFactory, storageFactory} from './factories';
import {DefaultAuthService} from './default-auth.service';
import {storageFactory} from './factories';
import {Browser} from 'ionic-appauth';
import {CapacitorBrowser} from 'ionic-appauth/lib/capacitor';
import {httpFactory} from './factories/http.factory';
import {HttpClient} from '@angular/common/http';
import {PAIAAuthService} from './paia/paia-auth.service';
import {AuthRoutingModule} from './auth-routing.module';
import {TranslateModule} from '@ngx-translate/core';
import {ConfigProvider} from '../config/config.provider';
import {AuthCallbackPageComponent} from './auth-callback/page/auth-callback-page.component';
import {PAIAAuthCallbackPageComponent} from './paia/auth-callback/page/paiaauth-callback-page.component';
import {DefaultAuthService} from './default-auth.service';
import {PAIAAuthService} from './paia/paia-auth.service';
@NgModule({
declarations: [AuthCallbackPageComponent, PAIAAuthCallbackPageComponent],
@@ -33,16 +32,8 @@ import {PAIAAuthCallbackPageComponent} from './paia/auth-callback/page/paiaauth-
provide: Browser,
useClass: CapacitorBrowser,
},
{
provide: DefaultAuthService,
useFactory: authFactory,
deps: [Requestor, Browser, StorageBackend, ConfigProvider],
},
{
provide: PAIAAuthService,
useFactory: paiaAuthFactory,
deps: [Requestor, Browser, StorageBackend, ConfigProvider],
},
DefaultAuthService,
PAIAAuthService,
],
})
export class AuthModule {}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 StApps
* Copyright (C) 2022 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -13,68 +13,20 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
StorageBackend,
Requestor,
AuthorizationServiceConfiguration,
AuthorizationServiceConfigurationJson,
} from '@openid/appauth';
import {Browser, IAuthConfig} from 'ionic-appauth';
import {PAIAAuthService} from '../paia/paia-auth.service';
import {ConfigProvider} from '../../config/config.provider';
import {AuthorizationServiceConfigurationJson} from '@openid/appauth';
import {IAuthConfig} from 'ionic-appauth';
import {
SCAuthorizationProvider,
SCAuthorizationProviderType,
} from '@openstapps/core';
import {DefaultAuthService} from '../default-auth.service';
import {Capacitor} from '@capacitor/core';
import {authPaths} from '../auth-paths';
import {environment} from '../../../../environments/environment';
export const authFactory = (
requestor: Requestor,
browser: Browser,
storage: StorageBackend,
configProvider: ConfigProvider,
) => {
const authService = new DefaultAuthService(browser, storage, requestor);
const authConfig = configProvider.getAnyValue('auth') as {
default: SCAuthorizationProvider;
};
authService.authConfig = getClientConfig('default', authConfig);
authService.localConfiguration = new AuthorizationServiceConfiguration(
getEndpointsConfig('default', authConfig),
);
return authService;
};
export const paiaAuthFactory = (
requestor: Requestor,
browser: Browser,
storage: StorageBackend,
configProvider: ConfigProvider,
) => {
const authService = new PAIAAuthService(browser, storage, requestor);
const authConfig = configProvider.getAnyValue('auth') as {
paia: SCAuthorizationProvider;
};
authService.authConfig = getClientConfig('paia', authConfig);
authService.localConfiguration = new AuthorizationServiceConfiguration(
getEndpointsConfig('paia', authConfig),
);
return authService;
};
import {authPaths} from './auth-paths';
import {environment} from '../../../environments/environment';
/**
* Get configuration of an OAuth2 client
*/
function getClientConfig(
export function getClientConfig(
providerType: SCAuthorizationProviderType,
authConfig: {
default?: SCAuthorizationProvider;
@@ -95,7 +47,7 @@ function getClientConfig(
/**
* Get configuration about endpoints of an OAuth2 server
*/
function getEndpointsConfig(
export function getEndpointsConfig(
providerType: SCAuthorizationProviderType,
authConfig: {
default?: SCAuthorizationProvider;

View File

@@ -15,9 +15,16 @@ import {
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;
@@ -33,6 +40,7 @@ export class DefaultAuthService extends AuthService {
protected browser: Browser = new DefaultBrowser(),
protected storage: StorageBackend = new LocalStorageBackend(),
protected requestor: Requestor = new JQueryRequestor(),
private readonly configProvider: ConfigProvider,
) {
super(browser, storage, requestor);
}
@@ -44,6 +52,16 @@ export class DefaultAuthService extends AuthService {
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));

View File

@@ -1,3 +1,2 @@
export * from './auth.factory';
export * from './browser.factory';
export * from './storage.factory';

View File

@@ -31,6 +31,10 @@ import {PAIAAuthorizationResponse} from './paia-authorization-response';
import {PAIAAuthorizationNotifier} from './paia-authorization-notifier';
import {PAIATokenResponse} from './paia-token-response';
import {IPAIAAuthAction, PAIAAuthActionBuilder} from './paia-auth-action';
import {SCAuthorizationProvider} from '@openstapps/core';
import {ConfigProvider} from '../../config/config.provider';
import {getClientConfig, getEndpointsConfig} from '../auth.provider.methods';
import {Injectable} from '@angular/core';
const TOKEN_KEY = 'auth_paia_token';
const AUTH_EXPIRY_BUFFER = 10 * 60 * -1; // 10 mins in seconds
@@ -44,7 +48,10 @@ export interface IAuthService {
getValidToken(buffer?: number): Promise<PAIATokenResponse>;
}
export class PAIAAuthService implements IAuthService {
@Injectable({
providedIn: 'root',
})
export class PAIAAuthService {
private _authConfig?: IAuthConfig;
private _authSubject: AuthSubject = new AuthSubject();
@@ -78,14 +85,14 @@ export class PAIAAuthService implements IAuthService {
protected browser: Browser = new DefaultBrowser(),
protected storage: StorageBackend = new LocalStorageBackend(),
protected requestor: Requestor = new JQueryRequestor(),
utils = new BasicQueryStringUtils(),
private readonly configProvider: ConfigProvider,
) {
this.tokenHandler = new PAIATokenRequestHandler(requestor);
this.userInfoHandler = new IonicUserInfoHandler(requestor);
this.requestHandler = new PAIAAuthorizationRequestHandler(
browser,
storage,
utils,
new BasicQueryStringUtils(),
crypto,
);
this.endSessionHandler = new IonicEndSessionHandler(browser);
@@ -130,8 +137,19 @@ export class PAIAAuthService implements IAuthService {
}
public async init() {
this.setupConfiguration();
this.setupAuthorizationNotifier();
this.loadTokenFromStorage();
await this.loadTokenFromStorage();
}
setupConfiguration() {
const authConfig = this.configProvider.getAnyValue('auth') as {
paia: SCAuthorizationProvider;
};
this.authConfig = getClientConfig('paia', authConfig);
this.localConfiguration = new AuthorizationServiceConfiguration(
getEndpointsConfig('paia', authConfig),
);
}
protected notifyActionListers(action: IPAIAAuthAction) {