2
2
// configuration parameters are located at authConfig.js
3
3
const myMSALObj = new msal . PublicClientApplication ( msalConfig ) ;
4
4
5
- let username = "" ;
5
+ let username = '' ;
6
6
7
7
function selectAccount ( ) {
8
-
9
8
/**
10
- * See here for more info on account retrieval:
9
+ * See here for more info on account retrieval:
11
10
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
12
11
*/
13
12
@@ -17,7 +16,7 @@ function selectAccount() {
17
16
return ;
18
17
} else if ( currentAccounts . length > 1 ) {
19
18
// Add choose account code here
20
- console . warn ( " Multiple accounts detected." ) ;
19
+ console . warn ( ' Multiple accounts detected.' ) ;
21
20
} else if ( currentAccounts . length === 1 ) {
22
21
username = currentAccounts [ 0 ] . username ;
23
22
showWelcomeMessage ( username ) ;
@@ -40,57 +39,126 @@ function handleResponse(response) {
40
39
}
41
40
42
41
function signIn ( ) {
43
-
42
+
44
43
/**
45
44
* You can pass a custom request object below. This will override the initial configuration. For more information, visit:
46
45
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
47
46
*/
48
47
49
- myMSALObj . loginPopup ( loginRequest )
48
+ myMSALObj
49
+ . loginPopup ( loginRequest )
50
50
. then ( handleResponse )
51
- . catch ( error => {
51
+ . catch ( ( error ) => {
52
52
console . error ( error ) ;
53
53
} ) ;
54
54
}
55
55
56
56
function signOut ( ) {
57
-
58
57
/**
59
58
* You can pass a custom request object below. This will override the initial configuration. For more information, visit:
60
59
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
61
60
*/
61
+ const account = myMSALObj . getAccountByUsername ( username ) ;
62
62
const logoutRequest = {
63
- account : myMSALObj . getAccountByUsername ( username )
63
+ account : account ,
64
+ redirectUri : '/redirect' ,
65
+ mainWindowRedirectUri : '/signout' ,
64
66
} ;
65
-
66
- myMSALObj . logout ( logoutRequest ) ;
67
+ clearStorage ( account ) ;
68
+ myMSALObj . logoutPopup ( logoutRequest ) . catch ( ( error ) => {
69
+ console . log ( error ) ;
70
+ } ) ;
67
71
}
68
72
69
73
function seeProfile ( ) {
70
-
74
+ const account = myMSALObj . getAccountByUsername ( username ) ;
71
75
getGraphClient ( {
72
- account : myMSALObj . getAccountByUsername ( username ) ,
76
+ account : account ,
73
77
scopes : graphConfig . graphMeEndpoint . scopes ,
74
- interactionType : msal . InteractionType . Popup
75
- } ) . api ( '/me' ) . get ( )
78
+ interactionType : msal . InteractionType . Popup ,
79
+ } )
80
+ . api ( '/me' )
81
+ . responseType ( 'raw' )
82
+ . get ( )
83
+ . then ( ( response ) => {
84
+ return handleClaimsChallenge ( account , response , graphConfig . graphMeEndpoint . uri ) ;
85
+ } )
76
86
. then ( ( response ) => {
87
+ if ( response && response . error === 'claims_challenge_occurred' ) throw response . error ;
77
88
return updateUI ( response , graphConfig . graphMeEndpoint . uri ) ;
78
- } ) . catch ( ( error ) => {
79
- console . log ( error ) ;
89
+ } )
90
+ . catch ( ( error ) => {
91
+ if ( error === 'claims_challenge_occurred' ) {
92
+ const resource = new URL ( graphConfig . graphMeEndpoint . uri ) . hostname ;
93
+ const claims =
94
+ account &&
95
+ getClaimsFromStorage ( `cc.${ msalConfig . auth . clientId } .${ account . idTokenClaims . oid } .${ resource } ` )
96
+ ? window . atob (
97
+ getClaimsFromStorage (
98
+ `cc.${ msalConfig . auth . clientId } .${ account . idTokenClaims . oid } .${ resource } `
99
+ )
100
+ )
101
+ : undefined ; // e.g {"access_token":{"xms_cc":{"values":["cp1"]}}}
102
+ let request = {
103
+ account : account ,
104
+ scopes : graphConfig . graphMeEndpoint . scopes ,
105
+ claims : claims ,
106
+ redirectUri : '/redirect' ,
107
+ } ;
108
+
109
+ myMSALObj . acquireTokenPopup ( request ) . catch ( ( error ) => {
110
+ console . log ( error ) ;
111
+ } ) ;
112
+ } else {
113
+ console . log ( error )
114
+ }
80
115
} ) ;
81
116
}
82
117
83
- function readMail ( ) {
84
-
118
+ function readContacts ( ) {
119
+ const account = myMSALObj . getAccountByUsername ( username ) ;
85
120
getGraphClient ( {
86
- account : myMSALObj . getAccountByUsername ( username ) ,
87
- scopes : graphConfig . graphMailEndpoint . scopes ,
88
- interactionType : msal . InteractionType . Popup
89
- } ) . api ( '/me/messages' ) . get ( )
121
+ account : account ,
122
+ scopes : graphConfig . graphContactsEndpoint . scopes ,
123
+ interactionType : msal . InteractionType . Popup ,
124
+ } )
125
+ . api ( '/me/contacts' )
126
+ . responseType ( 'raw' )
127
+ . get ( )
128
+ . then ( ( response ) => {
129
+ return handleClaimsChallenge ( account , response , graphConfig . graphContactsEndpoint . uri ) ;
130
+ } )
90
131
. then ( ( response ) => {
91
- return updateUI ( response , graphConfig . graphMailEndpoint . uri ) ;
92
- } ) . catch ( ( error ) => {
93
- console . log ( error ) ;
132
+ if ( response && response . error === 'claims_challenge_occurred' ) throw response . error ;
133
+ return updateUI ( response , graphConfig . graphContactsEndpoint . uri ) ;
134
+ } )
135
+ . catch ( ( error ) => {
136
+ if ( error === 'claims_challenge_occurred' ) {
137
+ const resource = new URL ( graphConfig . graphContactsEndpoint . uri ) . hostname ;
138
+ const claims =
139
+ account &&
140
+ getClaimsFromStorage ( `cc.${ msalConfig . auth . clientId } .${ account . idTokenClaims . oid } .${ resource } ` )
141
+ ? window . atob (
142
+ getClaimsFromStorage (
143
+ `cc.${ msalConfig . auth . clientId } .${ account . idTokenClaims . oid } .${ resource } `
144
+ )
145
+ )
146
+ : undefined ; // e.g {"access_token":{"xms_cc":{"values":["cp1"]}}}
147
+ let request = {
148
+ account : account ,
149
+ scopes : graphConfig . graphContactsEndpoint . scopes ,
150
+ claims : claims ,
151
+ redirectUri : '/redirect' ,
152
+ } ;
153
+
154
+ myMSALObj . acquireTokenPopup ( request ) . catch ( ( error ) => {
155
+ console . log ( error ) ;
156
+ } ) ;
157
+ } else if ( error . toString ( ) . includes ( '404' ) ) {
158
+ return updateUI ( null , graphConfig . graphContactsEndpoint . uri ) ;
159
+ } else {
160
+ console . log ( error ) ;
161
+ }
94
162
} ) ;
95
163
}
96
164
0 commit comments