diff --git a/SimpleADALSample/README.md b/SimpleADALSample/README.md new file mode 100644 index 00000000..67f29efa --- /dev/null +++ b/SimpleADALSample/README.md @@ -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. + \ No newline at end of file diff --git a/SimpleADALSample/Web.Debug.config b/SimpleADALSample/Web.Debug.config new file mode 100644 index 00000000..c3584446 --- /dev/null +++ b/SimpleADALSample/Web.Debug.config @@ -0,0 +1,31 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/SimpleADALSample/Web.config b/SimpleADALSample/Web.config new file mode 100644 index 00000000..e2b2e913 --- /dev/null +++ b/SimpleADALSample/Web.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + diff --git a/SimpleADALSample/app.js b/SimpleADALSample/app.js new file mode 100644 index 00000000..11cf667c --- /dev/null +++ b/SimpleADALSample/app.js @@ -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); +}()); \ No newline at end of file diff --git a/SimpleADALSample/index.html b/SimpleADALSample/index.html new file mode 100644 index 00000000..086384c9 --- /dev/null +++ b/SimpleADALSample/index.html @@ -0,0 +1,20 @@ + + + + Simple ADAL.JS Demo + + +

Simple ADAL.JS Demo

+

This sample demonstrates how to take use ADAL.JS for adding Azure AD authentication to a simple HTML/JavaScript app.

+

+

+

+
+ + + + + + + + \ No newline at end of file diff --git a/bower.json b/bower.json index 92c0af03..9b57d173 100644 --- a/bower.json +++ b/bower.json @@ -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" diff --git a/lib/adal.js b/lib/adal.js index 6f627a8d..bb9325dc 100644 --- a/lib/adal.js +++ b/lib/adal.js @@ -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 @@ -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; @@ -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 () { @@ -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; @@ -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() { diff --git a/package.json b/package.json index 94d89b37..bb21fde9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/unit/spec/AdalSpec.js b/tests/unit/spec/AdalSpec.js index bf925dc4..9765e6dd 100644 --- a/tests/unit/spec/AdalSpec.js +++ b/tests/unit/spec/AdalSpec.js @@ -33,6 +33,7 @@ describe('Adal', function () { } }, localStorage: {}, + sessionStorage: {}, atob: atobHelper }; var mathMock = { @@ -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);