-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add a custom auth config provider #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
99a8fb5
Custom auth config provider
gkrajniaksap 11f9652
Add custom auth config provider and implement default idp processing.
gkrajniaksap 96b606e
Add custom auth config provider and implement default idp processing.
gkrajniaksap 74e9143
Add custom auth config provider and implement default idp processing.
gkrajniaksap 987ac19
Add custom auth config provider and implement default idp processing.
gkrajniaksap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...viders/iam/auth-callback-provider.spec.ts → ...al-options/auth-callback-provider.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e-providers/iam/auth-callback-provider.ts → src/portal-options/auth-callback-provider.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { PMAuthConfigProvider } from './auth-config-provider.js'; | ||
| import { HttpException } from '@nestjs/common'; | ||
| import { | ||
| DiscoveryService, | ||
| EnvAuthConfigService, | ||
| } from '@openmfp/portal-server-lib'; | ||
| import type { Request } from 'express'; | ||
| import { mock } from 'jest-mock-extended'; | ||
|
|
||
| describe('PMAuthConfigProvider', () => { | ||
| let provider: PMAuthConfigProvider; | ||
| let discoveryService: jest.Mocked<DiscoveryService>; | ||
| let envAuthConfigService: jest.Mocked<EnvAuthConfigService>; | ||
|
|
||
| beforeEach(() => { | ||
| discoveryService = mock<DiscoveryService>(); | ||
| envAuthConfigService = mock<EnvAuthConfigService>(); | ||
| provider = new PMAuthConfigProvider(discoveryService, envAuthConfigService); | ||
| jest.resetModules(); | ||
| process.env = { | ||
| AUTH_SERVER_URL_DEFAULT: 'authUrl', | ||
| TOKEN_URL_DEFAULT: 'tokenUrl', | ||
| BASE_DOMAINS_DEFAULT: 'example.com', | ||
| OIDC_CLIENT_ID_DEFAULT: 'client123', | ||
| OIDC_CLIENT_SECRET_DEFAULT: 'secret123', | ||
| }; | ||
| }); | ||
|
|
||
| it('should delegate to EnvAuthConfigService if available', async () => { | ||
| const req = { hostname: 'foo.example.com' } as Request; | ||
| const expected = { | ||
| idpName: 'idp', | ||
| baseDomain: 'example.com', | ||
| oauthServerUrl: 'url', | ||
| oauthTokenUrl: 'token', | ||
| clientId: 'cid', | ||
| clientSecret: 'sec', | ||
| }; | ||
| envAuthConfigService.getAuthConfig.mockResolvedValue(expected); | ||
|
|
||
| const result = await provider.getAuthConfig(req); | ||
|
|
||
| expect(result).toEqual(expected); | ||
| }); | ||
|
|
||
| it('should fall back to default configuration if EnvAuthConfigService throws', async () => { | ||
| const req = { hostname: 'foo.example.com' } as Request; | ||
| envAuthConfigService.getAuthConfig.mockRejectedValue(new Error('fail')); | ||
| discoveryService.getOIDC.mockResolvedValue({ | ||
| authorization_endpoint: 'authUrl', | ||
| token_endpoint: 'tokenUrl', | ||
| }); | ||
|
|
||
| const result = await provider.getAuthConfig(req); | ||
|
|
||
| expect(result).toMatchObject({ | ||
| baseDomain: 'example.com', | ||
| oauthServerUrl: 'authUrl', | ||
| oauthTokenUrl: 'tokenUrl', | ||
| clientId: 'client123', | ||
| clientSecret: 'secret123', | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw if default configuration incomplete', async () => { | ||
| const req = { hostname: 'foo.example.com' } as Request; | ||
| envAuthConfigService.getAuthConfig.mockRejectedValue(new Error('fail')); | ||
| discoveryService.getOIDC.mockResolvedValue(null); | ||
| process.env = {}; | ||
|
|
||
| await expect(provider.getAuthConfig(req)).rejects.toThrow(HttpException); | ||
| }); | ||
|
|
||
| it('getDomain should return organization and baseDomain', () => { | ||
| const req = { hostname: 'foo.example.com' } as Request; | ||
| const result = provider.getDomain(req); | ||
| expect(result).toEqual({ | ||
| organization: 'foo', | ||
| baseDomain: 'example.com', | ||
| }); | ||
| }); | ||
|
|
||
| it('getDomain should return clientId if hostname equals baseDomain', () => { | ||
| const req = { hostname: 'example.com' } as Request; | ||
| const result = provider.getDomain(req); | ||
| expect(result).toEqual({ | ||
| organization: 'client123', | ||
| baseDomain: 'example.com', | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
| import { | ||
| AuthConfigService, | ||
| DiscoveryService, | ||
| EnvAuthConfigService, | ||
| ServerAuthVariables, | ||
| } from '@openmfp/portal-server-lib'; | ||
| import type { Request } from 'express'; | ||
|
|
||
| @Injectable() | ||
| export class PMAuthConfigProvider implements AuthConfigService { | ||
| constructor( | ||
| private discoveryService: DiscoveryService, | ||
| private envEuthConfigService: EnvAuthConfigService, | ||
| ) {} | ||
|
|
||
| async getAuthConfig(request: Request): Promise<ServerAuthVariables> { | ||
| try { | ||
| return await this.envEuthConfigService.getAuthConfig(request); | ||
| } catch { | ||
| console.log( | ||
| 'Failed to retrieve auth config from environment variables based on provided IDP.', | ||
| ); | ||
| } | ||
|
|
||
| console.log('Resolving auth config from default configuration.'); | ||
|
|
||
| const oidc = await this.discoveryService.getOIDC('DEFAULT'); | ||
| const oauthServerUrl = | ||
| oidc?.authorization_endpoint ?? process.env['AUTH_SERVER_URL_DEFAULT']; | ||
| const oauthTokenUrl = | ||
| oidc?.token_endpoint ?? process.env['TOKEN_URL_DEFAULT']; | ||
|
|
||
| const baseDomain = process.env['BASE_DOMAINS_DEFAULT']; | ||
| const clientId = process.env['OIDC_CLIENT_ID_DEFAULT']; | ||
| const clientSecretEnvVar = 'OIDC_CLIENT_SECRET_DEFAULT'; | ||
| const clientSecret = process.env[clientSecretEnvVar]; | ||
|
|
||
| if (!oauthServerUrl || !oauthTokenUrl || !clientId || !clientSecret) { | ||
| const hasClientSecret = !!clientSecret; | ||
| throw new HttpException( | ||
| { | ||
| message: 'Default auth configuration incomplete.', | ||
| error: `The default properly configured. oauthServerUrl: '${oauthServerUrl}' oauthTokenUrl: '${oauthTokenUrl}' clientId: '${clientId}', has client secret (${clientSecretEnvVar}): ${String( | ||
| hasClientSecret, | ||
| )}`, | ||
| statusCode: HttpStatus.NOT_FOUND, | ||
| }, | ||
| HttpStatus.NOT_FOUND, | ||
| ); | ||
| } | ||
|
|
||
| const subDomain = request.hostname.split('.')[0]; | ||
| return { | ||
| idpName: request.hostname === baseDomain ? clientId : subDomain, | ||
| baseDomain, | ||
| oauthServerUrl, | ||
| clientId, | ||
| clientSecret, | ||
| oauthTokenUrl, | ||
| }; | ||
| } | ||
|
|
||
| getDomain(request: Request): { organization?: string; baseDomain?: string } { | ||
| const subDomain = request.hostname.split('.')[0]; | ||
| const clientId = process.env['OIDC_CLIENT_ID_DEFAULT']; | ||
| const baseDomain = process.env['BASE_DOMAINS_DEFAULT']; | ||
| return { | ||
| organization: request.hostname === baseDomain ? clientId : subDomain, | ||
| baseDomain, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| export * from './entity-context-provider/account-entity-context-provider.service.js'; | ||
| export * from './portal-context-provider/openmfp-portal-context.service.js'; | ||
| export * from './request-context-provider/openmfp-request-context-provider.js'; | ||
| export * from './account-entity-context-provider.service.js'; | ||
| export * from './openmfp-portal-context.service.js'; | ||
| export * from './openmfp-request-context-provider.js'; | ||
| export * from './auth-config-provider.js'; | ||
| export * from './service-providers/content-configuration-service-providers.service.js'; | ||
| export * from './service-providers/iam/auth-callback-provider.js'; | ||
| export * from './service-providers/iam/iam-graphql.service.js'; | ||
| export * from './auth-callback-provider.js'; | ||
| export * from './services/iam-graphql.service.js'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.