Skip to content

Commit 75bb7a8

Browse files
author
derisen
committed
update schema
1 parent 7c697c2 commit 75bb7a8

File tree

11 files changed

+107
-122
lines changed

11 files changed

+107
-122
lines changed

2-Authorization-I/1-call-graph/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Here you'll learn about [Access Tokens](https://docs.microsoft.com/azure/active-
4646
| `src/app/auth-config.ts` | Authentication parameters reside here. |
4747
| `src/app/app.module.ts` | MSAL-Angular configuration parameters reside here. |
4848
| `src/app/app-routing.module.ts` | Configure your MSAL-Guard here. |
49-
| `src/app/graph.service.ts` | class to call graph API. |
49+
| `src/app/graph.service.ts` | Class to call graph API. |
5050

5151
## Prerequisites
5252

@@ -131,13 +131,11 @@ To manually register the apps, as a first step you'll need to:
131131
1. Since this app signs-in users, we will now proceed to select **delegated permissions**, which is is required by apps signing-in users.
132132
1. In the app's registration screen, select the **API permissions** blade in the left to open the page where we add access to the APIs that your application needs:
133133
1. Select the **Add a permission** button and then:
134-
135134
1. Ensure that the **Microsoft APIs** tab is selected.
136135
1. In the *Commonly used Microsoft APIs* section, select **Microsoft Graph**
137136
1. In the **Delegated permissions** section, select the **User.Read** in the list. Use the search box if necessary.
138137
1. Select the **Add permissions** button at the bottom.
139138
1. Select the **Add a permission** button and then:
140-
141139
1. Ensure that the **Microsoft APIs** tab is selected.
142140
1. In the list of APIs, select the API `Windows Azure Service Management API`.
143141
1. In the **Delegated permissions** section, select the **user_impersonation** in the list. Use the search box if necessary.

2-Authorization-I/1-call-graph/SPA/src/app/app.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Component, OnInit, Inject, OnDestroy } from '@angular/core';
2-
import { MsalService, MsalBroadcastService, MSAL_GUARD_CONFIG, MsalGuardConfiguration } from '@azure/msal-angular';
3-
import { AuthenticationResult, InteractionStatus, InteractionType, PopupRequest, RedirectRequest } from '@azure/msal-browser';
42
import { Subject } from 'rxjs';
53
import { filter, takeUntil } from 'rxjs/operators';
6-
import { clearStorage } from "./util/storage.utils";
4+
5+
import { MsalService, MsalBroadcastService, MSAL_GUARD_CONFIG, MsalGuardConfiguration } from '@azure/msal-angular';
6+
import { AuthenticationResult, InteractionStatus, InteractionType, PopupRequest, RedirectRequest } from '@azure/msal-browser';
7+
8+
import { clearStorage } from "./utils/storageUtils";
79

