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
@@ -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}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user