From eaf9da3c84bc2fb0b1710540534266e25bf4e558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Kruni=C4=87?= Date: Mon, 14 Feb 2022 11:12:02 +0100 Subject: [PATCH] refactor: get appropriate provider via auth helper --- src/app/app.component.spec.ts | 9 ++++ src/app/app.component.ts | 34 ++++++--------- src/app/app.module.ts | 2 - .../page/auth-callback-page.component.html | 4 +- .../page/auth-callback-page.component.ts | 39 ++++++++++++----- src/app/modules/auth/auth-guard.service.ts | 25 +++-------- src/app/modules/auth/auth-helper.service.ts | 18 ++++++++ src/app/modules/auth/auth-routing.module.ts | 4 +- src/app/modules/auth/auth.module.ts | 4 +- .../auth/end-session/end-session.module.ts | 26 ----------- .../page/end-session-page.component.html | 1 - .../page/end-session-page.component.scss | 0 .../page/end-session-page.component.ts | 20 --------- .../page/auth-callback-page.component.html | 3 -- .../page/auth-callback-page.component.scss | 0 .../page/auth-callback-page.component.ts | 43 ------------------- .../page/paiaauth-callback-page.component.ts | 22 ++++++++++ src/app/modules/map/map.provider.ts | 2 +- .../profile/page/profile-page.component.html | 14 +++--- .../page/profile-page.component.spec.ts | 13 +++++- .../profile/page/profile-page.component.ts | 40 +++++++---------- 21 files changed, 141 insertions(+), 182 deletions(-) delete mode 100644 src/app/modules/auth/end-session/end-session.module.ts delete mode 100644 src/app/modules/auth/end-session/page/end-session-page.component.html delete mode 100644 src/app/modules/auth/end-session/page/end-session-page.component.scss delete mode 100644 src/app/modules/auth/end-session/page/end-session-page.component.ts delete mode 100644 src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.html delete mode 100644 src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.scss delete mode 100644 src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.ts create mode 100644 src/app/modules/auth/paia/auth-callback/page/paiaauth-callback-page.component.ts diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index f9be438d..f6fe445e 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -29,6 +29,7 @@ import {NGXLogger} from 'ngx-logger'; import {RouterTestingModule} from '@angular/router/testing'; import {ScheduleSyncService} from './modules/background/schedule/schedule-sync.service'; import {sampleAuthConfiguration} from './_helpers/data/sample-configuration'; +import {StorageProvider} from './modules/storage/storage.provider'; describe('AppComponent', () => { let platformReadySpy: any; @@ -40,6 +41,7 @@ describe('AppComponent', () => { let ngxLogger: jasmine.SpyObj; let scheduleSyncServiceSpy: jasmine.SpyObj; let platformIsSpy; + let storageProvider: jasmine.SpyObj; beforeEach( waitForAsync(() => { @@ -75,6 +77,12 @@ describe('AppComponent', () => { return sampleAuthConfiguration; }); ngxLogger = jasmine.createSpyObj('NGXLogger', ['log', 'error', 'warn']); + storageProvider = jasmine.createSpyObj('StorageProvider', [ + 'init', + 'get', + 'has', + 'put', + ]); TestBed.configureTestingModule({ imports: [ @@ -91,6 +99,7 @@ describe('AppComponent', () => { {provide: SettingsProvider, useValue: settingsProvider}, {provide: ConfigProvider, useValue: configProvider}, {provide: NGXLogger, useValue: ngxLogger}, + {provide: StorageProvider, useValue: storageProvider}, ], schemas: [CUSTOM_ELEMENTS_SCHEMA], }).compileComponents(); diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 5ba5648c..2da40227 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -18,8 +18,6 @@ import {App, URLOpenListenerEvent} from '@capacitor/app'; import {SplashScreen} from '@capacitor/splash-screen'; import {Platform, ToastController} from '@ionic/angular'; import {SettingsProvider} from './modules/settings/settings.provider'; -import {PAIAAuthService} from './modules/auth/paia/paia-auth.service'; -import {DefaultAuthService} from './modules/auth/default-auth.service'; import {AuthHelperService} from './modules/auth/auth-helper.service'; import {ScheduleSyncService} from './modules/background/schedule/schedule-sync.service'; import {environment} from '../environments/environment'; @@ -52,9 +50,7 @@ export class AppComponent implements AfterContentInit { * @param settingsProvider TODO * @param router The angular router * @param zone The angular zone - * @param defaultAuth Auth Service - * @param paiaAuth Auth Service - * @param authHelperService Helper service for OAuth providers + * @param authHelper Helper service for OAuth providers * @param toastController Toast controller * @param scheduleSync TODO */ @@ -63,9 +59,7 @@ export class AppComponent implements AfterContentInit { private readonly settingsProvider: SettingsProvider, private readonly router: Router, private readonly zone: NgZone, - private readonly defaultAuth: DefaultAuthService, - private readonly paiaAuth: PAIAAuthService, - private readonly authHelperService: AuthHelperService, + private readonly authHelper: AuthHelperService, private readonly toastController: ToastController, private readonly scheduleSync: ScheduleSyncService, ) { @@ -92,8 +86,6 @@ export class AppComponent implements AfterContentInit { }); this.platform.ready().then(async () => { await this.authInit(); - await this.defaultAuth.init(); - await this.paiaAuth.init(); await SplashScreen.hide(); // set order of categories in settings @@ -107,16 +99,18 @@ export class AppComponent implements AfterContentInit { } private async authInit() { - await this.defaultAuth.init(); - await this.paiaAuth.init(); - this.defaultAuth.events$.subscribe(action => - this.showMessage( - this.authHelperService.getAuthMessage('default', action), - ), - ); - this.paiaAuth.events$.subscribe(action => - this.showMessage(this.authHelperService.getAuthMessage('paia', action)), - ); + await this.authHelper.getProvider('default').init(); + await this.authHelper.getProvider('paia').init(); + this.authHelper + .getProvider('default') + .events$.subscribe(action => + this.showMessage(this.authHelper.getAuthMessage('default', action)), + ); + this.authHelper + .getProvider('paia') + .events$.subscribe(action => + this.showMessage(this.authHelper.getAuthMessage('paia', action)), + ); } private async showMessage(message?: string) { diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 3a8f733c..3de84aaa 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -56,7 +56,6 @@ import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; import {AboutModule} from './modules/about/about.module'; import {FavoritesModule} from './modules/favorites/favorites.module'; import {ProfilePageModule} from './modules/profile/profile.module'; -import {EndSessionPageModule} from './modules/auth/end-session/end-session.module'; import {FeedbackModule} from './modules/feedback/feedback.module'; import {DebugDataCollectorService} from './modules/data/debug-data-collector.service'; import {Browser} from './util/browser.factory'; @@ -140,7 +139,6 @@ export function createTranslateLoader(http: HttpClient) { ConfigModule, DataModule, HebisModule, - EndSessionPageModule, IonicModule.forRoot(), FavoritesModule, LibraryModule, diff --git a/src/app/modules/auth/auth-callback/page/auth-callback-page.component.html b/src/app/modules/auth/auth-callback/page/auth-callback-page.component.html index fe073705..dbe5f612 100644 --- a/src/app/modules/auth/auth-callback/page/auth-callback-page.component.html +++ b/src/app/modules/auth/auth-callback/page/auth-callback-page.component.html @@ -1,3 +1,5 @@
-

{{ 'auth.messages.default.authorizing' | translate }}

+

+ {{ 'auth.messages' + '.' + providerType + '.' + 'authorizing' | translate }} +

diff --git a/src/app/modules/auth/auth-callback/page/auth-callback-page.component.ts b/src/app/modules/auth/auth-callback/page/auth-callback-page.component.ts index 43441d46..81c973cb 100644 --- a/src/app/modules/auth/auth-callback/page/auth-callback-page.component.ts +++ b/src/app/modules/auth/auth-callback/page/auth-callback-page.component.ts @@ -1,33 +1,52 @@ -import {Component, OnInit, OnDestroy} from '@angular/core'; +/* + * 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. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +import {OnInit, OnDestroy, Component} from '@angular/core'; import {NavController} from '@ionic/angular'; import {Router} from '@angular/router'; import {AuthActions, IAuthAction} from 'ionic-appauth'; import {Subscription} from 'rxjs'; -import {DefaultAuthService} from '../../default-auth.service'; +import {SCAuthorizationProviderType} from '@openstapps/core'; import {AuthHelperService} from '../../auth-helper.service'; @Component({ - selector: 'auth-callback', - templateUrl: './auth-callback-page.component.html', - styleUrls: ['./auth-callback-page.component.scss'], + templateUrl: 'auth-callback-page.component.html', + styleUrls: ['auth-callback-page.component.scss'], }) export class AuthCallbackPageComponent implements OnInit, OnDestroy { - sub: Subscription; + providerType: SCAuthorizationProviderType = 'default'; + + private authEvents: Subscription; constructor( - private auth: DefaultAuthService, private navCtrl: NavController, private router: Router, private authHelper: AuthHelperService, ) {} ngOnInit() { - this.sub = this.auth.events$.subscribe(action => this.postCallback(action)); - this.auth.authorizationCallback(window.location.origin + this.router.url); + this.authEvents = this.authHelper + .getProvider(this.providerType) + .events$.subscribe((action: IAuthAction) => this.postCallback(action)); + this.authHelper + .getProvider(this.providerType) + .authorizationCallback(window.location.origin + this.router.url); } ngOnDestroy() { - this.sub.unsubscribe(); + this.authEvents.unsubscribe(); } async postCallback(action: IAuthAction) { diff --git a/src/app/modules/auth/auth-guard.service.ts b/src/app/modules/auth/auth-guard.service.ts index cdc9b43c..5b89d594 100644 --- a/src/app/modules/auth/auth-guard.service.ts +++ b/src/app/modules/auth/auth-guard.service.ts @@ -5,38 +5,23 @@ import { Router, RouterStateSnapshot, } from '@angular/router'; -import {DefaultAuthService} from './default-auth.service'; -import {PAIAAuthService} from './paia/paia-auth.service'; -import {IAuthService} from 'ionic-appauth'; import {ActivatedProtectedRouteSnapshot} from './protected.routes'; +import {AuthHelperService} from './auth-helper.service'; @Injectable({ providedIn: 'root', }) export class AuthGuardService implements CanActivate { - authService: IAuthService | PAIAAuthService; - - constructor( - private defaultAuth: DefaultAuthService, - private paiaAuth: PAIAAuthService, - private router: Router, - ) {} + constructor(private authHelper: AuthHelperService, private router: Router) {} public async canActivate( route: ActivatedProtectedRouteSnapshot, _state: RouterStateSnapshot, ) { - switch (route.data.authProvider) { - case 'paia': - this.authService = this.paiaAuth; - break; - default: - this.authService = this.defaultAuth; - break; - } - try { - await this.authService.getValidToken(); + await this.authHelper + .getProvider(route.data.authProvider) + .getValidToken(); } catch { const originNavigation = this.router.getCurrentNavigation(); let extras: NavigationExtras = {}; diff --git a/src/app/modules/auth/auth-helper.service.ts b/src/app/modules/auth/auth-helper.service.ts index b7b994e2..2c1427a8 100644 --- a/src/app/modules/auth/auth-helper.service.ts +++ b/src/app/modules/auth/auth-helper.service.ts @@ -12,6 +12,8 @@ import { } from '@openstapps/core'; import {ConfigProvider} from '../config/config.provider'; import {StorageProvider} from '../storage/storage.provider'; +import {DefaultAuthService} from './default-auth.service'; +import {PAIAAuthService} from './paia/paia-auth.service'; const AUTH_ORIGIN_PATH = 'stapps.auth.origin_path'; @Injectable({ @@ -24,6 +26,8 @@ export class AuthHelperService { private translateService: TranslateService, private configProvider: ConfigProvider, private storageProvider: StorageProvider, + private defaultAuth: DefaultAuthService, + private paiaAuth: PAIAAuthService, ) { this.userConfigurationMap = ( this.configProvider.getAnyValue('auth') as { @@ -83,4 +87,18 @@ export class AuthHelperService { } return originPath; } + + getProvider( + providerType: B, + ): B extends 'paia' ? PAIAAuthService : DefaultAuthService; + /** + * Provides appropriate auth service instance based on type (string) parameter + */ + getProvider( + providerType: SCAuthorizationProviderType, + ): DefaultAuthService | PAIAAuthService { + return providerType === 'paia' + ? (this.paiaAuth as PAIAAuthService) + : (this.defaultAuth as DefaultAuthService); + } } diff --git a/src/app/modules/auth/auth-routing.module.ts b/src/app/modules/auth/auth-routing.module.ts index 20dc2afb..d8c5f9e5 100644 --- a/src/app/modules/auth/auth-routing.module.ts +++ b/src/app/modules/auth/auth-routing.module.ts @@ -15,9 +15,9 @@ import {RouterModule, Routes} from '@angular/router'; import {NgModule} from '@angular/core'; -import {AuthCallbackPageComponent} from './auth-callback/page/auth-callback-page.component'; -import {PAIAAuthCallbackPageComponent} from './paia/auth-callback/page/auth-callback-page.component'; import {authPaths} from './auth-paths'; +import {AuthCallbackPageComponent} from './auth-callback/page/auth-callback-page.component'; +import {PAIAAuthCallbackPageComponent} from './paia/auth-callback/page/paiaauth-callback-page.component'; const authRoutes: Routes = [ { diff --git a/src/app/modules/auth/auth.module.ts b/src/app/modules/auth/auth.module.ts index 7a173129..ec2be531 100644 --- a/src/app/modules/auth/auth.module.ts +++ b/src/app/modules/auth/auth.module.ts @@ -11,9 +11,9 @@ 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 {AuthCallbackPageComponent} from './auth-callback/page/auth-callback-page.component'; -import {PAIAAuthCallbackPageComponent} from './paia/auth-callback/page/auth-callback-page.component'; 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'; @NgModule({ declarations: [AuthCallbackPageComponent, PAIAAuthCallbackPageComponent], diff --git a/src/app/modules/auth/end-session/end-session.module.ts b/src/app/modules/auth/end-session/end-session.module.ts deleted file mode 100644 index 0e3655a8..00000000 --- a/src/app/modules/auth/end-session/end-session.module.ts +++ /dev/null @@ -1,26 +0,0 @@ -import {NgModule} from '@angular/core'; -import {CommonModule} from '@angular/common'; -import {FormsModule} from '@angular/forms'; -import {Routes, RouterModule} from '@angular/router'; - -import {IonicModule} from '@ionic/angular'; - -import {EndSessionPageComponent} from './page/end-session-page.component'; - -const routes: Routes = [ - { - path: 'logout', - component: EndSessionPageComponent, - }, -]; - -@NgModule({ - imports: [ - CommonModule, - FormsModule, - IonicModule, - RouterModule.forChild(routes), - ], - declarations: [EndSessionPageComponent], -}) -export class EndSessionPageModule {} diff --git a/src/app/modules/auth/end-session/page/end-session-page.component.html b/src/app/modules/auth/end-session/page/end-session-page.component.html deleted file mode 100644 index dfb55534..00000000 --- a/src/app/modules/auth/end-session/page/end-session-page.component.html +++ /dev/null @@ -1 +0,0 @@ -

Signing out...

diff --git a/src/app/modules/auth/end-session/page/end-session-page.component.scss b/src/app/modules/auth/end-session/page/end-session-page.component.scss deleted file mode 100644 index e69de29b..00000000 diff --git a/src/app/modules/auth/end-session/page/end-session-page.component.ts b/src/app/modules/auth/end-session/page/end-session-page.component.ts deleted file mode 100644 index dafab82b..00000000 --- a/src/app/modules/auth/end-session/page/end-session-page.component.ts +++ /dev/null @@ -1,20 +0,0 @@ -import {Component, OnInit} from '@angular/core'; -import {NavController} from '@ionic/angular'; -import {DefaultAuthService} from '../../default-auth.service'; - -@Component({ - selector: 'end-session', - templateUrl: './end-session-page.component.html', - styleUrls: ['./end-session-page.component.scss'], -}) -export class EndSessionPageComponent implements OnInit { - constructor( - private auth: DefaultAuthService, - private navCtrl: NavController, - ) {} - - async ngOnInit() { - this.auth.endSessionCallback(); - await this.navCtrl.navigateRoot('profile'); - } -} diff --git a/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.html b/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.html deleted file mode 100644 index c531a22b..00000000 --- a/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.html +++ /dev/null @@ -1,3 +0,0 @@ -
-

{{ 'auth.messages.paia.authorizing' | translate }}

-
diff --git a/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.scss b/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.scss deleted file mode 100644 index e69de29b..00000000 diff --git a/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.ts b/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.ts deleted file mode 100644 index 174b6dee..00000000 --- a/src/app/modules/auth/paia/auth-callback/page/auth-callback-page.component.ts +++ /dev/null @@ -1,43 +0,0 @@ -import {Component, OnInit, OnDestroy} from '@angular/core'; -import {NavController} from '@ionic/angular'; -import {Router} from '@angular/router'; -import {AuthActions, IAuthAction} from 'ionic-appauth'; -import {Subscription} from 'rxjs'; -import {PAIAAuthService} from '../../paia-auth.service'; -import {AuthHelperService} from '../../../auth-helper.service'; - -@Component({ - selector: 'auth-callback', - templateUrl: './auth-callback-page.component.html', - styleUrls: ['./auth-callback-page.component.scss'], -}) -export class PAIAAuthCallbackPageComponent implements OnInit, OnDestroy { - sub: Subscription; - - constructor( - private auth: PAIAAuthService, - private navCtrl: NavController, - private router: Router, - private authHelper: AuthHelperService, - ) {} - - ngOnInit() { - this.sub = this.auth.events$.subscribe(action => this.postCallback(action)); - this.auth.authorizationCallback(window.location.origin + this.router.url); - } - - ngOnDestroy() { - this.sub.unsubscribe(); - } - - async postCallback(action: IAuthAction) { - if (action.action === AuthActions.SignInSuccess) { - const originPath = await this.authHelper.getOriginPath(); - this.navCtrl.navigateRoot(originPath ?? 'profile'); - this.authHelper.deleteOriginPath(); - } - if (action.action === AuthActions.SignInFailed) { - this.navCtrl.navigateRoot('profile'); - } - } -} diff --git a/src/app/modules/auth/paia/auth-callback/page/paiaauth-callback-page.component.ts b/src/app/modules/auth/paia/auth-callback/page/paiaauth-callback-page.component.ts new file mode 100644 index 00000000..5881ccbb --- /dev/null +++ b/src/app/modules/auth/paia/auth-callback/page/paiaauth-callback-page.component.ts @@ -0,0 +1,22 @@ +import {Component} from '@angular/core'; +import {AuthCallbackPageComponent} from '../../../auth-callback/page/auth-callback-page.component'; +import {SCAuthorizationProviderType} from '@openstapps/core'; +import {NavController} from '@ionic/angular'; +import {Router} from '@angular/router'; +import {AuthHelperService} from '../../../auth-helper.service'; + +@Component({ + templateUrl: '../../../auth-callback/page/auth-callback-page.component.html', + styleUrls: ['../../../auth-callback/page/auth-callback-page.component.scss'], +}) +export class PAIAAuthCallbackPageComponent extends AuthCallbackPageComponent { + providerType = 'paia' as SCAuthorizationProviderType; + + constructor( + navCtrl: NavController, + router: Router, + authHelper: AuthHelperService, + ) { + super(navCtrl, router, authHelper); + } +} diff --git a/src/app/modules/map/map.provider.ts b/src/app/modules/map/map.provider.ts index b63f6941..fe345033 100644 --- a/src/app/modules/map/map.provider.ts +++ b/src/app/modules/map/map.provider.ts @@ -134,7 +134,7 @@ export class MapProvider { /** * Provide places (buildings and canteens) const result = await this.dataProvider.search(query); - + * * @param contextFilter Additional contextual filter (e.g. from the context menu) * @param queryText Query (text) of the search query diff --git a/src/app/modules/profile/page/profile-page.component.html b/src/app/modules/profile/page/profile-page.component.html index 7b90518d..ff60da52 100644 --- a/src/app/modules/profile/page/profile-page.component.html +++ b/src/app/modules/profile/page/profile-page.component.html @@ -46,11 +46,13 @@