-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-sso.js
561 lines (489 loc) · 18.7 KB
/
vue-sso.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import * as msal from "@azure/msal-browser";
let myMSALObj = null;
function loggerCallback(level, message, containsPii) {
if (containsPii) {
return;
}
switch (level) {
case msal.LogLevel.Error:
console.error(message);
return;
case msal.LogLevel.Info:
console.info(message);
return;
case msal.LogLevel.Verbose:
console.debug(message);
return;
case msal.LogLevel.Warning:
console.warn(message);
return;
}
}
const ssoLib = (Vue) => {
// Store object.
const phillyAccount = {
namespaced: true,
state: {
customPostbackObject: {},
signInAction: "",
signOutAction: "",
forgotPasswordAction: "",
errorHandler: "",
msalAccount: {},
accessToken: null,
// Statuses
signingIn: false,
signingOut: false,
redirectingForgotPassword: false,
debug: false,
signingInPolicy: null,
b2cScopes: [],
settings: {
clientId: null,
b2cEnvironment: "PhilaB2CDev",
authorityDomain: "PhilaB2CDev.b2clogin.com",
redirectUri: "http://localhost:8080/auth",
postLogoutRedirectUri: null,
signUpSignInPolicy: "B2C_1A_SIGNUP_SIGNIN",
signInOnlyPolicy: "B2C_1A_AD_SIGNIN_ONLY",
resetPasswordPolicy: "B2C_1A_PASSWORDRESET",
signInAction: "auth/authenticate",
signOutAction: "auth/signOut",
forgotPasswordAction: null,
errorHandler: null,
debug: false, // Adding debug instead of removing all console log, At least for now this is needed.
tenantId: false,
state: null,
},
b2cPolicies: {},
msalConfig: {},
loginRequest: {},
tokenRequest: {},
},
mutations: {
setMSALObject(state, config) {
myMSALObj = null;
state.b2cScopes = [
`https://${state.settings.b2cEnvironment}.onmicrosoft.com/api/read_data`,
];
// check if config.state is either null or an object. If not, then fail
if (typeof config.state !== "object") {
try {
config.state = JSON.parse(window.atob(config.state));
} catch (error) {
// Silence is power.
if (config.debug)
console.log("State is not an object.", config.state);
config.state = null;
}
}
// Default scopes
state.settings.loginRequestScopes = ["openid", ...state.b2cScopes];
state.settings.tokenRequestScopes = [...state.b2cScopes];
const localSettings = !config ? {} : config;
for (const s in localSettings) {
if (typeof state.settings[s] !== "undefined") {
Vue.set(state.settings, s, localSettings[s]);
console.log(`Setting ${s} to ${localSettings[s]}`);
}
}
if (state.settings.state != null) {
if (state.settings.debug)
console.log(
"state.settings.state: ",
JSON.stringify(state.settings.state)
);
state.settings.state = window.btoa(
JSON.stringify({ ...state.settings.state })
);
}
state.signInAction = state.settings.signInAction;
state.signOutAction = state.settings.signOutAction;
state.forgotPasswordAction = state.settings.forgotPasswordAction;
state.errorHandler = state.settings.errorHandler;
state.debug = state.settings.debug;
// Set postLogoutRedirectUri
if (!state.settings.postLogoutRedirectUri) {
state.settings.postLogoutRedirectUri = state.settings.redirectUri;
}
let signUpSignInAuthority = "";
if (!state.settings.tenantId) {
signUpSignInAuthority = `https://${state.settings.authorityDomain}/${state.settings.b2cEnvironment}.onmicrosoft.com/${state.settings.signUpSignInPolicy}`;
} else {
signUpSignInAuthority = `https://${state.settings.authorityDomain}/${state.settings.tenantId}/${state.settings.signUpSignInPolicy}`;
}
let forgotPasswordAuthority = "";
if (!state.settings.tenantId) {
forgotPasswordAuthority = `https://${state.settings.authorityDomain}/${state.settings.b2cEnvironment}.onmicrosoft.com/${state.settings.resetPasswordPolicy}`;
} else {
forgotPasswordAuthority = `https://${state.settings.authorityDomain}/${state.settings.tenantId}/${state.settings.resetPasswordPolicy}`;
}
let signInOnlyAuthority = "";
if (!state.settings.tenantId) {
signInOnlyAuthority = `https://${state.settings.authorityDomain}/${state.settings.b2cEnvironment}.onmicrosoft.com/${state.settings.signInOnlyPolicy}`;
} else {
signInOnlyAuthority = `https://${state.settings.authorityDomain}/${state.settings.tenantId}/${state.settings.signInOnlyPolicy}`;
}
state.b2cPolicies = {
names: {
signUpSignIn: state.settings.signUpSignInPolicy,
forgotPassword: state.settings.resetPasswordPolicy,
signInOnly: state.settings.signInOnlyPolicy,
},
authorities: {
signUpSignIn: {
authority: signUpSignInAuthority,
},
forgotPassword: {
authority: forgotPasswordAuthority,
},
signInOnly: {
authority: signInOnlyAuthority,
},
},
authorityDomain: state.settings.authorityDomain,
};
state.msalConfig = {
auth: {
clientId: state.settings.clientId,
authority1: {
authority: state.b2cPolicies.authorities.signUpSignIn.authority,
clientId: state.settings.clientId,
signInPolicy: state.b2cPolicies.names.signUpSignIn,
},
authority2: {
authority: state.b2cPolicies.authorities.signInOnly.authority,
clientId: state.settings.clientId,
signInPolicy: state.b2cPolicies.names.signInOnly,
},
knownAuthorities: [state.b2cPolicies.authorityDomain],
redirectUri: state.settings.redirectUri,
postLogoutRedirectUri: state.settings.postLogoutRedirectUri,
navigateToLoginRequestUrl: false,
},
cache: {
cacheLocation: "sessionStorage",
storeAuthStateInCookie: false,
},
system: {
logger: state.settings.debug
? new msal.Logger(loggerCallback, {
level: msal.LogLevel.Verbose,
piiLoggingEnabled: false,
})
: null,
},
};
state.loginRequest = {
scopes: state.settings.loginRequestScopes,
state: state.settings.state,
};
state.tokenRequest = {
scopes: state.settings.tokenRequestScopes,
forceRefresh: false,
};
myMSALObj = new msal.PublicClientApplication(state.msalConfig);
},
setLoginRequest(state, payload) {
state.loginRequest = Object.assign(state.loginRequest, payload);
},
setTokenRequest(state, payload) {
state.tokenRequest.account = myMSALObj.getAccountByHomeId(
state.msalAccount.homeAccountId
);
let authoritySettings = {};
if (state.signingInPolicy === "signUpSignIn") {
authoritySettings = {
authority: state.msalConfig.auth.authority1.authority,
signInPolicy: state.msalConfig.auth.authority1.signInPolicy,
clientId: state.msalConfig.auth.authority1.clientId,
};
} else if (state.signingInPolicy === "signInOnly") {
authoritySettings = {
authority: state.msalConfig.auth.authority2.authority,
signInPolicy: state.msalConfig.auth.authority2.signInPolicy,
clientId: state.msalConfig.auth.authority2.clientId,
};
}
payload = {
...payload,
...authoritySettings,
};
state.tokenRequest = Object.assign(state.tokenRequest, payload);
if (state.debug) console.log("Token request: ", state.tokenRequest);
},
setMSALAccount(state, account) {
state.msalAccount = account;
},
setToken(state, token) {
state.accessToken = token;
},
setSigningIn(state, signingIn) {
state.signingIn = signingIn;
},
setSigningOut(state, signingOut) {
state.signingOut = signingOut;
},
setRedirectingForgotPassword(state, redirectingForgotPassword) {
state.redirectingForgotPassword = redirectingForgotPassword;
},
setCustomPostBackObject(state, customPostbackObject) {
state.customPostbackObject = customPostbackObject;
},
setSigningInPolicy(state, signingInPolicy) {
state.signingInPolicy = signingInPolicy;
},
},
actions: {
async selectAccount({ state, commit, dispatch }) {
const currentAccounts = myMSALObj.getAllAccounts();
if (currentAccounts.length < 1) {
return;
} else if (currentAccounts.length > 1) {
const accounts = currentAccounts.filter(
(account) =>
account.homeAccountId
.toUpperCase()
.includes(
state.b2cPolicies.names[
`${state.signingInPolicy}`
].toUpperCase()
) &&
account.idTokenClaims.iss
.toUpperCase()
.includes(state.b2cPolicies.authorityDomain.toUpperCase()) &&
account.idTokenClaims.aud === state.msalConfig.auth.clientId
);
if (accounts.length > 1) {
// localAccountId identifies the entity for which the token asserts information.
if (
accounts.every(
(account) =>
account.localAccountId === accounts[0].localAccountId
)
) {
// All accounts belong to the same user
if (state.debug) console.log("Setting account to: ", accounts[0]);
commit("setMSALAccount", accounts[0]);
} else {
// Multiple users detected. Logout all to be safe.
await dispatch("msalSignOut");
return null;
}
} else if (accounts.length === 1) {
commit("setMSALAccount", accounts[0]);
}
} else if (currentAccounts.length === 1) {
if (state.debug)
console.log("Setting account to: ", currentAccounts[0]);
commit("setMSALAccount", currentAccounts[0]);
}
return;
},
async msalSignIn({ state, commit }, params = {}) {
commit("setSigningIn", true);
commit("setLoginRequest", {
...params,
...{
authority: state.msalConfig.auth.authority1.authority,
signInPolicy: state.msalConfig.auth.authority1.signInPolicy,
clientId: state.msalConfig.auth.authority1.clientId,
},
});
if (state.debug) console.log("Login request: ", state.loginRequest);
return myMSALObj.loginRedirect(state.loginRequest);
},
async cityEmployeeSignIn({ state, commit }, params = {}) {
// commit("setSigningIn", true);
commit("setLoginRequest", {
...params,
...{
authority: state.msalConfig.auth.authority2.authority,
signInPolicy: state.msalConfig.auth.authority2.signInPolicy,
clientId: state.msalConfig.auth.authority2.clientId,
},
});
if (state.debug) console.log("Login request: ", state.loginRequest);
return myMSALObj.loginRedirect(state.loginRequest);
},
async msalSignOut({ state, commit, dispatch }, redirectQueryParams = "") {
await myMSALObj.initialize();
commit("setSigningOut", true);
let redirectURL = state.msalConfig.auth.postLogoutRedirectUri;
if (
typeof redirectQueryParams === "string" &&
redirectQueryParams != ""
) {
redirectURL += `?${redirectQueryParams}`;
}
const logoutRequest = {
postLogoutRedirectUri: redirectURL,
...{
authority: state.msalConfig.auth.authority1.authority,
signInPolicy: state.msalConfig.auth.authority1.signInPolicy,
clientId: state.msalConfig.auth.authority1.clientId,
},
};
await dispatch(state.signOutAction, {}, { root: true });
return myMSALObj.logoutRedirect(logoutRequest);
},
msalForgotPassword({ state, commit }) {
commit("setRedirectingForgotPassword", true);
return myMSALObj.loginRedirect(
state.b2cPolicies.authorities.forgotPassword
);
},
async getAuthToken({ state, commit, dispatch }, params = {}) {
commit("setTokenRequest", params);
try {
const response = await myMSALObj.acquireTokenSilent(
state.tokenRequest
);
if (!response.accessToken || response.accessToken === "") {
throw new msal.InteractionRequiredAuthError();
} else {
if (state.debug)
console.log("access_token acquired at: " + new Date().toString());
commit("setToken", response.accessToken);
const payload = {
...response,
customPostbackObject: state.customPostbackObject,
};
await dispatch(state.signInAction, payload, { root: true });
}
} catch (error) {
if (state.debug)
console.log(
"Silent token acquisition fails. Acquiring token using redirect. \n",
error
);
if (error instanceof msal.InteractionRequiredAuthError) {
// fallback to interaction when silent call fails
try {
return myMSALObj.acquireTokenRedirect(state.tokenRequest);
} catch (error) {
if (state.debug) console.log(error);
}
} else {
if (state.debug) console.log(error);
}
}
},
async handleRedirect({ state, commit, dispatch }, authTokenParams = {}) {
if (state.debug)
console.log("Attempting to handle redirect promise...");
await myMSALObj.initialize();
try {
const response = await myMSALObj.handleRedirectPromise();
if (state.debug)
console.log("Redirect response: ", JSON.stringify(response));
if (response) {
if (
response.idTokenClaims["acr"].toUpperCase() ===
state.b2cPolicies.names.signUpSignIn.toUpperCase()
) {
commit("setSigningInPolicy", "signUpSignIn");
} else if (
response.idTokenClaims["acr"].toUpperCase() ===
state.b2cPolicies.names.signInOnly.toUpperCase()
) {
commit("setSigningInPolicy", "signInOnly");
} else {
commit("setSigningInPolicy", null);
}
if (state.signingInPolicy) {
// Set the state signing-in to true, the user is still signing into the system.
commit("setSigningIn", true);
// Set the phillyAccount information into the state
await dispatch("selectAccount");
// Let's get the SSO token.
await dispatch("getAuthToken", authTokenParams);
return response;
} else if (
response.idTokenClaims["acr"].toUpperCase() ===
state.b2cPolicies.names.forgotPassword.toUpperCase()
) {
if (state.debug) console.log("Went throu forgot password");
if (state.forgotPasswordAction) {
commit("setRedirectingForgotPassword", true);
await dispatch(state.forgotPasswordAction, response, {
root: true,
});
}
return response;
}
}
commit("setSigningIn", false);
return null;
} catch (error) {
if (state.debug)
console.log("Error while handling redirect promise", error);
if (error.errorMessage) {
if (error.errorMessage.indexOf("AADB2C90118") > -1) {
await dispatch("msalForgotPassword");
return null;
} else {
if (error instanceof msal.AuthError) {
// The user probably canceled the login. Just console.log it and ignore.
if (state.debug) console.log("Error code: ", error.errorCode);
if (state.debug)
console.log("Error message: ", error.errorMessage);
} else {
// I believe it is better to throw and error and let the user handle it at convinience.
if (state.errorHandler) {
await dispatch(state.errorHandler, error, { root: true });
} else {
throw Error(error);
}
}
commit("setSigningIn", false);
}
}
return error;
}
},
},
};
return phillyAccount;
};
export default {
install: (Vue, { store, config }) => {
if (!store) {
throw Error("You must pass-over the store when registering this plugin.");
}
let clientInfoObject = {};
let customPostbackObject = {};
if (config.debug) console.log("Hash: ", window.location.hash);
if (window.location.hash) {
const regex = /client_info=([^&]+)/;
const match = String(window.location.hash).match(regex);
if (match && match[1]) {
let clientInfoValue = decodeURIComponent(match[1]);
clientInfoObject = JSON.parse(window.atob(clientInfoValue));
}
const regex2 = /state=([^&]+)/;
const match2 = String(window.location.hash).match(regex2);
if (match2 && match2[1]) {
let state = decodeURIComponent(match2[1]);
// split state value by Pipe |
const states = state.split("|");
if (config.debug) console.log("State values: ", states);
if (states.length > 1) {
customPostbackObject = JSON.parse(window.atob(states[1]));
}
}
}
if (config.debug)
console.log("clientInfoObject: ", JSON.stringify(clientInfoObject));
// Dinamically register module
const phillyAccount = ssoLib(Vue);
store.registerModule("phillyAccount", phillyAccount);
store.commit("phillyAccount/setCustomPostBackObject", customPostbackObject);
store.commit("phillyAccount/setMSALObject", config);
// Handle page refresh.
// store.dispatch("phillyAccount/selectAccount");
if (!config.dontHandleRedirectAutomatically) {
store.dispatch("phillyAccount/handleRedirect");
}
},
};