|
| 1 | +# Authentication configuration |
| 2 | + |
| 3 | +The portal does not provide any authentication mechanism. It requires third-party services to provide authentication. |
| 4 | +But it has the means to facilitate the integration of such services. |
| 5 | +If you want to specify the OAuth2 services to be used, you can use the `EnvAuthConfigService` as a `authConfigProvider` option, |
| 6 | +along with providing the environment variable to the portal, according to the [instruction](https://github.com/openmfp/portal-server-lib?tab=readme-ov-file#environment-properties). |
| 7 | + |
| 8 | +```ts |
| 9 | +import { Module } from '@nestjs/common'; |
| 10 | +import { PortalModule, PortalModuleOptions, EnvAuthConfigService } from '@openmfp/portal-server-lib'; |
| 11 | + |
| 12 | +const portalOptions: PortalModuleOptions = { |
| 13 | + authConfigProvider: EnvAuthConfigService |
| 14 | +}; |
| 15 | + |
| 16 | +@Module({ |
| 17 | + imports: [PortalModule.create(portalOptions)], |
| 18 | +}) |
| 19 | +export class AppModule {} |
| 20 | +``` |
| 21 | + |
| 22 | +The variables will be read from the environment and used to facilitate the authentication flow. |
| 23 | + |
| 24 | +## AuthConfigService Interface |
| 25 | + |
| 26 | +You might want as well to provide your own implementation of the `AuthConfigService` interface, meeting the requirements below: |
| 27 | + |
| 28 | +```ts |
| 29 | +export interface ServerAuthVariables { |
| 30 | + idpName?: string; |
| 31 | + baseDomain: string; |
| 32 | + oauthServerUrl: string; |
| 33 | + oauthTokenUrl: string; |
| 34 | + clientId: string; |
| 35 | + clientSecret: string; |
| 36 | + oidcIssuerUrl?: string; |
| 37 | + endSessionUrl?: string; |
| 38 | +} |
| 39 | + |
| 40 | +export interface AuthConfigService { |
| 41 | + getAuthConfig(request: Request): Promise<ServerAuthVariables>; |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Provide your implemented services to the portal options |
| 46 | + |
| 47 | +```ts |
| 48 | +import { Module } from '@nestjs/common'; |
| 49 | +import { PortalModule, PortalModuleOptions } from '@openmfp/portal-server-lib'; |
| 50 | + |
| 51 | +const portalOptions: PortalModuleOptions = { |
| 52 | + authConfigProvider: AuthConfigServiceImpl |
| 53 | +}; |
| 54 | + |
| 55 | +@Module({ |
| 56 | + imports: [PortalModule.create(portalOptions)], |
| 57 | +}) |
| 58 | +export class AppModule {} |
| 59 | +``` |
| 60 | + |
| 61 | +Once implemented, the portal will use your implementation to get the authentication variables. |
| 62 | + |
| 63 | + |
| 64 | +## Authentication callback |
| 65 | + |
| 66 | +You might want as well to provide an authentication callback to act upon different authentication outcomes, |
| 67 | +failure or success. |
| 68 | + |
| 69 | +```ts |
| 70 | +export interface AuthCallback { |
| 71 | + handleSuccess( |
| 72 | + request: Request, |
| 73 | + response: Response, |
| 74 | + authTokenResponse: AuthTokenData, |
| 75 | + ): Promise<any>; |
| 76 | + |
| 77 | + handleFailure(request: Request, response: Response): Promise<any>; |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +And add it to the portal options: |
| 82 | + |
| 83 | +```ts |
| 84 | +import { Module } from '@nestjs/common'; |
| 85 | +import { PortalModule, PortalModuleOptions } from '@openmfp/portal-server-lib'; |
| 86 | + |
| 87 | +const portalOptions: PortalModuleOptions = { |
| 88 | + authCallbackProvider: AuthCallbackProviderImpl, |
| 89 | +}; |
| 90 | + |
| 91 | +@Module({ |
| 92 | + imports: [PortalModule.create(portalOptions)], |
| 93 | +}) |
| 94 | +export class AppModule {} |
| 95 | +``` |
| 96 | + |
| 97 | +## Logout callback |
| 98 | + |
| 99 | +By specifying the `logoutCallbackProvider` option, you can provide a logout callback to be called upon logout. |
| 100 | + |
| 101 | +### LogoutCallback interface |
| 102 | + |
| 103 | +```ts |
| 104 | +export interface LogoutCallback { |
| 105 | + handleLogout(request: Request, response: Response): Promise<string | void>; |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +The logout action redirects to the url specified by the environment variable `LOGOUT_REDIRECT_URL`, |
| 110 | +but you can override it according to your needs and logic in the `LogoutCallback` implementation, and the return |
| 111 | +value of the callback will be used as the redirect URL. |
| 112 | + |
| 113 | +### Provide your implemented services to the portal options |
| 114 | + |
| 115 | +```ts |
| 116 | +import { Module } from '@nestjs/common'; |
| 117 | +import { PortalModule, PortalModuleOptions } from '@openmfp/portal-server-lib'; |
| 118 | + |
| 119 | +const portalOptions: PortalModuleOptions = { |
| 120 | + logoutCallbackProvider: LogoutCallbackImpl, |
| 121 | +}; |
| 122 | + |
| 123 | +@Module({ |
| 124 | + imports: [PortalModule.create(portalOptions)], |
| 125 | +}) |
| 126 | +export class AppModule {} |
| 127 | +``` |
0 commit comments