mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 16:42:56 +00:00
refactor: get appropriate provider via auth helper
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
<div class="centeredMessageContainer">
|
||||
<p>{{ 'auth.messages.default.authorizing' | translate }}</p>
|
||||
<p>
|
||||
{{ 'auth.messages' + '.' + providerType + '.' + 'authorizing' | translate }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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) {
|
||||
|
||||
@@ -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 = {};
|
||||
|
||||
@@ -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<B extends SCAuthorizationProviderType>(
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 = [
|
||||
{
|
||||
|
||||
@@ -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],
|
||||
|
||||
@@ -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 {}
|
||||
@@ -1 +0,0 @@
|
||||
<p>Signing out...</p>
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
<div class="centeredMessageContainer">
|
||||
<p>{{ 'auth.messages.paia.authorizing' | translate }}</p>
|
||||
</div>
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user