Created
January 9, 2018 00:13
-
-
Save rbrayb/192901331fe0806445239413e8c36898 to your computer and use it in GitHub Desktop.
ADFS - Cordova
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
/* var authority = "https://login.windows.net/common", | |
redirectUri = "http://MyDirectorySearcherApp", | |
resourceUri = "https://graph.windows.net", | |
clientId = "c5bbc0ed-500c-4cf4-8f81-f3d6b190ba0e", | |
graphApiVersion = "2013-11-08"; */ | |
// ADFS | |
var authority = "https://my-adfs.cloudapp.net/adfs", | |
redirectUri = "http://MyDirectorySearcherApp", | |
resourceUri = "urn:microsoft:userinfo", | |
clientId = "77403d77-fcd0-49c6-9bb7-c114ae05e349", | |
graphApiVersion = "2013-11-08"; | |
var app = { | |
// Invoked when Cordova is fully loaded. | |
onDeviceReady: function() { | |
document.getElementById('search').addEventListener('click', app.search); | |
}, | |
// Implements search operations. | |
search: function () { | |
document.getElementById('userlist').innerHTML = ""; | |
app.authenticate(function (authresult) { | |
var searchText = document.getElementById('searchfield').value; | |
app.requestData(authresult, searchText); | |
}); | |
}, | |
// Shows user authentication dialog if required. | |
authenticate: function (authCompletedCallback) { | |
// ADFS | |
app.context = new Microsoft.ADAL.AuthenticationContext(authority, false); | |
app.context.tokenCache.readItems().then(function (items) { | |
if (items.length > 0) { | |
authority = items[0].authority; | |
// ADFS | |
app.context = new Microsoft.ADAL.AuthenticationContext(authority, false); | |
} | |
// Attempt to authorize user silently | |
app.context.acquireTokenSilentAsync(resourceUri, clientId) | |
.then(authCompletedCallback, function () { | |
// We require user cridentials so triggers authentication dialog | |
app.context.acquireTokenAsync(resourceUri, clientId, redirectUri) | |
.then(authCompletedCallback, function (err) { | |
app.error("Failed to authenticate: " + err); | |
}); | |
}); | |
}); | |
}, | |
// Makes Api call to receive user list. | |
requestData: function (authResult, searchText) { | |
var req = new XMLHttpRequest(); | |
// ADFS | |
var url = "https://my-adfs.cloudapp.net/adfs/userinfo"; | |
req.open("GET", url, true); | |
req.setRequestHeader('Authorization', 'Bearer ' + authResult.accessToken); | |
req.onload = function(e) { | |
if (e.target.status >= 200 && e.target.status < 300) { | |
// ADFS | |
app.renderData(e.target.response); | |
return; | |
} | |
// ADFS | |
app.error('Data request failed:' + ' Status - ' + e.target.status + ' Response - ' + e.target.response); | |
}; | |
req.onerror = function(e) { | |
app.error('Data request failed: ' + e.error); | |
} | |
req.send(); | |
}, | |
// Renders user list. | |
renderData: function(data) { | |
// ADFS | |
var users = data; | |
if (users.length === 0) { | |
app.error("No users found"); | |
return; | |
} | |
var userlist = document.getElementById('userlist'); | |
userlist.innerHTML = ""; | |
// Helper function for generating HTML | |
function $new(eltName, classlist, innerText, children, attributes) { | |
var elt = document.createElement(eltName); | |
classlist.forEach(function (className) { | |
elt.classList.add(className); | |
}); | |
if (innerText) { | |
elt.innerText = innerText; | |
} | |
if (children && children.constructor === Array) { | |
children.forEach(function (child) { | |
elt.appendChild(child); | |
}); | |
} else if (children instanceof HTMLElement) { | |
elt.appendChild(children); | |
} | |
if(attributes && attributes.constructor === Object) { | |
for(var attrName in attributes) { | |
elt.setAttribute(attrName, attributes[attrName]); | |
} | |
} | |
return elt; | |
} | |
users.map(function(userInfo) { | |
return $new('li', ['topcoat-list__item'], null, [ | |
$new('div', [], null, [ | |
$new('p', ['userinfo-label'], 'First name: '), | |
$new('input', ['topcoat-text-input', 'userinfo-data-field'], null, null, { | |
type: 'text', | |
readonly: '', | |
placeholder: '', | |
value: userInfo.givenName || '' | |
}) | |
]), | |
$new('div', [], null, [ | |
$new('p', ['userinfo-label'], 'Last name: '), | |
$new('input', ['topcoat-text-input', 'userinfo-data-field'], null, null, { | |
type: 'text', | |
readonly: '', | |
placeholder: '', | |
value: userInfo.surname || '' | |
}) | |
]), | |
$new('div', [], null, [ | |
$new('p', ['userinfo-label'], 'UPN: '), | |
$new('input', ['topcoat-text-input', 'userinfo-data-field'], null, null, { | |
type: 'text', | |
readonly: '', | |
placeholder: '', | |
value: userInfo.userPrincipalName || '' | |
}) | |
]), | |
$new('div', [], null, [ | |
$new('p', ['userinfo-label'], 'Phone: '), | |
$new('input', ['topcoat-text-input', 'userinfo-data-field'], null, null, { | |
type: 'text', | |
readonly: '', | |
placeholder: '', | |
value: userInfo.telephoneNumber || '' | |
}) | |
]) | |
]); | |
}).forEach(function(userListItem) { | |
userlist.appendChild(userListItem); | |
}); | |
}, | |
// Renders application error. | |
error: function(err) { | |
var userlist = document.getElementById('userlist'); | |
userlist.innerHTML = ""; | |
var errorItem = document.createElement('li'); | |
errorItem.classList.add('topcoat-list__item'); | |
errorItem.classList.add('error-item'); | |
errorItem.innerText = err; | |
userlist.appendChild(errorItem); | |
} | |
}; | |
document.addEventListener('deviceready', app.onDeviceReady, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://medium.com/the-new-control-plane/connecting-adfs-to-a-cordova-application-64cb9a389e52