-
Notifications
You must be signed in to change notification settings - Fork 8
Refactor igx-ts side-nav-auth template to standalone components and angular-auth-oidc-client v21 #1554
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
Refactor igx-ts side-nav-auth template to standalone components and angular-auth-oidc-client v21 #1554
Changes from 5 commits
eba1717
9b17452
dcec8e5
740b8fe
2658cf5
4582360
455460d
fbdbef8
62e36c6
96e8c95
c4cb896
2252821
2dd06a8
4eb4a78
ac3f5e3
b7697ea
97ac362
67bf7be
e75af5c
4f06d3f
09f09f8
30cf341
7198fc6
8355531
ad5db64
6d18c67
1348032
3947cb4
e504f1f
cf76d0d
dcbf515
24a7db3
658a2bd
6ba1589
842d26e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| import { Routes } from '@angular/router'; | ||
| import { AuthGuard } from './authentication/auth.guard'; | ||
| import { Profile } from './authentication/profile/profile'; | ||
| import { Redirect } from './authentication/redirect/redirect'; | ||
| import { ExternalAuthProvider } from './authentication/services/external-auth-configs'; | ||
| import { ExternalAuthRedirectUrl } from './authentication/services/external-auth.service'; | ||
|
|
||
| export const routes: Routes = []; | ||
| export const routes: Routes = [ | ||
| { path: 'profile', component: Profile, canActivate: [AuthGuard] }, | ||
| { path: ExternalAuthRedirectUrl.Google, component: Redirect, data: { provider: ExternalAuthProvider.Google } }, | ||
| { path: ExternalAuthRedirectUrl.Facebook, component: Redirect, data: { provider: ExternalAuthProvider.Facebook } }, | ||
| { path: ExternalAuthRedirectUrl.Microsoft, component: Redirect, data: { provider: ExternalAuthProvider.Microsoft } } | ||
|
Hristo313 marked this conversation as resolved.
Outdated
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be neater if those are exported by the main auth barrel as something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in commit |
||
| ]; | ||
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,71 +1,34 @@ | ||
| import { | ||
| AuthWellKnownEndpoints, | ||
| OidcConfigService, | ||
| OidcSecurityService, | ||
| OpenIDImplicitFlowConfiguration | ||
| } from 'angular-auth-oidc-client'; | ||
| import { take } from 'rxjs/operators'; | ||
| import { OidcSecurityService } from 'angular-auth-oidc-client'; | ||
| import { firstValueFrom } from 'rxjs'; | ||
| import { ExternalLogin } from '../models/login'; | ||
| import { ExternalAuthConfig } from '../services/external-auth-configs'; | ||
| import { AuthProvider } from './auth-provider'; | ||
|
|
||
| /** Base provider for OpenID Connect (OIDC) https://openid.net/connect/ */ | ||
| export abstract class BaseOidcProvider implements AuthProvider { | ||
| constructor( | ||
| protected oidcConfigService: OidcConfigService, | ||
| protected oidcSecurityService: OidcSecurityService, | ||
| protected externalStsConfig: ExternalAuthConfig) { } | ||
|
|
||
| public config() { | ||
| const openIDImplicitFlowConfiguration = new OpenIDImplicitFlowConfiguration(); | ||
| openIDImplicitFlowConfiguration.stsServer = this.externalStsConfig.stsServer; | ||
| openIDImplicitFlowConfiguration.redirect_url = this.externalStsConfig.redirect_url; | ||
| openIDImplicitFlowConfiguration.client_id = this.externalStsConfig.client_id; | ||
| openIDImplicitFlowConfiguration.response_type = this.externalStsConfig.response_type; | ||
| openIDImplicitFlowConfiguration.scope = this.externalStsConfig.scope; | ||
| openIDImplicitFlowConfiguration.post_logout_redirect_uri = this.externalStsConfig.redirect_url; | ||
| openIDImplicitFlowConfiguration.post_login_route = this.externalStsConfig.post_login_route; | ||
| openIDImplicitFlowConfiguration.auto_userinfo = this.externalStsConfig.auto_userinfo; | ||
| openIDImplicitFlowConfiguration.max_id_token_iat_offset_allowed_in_seconds = | ||
| this.externalStsConfig.max_id_token_iat_offset_allowed_in_seconds; | ||
|
|
||
| const authWellKnownEndpoints = new AuthWellKnownEndpoints(); | ||
| authWellKnownEndpoints.setWellKnownEndpoints(this.oidcConfigService.wellKnownEndpoints); | ||
| this.oidcSecurityService.setupModule(openIDImplicitFlowConfiguration, authWellKnownEndpoints); | ||
| } | ||
|
|
||
| public login() { | ||
| this.oidcConfigService.onConfigurationLoaded.pipe(take(1)).subscribe(() => { | ||
| this.config(); | ||
| this.oidcSecurityService.authorize(); | ||
| }); | ||
| this.oidcConfigService.load_using_stsServer(this.externalStsConfig.stsServer); | ||
| this.oidcSecurityService.authorize(this.externalStsConfig.configId); | ||
| } | ||
|
|
||
| public getUserInfo(): Promise<ExternalLogin> { | ||
| let resolve: (val: ExternalLogin) => void; | ||
| let reject: () => void; | ||
| const user = new Promise<ExternalLogin>((res, rej) => { | ||
| resolve = res; | ||
| reject = rej; | ||
| }); | ||
| this.oidcConfigService.onConfigurationLoaded.pipe(take(1)).subscribe(() => { | ||
| this.config(); | ||
| this.oidcSecurityService.onAuthorizationResult.subscribe(() => { | ||
| this.oidcSecurityService.getUserData().subscribe(userData => { | ||
| resolve(this.formatUserData(userData)); | ||
| }); | ||
| }); | ||
| this.oidcSecurityService.authorizedImplicitFlowCallback(); | ||
| }); | ||
| this.oidcConfigService.load_using_stsServer(this.externalStsConfig.stsServer); | ||
| return user; | ||
| public async getUserInfo(): Promise<ExternalLogin> { | ||
| const result = await firstValueFrom(this.oidcSecurityService.userData$); | ||
| const configData = result.allUserData?.find( | ||
| d => d.configId === this.externalStsConfig.configId | ||
| ); | ||
| if (configData?.userData) { | ||
| return this.formatUserData(configData.userData); | ||
| } | ||
|
Hristo313 marked this conversation as resolved.
|
||
| return Promise.reject(null); | ||
| } | ||
|
|
||
| public logout() { | ||
| this.oidcSecurityService.logoff(); | ||
| this.oidcSecurityService.logoff(this.externalStsConfig.configId); | ||
| } | ||
|
|
||
| /** Format received user data per provider claims */ | ||
| protected abstract formatUserData(userData: { [key: string]: any; }): ExternalLogin; | ||
| protected abstract formatUserData(userData: { [key: string]: any; }): Promise<ExternalLogin>; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Don't move all of the previous config of AuthenticationModule into the main app config - it's job was to keep the authentication self-contained. Since we're not working with modules anymore, the modern approach for that would be to wrap the necessary config in something like
provideAuthetication()that can be added to the configThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit
4582360. Createdauthentication/provide-authentication.tswith aprovideAuthentication()function that encapsulates all auth-related providers (OIDCAuthModule, HTTP client with interceptors, JWT interceptor, fake backend).app.config.tsnow simply spreads...provideAuthentication(), keeping the auth setup self-contained.