Windows Azure Active Directory Client Library for js
npm install omelek-adal-angularActive Directory Authentication Library (ADAL) for JavaScript
====================================

Active Directory Authentication Library for JavaScript (ADAL JS) helps you to use Azure AD for handling authentication in your single page applications.
This library is optimized for working together with AngularJS.
log method depending on how you want to redirect logs.
js
var app = angular.module('demoApp', ['ngRoute', 'AdalAngular']);
`
3- When HTML5 mode is configured, ensure the $locationProvider hashPrefix is set
`js
// using '!' as the hashPrefix but can be a character of your choosing
app.config(['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode(true).hashPrefix('!');
}]);
`
Without the hashPrefix set, the AAD login will loop indefinitely as the callback URL from AAD (in the form of, {yourBaseUrl}/#{AADTokenAndState}) will be rewritten to remove the '#' causing the token parsing to fail and login sequence to occur again.
4- Initialize ADAL with the AAD app coordinates at app config time. The minimum required object to initialize ADAL is:
`js
adalAuthenticationServiceProvider.init({
// clientId is the identifier assigned to your app by Azure Active Directory.
clientId: "e9a5a8b6-8af7-4719-9821-0deef255f68e"
})
`
A single-tenant configuration, with CORS, looks like this:
`js
// endpoint to resource mapping(optional)
var endpoints = {
"https://yourhost/api": "b6a68585-5287-45b2-ba82-383ba1f60932",
};
adalAuthenticationServiceProvider.init(
{
// Config to specify endpoints and similar for your app
tenant: "52d4b072-9470-49fb-8721-bc3a1c9912a1", // Optional by default, it sends common
clientId: "e9a5a8b6-8af7-4719-9821-0deef255f68e", // Required
//localLoginUrl: "/login", // optional
//redirectUri : "your site", optional
endpoints: endpoints // If you need to send CORS api requests.
},
$httpProvider // pass http provider to inject request interceptor to attach tokens
);
`
5- Define which routes you want to secure via ADAL - by adding requireADLogin: true to their definition
`js
$routeProvider.
when("/todoList", {
controller: "todoListController",
templateUrl: "/App/Views/todoList.html",
requireADLogin: true
});
`
6- Any service invocation code you might have will remain unchanged. ADAL's interceptor will automatically add tokens for every outgoing call.
Anonymous endpoints, introduced in version 1.0.10, is an array of values that will be ignored by the ADAL route/state change handlers. ADAL will not attach a token to outgoing requests that have these keywords or URI. Routes that do not specify the `requireADLogin=true` property are added to the `anonymousEndpoints` array automatically.
Optional
7- If you so choose, in addition (or substitution) to route level protection you can add explicit login/logout UX elements. Furthermore, you can access properties of the currently signed in user directly form JavaScript (via userInfo and userInfo.profile).
The userInfo.profile property provides access to the claims in the ID token received from AAD. The claims can be used by the application for validation, to identify the subject's directory tenant, and so on. The complete list of claims with a brief description of each value is here, Claims in Azure AD Security Tokens:
`html
Angular Adal Sample
Home
ToDo List
Welcome {{userInfo.userName}}
{{userInfo.loginError}}
{{testMessage}}
Your view will appear here.
`
7- You have full control on how to trigger sign in, sign out and how to deal with errors:
`js
'use strict';
app.controller('homeController', ['$scope', '$location', 'adalAuthenticationService', function ($scope, $location, adalAuthenticationService) {
// this is referencing adal module to do login
//userInfo is defined at the $rootscope with adalAngular module
$scope.testMessage = "";
$scope.init = function () {
$scope.testMessage = "";
};
$scope.logout = function () {
adalAuthenticationService.logOut();
};
$scope.login = function () {
adalAuthenticationService.login();
};
// optional
$scope.$on("adal:loginSuccess", function () {
$scope.testMessage = "loginSuccess";
});
// optional
$scope.$on("adal:loginFailure", function () {
$scope.testMessage = "loginFailure";
$location.path("/login");
});
// optional
$scope.$on("adal:notAuthorized", function (event, rejection, forResource) {
$scope.testMessage = "It is not Authorized for resource:" + forResource;
});
}]);
`
$3
By default, you have multi-tenant support. ADAL will set tenant to 'common', if it is not specified in the config. This allows any Microsoft account to authenticate to your application. If you are not interested in multi-tenant behavior, you will need to set the `tenant` property as shown above.
If you allow multi-tenant authentication, and you do not wish to allow all Microsoft account users to use your application, you must provide your own method of filtering the token issuers to only those tenants who are allowed to login.
$3
Default storage location is sessionStorage. You can specify localStorage in the config as well.
`js
adalAuthenticationServiceProvider.init(
{
// Config to specify endpoints and similar for your app
clientId: 'cb68f72f...',
cacheLocation: 'localStorage' // optional cache location default is sessionStorage
},
$httpProvider // pass http provider to inject request interceptor to attach tokens
);
`
$3
Tokens are accessible from javascript since ADAL.JS is using HTML5 storage. Default storage option is sessionStorage, which keeps the tokens per session. You should ask user to login again for important operations on your app.
You should protect your site for XSS. Please check the article here: https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet_Prevention_Cheat_Sheet)
$3
ADAL will get access token for given CORS API endpoints in the config. Access token is requested using Iframe. Iframe needs to access the cookies for the same domain that you did the initial sign in. IE does not allow to access cookies in IFrame for localhost. Your url needs to be fully qualified domain i.e http://yoursite.azurewebsites.com. Chrome does not have this restriction.
To make CORS API call, you need to specify endpoints in the config for your CORS API. Your service will be similar to this to make the call from JS. In your API project, you need to enable CORS API requests to receive flight requests. You can check the sample for CORS API sample.
`js
'use strict';
app.factory('contactService', ['$http', function ($http) {
var serviceFactory = {};
var _getItems = function () {
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
return $http.get('http://adaljscors.azurewebsites.net/api/contacts');
};
serviceFactory.getItems = _getItems;
return serviceFactory;
}]);
``