mirror of https://github.com/portainer/portainer
Revert "refactor(auth): extract oauth login mechanism to service"
This reverts commit 0a439b3893
.
pull/2749/head
parent
3699b794eb
commit
41ded64037
|
@ -1,41 +0,0 @@
|
|||
angular.module('portainer.extensions.oauth').service('OAuthService', [
|
||||
'API_ENDPOINT_OAUTH', 'OAuth', 'urlHelper', 'Notifications',
|
||||
function OAuthService(API_ENDPOINT_OAUTH, OAuth, urlHelper, Notifications) {
|
||||
this.login = login;
|
||||
|
||||
function login() {
|
||||
var loginUrl = API_ENDPOINT_OAUTH + '/login';
|
||||
var popup = window.open(loginUrl, 'login-popup', 'width=800, height=600');
|
||||
if (!popup) {
|
||||
Notifications.warn('Please enable popups for this page');
|
||||
}
|
||||
return waitForCode(popup).then(function onCodeReady(code) {
|
||||
return OAuth.validate({ code: code }).$promise;
|
||||
});
|
||||
}
|
||||
|
||||
function waitForCode(popup) {
|
||||
return waitFor(function checkIfCodeIsAvailable() {
|
||||
if (popup.document.URL.indexOf('code') !== -1) {
|
||||
var queryParams = popup.location.search;
|
||||
popup.close();
|
||||
return urlHelper.getParameter(queryParams, 'code');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function waitFor(clbk, interval) {
|
||||
interval = interval || 100;
|
||||
var intervalId;
|
||||
return new Promise(function executor(resolve) {
|
||||
intervalId = setInterval(function intervalFunction() {
|
||||
var callbackReturn = clbk();
|
||||
if (callbackReturn) {
|
||||
clearInterval(intervalId);
|
||||
resolve(callbackReturn);
|
||||
}
|
||||
}, interval);
|
||||
});
|
||||
}
|
||||
},
|
||||
]);
|
|
@ -2,13 +2,13 @@ angular.module('portainer.app').service('urlHelper', function urlHelper($window)
|
|||
this.getParameter = getParameter;
|
||||
this.cleanParameters = cleanParameters;
|
||||
|
||||
function getParameter(queryParams, param) {
|
||||
var parameters = extractParameters(queryParams);
|
||||
function getParameter(param) {
|
||||
var parameters = extractParameters();
|
||||
return parameters[param];
|
||||
}
|
||||
|
||||
function extractParameters(queryParams) {
|
||||
var queryString = queryParams.replace(/.*?\?/,'').split('&');
|
||||
function extractParameters() {
|
||||
var queryString = $window.location.search.replace(/.*?\?/,'').split('&');
|
||||
return queryString.reduce(function(acc, keyValStr) {
|
||||
var keyVal = keyValStr.split('=');
|
||||
var key = keyVal[0];
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
angular.module('portainer.app')
|
||||
.factory('Authentication', [
|
||||
'Auth', 'OAuthService', 'jwtHelper', 'LocalStorage', 'StateManager', 'EndpointProvider',
|
||||
function AuthenticationFactory(Auth, OAuthService, jwtHelper, LocalStorage, StateManager, EndpointProvider) {
|
||||
'Auth', 'OAuth', 'jwtHelper', 'LocalStorage', 'StateManager', 'EndpointProvider',
|
||||
function AuthenticationFactory(Auth, OAuth, jwtHelper, LocalStorage, StateManager, EndpointProvider) {
|
||||
'use strict';
|
||||
|
||||
var service = {};
|
||||
|
@ -13,8 +13,6 @@ function AuthenticationFactory(Auth, OAuthService, jwtHelper, LocalStorage, Stat
|
|||
service.logout = logout;
|
||||
service.isAuthenticated = isAuthenticated;
|
||||
service.getUserDetails = getUserDetails;
|
||||
|
||||
|
||||
|
||||
function init() {
|
||||
var jwt = LocalStorage.getJWT();
|
||||
|
@ -24,10 +22,10 @@ function AuthenticationFactory(Auth, OAuthService, jwtHelper, LocalStorage, Stat
|
|||
}
|
||||
}
|
||||
|
||||
function OAuthLogin() {
|
||||
return OAuthService.login()
|
||||
.then(function onLoginSuccess(loginResponse) {
|
||||
setUser(loginResponse.jwt);
|
||||
function OAuthLogin(code) {
|
||||
return OAuth.login({ code: code }).$promise
|
||||
.then(function onLoginSuccess(response) {
|
||||
return setUser(response.jwt);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -28,9 +28,11 @@
|
|||
<!-- login button -->
|
||||
<div class="form-group">
|
||||
<div class="col-sm-12" >
|
||||
<button class="btn btn-primary btn-sm pull-left" ng-click="oauthLogin()" style="margin-left:2px" ng-if="AuthenticationMethod === 3">
|
||||
<i class="fa fa-sign-in-alt" aria-hidden="true"></i> Login with OAuth
|
||||
</button>
|
||||
<a ng-href="{{OAuthLoginURI}}" ng-if="AuthenticationMethod === 3">
|
||||
<div class="btn btn-primary btn-sm pull-left" style="margin-left:2px">
|
||||
<i class="fa fa-sign-in-alt" aria-hidden="true"></i> Login with OAuth
|
||||
</div>
|
||||
</a>
|
||||
|
||||
<button type="submit" class="btn btn-primary btn-sm pull-right" ng-click="authenticateUser()"><i class="fa fa-sign-in-alt" aria-hidden="true"></i> Login</button>
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
angular.module('portainer.app')
|
||||
.controller('AuthenticationController', ['$q', '$scope', '$state', '$stateParams', '$sanitize', 'Authentication', 'UserService', 'EndpointService', 'StateManager', 'Notifications', 'SettingsService',
|
||||
function ($q, $scope, $state, $stateParams, $sanitize, Authentication, UserService, EndpointService, StateManager, Notifications, SettingsService) {
|
||||
.controller('AuthenticationController', ['urlHelper','$q', '$scope', '$state', '$stateParams', '$sanitize', 'Authentication', 'UserService', 'EndpointService', 'StateManager', 'Notifications', 'SettingsService',
|
||||
function (urlHelper, $q, $scope, $state, $stateParams, $sanitize, Authentication, UserService, EndpointService, StateManager, Notifications, SettingsService) {
|
||||
$scope.logo = StateManager.getState().application.logo;
|
||||
|
||||
$scope.formValues = {
|
||||
|
@ -37,17 +37,6 @@ function ($q, $scope, $state, $stateParams, $sanitize, Authentication, UserServi
|
|||
});
|
||||
};
|
||||
|
||||
$scope.oauthLogin = function oauthLogin() {
|
||||
return Authentication.OAuthLogin()
|
||||
.then(function onLoginSuccess() {
|
||||
return $state.go('portainer.home');
|
||||
})
|
||||
.catch(function onError(error) {
|
||||
$scope.state.AuthenticationError = error.message;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
function unauthenticatedFlow() {
|
||||
EndpointService.endpoints()
|
||||
.then(function success(endpoints) {
|
||||
|
@ -115,10 +104,23 @@ function ($q, $scope, $state, $stateParams, $sanitize, Authentication, UserServi
|
|||
authenticatedFlow();
|
||||
}
|
||||
|
||||
|
||||
var code = urlHelper.getParameter('code');
|
||||
if (code) {
|
||||
oAuthLogin(code);
|
||||
}
|
||||
}
|
||||
|
||||
function oAuthLogin(code) {
|
||||
return Authentication.OAuthLogin(code)
|
||||
.then(function success() {
|
||||
urlHelper.cleanParameters();
|
||||
$state.go('portainer.home');
|
||||
})
|
||||
.catch(function error() {
|
||||
$scope.state.AuthenticationError = 'Failed to authenticate with OAuth2 Provider';
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
initView();
|
||||
}]);
|
||||
|
|
Loading…
Reference in New Issue