-
Notifications
You must be signed in to change notification settings - Fork 12
/
MSAdal.js
65 lines (57 loc) · 2.07 KB
/
MSAdal.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
import AuthenticationContext from './msadal/AuthenticationContext';
var AuthenticationResult = require("./msadal/AuthenticationResult");
function MSAdalLogin(authority, clientId, redirectUri, resourceUri) {
// Shows the user authentication dialog box if required
return new Promise(function(resolve, reject) {
// First attempt to login silently using the cache and keychain functions
// If this fails the prompt the user for their credentials
let context = new AuthenticationContext(authority);
context.tokenCache.readItems().then(function(items) {
if (items.length > 0) {
context = new AuthenticationContext(items[0].authority);
}
// Attempt to authorize the user silently
context.acquireTokenSilentAsync(resourceUri, clientId).then(
function(authDetails) {
resolve(authDetails);
},
function() {
// We require user credentials, so this triggers the authentication dialog box
context.acquireTokenAsync(resourceUri, clientId, redirectUri).then(
function(authDetails) {
resolve(authDetails);
},
function(err) {
reject(err);
}
);
}
);
});
});
}
function MSAdalLogout(authority, redirectUri) {
let context = new AuthenticationContext(authority);
context.tokenCache.clear();
const promise = fetch(
"https://login.windows.net/common/oauth2/logout?post_logout_redirect_uri=" +
redirectUri
);
promise.then(response => true);
return promise;
}
function getValidMSAdalToken(authority) {
let context = new AuthenticationContext(authority);
return context.tokenCache.readItems().then(function(items) {
if (items.length > 0) {
const lastToken = items[items.length - 1];
if (lastToken.expiresOn > new Date()) {
return new AuthenticationResult(lastToken);
} else {
return undefined;
}
}
});
}
const MSAdalAuthenticationContext = AuthenticationContext;
export {getValidMSAdalToken, MSAdalLogin, MSAdalLogout, MSAdalAuthenticationContext};