Skip to content

Instantly share code, notes, and snippets.

@MatthewDavidCampbell
Created November 15, 2018 11:11
Show Gist options
  • Save MatthewDavidCampbell/59b321cfe7d5a5baa0536b83a50b7853 to your computer and use it in GitHub Desktop.
Save MatthewDavidCampbell/59b321cfe7d5a5baa0536b83a50b7853 to your computer and use it in GitHub Desktop.
Adal.js access token for application id uri
<!DOCTYPE html>
<html>
<head>
<title>Use application id uri (PUT THIS UNDER public directory!)</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/3.3.1/jquery.min.js"></script>
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.17/js/adal.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
"use strict";
// Moving parts
var variables = {
// Domain of Azure AD tenant
tenant: "<tenant id>",
// ClientId of Azure AD application principal == application id
clientId: "<application id>",
// ApplicationIdUri
applicationIdUri: "<application id uri>"
}
// Create config and get AuthenticationContext
window.config = {
tenant: variables.tenant,
clientId: variables.clientId,
postLogoutRedirectUri: window.location.origin,
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();
}
// Access for application id uri
authContext.acquireToken(variables.applicationIdUri, function (error, token) {
if (error || !token) {
console.log("ADAL error occurred: " + error);
return;
}
else {
console.log("Access token granted")
}
});
});
</script>
</head>
<body>
<h1>Id token based on clientId == application id</h1>
<h2>Then acquire access token where resource == application id uri</h2>
</body>
</html>
var finalhandler = require('finalhandler')
var http = require('http')
var serveStatic = require('serve-static')
// Serve up public/ftp folder
var serve = serveStatic('public', {'index': ['index.html']})
// Create server
var server = http.createServer(function onRequest (req, res) {
serve(req, res, finalhandler(req, res))
})
// Listen
server.listen(5000)
{
"name": "serving-adal",
"description": "Serve Adal",
"version": "1.0.0",
"author": "Gary",
"license": "MIT",
"dependencies": {
"serve-static": "1.13.2",
"http": "0.0.0",
"finalhandler": "1.1.1"
},
"devDependencies": {},
"files": [
"index.js"
],
"engines": {
"node": ">= 0.8.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment