From a7decc194819d95d38fdaabc35f8f0a66dd0b339 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Sun, 27 Dec 2015 17:54:11 -0500 Subject: [PATCH 1/3] Fixing some issues with dynamically supporting multiple SSO providers --- lemur/auth/views.py | 25 +- lemur/static/app/angular/app.js | 268 ++++++++++-------- .../app/angular/authentication/login/login.js | 5 +- .../authentication/login/login.tpl.html | 4 +- .../app/angular/authentication/services.js | 4 - lemur/static/app/index.html | 2 +- lemur/users/models.py | 4 - 7 files changed, 160 insertions(+), 152 deletions(-) diff --git a/lemur/auth/views.py b/lemur/auth/views.py index aa8823e1..40716b85 100644 --- a/lemur/auth/views.py +++ b/lemur/auth/views.py @@ -231,7 +231,6 @@ class Ping(Resource): class Google(Resource): - def __init__(self): self.reqparse = reqparse.RequestParser() super(Google, self).__init__() @@ -271,34 +270,32 @@ class Google(Resource): class Providers(Resource): - def get(self): - - active_providers = dict() + active_providers = [] for provider in current_app.config.get("ACTIVE_PROVIDERS"): provider = provider.lower() if provider == "google": - - active_providers["google"] = { + active_providers.append({ + 'name': 'google', 'clientId': current_app.config.get("GOOGLE_CLIENT_ID"), 'url': api.url_for(Google) - } + }) elif provider == "ping": - - active_providers["oauth2"] = { + active_providers.append({ 'name': current_app.config.get("PING_NAME"), - 'url': api.url_for(Ping), - 'redirectUri': '', # TODO + 'url': current_app.config.get('PING_REDIRECT_URI'), + 'redirectUri': current_app.config.get("PING_REDIRECT_URI"), 'clientId': current_app.config.get("PING_CLIENT_ID"), 'responseType': 'code', 'scope': ['openid', 'email', 'profile', 'address'], 'scopeDelimeter': ' ', - 'authorizationEndpoint': '', # TODO - 'requiredUrlParams': ['scope'] - } + 'authorizationEndpoint': current_app.config.get("PING_AUTH_ENDPOINT"), + 'requiredUrlParams': ['scope'], + 'type': '2.0' + }) return active_providers diff --git a/lemur/static/app/angular/app.js b/lemur/static/app/angular/app.js index 5c72b1bd..b8a0302d 100644 --- a/lemur/static/app/angular/app.js +++ b/lemur/static/app/angular/app.js @@ -1,139 +1,157 @@ 'use strict'; -var lemur = angular - .module('lemur', [ - 'ui.router', - 'ngTable', - 'ngAnimate', - 'chart.js', - 'restangular', - 'angular-loading-bar', - 'ui.bootstrap', - 'angular-spinkit', - 'toaster', - 'uiSwitch', - 'mgo-angular-wizard', - 'satellizer', - 'ngLetterAvatar', - 'angular-clipboard', - 'ngFileSaver' - ]) - .config(function ($stateProvider, $urlRouterProvider, $authProvider, AuthenticationService) { - $urlRouterProvider.otherwise('/welcome'); +(function() { + var lemur = angular + .module('lemur', [ + 'ui.router', + 'ngTable', + 'ngAnimate', + 'chart.js', + 'restangular', + 'angular-loading-bar', + 'ui.bootstrap', + 'angular-spinkit', + 'toaster', + 'uiSwitch', + 'mgo-angular-wizard', + 'satellizer', + 'ngLetterAvatar', + 'angular-clipboard', + 'ngFileSaver' + ]); + fetchData().then(bootstrapApplication); + + function fetchData() { + var initInjector = angular.injector(['ng']); + var $http = initInjector.get('$http'); + + return $http.get('http://localhost:8000/api/1/auth/providers').then(function(response) { + lemur.constant('providers', response.data); + }, function(errorResponse) { + // Handle error case + }); + } + + function bootstrapApplication() { + angular.element(document).ready(function() { + angular.bootstrap(document, ["lemur"]); + }); + } + + lemur.config(function ($stateProvider, $urlRouterProvider, $authProvider, providers) { + $urlRouterProvider.otherwise('/welcome'); $stateProvider .state('welcome', { url: '/welcome', templateUrl: 'angular/welcome/welcome.html' }); - AuthenticationService.get_providers().then(function (active_providers) { - var provider_names = []; - for (var key in active_providers) { - if (active_providers.hasOwnProperty(key)) { - provider_names.push(key); - } - } - - for (var i=0; i < provider_names.length; i++) { - $authProvider[provider_names[i]](active_providers[provider_names[i]]); - } - } - }); - -lemur.service('MomentService', function () { - this.diffMoment = function (start, end) { - if (end !== 'None') { - return moment(end, 'YYYY-MM-DD HH:mm Z').diff(moment(start, 'YYYY-MM-DD HH:mm Z'), 'minutes') + ' minutes'; - } - return 'Unknown'; - }; - this.createMoment = function (date) { - if (date !== 'None') { - return moment(date, 'YYYY-MM-DD HH:mm Z').fromNow(); - } - return 'Unknown'; - }; -}); - -lemur.controller('datePickerController', function ($scope, $timeout){ - $scope.open = function() { - $timeout(function() { - $scope.opened = true; - }); - }; -}); - -lemur.service('DefaultService', function (LemurRestangular) { - var DefaultService = this; - DefaultService.get = function () { - return LemurRestangular.all('defaults').customGET().then(function (defaults) { - return defaults; - }); - }; -}); - -lemur.factory('LemurRestangular', function (Restangular, $location, $auth) { - return Restangular.withConfig(function (RestangularConfigurer) { - RestangularConfigurer.setBaseUrl('http://localhost:8000/api/1'); - RestangularConfigurer.setDefaultHttpFields({withCredentials: true}); - - RestangularConfigurer.addResponseInterceptor(function (data, operation) { - var extractedData; - - // .. to look for getList operations - if (operation === 'getList') { - // .. and handle the data and meta data - extractedData = data.items; - extractedData.total = data.total; + _.each(providers, function(provider) { + if ($authProvider.hasOwnProperty(provider.name)) { + $authProvider[provider.name] = provider; } else { - extractedData = data; - } - - return extractedData; - }); - - RestangularConfigurer.setErrorInterceptor(function(response) { - if (response.status === 400) { - if (response.data.message) { - var data = ''; - _.each(response.data.message, function (value, key) { - data = data + ' ' + key + ' ' + value; - }); - response.data.message = data; - } + $authProvider.oauth2(provider); } }); - - RestangularConfigurer.addFullRequestInterceptor(function (element, operation, route, url, headers, params) { - // We want to make sure the user is auth'd before any requests - if (!$auth.isAuthenticated()) { - $location.path('/login'); - return false; - } - - var regExp = /\[([^)]+)\]/; - - var s = 'sorting'; - var f = 'filter'; - var newParams = {}; - for (var item in params) { - if (item.indexOf(s) > -1) { - newParams.sortBy = regExp.exec(item)[1]; - newParams.sortDir = params[item]; - } else if (item.indexOf(f) > -1) { - var key = regExp.exec(item)[1]; - newParams.filter = key + ';' + params[item]; - } else { - newParams[item] = params[item]; - } - } - return { params: newParams }; - }); - }); -}); -lemur.run(['$templateCache', function ($templateCache) { - $templateCache.put('ng-table/pager.html', '
'); -}]); + lemur.service('MomentService', function () { + this.diffMoment = function (start, end) { + if (end !== 'None') { + return moment(end, 'YYYY-MM-DD HH:mm Z').diff(moment(start, 'YYYY-MM-DD HH:mm Z'), 'minutes') + ' minutes'; + } + return 'Unknown'; + }; + this.createMoment = function (date) { + if (date !== 'None') { + return moment(date, 'YYYY-MM-DD HH:mm Z').fromNow(); + } + return 'Unknown'; + }; + }); + + lemur.controller('datePickerController', function ($scope, $timeout){ + $scope.open = function() { + $timeout(function() { + $scope.opened = true; + }); + }; + }); + + lemur.service('DefaultService', function (LemurRestangular) { + var DefaultService = this; + DefaultService.get = function () { + return LemurRestangular.all('defaults').customGET().then(function (defaults) { + return defaults; + }); + }; + }); + + lemur.factory('LemurRestangular', function (Restangular, $location, $auth) { + return Restangular.withConfig(function (RestangularConfigurer) { + RestangularConfigurer.setBaseUrl('http://localhost:8000/api/1'); + RestangularConfigurer.setDefaultHttpFields({withCredentials: true}); + + RestangularConfigurer.addResponseInterceptor(function (data, operation) { + var extractedData; + + // .. to look for getList operations + if (operation === 'getList') { + // .. and handle the data and meta data + extractedData = data.items; + extractedData.total = data.total; + } else { + extractedData = data; + } + + return extractedData; + }); + + RestangularConfigurer.setErrorInterceptor(function(response) { + if (response.status === 400) { + if (response.data.message) { + var data = ''; + _.each(response.data.message, function (value, key) { + data = data + ' ' + key + ' ' + value; + }); + response.data.message = data; + } + } + }); + + RestangularConfigurer.addFullRequestInterceptor(function (element, operation, route, url, headers, params) { + // We want to make sure the user is auth'd before any requests + if (!$auth.isAuthenticated()) { + $location.path('/login'); + return false; + } + + var regExp = /\[([^)]+)\]/; + + var s = 'sorting'; + var f = 'filter'; + var newParams = {}; + for (var item in params) { + if (item.indexOf(s) > -1) { + newParams.sortBy = regExp.exec(item)[1]; + newParams.sortDir = params[item]; + } else if (item.indexOf(f) > -1) { + var key = regExp.exec(item)[1]; + newParams.filter = key + ';' + params[item]; + } else { + newParams[item] = params[item]; + } + } + return { params: newParams }; + }); + + }); + }); + + lemur.run(['$templateCache', function ($templateCache) { + $templateCache.put('ng-table/pager.html', '
'); + }]); +}()); + + diff --git a/lemur/static/app/angular/authentication/login/login.js b/lemur/static/app/angular/authentication/login/login.js index 8a4fc6ee..9c389737 100644 --- a/lemur/static/app/angular/authentication/login/login.js +++ b/lemur/static/app/angular/authentication/login/login.js @@ -8,11 +8,12 @@ angular.module('lemur') controller: 'LoginController' }); }) - .controller('LoginController', function ($rootScope, $scope, AuthenticationService, UserService) { + .controller('LoginController', function ($rootScope, $scope, AuthenticationService, UserService, providers) { $scope.login = AuthenticationService.login; $scope.authenticate = AuthenticationService.authenticate; $scope.logout = AuthenticationService.logout; - $scope.get_providers = AuthenticationService.get_providers; + + $scope.providers = providers; UserService.getCurrentUser().then(function (user) { $scope.currentUser = user; diff --git a/lemur/static/app/angular/authentication/login/login.tpl.html b/lemur/static/app/angular/authentication/login/login.tpl.html index 7b9a63e9..d47e2c3f 100644 --- a/lemur/static/app/angular/authentication/login/login.tpl.html +++ b/lemur/static/app/angular/authentication/login/login.tpl.html @@ -3,8 +3,8 @@
-
diff --git a/lemur/static/app/angular/authentication/services.js b/lemur/static/app/angular/authentication/services.js index 7719b444..dddffeb4 100644 --- a/lemur/static/app/angular/authentication/services.js +++ b/lemur/static/app/angular/authentication/services.js @@ -6,10 +6,6 @@ angular.module('lemur') .service('AuthenticationService', function ($location, $rootScope, AuthenticationApi, UserService, toaster, $auth) { var AuthenticationService = this; - AuthenticationService.get_providers = function () { - return AuthenticationApi.one('providers').get(); - }; - AuthenticationService.login = function (username, password) { AuthenticationApi.customPOST({'username': username, 'password': password}, 'login') .then( diff --git a/lemur/static/app/index.html b/lemur/static/app/index.html index b9df4202..2ef06c17 100644 --- a/lemur/static/app/index.html +++ b/lemur/static/app/index.html @@ -32,7 +32,7 @@ - +