Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #62 from flosim/SimpleADALSample
Browse files Browse the repository at this point in the history
Simple adal sample
  • Loading branch information
omercs committed Dec 22, 2014
2 parents 4806af0 + 46b15af commit bae8d71
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 14 deletions.
16 changes: 16 additions & 0 deletions SimpleADALSample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Simple ADAL.JS Sample
=====================

This simple example is intended to show how to use the adal.js methods to authenticate with Office 365 and obtain a token that can be used in REST calls to the Office 365 API.
The sample uses a simple HTML page and about 50 lines of JavaScript and only depends on adal.js and jQuery.

## Running the sample

Modify the configuration settings in app.js and ensure that you have your app settings in Azure AD .

Open a new website in Visual Studio (File->New->Web site...) selecting the directory containing these files. Visual Studio will allocate a
port number which is visible if you inspect the project properties. This address and port will need to be included in the list of redirect URLs
in your Azure AD app settings.

Clicking on the login button should take you to the login page. After you have logged in you should see your user id and name in the page.

31 changes: 31 additions & 0 deletions SimpleADALSample/Web.Debug.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
15 changes: 15 additions & 0 deletions SimpleADALSample/Web.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>

<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>

<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>

</configuration>
48 changes: 48 additions & 0 deletions SimpleADALSample/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
(function () {
// Simple JavaScript to demonstrate how to use ADAL.JS
var config =
{
tenant: 'contoso.onmicrosoft.com', // replace with your tenant id (e.g. contoso.onmicrosoft.com)
clientId: '79f10513-ca82-4e6a-8cc9-f10513ff6928', // replace with your clientID (GUID)
extraQueryParameter: 'nux=1',
url: window.location.href // use current page URL
};

var authContext = new AuthenticationContext(config);

if (authContext.isCallback(window.location.hash)) {
// callback from Azure AD auth page
var requestInfo = authContext.getRequestInfo(window.location.hash);
authContext.saveTokenFromHash(requestInfo);
window.location.hash = '';
}

// bind to button events
jQuery("#login").click(function () {
authContext.login();
});
jQuery("#logout").click(function () {
authContext.logOut();
});

var resource = authContext.getResourceForEndpoint(config.url);
var token = authContext.getCachedToken(resource); // auth token to be used against Office365 API
var isAuthenticated = token !== null && token.length > 0;
var loginError = authContext.getLoginError() || '';

var user = authContext.getCachedUser();
if (isAuthenticated && user !== null) {
jQuery("#userid").text(user.userName);
var name = "";
if (user.profile != null) name = user.profile.given_name + " " + user.profile.family_name;
jQuery("#username").text(name);
// TODO: demonstrate Office365 API REST call using bearer token
// (requires iframe to work in browser because of cross-site request)
}
else {
jQuery("#userid").text('user is not logged in');
jQuery("#username").text('');
}
jQuery("#error").text(loginError);
}());
20 changes: 20 additions & 0 deletions SimpleADALSample/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>Simple ADAL.JS Demo</title>
</head>
<body>
<h1>Simple ADAL.JS Demo</h1>
<p>This sample demonstrates how to take use ADAL.JS for adding Azure AD authentication to a simple HTML/JavaScript app.</p>
<h2 id="userid"></h2>
<h2 id="username"></h2>
<h2 id="error"></h2>
<br />
<button id="login">Login</button>
<button id="logout">Logout</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/0.0.4/js/adal.min.js"></script>
<script src="app.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adal-angular",
"version": "0.0.3",
"version": "0.0.5",
"homepage": "https://github.com/AzureAD/azure-activedirectory-library-for-js",
"authors": [
"MSOpentech"
Expand Down
58 changes: 46 additions & 12 deletions lib/adal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//----------------------------------------------------------------------
// AdalJS v0.0.4
// AdalJS v0.0.5
// @preserve Copyright (c) Microsoft Open Technologies, Inc.
// All Rights Reserved
// Apache License 2.0
Expand All @@ -21,10 +21,11 @@
// node.js usage for tests
var AuthenticationContext;
if (typeof module !== 'undefined' && module.exports) {
var window, localStorage, angular, document;
module.exports.inject = function (windowInj, localStorageInj, documentInj, MathInj, angularInj, conf) {
var window, localStorage, angular, document, sessionStorage;
module.exports.inject = function (windowInj, storageInj, documentInj, MathInj, angularInj, conf) {
window = windowInj;
localStorage = localStorageInj;
localStorage = storageInj;
sessionStorage = storageInj;
document = documentInj;
Math = MathInj; // jshint ignore:line
angular = angularInj;
Expand Down Expand Up @@ -966,23 +967,48 @@ AuthenticationContext.prototype._logstatus = function (msg) {
};

AuthenticationContext.prototype._saveItem = function (key, obj) {
if (!this._supportsLocalStorage()) {
this._logStatus('Local storage is not supported');
return false;

if (this.config && this.config.cacheLocation && this.config.cacheLocation === 'localStorage') {

if (!this._supportsLocalStorage()) {
this._logStatus('Local storage is not supported');
return false;
}

localStorage.setItem(key, obj);

return true;
}

localStorage.setItem(key, obj);
// Default as session storage
if (!this._supportsSessionStorage()) {
this._logstatus('Session storage is not supported');
return false;
}

sessionStorage.setItem(key, obj);
return true;
};

AuthenticationContext.prototype._getItem = function (key) {
if (!this._supportsLocalStorage()) {
this._logstatus('Local storage is not supported');

if (this.config && this.config.cacheLocation && this.config.cacheLocation === 'localStorage') {

if (!this._supportsLocalStorage()) {
this._logstatus('Local storage is not supported');
return null;
}

return localStorage.getItem(key);
}

// Default as session storage
if (!this._supportsSessionStorage()) {
this._logstatus('Session storage is not supported');
return null;
}

return localStorage.getItem(key);
return sessionStorage.getItem(key);
};

AuthenticationContext.prototype._supportsLocalStorage = function () {
Expand All @@ -993,6 +1019,14 @@ AuthenticationContext.prototype._supportsLocalStorage = function () {
}
};

AuthenticationContext.prototype._supportsSessionStorage = function () {
try {
return 'sessionStorage' in window && window['sessionStorage'];
} catch (e) {
return false;
}
};

AuthenticationContext.prototype._cloneConfig = function (obj) {
if (null === obj || 'object' !== typeof obj) {
return obj;
Expand All @@ -1008,7 +1042,7 @@ AuthenticationContext.prototype._cloneConfig = function (obj) {
};

AuthenticationContext.prototype._libVersion = function () {
return '0.0.4';
return '0.0.5';
};

AuthenticationContext.prototype._addClientId = function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": ""
},
"version": "0.0.4",
"version": "0.0.5",
"description": "Windows Azure Active Directory Client Library for js",
"keywords": [
"implicit",
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/spec/AdalSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('Adal', function () {
}
},
localStorage: {},
sessionStorage: {},
atob: atobHelper
};
var mathMock = {
Expand Down Expand Up @@ -94,6 +95,7 @@ describe('Adal', function () {
storageFake.setItem(STORAGE_TOKEN_KEYS, RESOURCE1 + '|');

window.localStorage = storageFake;
window.sessionStorage = storageFake;

// Init adal
adal = new AdalModule.inject(window, storageFake, documentMock, mathMock, angularMock, conf);
Expand Down

0 comments on commit bae8d71

Please sign in to comment.