-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feature/msal #245
Open
BerendWouters
wants to merge
14
commits into
develop
Choose a base branch
from
feature/msal
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/msal #245
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d1a53c4
Replace login flow with Entra ID
BerendWouters 32f94ca
Introduce MSAL for b2c in frontend
BerendWouters a1c2163
feat: configurable B2C parameters
BerendWouters 501ca10
feat: Remove dependency on IdentityServer
BerendWouters 2742bd8
fix: Remove clientId
BerendWouters c6a552c
fix: remove endpoint
BerendWouters 3db7d70
test: setup databasecontext for tests
BerendWouters 83d1fbc
build: align msal-browser
BerendWouters 1b8834b
build: fix msal-browser
BerendWouters 5fa911c
fix: imports were in different module
BerendWouters 072c4ca
fix: remove comments
BerendWouters 7c1c160
fix: remove absolute path
BerendWouters ab554ff
fix: reintroduce auth service and guards
BerendWouters eca12b8
fix: remove unused code
BerendWouters 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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,28 +1,36 @@ | ||
import { Routes, RouterModule } from '@angular/router'; | ||
import { NgModule } from '@angular/core'; | ||
import { MainComponent } from './main.component'; | ||
import { BrowserUtils } from '@azure/msal-browser'; | ||
import { MsalRedirectComponent } from '@azure/msal-angular'; | ||
|
||
const routes: Routes = [ | ||
{ path: 'login', loadChildren: () => import('./modules/login/login.module').then(m => m.LoginModule) }, | ||
{ path: 'login', loadChildren: () => import('./modules/login/login.module').then((m) => m.LoginModule) }, | ||
{ | ||
// Needed for handling redirect after login | ||
path: 'auth', | ||
loadChildren: () => import('./modules/login/login.module').then(m => m.LoginModule), | ||
component: MsalRedirectComponent, | ||
}, | ||
{ | ||
path: 'dashboard', | ||
loadChildren: () => import('./modules/dashboard/dashboard.module').then(m => m.DashboardModule), | ||
loadChildren: () => import('./modules/dashboard/dashboard.module').then((m) => m.DashboardModule), | ||
component: MainComponent, | ||
}, | ||
{ | ||
path: 'voogden', | ||
loadChildren: () => import('./modules/legalguardians/legalguardians.module').then(m => m.LegalguardiansModule), | ||
loadChildren: () => import('./modules/legalguardians/legalguardians.module').then((m) => m.LegalguardiansModule), | ||
component: MainComponent, | ||
}, | ||
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' }, | ||
]; | ||
|
||
@NgModule({ | ||
imports: [RouterModule.forRoot(routes, { relativeLinkResolution: 'legacy' })], | ||
imports: [ | ||
RouterModule.forRoot(routes, { | ||
relativeLinkResolution: 'legacy', // Don't perform initial navigation in iframes or popups | ||
initialNavigation: !BrowserUtils.isInIframe() && !BrowserUtils.isInPopup() ? 'enabledNonBlocking' : 'disabled', // Set to enabledBlocking to use Angular Universal | ||
}), | ||
], | ||
exports: [RouterModule], | ||
}) | ||
export class AppRoutingModule {} |
This file contains 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,4 +1,4 @@ | ||
<app-nav-menu (logoutEvent)="onLogout()"></app-nav-menu> | ||
<app-nav-menu (logoutEvent)="logout()"></app-nav-menu> | ||
<div class="container"> | ||
<router-outlet></router-outlet> | ||
<router-outlet *ngIf="!isIframe"></router-outlet> | ||
</div> |
This file contains 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,20 +1,163 @@ | ||
import { Component } from '@angular/core'; | ||
import { AuthService } from './modules/core/services/auth.service'; | ||
import { Router } from '@angular/router'; | ||
import { Component, OnInit, Inject, OnDestroy } from '@angular/core'; | ||
import { Subject } from 'rxjs'; | ||
import { filter, takeUntil } from 'rxjs/operators'; | ||
|
||
import { | ||
AuthenticationResult, | ||
EventMessage, | ||
EventType, | ||
InteractionStatus, | ||
InteractionType, | ||
PopupRequest, | ||
RedirectRequest, | ||
} from '@azure/msal-browser'; | ||
import { MsalService, MsalBroadcastService, MSAL_GUARD_CONFIG, MsalGuardConfiguration } from '@azure/msal-angular'; | ||
|
||
import { B2PCPolicyStore } from './modules/core/services/b2cpolicy.state.store'; | ||
|
||
interface Payload extends AuthenticationResult { | ||
idTokenClaims: { | ||
tfp?: string; | ||
}; | ||
} | ||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css'], | ||
}) | ||
export class AppComponent { | ||
title = 'Singer'; | ||
export class AppComponent implements OnInit, OnDestroy { | ||
title = 'Microsoft identity platform'; | ||
isIframe = false; | ||
loginDisplay = false; | ||
private readonly _destroying$ = new Subject<void>(); | ||
|
||
constructor( | ||
@Inject(MSAL_GUARD_CONFIG) private msalGuardConfig: MsalGuardConfiguration, | ||
private authService: MsalService, | ||
private msalBroadcastService: MsalBroadcastService, | ||
private b2cPolicyStateStore: B2PCPolicyStore | ||
) {} | ||
|
||
ngOnInit(): void { | ||
this.isIframe = window !== window.parent && !window.opener; | ||
this.setLoginDisplay(); | ||
|
||
this.authService.instance.enableAccountStorageEvents(); // Optional - This will enable ACCOUNT_ADDED and ACCOUNT_REMOVED events emitted when a user logs in or out of another tab or window | ||
this.msalBroadcastService.inProgress$ | ||
.pipe( | ||
filter((status: InteractionStatus) => status === InteractionStatus.None), | ||
takeUntil(this._destroying$) | ||
) | ||
.subscribe(() => { | ||
this.setLoginDisplay(); | ||
this.checkAndSetActiveAccount(); | ||
}); | ||
|
||
this.msalBroadcastService.msalSubject$ | ||
.pipe( | ||
filter( | ||
(msg: EventMessage) => | ||
msg.eventType === EventType.LOGIN_SUCCESS || msg.eventType === EventType.ACQUIRE_TOKEN_SUCCESS | ||
), | ||
takeUntil(this._destroying$) | ||
) | ||
.subscribe((result: EventMessage) => { | ||
let payload: Payload = <AuthenticationResult>result.payload; | ||
|
||
/** | ||
* For the purpose of setting an active account for UI update, we want to consider only the auth response resulting | ||
* from SUSI flow. "tfp" claim in the id token tells us the policy (NOTE: legacy policies may use "acr" instead of "tfp"). | ||
* To learn more about B2C tokens, visit https://docs.microsoft.com/en-us/azure/active-directory-b2c/tokens-overview | ||
*/ | ||
if (payload.idTokenClaims['tfp'] === this.b2cPolicyStateStore.getPolicies().b2cPolicyNames.editProfile) { | ||
window.alert('Profile has been updated successfully. \nPlease sign-in again.'); | ||
return this.logout(); | ||
} | ||
|
||
return result; | ||
}); | ||
|
||
this.msalBroadcastService.msalSubject$ | ||
.pipe( | ||
filter( | ||
(msg: EventMessage) => | ||
msg.eventType === EventType.LOGIN_FAILURE || msg.eventType === EventType.ACQUIRE_TOKEN_FAILURE | ||
), | ||
takeUntil(this._destroying$) | ||
) | ||
.subscribe((result: EventMessage) => { | ||
// Add your auth error handling logic here | ||
}); | ||
} | ||
|
||
constructor(private authService: AuthService, private router: Router) {} | ||
setLoginDisplay() { | ||
this.loginDisplay = this.authService.instance.getAllAccounts().length > 0; | ||
} | ||
|
||
checkAndSetActiveAccount() { | ||
/** | ||
* If no active account set but there are accounts signed in, sets first account to active account | ||
* To use active account set here, subscribe to inProgress$ first in your component | ||
* Note: Basic usage demonstrated. Your app may require more complicated account selection logic | ||
*/ | ||
let activeAccount = this.authService.instance.getActiveAccount(); | ||
|
||
if (!activeAccount && this.authService.instance.getAllAccounts().length > 0) { | ||
let accounts = this.authService.instance.getAllAccounts(); | ||
this.authService.instance.setActiveAccount(accounts[0]); | ||
} | ||
} | ||
|
||
login(userFlowRequest?: RedirectRequest | PopupRequest) { | ||
if (this.msalGuardConfig.interactionType === InteractionType.Popup) { | ||
if (this.msalGuardConfig.authRequest) { | ||
this.authService | ||
.loginPopup({ | ||
...this.msalGuardConfig.authRequest, | ||
...userFlowRequest, | ||
} as PopupRequest) | ||
.subscribe((response: AuthenticationResult) => { | ||
this.authService.instance.setActiveAccount(response.account); | ||
}); | ||
} else { | ||
this.authService.loginPopup(userFlowRequest).subscribe((response: AuthenticationResult) => { | ||
this.authService.instance.setActiveAccount(response.account); | ||
}); | ||
} | ||
} else { | ||
if (this.msalGuardConfig.authRequest) { | ||
this.authService.loginRedirect({ | ||
...this.msalGuardConfig.authRequest, | ||
...userFlowRequest, | ||
} as RedirectRequest); | ||
} else { | ||
this.authService.loginRedirect(userFlowRequest); | ||
} | ||
} | ||
} | ||
|
||
logout() { | ||
if (this.msalGuardConfig.interactionType === InteractionType.Popup) { | ||
this.authService.logoutPopup({ | ||
mainWindowRedirectUri: '/', | ||
}); | ||
} else { | ||
this.authService.logoutRedirect(); | ||
} | ||
} | ||
|
||
editProfile() { | ||
let editProfileFlowRequest: RedirectRequest | PopupRequest = { | ||
authority: this.b2cPolicyStateStore.getPolicies().authorities.editProfile.authority, | ||
scopes: [], | ||
}; | ||
|
||
this.login(editProfileFlowRequest); | ||
} | ||
|
||
onLogout() { | ||
this.router.navigateByUrl('/dashboard').then(() => { | ||
this.authService.logout(); | ||
}); | ||
// unsubscribe to events when component is destroyed | ||
ngOnDestroy(): void { | ||
this._destroying$.next(undefined); | ||
this._destroying$.complete(); | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.
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.
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.
Maybe add a todo to make sure we add a substitute here?