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 #47 from AzureAD/dev
Browse files Browse the repository at this point in the history
Push to master
  • Loading branch information
omercs committed Nov 25, 2014
2 parents 49ea459 + a416e24 commit 3e81b8d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This preview is optimized for working together with AngularJS.
## The Library

This is an early developer preview, released for the purpose of eliciting feedback.
The current version is **0.0.2**.
The current version is **0.0.3**.

You have multiple ways of getting ADAL JS:

Expand All @@ -16,7 +16,7 @@ Via CDN:
<!-- Latest compiled and minified JavaScript -->
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/0.0.1/js/adal.min.js"></script>

CDN will be updated to latest version 0.0.2.
CDN will be updated to latest version 0.0.3.

Via Bower:

Expand Down
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.2",
"version": "0.0.3",
"homepage": "https://github.com/AzureAD/azure-activedirectory-library-for-js",
"authors": [
"MSOpentech"
Expand Down
130 changes: 58 additions & 72 deletions lib/adal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//----------------------------------------------------------------------
// AdalJS v0.0.2
// AdalJS v0.0.3
// @preserve Copyright (c) Microsoft Open Technologies, Inc.
// All Rights Reserved
// Apache License 2.0
Expand Down Expand Up @@ -1003,7 +1003,7 @@ AuthenticationContext.prototype._cloneConfig = function (obj) {
};

AuthenticationContext.prototype._libVersion = function () {
return '0.0.2';
return '0.0.3';
};

AuthenticationContext.prototype._addClientId = function() {
Expand All @@ -1018,68 +1018,7 @@ function adalAngularModules() {

var AdalModule = angular.module('AdalAngular', []);

// Interceptor for http if needed
AdalModule.factory('ProtectedResourceInterceptor', ['$q', '$rootScope', '$injector', function ($q, $rootScope, $injector) {
var authService;
var injectAuthService = function () {
authService = authService || $injector.get('adalAuthenticationService');
};

return {
request: function (config) {
if (config) {
if (!$injector.has('adalAuthenticationService')) {
return config;
}
injectAuthService();
// This interceptor needs to load service, but dependeny definition causes circular reference error.
// Loading with injector is suggested at github. https://github.com/angular/angular.js/issues/2367
authService = authService || $injector.get('adalAuthenticationService');
config.headers = config.headers || {};

var resource = authService.getResourceForEndpoint(config.url);
var tokenStored = authService.getCachedToken(resource);
if (tokenStored) {
// check endpoint mapping if provided
config.headers.Authorization = 'Bearer ' + tokenStored;
return config;
} else {
// Cancel request if login is starting
if (authService.loginInProgress()) {
$q.reject();
return;
} else if (authService.config && resource !== authService.config.clientId) {
// external endpoints
// delayed request to return after iframe completes
var delayedRequest = $q.defer();
authService.acquireToken(resource).then(function (token) {
config.headers.Authorization = 'Bearer ' + token;
delayedRequest.resolve(config);
}, function (err) {
delayedRequest.reject(err);
});

return delayedRequest.promise;
}
}

return config;
}
},
responseError: function (rejection) {
if (rejection.status === 401) {
injectAuthService();
var resource = authService.getResourceForEndpoint(rejection.config.url);
authService.clearCacheForResource(resource);
$rootScope.$broadcast('adal:notAuthorized', rejection, resource);
}

return $q.reject(rejection);
}
};
}]);

var AdalService = function () {
AdalModule.provider('adalAuthenticationService', function () {
var _adal = null;
var _oauthData = { isAuthenticated: false, userName: '', loginError: '', profile: '' };

Expand Down Expand Up @@ -1119,8 +1058,8 @@ function adalAngularModules() {
};

// special function that exposes methods in Angular controller
// $rootScope, $window, $http, $q, $location, $timeout are injected by Angular
this.$get = function ($rootScope, $window, $http, $q, $location, $timeout) {
// $rootScope, $window, $q, $location, $timeout are injected by Angular
this.$get = ['$rootScope', '$window', '$q', '$location', '$timeout', function ($rootScope, $window, $q, $location, $timeout) {

var locationChangeHandler = function () {
var hash = $window.location.hash;
Expand Down Expand Up @@ -1274,13 +1213,60 @@ function adalAngularModules() {
_adal.clearCacheForResource(resource);
}
};
};
};

// Service that is accessible at config to init and controllers to use API methods
AdalModule.provider('adalAuthenticationService', function () {
return new AdalService();
}];
});

// Interceptor for http if needed
AdalModule.factory('ProtectedResourceInterceptor', ['adalAuthenticationService', '$q', '$rootScope', function (authService, $q, $rootScope) {

return {
request: function (config) {
if (config) {

// This interceptor needs to load service, but dependeny definition causes circular reference error.
// Loading with injector is suggested at github. https://github.com/angular/angular.js/issues/2367

config.headers = config.headers || {};

var resource = authService.getResourceForEndpoint(config.url);
var tokenStored = authService.getCachedToken(resource);
if (tokenStored) {
// check endpoint mapping if provided
config.headers.Authorization = 'Bearer ' + tokenStored;
return config;
} else {
// Cancel request if login is starting
if (authService.loginInProgress()) {
return $q.reject();
} else if (authService.config && resource !== authService.config.clientId) {
// external endpoints
// delayed request to return after iframe completes
var delayedRequest = $q.defer();
authService.acquireToken(resource).then(function (token) {
config.headers.Authorization = 'Bearer ' + token;
delayedRequest.resolve(config);
}, function (err) {
delayedRequest.reject(err);
});

return delayedRequest.promise;
}
}

return config;
}
},
responseError: function (rejection) {
if (rejection && rejection.status === 401) {
var resource = authService.getResourceForEndpoint(rejection.config.url);
authService.clearCacheForResource(resource);
$rootScope.$broadcast('adal:notAuthorized', rejection, resource);
}

return $q.reject(rejection);
}
};
}]);
} else {
console.log('Angular.JS is not included');
}
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.2",
"version": "0.0.3",
"description": "Windows Azure Active Directory Client Library for js",
"keywords": [
"implicit",
Expand Down
11 changes: 8 additions & 3 deletions tests/angularModuleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ describe('TaskCtl', function () {

adalServiceProvider.getCachedToken = function (resource) {
console.log('Requesting token for resource:' + resource);
if (resource) {
if (resource === 'resource1') {
return 'Token3434';
}

if (resource === 'resource2') {
return 'Token123';
}


return '';
};

Expand All @@ -69,9 +74,9 @@ describe('TaskCtl', function () {
});

it('does not sent tokens for other webapi calls', function () {
$httpBackend.expectGET('/api/Item/13', function (headers) {
$httpBackend.expectGET('/anotherApi/Item/13', function (headers) {
console.log('headers test' + headers.Authorization);
return !headers.Authorization;
return headers.Authorization === 'Bearer Token123';
}).respond(200, { id: 5, itemName: 'ItemWithoutAuth' });
scope.itemCall();
$httpBackend.flush();
Expand Down
3 changes: 2 additions & 1 deletion tests/testApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ app.config(['$httpProvider', '$routeProvider', 'adalAuthenticationServiceProvide

var endpoints = {
'/api/Todo/': 'resource1',
'/anotherApi/Item/': 'resource2'
};

adalAuthenticationServiceProvider.init(
Expand All @@ -48,7 +49,7 @@ app.config(['$httpProvider', '$routeProvider', 'adalAuthenticationServiceProvide
app.factory('ItemFactory', ['$http', function ($http) {
var serviceFactory = {};
var _getItem = function (id) {
return $http.get('/api/Item/' + id);
return $http.get('/anotherApi/Item/' + id);
};
serviceFactory.getItem = _getItem;
return serviceFactory;
Expand Down

0 comments on commit 3e81b8d

Please sign in to comment.