forked from daattali/beautiful-jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aad.html
121 lines (110 loc) · 4.05 KB
/
aad.html
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
<!DOCTYPE html>
<html>
<head>
<!-- note hitting "Grant Permission" for your app at https://portal.azure.com is required to workaround user granting permission to the app -->
<title>Authenticate an Office 365 user with ADAL JS</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style>body{font:normal normal normal 14px/1.5em "Century Gothic", sans-serif;}</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.0/js/adal.js"></script>
<script type="text/javascript">
$(document).ready(function() {
"use strict";
// Assign variables
var variables = {
// Domain of Azure AD tenant
azureAD: "M365B034593.onmicrosoft.com",
// ClientId of Azure AD application principal
clientId: "0c24c929-9153-48a2-86bb-1f6d52091b8a",
// GUID of SharePoint list from https://m365b034593.sharepoint.com/_layouts/15/listedit.aspx?List=%7Be82a20e7-2385-471f-a524-cada116967fe%7D
listId: "48a02fce-4f9c-4e52-bdc8-14f2b5c01d14",
// Name of SharePoint tenant
sharePointTenant: "M365B034593"
}
// Create config and get AuthenticationContext
window.config = {
tenant: variables.azureAD,
clientId: variables.clientId,
postLogoutRedirectUri: window.location.origin,
endpoints: {
graphApiUri: "https://graph.microsoft.com",
sharePointUri: "https://" + variables.sharePointTenant + ".sharepoint.com",
},
cacheLocation: "localStorage"
};
var authContext = new AuthenticationContext(config);
var isCallback = authContext.isCallback(window.location.hash);
authContext.handleWindowCallback();
if (isCallback && !authContext.getLoginError()) {
window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
}
var user = authContext.getCachedUser();
if (!user) {
authContext.login();
}
// Get OneDrive documents of current user with AuthenticationContext of Graph API
authContext.acquireToken(config.endpoints.graphApiUri, function (error, token) {
if (error || !token) {
console.log("ADAL error occurred: " + error);
return;
}
else {
var filesUri = config.endpoints.graphApiUri + "/v1.0/me/drive/root/children";
$.ajax({
type: "GET",
url: filesUri,
headers: {
"Authorization": "Bearer " + token
}
}).done(function (response) {
console.log("Successfully fetched files from OneDrive.");
var items = response.value;
for (var i = 0; i < items.length; i++){
console.log(items[i].name);
$("#OneDrive").append("<li>" + items[i].name + "</li>");
}
}).fail(function () {
console.log("Fetching files from OneDrive failed.");
});
}
});
// Get SharePoint documents of list with AuthenticationContext of SharePoint
authContext.acquireToken(config.endpoints.sharePointUri, function (error, token) {
if (error || !token) {
console.log("ADAL error occurred: " + error);
return;
}
else {
var listUri = config.endpoints.sharePointUri + "/_api/web/lists('" + variables.listId + "')/items?$select=Title";
$.ajax({
type: "GET",
url: listUri,
headers: {
"Authorization": "Bearer " + token,
"accept": "application/json;odata=verbose"
}
}).done(function (response) {
console.log("Successfully fetched list from SharePoint.");
var items = response.d.results;
for (var i = 0; i < items.length; i++){
console.log(items[i].Title);
$("#SharePoint").append("<li>" + items[i].Title + "</li>");
}
}).fail(function () {
console.log("Fetching list from SharePoint failed.");
});
}
});
});
</script>
</head>
<body>
<h1>Authenticate an Office 365 user with ADAL JS</h1>
<h2>OneDrive documents</h2>
<ul id="OneDrive">
</ul>
<h2>SharePoint documents</h2>
<ul id="SharePoint">
</ul>
</body>
</html>