Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oAuth Resource owner credentials Grant UI Changes #1310

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 37 additions & 5 deletions app/scripts/services/AuthenticationService.js
@@ -1,6 +1,6 @@
(function (module) {
mifosX.services = _.extend(module, {
AuthenticationService: function (scope, httpService, localStorageService) {
AuthenticationService: function (scope, httpService, localStorageService,timeout, webStorage) {
var onSuccess = function (data) {
scope.$broadcast("UserAuthenticationSuccessEvent", data);
localStorageService.addToLocalStorage('userData', data);
Expand All @@ -9,18 +9,50 @@
var onFailure = function (data, status) {
scope.$broadcast("UserAuthenticationFailureEvent", data, status);
};

var apiVer = '/mifosng-provider/api/v1';

var getUserDetails = function(data){

localStorageService.addToLocalStorage('tokendetails', data);
setTimer(data.expires_in);
httpService.get( apiVer + "/userdetails?access_token=" + data.access_token)
.success(onSuccess)
.error(onFailure);

}

var updateAccessDetails = function(data){
var sessionData = webStorage.get('sessionData');
sessionData.authenticationKey = data.access_token;
webStorage.add("sessionData",sessionData);
localStorageService.addToLocalStorage('tokendetails', data);
var userDate = localStorageService.getFromLocalStorage("userData");
userDate.accessToken = data.access_token;
localStorageService.addToLocalStorage('userData', userDate);
httpService.setAuthorization(data.access_token);
setTimer(data.expires_in);
}

var setTimer = function(time){
timeout(getAccessToken, time * 1000);
}

var getAccessToken = function(){
var refreshToken = localStorageService.getFromLocalStorage("tokendetails").refresh_token;
httpService.cancelAuthorization();
httpService.post( "/mifosng-provider/api/oauth/token?&client_id=community-app&grant_type=refresh_token&client_secret=123&refresh_token=" + refreshToken)
.success(updateAccessDetails);
}

this.authenticateWithUsernamePassword = function (credentials) {
scope.$broadcast("UserAuthenticationStartEvent");
httpService.post(apiVer + "/authentication?username=" + credentials.username + "&password=" + credentials.password)
.success(onSuccess)
httpService.post( "/mifosng-provider/api/oauth/token?username=" + credentials.username + "&password=" + credentials.password +"&client_id=community-app&grant_type=password&client_secret=123")
.success(getUserDetails)
.error(onFailure);
};
}
});
mifosX.ng.services.service('AuthenticationService', ['$rootScope', 'HttpService', 'localStorageService', mifosX.services.AuthenticationService]).run(function ($log) {
mifosX.ng.services.service('AuthenticationService', ['$rootScope', 'HttpService', 'localStorageService','$timeout','webStorage', mifosX.services.AuthenticationService]).run(function ($log) {
$log.info("AuthenticationService initialized");
});
}(mifosX.services || {}));
6 changes: 3 additions & 3 deletions app/scripts/services/HttpServiceProvider.js
Expand Up @@ -40,7 +40,7 @@
};
});
this.setAuthorization = function (key) {
http.defaults.headers.common.Authorization = "Basic " + key;
http.defaults.headers.common.Authorization = "bearer " + key;
};

this.cancelAuthorization = function () {
Expand All @@ -54,6 +54,6 @@
mifosX.ng.services.config(function ($provide) {
$provide.provider('HttpService', mifosX.services.HttpServiceProvider);
}).run(function ($log) {
$log.info("HttpService initialized");
});
$log.info("HttpService initialized");
});
}(mifosX.services || {}));
24 changes: 13 additions & 11 deletions app/scripts/services/SessionManager.js
@@ -1,14 +1,15 @@
(function (module) {
mifosX.services = _.extend(module, {
SessionManager: function (webStorage, httpService, resourceFactory) {
SessionManager: function (webStorage, httpService, resourceFactory, localStorageService) {
var EMPTY_SESSION = {};

this.get = function (data) {
var accessToken = localStorageService.getFromLocalStorage("tokendetails").access_token;
if (data.shouldRenewPassword) {
httpService.setAuthorization(data.base64EncodedAuthenticationKey);
httpService.setAuthorization(data.accessToken);
} else{
webStorage.add("sessionData", {userId: data.userId, authenticationKey: data.base64EncodedAuthenticationKey, userPermissions: data.permissions});
httpService.setAuthorization(data.base64EncodedAuthenticationKey);
webStorage.add("sessionData", {userId: data.userId, authenticationKey: data.accessToken, userPermissions: data.permissions});
httpService.setAuthorization(data.accessToken);
return {user: new mifosX.models.LoggedInUser(data)};
};
}
Expand All @@ -34,11 +35,12 @@
}
});
mifosX.ng.services.service('SessionManager', [
'webStorage',
'HttpService',
'ResourceFactory',
mifosX.services.SessionManager
]).run(function ($log) {
$log.info("SessionManager initialized");
});
'webStorage',
'HttpService',
'ResourceFactory',
'localStorageService',
mifosX.services.SessionManager
]).run(function ($log) {
$log.info("SessionManager initialized");
});
}(mifosX.services || {}));