mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
refactor: read auth provider info from app config
This commit is contained in:
committed by
Rainer Killinger
parent
a1592f84cc
commit
fb7b3fd1d2
@@ -3,7 +3,7 @@ import {CanActivate, Router, RouterStateSnapshot} from '@angular/router';
|
||||
import {DefaultAuthService} from './default-auth.service';
|
||||
import {PAIAAuthService} from './paia/paia-auth.service';
|
||||
import {IAuthService} from 'ionic-appauth';
|
||||
import {ActivatedAuthRouteSnapshot} from './auth-routes';
|
||||
import {ActivatedProtectedRouteSnapshot} from './protected.routes';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -18,7 +18,7 @@ export class AuthGuardService implements CanActivate {
|
||||
) {}
|
||||
|
||||
public async canActivate(
|
||||
route: ActivatedAuthRouteSnapshot,
|
||||
route: ActivatedProtectedRouteSnapshot,
|
||||
_state: RouterStateSnapshot,
|
||||
) {
|
||||
switch (route.data.authProvider) {
|
||||
|
||||
@@ -1,20 +1,33 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {
|
||||
SCAuthorizationProviderType,
|
||||
SCUserConfiguration,
|
||||
userMapping,
|
||||
} from '../profile/user';
|
||||
import {IPAIAAuthAction} from './paia/paia-auth-action';
|
||||
import {AuthActions, IAuthAction} from 'ionic-appauth';
|
||||
import {TranslateService} from '@ngx-translate/core';
|
||||
import {JSONFile} from '@angular/cli/utilities/json-file';
|
||||
import {JSONPath} from 'jsonpath-plus';
|
||||
import {
|
||||
SCAuthorizationProvider,
|
||||
SCAuthorizationProviderType,
|
||||
SCUserConfiguration,
|
||||
SCUserConfigurationMap,
|
||||
} from '@openstapps/core';
|
||||
import {ConfigProvider} from '../config/config.provider';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthHelperService {
|
||||
constructor(private translateService: TranslateService) {}
|
||||
userConfigurationMap: SCUserConfigurationMap;
|
||||
|
||||
constructor(
|
||||
private translateService: TranslateService,
|
||||
private configProvider: ConfigProvider,
|
||||
) {
|
||||
this.userConfigurationMap = (
|
||||
this.configProvider.getAnyValue('auth') as {
|
||||
default: SCAuthorizationProvider;
|
||||
}
|
||||
).default.endpoints.mapping;
|
||||
}
|
||||
|
||||
public getAuthMessage(
|
||||
provider: SCAuthorizationProviderType,
|
||||
@@ -38,9 +51,11 @@ export class AuthHelperService {
|
||||
|
||||
getUserFromUserInfo(userInfo: JSONFile) {
|
||||
const user: SCUserConfiguration = {id: '', name: '', role: 'student'};
|
||||
for (const key in userMapping) {
|
||||
for (const key in this.userConfigurationMap) {
|
||||
user[key as keyof SCUserConfiguration] = JSONPath({
|
||||
path: userMapping[key as keyof SCUserConfiguration] as string,
|
||||
path: this.userConfigurationMap[
|
||||
key as keyof SCUserConfiguration
|
||||
] as string,
|
||||
json: userInfo,
|
||||
})[0];
|
||||
}
|
||||
|
||||
26
src/app/modules/auth/auth-paths.ts
Normal file
26
src/app/modules/auth/auth-paths.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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 {SCAuthorizationProviderType} from '@openstapps/core';
|
||||
|
||||
export const authPaths: {
|
||||
[key in SCAuthorizationProviderType]: {redirect_path: string};
|
||||
} = {
|
||||
default: {
|
||||
redirect_path: 'auth/callback',
|
||||
},
|
||||
paia: {
|
||||
redirect_path: 'auth/paia/callback',
|
||||
},
|
||||
};
|
||||
@@ -17,10 +17,17 @@ 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';
|
||||
|
||||
const authRoutes: Routes = [
|
||||
{path: 'auth/callback', component: AuthCallbackPageComponent},
|
||||
{path: 'auth/paia/callback', component: PAIAAuthCallbackPageComponent},
|
||||
{
|
||||
path: authPaths.default.redirect_path,
|
||||
component: AuthCallbackPageComponent,
|
||||
},
|
||||
{
|
||||
path: authPaths.paia.redirect_path,
|
||||
component: PAIAAuthCallbackPageComponent,
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,6 +13,7 @@ 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';
|
||||
|
||||
@NgModule({
|
||||
declarations: [AuthCallbackPageComponent, PAIAAuthCallbackPageComponent],
|
||||
@@ -35,12 +36,12 @@ import {PAIAAuthCallbackPageComponent} from './paia/auth-callback/page/auth-call
|
||||
{
|
||||
provide: DefaultAuthService,
|
||||
useFactory: authFactory,
|
||||
deps: [Requestor, Browser, StorageBackend],
|
||||
deps: [Requestor, Browser, StorageBackend, ConfigProvider],
|
||||
},
|
||||
{
|
||||
provide: PAIAAuthService,
|
||||
useFactory: paiaAuthFactory,
|
||||
deps: [Requestor, Browser, StorageBackend],
|
||||
deps: [Requestor, Browser, StorageBackend, ConfigProvider],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
@@ -17,21 +17,36 @@ import {
|
||||
StorageBackend,
|
||||
Requestor,
|
||||
AuthorizationServiceConfiguration,
|
||||
AuthorizationServiceConfigurationJson,
|
||||
} from '@openid/appauth';
|
||||
import {Browser} from 'ionic-appauth';
|
||||
import {environment} from 'src/environments/environment';
|
||||
import {DefaultAuthService} from '../default-auth.service';
|
||||
import {Browser, IAuthConfig} from 'ionic-appauth';
|
||||
import {PAIAAuthService} from '../paia/paia-auth.service';
|
||||
import config from '../../../../../capacitor.config';
|
||||
import {ConfigProvider} from '../../config/config.provider';
|
||||
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);
|
||||
authService.authConfig = environment.oauth2.client.his;
|
||||
const authConfig = configProvider.getAnyValue('auth') as {
|
||||
default: SCAuthorizationProvider;
|
||||
};
|
||||
|
||||
authService.authConfig = getClientConfig('default', authConfig);
|
||||
|
||||
authService.localConfiguration = new AuthorizationServiceConfiguration(
|
||||
environment.oauth2.service.his,
|
||||
getEndpointsConfig('default', authConfig),
|
||||
);
|
||||
|
||||
return authService;
|
||||
@@ -41,12 +56,67 @@ export const paiaAuthFactory = (
|
||||
requestor: Requestor,
|
||||
browser: Browser,
|
||||
storage: StorageBackend,
|
||||
configProvider: ConfigProvider,
|
||||
) => {
|
||||
const authService = new PAIAAuthService(browser, storage, requestor);
|
||||
authService.authConfig = environment.oauth2.client.paia;
|
||||
const authConfig = configProvider.getAnyValue('auth') as {
|
||||
paia: SCAuthorizationProvider;
|
||||
};
|
||||
|
||||
authService.authConfig = getClientConfig('paia', authConfig);
|
||||
|
||||
authService.localConfiguration = new AuthorizationServiceConfiguration(
|
||||
environment.oauth2.service.paia,
|
||||
getEndpointsConfig('paia', authConfig),
|
||||
);
|
||||
|
||||
return authService;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get configuration of an OAuth2 client
|
||||
*/
|
||||
function getClientConfig(
|
||||
providerType: SCAuthorizationProviderType,
|
||||
authConfig: {
|
||||
default?: SCAuthorizationProvider;
|
||||
paia?: SCAuthorizationProvider;
|
||||
},
|
||||
): IAuthConfig {
|
||||
const providerConfig = authConfig[providerType] as SCAuthorizationProvider;
|
||||
return {
|
||||
end_session_redirect_url: '',
|
||||
pkce: true,
|
||||
scopes: providerConfig.client.scopes,
|
||||
server_host: providerConfig.client.url,
|
||||
client_id: providerConfig.client.clientId,
|
||||
redirect_url: getRedirectUrl(authPaths[providerType].redirect_path),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get configuration about endpoints of an OAuth2 server
|
||||
*/
|
||||
function getEndpointsConfig(
|
||||
providerType: SCAuthorizationProviderType,
|
||||
authConfig: {
|
||||
default?: SCAuthorizationProvider;
|
||||
paia?: SCAuthorizationProvider;
|
||||
},
|
||||
): AuthorizationServiceConfigurationJson {
|
||||
const providerConfig = authConfig[providerType] as SCAuthorizationProvider;
|
||||
return {
|
||||
authorization_endpoint: providerConfig.endpoints.authorization,
|
||||
end_session_endpoint: providerConfig.endpoints.endSession,
|
||||
revocation_endpoint: providerConfig.endpoints.revoke ?? '',
|
||||
token_endpoint: providerConfig.endpoints.token,
|
||||
userinfo_endpoint: providerConfig.endpoints.userinfo,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a URL of the app, depending on the platform where it is running
|
||||
*/
|
||||
function getRedirectUrl(routePath: string): string {
|
||||
const appSchema = Capacitor.isNativePlatform() ? config.appId : 'https';
|
||||
return `${appSchema}://${environment.app_host}/${routePath}`;
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@
|
||||
*/
|
||||
|
||||
import {ActivatedRouteSnapshot, Data, Route} from '@angular/router';
|
||||
import {SCAuthorizationProviderType} from '../profile/user';
|
||||
import {SCAuthorizationProviderType} from '@openstapps/core';
|
||||
|
||||
export interface AuthRoute extends Route {
|
||||
export interface ProtectedRoute extends Route {
|
||||
data: {
|
||||
authProvider: SCAuthorizationProviderType;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@@ -24,8 +24,8 @@ export interface AuthRoute extends Route {
|
||||
};
|
||||
}
|
||||
|
||||
export class ActivatedAuthRouteSnapshot extends ActivatedRouteSnapshot {
|
||||
data: Data & {authProvider: AuthRoute['data']['authProvider']};
|
||||
export class ActivatedProtectedRouteSnapshot extends ActivatedRouteSnapshot {
|
||||
data: Data & {authProvider: ProtectedRoute['data']['authProvider']};
|
||||
}
|
||||
|
||||
export type AuthRoutes = AuthRoute[];
|
||||
export type ProtectedRoutes = ProtectedRoute[];
|
||||
Reference in New Issue
Block a user