810
@Component({
911
selector: 'app-root',

2-Authorization-I/1-call-graph/SPA/src/app/app.module.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { MsalGuard, MsalInterceptor, MsalBroadcastService, MsalInterceptorConfig
2020

2121
import { msalConfig, loginRequest, protectedResources } from './auth-config';
2222

23-
import { getClaimsFromStorage, getStorageSchema } from './util/storage.utils';
23+
import { getClaimsFromStorage } from './utils/storageUtils';
2424

2525
/**
2626
* Here we pass the configuration parameters to create an MSAL instance.
@@ -38,33 +38,24 @@ export function MSALInstanceFactory(): IPublicClientApplication {
3838
*/
3939
export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
4040
const protectedResourceMap = new Map<string, Array<string>>();
41-
42-
protectedResourceMap.set(
43-
protectedResources.graphMe.endpoint,
44-
protectedResources.graphMe.scopes,
45-
);
41+
42+
protectedResourceMap.set(protectedResources.graphMe.endpoint, protectedResources.graphMe.scopes);
4643
protectedResourceMap.set(protectedResources.armTenants.endpoint, protectedResources.armTenants.scopes);
4744

4845
return {
4946
interactionType: InteractionType.Redirect,
5047
protectedResourceMap,
5148
authRequest: (msalService, httpReq, originalAuthRequest) => {
52-
const resource = getStorageSchema(httpReq.url);
53-
let claim =
54-
msalService.instance.getActiveAccount()! &&
49+
const resource = new URL(httpReq.url).hostname;
50+
51+
let claim = msalService.instance.getActiveAccount()! &&
5552
getClaimsFromStorage(
56-
`cc.${msalConfig.auth.clientId}.${
57-
msalService.instance.getActiveAccount()?.idTokenClaims?.oid
58-
}.${resource}`
59-
)
60-
? window.atob(
61-
getClaimsFromStorage(
62-
`cc.${msalConfig.auth.clientId}.${
63-
msalService.instance.getActiveAccount()?.idTokenClaims?.oid
64-
}.${resource}`
65-
)
66-
)
67-
: '';
53+
`cc.${msalConfig.auth.clientId}.${msalService.instance.getActiveAccount()?.idTokenClaims?.oid}.${resource}`
54+
) ? window.atob(
55+
getClaimsFromStorage(
56+
`cc.${msalConfig.auth.clientId}.${msalService.instance.getActiveAccount()?.idTokenClaims?.oid}.${resource}`
57+
))
58+
: undefined;
6859
return {
6960
...originalAuthRequest,
7061
claims: claim,
@@ -78,7 +69,7 @@ export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
7869
* additional scopes you want the user to consent upon login, add them here as well.
7970
*/
8071
export function MSALGuardConfigFactory(): MsalGuardConfiguration {
81-
return {
72+
return {
8273
interactionType: InteractionType.Redirect,
8374
authRequest: loginRequest
8475
};

2-Authorization-I/1-call-graph/SPA/src/app/auth-config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ export const protectedResources = {
4444
graphMe: {
4545
endpoint: 'https://graph.microsoft.com/v1.0/me',
4646
scopes: ['User.Read'],
47-
resource: 'graph'
4847
},
4948
armTenants: {
5049
endpoint: 'https://management.azure.com/tenants',
5150
scopes: ['https://management.azure.com/user_impersonation'],
52-
resource: 'management.azure'
5351
},
5452
};
5553

2-Authorization-I/1-call-graph/SPA/src/app/graph.service.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { MsalService } from '@azure/msal-angular';
33
import { HttpClient } from '@angular/common/http';
44

55
import { protectedResources, msalConfig } from './auth-config';
6-
import { addClaimsToStorage, getClaimsFromStorage } from './util/storage.utils';
6+
import { addClaimsToStorage, getClaimsFromStorage } from './utils/storageUtils';
7+
import { AccountInfo } from '@azure/msal-browser';
78

89
@Injectable({
910
providedIn: 'root',
1011
})
1112
export class GraphService {
12-
constructor(private authService: MsalService, private http: HttpClient) {}
13+
constructor(private authService: MsalService, private http: HttpClient) { }
1314

1415
/**
1516
* Makes a GET request using authorization header For more, visit:
@@ -25,7 +26,9 @@ export class GraphService {
2526
},
2627
(error: any) => {
2728
if (error.status === 401) {
28-
this.handleClaimsChallenge(error);
29+
if (error.headers.get('www-authenticate')) {
30+
this.handleClaimsChallenge(error, endpoint);
31+
}
2932
}
3033
reject(error);
3134
}
@@ -39,48 +42,43 @@ export class GraphService {
3942
* For more information, visit: https://docs.microsoft.com/en-us/azure/active-directory/develop/claims-challenge#claims-challenge-header-format
4043
* @param response
4144
*/
42-
handleClaimsChallenge(response: any): void {
43-
if (response.headers.get('www-authenticate')) {
44-
const authenticateHeader: string =
45-
response.headers.get('www-authenticate');
45+
handleClaimsChallenge(response: any, endpoint: string): void {
46+
const authenticateHeader: string = response.headers.get('www-authenticate');
47+
4648
const claimsChallenge: any = authenticateHeader
4749
?.split(' ')
4850
?.find((entry) => entry.includes('claims='))
4951
?.split('claims="')[1]
5052
?.split('",')[0];
51-
let account: any = this.authService.instance.getActiveAccount();
53+
54+
let account: AccountInfo = this.authService.instance.getActiveAccount()!;
55+
5256
addClaimsToStorage(
5357
claimsChallenge,
54-
`cc.${msalConfig.auth.clientId}.${account?.idTokenClaims?.oid}.${protectedResources.graphMe.resource}`
58+
`cc.${msalConfig.auth.clientId}.${account?.idTokenClaims?.oid}.${new URL(endpoint).hostname}`
5559
);
5660

57-
this.getAccessTokenInteractively();
58-
}
61+
this.getAccessTokenInteractively(endpoint);
5962
}
6063

6164
/**
6265
* This method fetches a new access token interactively
6366
*/
64-
getAccessTokenInteractively(): void {
67+
getAccessTokenInteractively(endpoint: string): void {
6568
this.authService.instance.acquireTokenRedirect({
6669
account: this.authService.instance.getActiveAccount()!,
67-
scopes: protectedResources.graphMe.scopes,
68-
claims:
69-
this.authService.instance.getActiveAccount()! &&
70+
scopes: Object.values(protectedResources).find((resource: { endpoint: string, scopes: string[]}) => resource.endpoint === endpoint)?.scopes || [],
71+
claims: this.authService.instance.getActiveAccount()! &&
7072
getClaimsFromStorage(
71-
`cc.${msalConfig.auth.clientId}.${
72-
this.authService.instance.getActiveAccount()?.idTokenClaims?.oid
73-
}.${protectedResources.graphMe.resource}`
73+
`cc.${msalConfig.auth.clientId}.${this.authService.instance.getActiveAccount()?.idTokenClaims?.oid
74+
}.${new URL(endpoint).hostname}`
75+
) ?
76+
window.atob(
77+
getClaimsFromStorage(
78+
`cc.${msalConfig.auth.clientId}.${this.authService.instance.getActiveAccount()?.idTokenClaims?.oid}.${new URL(endpoint).hostname}`
79+
)
7480
)
75-
? window.atob(
76-
getClaimsFromStorage(
77-
`cc.${msalConfig.auth.clientId}.${
78-
this.authService.instance.getActiveAccount()?.idTokenClaims
79-
?.oid
80-
}.${protectedResources.graphMe.resource}`
81-
)
82-
)
83-
: '',
81+
: undefined,
8482
});
8583
}
8684
}

2-Authorization-I/1-call-graph/SPA/src/app/home/home.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class HomeComponent implements OnInit {
1414

1515
loginDisplay = false;
1616
displayedColumns: string[] = ['claim', 'value'];
17-
dataSource: any =[];
17+
dataSource: any = [];
1818

1919
private readonly _destroying$ = new Subject<void>();
2020

@@ -32,7 +32,7 @@ export class HomeComponent implements OnInit {
3232
this.authService.instance.setActiveAccount(payload.account);
3333
});
3434

35-
this.msalBroadcastService.inProgress$
35+
this.msalBroadcastService.inProgress$
3636
.pipe(
3737
filter((status: InteractionStatus) => status === InteractionStatus.None)
3838
)
@@ -64,9 +64,9 @@ export class HomeComponent implements OnInit {
6464

6565
getClaims(claims: any) {
6666
this.dataSource = [
67-
{id: 1, claim: "Display Name", value: claims ? claims['name'] : null},
68-
{id: 2, claim: "User Principal Name (UPN)", value: claims ? claims['preferred_username'] : null},
69-
{id: 2, claim: "OID", value: claims ? claims['oid']: null}
67+
{ id: 1, claim: "Display Name", value: claims ? claims['name'] : null },
68+
{ id: 2, claim: "User Principal Name (UPN)", value: claims ? claims['preferred_username'] : null },
69+
{ id: 2, claim: "OID", value: claims ? claims['oid'] : null }
7070
];
7171
}
7272

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export type Profile = {
2+
id?: string,
3+
userPrincipalName?: string,
4+
businessPhones?: Array<string>,
5+
displayName?: string,
6+
givenName?: string,
7+
jobTitle?: string,
8+
mail?: string,
9+
mobilePhone?: string,
10+
officeLocation?: string,
11+
preferredLanguage?: string,
12+
surname?: string
13+
};
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import { protectedResources } from '../auth-config';
33
import { GraphService } from '../graph.service';
4+
import { Profile } from '../profile';
45

56
@Component({
67
selector: 'app-profile',
@@ -13,39 +14,37 @@ export class ProfileComponent implements OnInit {
1314

1415
constructor(
1516
private graphService: GraphService,
16-
) {}
17+
) { }
1718

1819
ngOnInit() {
1920
this.graphService
2021
.getData(protectedResources.graphMe.endpoint)
21-
.then((profileResponse: any) => {
22+
.then((profileResponse: Profile) => {
2223
this.dataSource = [
2324
{
2425
id: 1,
2526
claim: 'Name',
26-
value: profileResponse ? profileResponse['givenName'] : null,
27+
value: profileResponse ? profileResponse.givenName : null,
2728
},
2829
{
2930
id: 2,
3031
claim: 'Surname',
31-
value: profileResponse ? profileResponse['surname'] : null,
32+
value: profileResponse ? profileResponse.surname : null,
3233
},
3334
{
3435
id: 3,
3536
claim: 'User Principal Name (UPN)',
36-
value: profileResponse
37-
? profileResponse['userPrincipalName']
38-
: null,
37+
value: profileResponse ? profileResponse.userPrincipalName : null,
3938
},
4039
{
4140
id: 4,
4241
claim: 'ID',
43-
value: profileResponse ? profileResponse['id'] : null,
42+
value: profileResponse ? profileResponse.id : null,
4443
},
4544
];
4645
})
4746
.catch((error) => {
4847
console.log(error);
49-
});
48+
});
5049
}
5150
}

2-Authorization-I/1-call-graph/SPA/src/app/tenant/tenant.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { protectedResources } from '../auth-config';
1010
export class TenantComponent implements OnInit {
1111

1212
displayedColumns: string[] = ['claim', 'value'];
13-
dataSource: any =[];
13+
dataSource: any = [];
1414

1515
constructor(
1616
private http: HttpClient
@@ -24,10 +24,10 @@ export class TenantComponent implements OnInit {
2424
this.http.get(protectedResources.armTenants.endpoint + '?api-version=2020-01-01')
2525
.subscribe((tenant: any) => {
2626
this.dataSource = [
27-
{id: 1, claim: "Display Name", value: tenant ? tenant.value[0]['displayName'] : null},
28-
{id: 2, claim: "Default Domain", value: tenant ? tenant.value[0]['defaultDomain'] : null},
29-
{id: 3, claim: "Tenant Id", value: tenant ? tenant.value[0]['tenantId'] : null},
30-
{id: 3, claim: "Tenant Type", value: tenant ? tenant.value[0]['tenantType'] : null},
27+
{ id: 1, claim: "Display Name", value: tenant ? tenant.value[0]['displayName'] : null },
28+
{ id: 2, claim: "Default Domain", value: tenant ? tenant.value[0]['defaultDomain'] : null },
29+
{ id: 3, claim: "Tenant Id", value: tenant ? tenant.value[0]['tenantId'] : null },
30+
{ id: 3, claim: "Tenant Type", value: tenant ? tenant.value[0]['tenantType'] : null },
3131
];
3232
});
3333
}

2-Authorization-I/1-call-graph/SPA/src/app/util/storage.utils.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)