Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 1 | 'use strict'; |
| 2 | angular.module('Authentication', []); |
| 3 | var app = angular.module('bmcApp', [ |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 4 | 'Authentication', 'ngCookies', 'ui.bootstrap', 'ui.router', |
| 5 | 'ngSanitize', 'ngWebSocket', 'ngResource' |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 6 | ]); |
| 7 | |
| 8 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 9 | app.controller('MainCtrl', function($scope) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 10 | |
| 11 | }); |
| 12 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 13 | app.service('loginInterceptor', function($q, $state) { |
| 14 | var service = this; |
| 15 | |
| 16 | service.responseError = function(response) { |
| 17 | if (response.status == 401){ |
| 18 | console.log("Login required... "); |
| 19 | |
| 20 | var invalidate_reason = "Your user was logged out."; |
| 21 | var continue_promise_chain = false; |
| 22 | |
| 23 | // if we're attempting to log in, we need to |
| 24 | // continue the promise chain to make sure the user is informed |
| 25 | if ($state.current.name === "login") { |
| 26 | invalidate_reason = "Your username and password was incorrect"; |
| 27 | continue_promise_chain = true |
| 28 | } else { |
| 29 | $state.after_login_state = $state.current.name; |
| 30 | $state.go('login'); |
| 31 | } |
| 32 | AuthenticationService.ClearCredentials(invalidate_reason); |
| 33 | } |
| 34 | //return $q.reject(response); |
| 35 | }; |
| 36 | }) |
| 37 | |
| 38 | app.config(['$httpProvider', function ($httpProvider) { |
| 39 | $httpProvider.interceptors.push('loginInterceptor'); |
| 40 | }]); |
| 41 | |
| 42 | app.run(['$rootScope', '$cookieStore', '$state', '$resource', 'AuthenticationService', |
| 43 | function($rootScope, $cookieStore, $state, $resource, AuthenticationService) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 44 | if ($rootScope.globals == undefined){ |
| 45 | $rootScope.globals = {}; |
| 46 | } |
| 47 | |
| 48 | // keep user logged in after page refresh |
| 49 | AuthenticationService.RestoreCredientials(); |
| 50 | |
| 51 | $rootScope.$on( |
| 52 | '$stateChangeStart', |
| 53 | function(event, toState, toParams, fromState, fromParams, options) { |
| 54 | // redirect to login page if not logged in |
| 55 | if (toState.name !== 'login' && !$rootScope.globals.currentUser) { |
| 56 | // If logged out and transitioning to a logged in page: |
| 57 | event.preventDefault(); |
| 58 | $state.go('login'); |
| 59 | } |
| 60 | }); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 61 | } |
| 62 | ]); |
| 63 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 64 | app.config(function($stateProvider, $urlRouterProvider) { |
| 65 | |
| 66 | $urlRouterProvider.otherwise('/systeminfo'); |
| 67 | |
| 68 | $stateProvider |
| 69 | // nested list with just some random string data |
| 70 | .state('login', { |
| 71 | url: '/login', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 72 | templateUrl: 'static/login.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 73 | controller: 'LoginController', |
| 74 | }) |
| 75 | // systeminfo view ======================================== |
| 76 | .state( |
| 77 | 'systeminfo', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 78 | {url: '/systeminfo', templateUrl: 'static/partial-systeminfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 79 | |
| 80 | |
| 81 | // HOME STATES AND NESTED VIEWS ======================================== |
| 82 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 83 | 'eventlog', {url: '/eventlog', templateUrl: 'static/partial-eventlog.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 84 | |
| 85 | |
| 86 | .state( |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 87 | 'kvm', {url: '/kvm', templateUrl: 'static/partial-kvm.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 88 | |
| 89 | // ABOUT PAGE AND MULTIPLE NAMED VIEWS ================================= |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 90 | .state('about', {url: '/about', templateUrl: 'static/partial-fruinfo.html'}) |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 91 | |
| 92 | // nested list with custom controller |
| 93 | .state('about.list', { |
| 94 | url: '/list', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 95 | templateUrl: 'static/partial-home-list.html', |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 96 | controller: function($scope) { |
| 97 | $scope.dogs = ['Bernese', 'Husky', 'Goldendoodle']; |
| 98 | } |
| 99 | }) |
| 100 | |
| 101 | |
| 102 | }); |
| 103 | |
| 104 | app.controller('PaginationDemoCtrl', function($scope, $log) { |
| 105 | $scope.totalItems = 64; |
| 106 | $scope.currentPage = 4; |
| 107 | |
| 108 | $scope.setPage = function(pageNo) { $scope.currentPage = pageNo; }; |
| 109 | |
| 110 | $scope.pageChanged = function() { |
| 111 | $log.log('Page changed to: ' + $scope.currentPage); |
| 112 | }; |
| 113 | |
| 114 | $scope.maxSize = 5; |
| 115 | $scope.bigTotalItems = 175; |
| 116 | $scope.bigCurrentPage = 1; |
| 117 | }); |
| 118 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 119 | angular.module('Authentication') |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 120 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 121 | .factory( |
| 122 | 'AuthenticationService', |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 123 | ['$cookieStore', '$rootScope', '$timeout', '$resource', '$log', |
| 124 | function($cookieStore, $rootScope, $timeout, $resource, $log) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 125 | var service = {}; |
| 126 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 127 | service.Login = function(username, password, success_callback, fail_callback) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 128 | |
| 129 | var user = {"username": username, "password": password}; |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame^] | 130 | var UserLogin = $resource("/login"); |
| 131 | var this_login = new UserLogin(); |
| 132 | this_login.data = {"username": username, "password": password}; |
| 133 | UserLogin.save(user, success_callback, fail_callback); |
| 134 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | service.SetCredentials = function(username, token) { |
| 138 | $rootScope.globals["currentUser"] = {username: username, authdata: token}; |
| 139 | Restangular.setDefaultHeaders( |
| 140 | {'Authorization': 'Token ' + token}); |
| 141 | $cookieStore.put('globals', $rootScope.globals); |
| 142 | }; |
| 143 | |
| 144 | service.ClearCredentials = function(reason) { |
| 145 | $rootScope.globals["currentUser"] = null; |
| 146 | if (reason !== null) { |
| 147 | service.logoutreason = reason; |
| 148 | } |
| 149 | $cookieStore.remove('globals'); |
| 150 | Restangular.setDefaultHeaders({}); |
| 151 | }; |
| 152 | |
| 153 | service.RestoreCredientials = function() { |
| 154 | var globals = $cookieStore.get('globals') || {}; |
| 155 | if (globals.currentUser) { |
| 156 | service.SetCredentials( |
| 157 | globals.currentUser.username, globals.currentUser.authdata); |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | service.logoutreason = ""; |
| 162 | return service; |
| 163 | } |
| 164 | ]) |
| 165 | |
| 166 | .factory('Websocket_URI', |
| 167 | function($rootScope, $http) { |
| 168 | var loc = window.location, websocket_uri; |
| 169 | if (loc.protocol === "https:") { |
| 170 | websocket_uri = "wss:"; |
| 171 | } else { |
| 172 | websocket_uri = "ws:"; |
| 173 | } |
| 174 | websocket_uri += "//" + loc.hostname + ":9000"; |
| 175 | // Append the authentication token |
| 176 | websocket_uri += "?token=" |
| 177 | websocket_uri += $rootScope.globals["currentUser"]["authdata"] |
| 178 | var methods = { |
| 179 | uri: websocket_uri |
| 180 | } |
| 181 | return methods; |
| 182 | }) |
| 183 | .factory('Base64', function() { |
| 184 | /* jshint ignore:start */ |
| 185 | |
| 186 | var keyStr = |
| 187 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; |
| 188 | |
| 189 | return { |
| 190 | encode: function(input) { |
| 191 | var output = ""; |
| 192 | var chr1, chr2, chr3 = ""; |
| 193 | var enc1, enc2, enc3, enc4 = ""; |
| 194 | var i = 0; |
| 195 | |
| 196 | do { |
| 197 | chr1 = input.charCodeAt(i++); |
| 198 | chr2 = input.charCodeAt(i++); |
| 199 | chr3 = input.charCodeAt(i++); |
| 200 | |
| 201 | enc1 = chr1 >> 2; |
| 202 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); |
| 203 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); |
| 204 | enc4 = chr3 & 63; |
| 205 | |
| 206 | if (isNaN(chr2)) { |
| 207 | enc3 = enc4 = 64; |
| 208 | } else if (isNaN(chr3)) { |
| 209 | enc4 = 64; |
| 210 | } |
| 211 | |
| 212 | output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + |
| 213 | keyStr.charAt(enc3) + keyStr.charAt(enc4); |
| 214 | chr1 = chr2 = chr3 = ""; |
| 215 | enc1 = enc2 = enc3 = enc4 = ""; |
| 216 | } while (i < input.length); |
| 217 | |
| 218 | return output; |
| 219 | }, |
| 220 | |
| 221 | decode: function(input) { |
| 222 | var output = ""; |
| 223 | var chr1, chr2, chr3 = ""; |
| 224 | var enc1, enc2, enc3, enc4 = ""; |
| 225 | var i = 0; |
| 226 | |
| 227 | // remove all characters that are not A-Z, a-z, 0-9, +, /, or = |
| 228 | var base64test = /[^A-Za-z0-9\+\/\=]/g; |
| 229 | if (base64test.exec(input)) { |
| 230 | window.alert( |
| 231 | "There were invalid base64 characters in the input text.\n" + |
| 232 | "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" + |
| 233 | "Expect errors in decoding."); |
| 234 | } |
| 235 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); |
| 236 | |
| 237 | do { |
| 238 | enc1 = keyStr.indexOf(input.charAt(i++)); |
| 239 | enc2 = keyStr.indexOf(input.charAt(i++)); |
| 240 | enc3 = keyStr.indexOf(input.charAt(i++)); |
| 241 | enc4 = keyStr.indexOf(input.charAt(i++)); |
| 242 | |
| 243 | chr1 = (enc1 << 2) | (enc2 >> 4); |
| 244 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); |
| 245 | chr3 = ((enc3 & 3) << 6) | enc4; |
| 246 | |
| 247 | output = output + String.fromCharCode(chr1); |
| 248 | |
| 249 | if (enc3 != 64) { |
| 250 | output = output + String.fromCharCode(chr2); |
| 251 | } |
| 252 | if (enc4 != 64) { |
| 253 | output = output + String.fromCharCode(chr3); |
| 254 | } |
| 255 | |
| 256 | chr1 = chr2 = chr3 = ""; |
| 257 | enc1 = enc2 = enc3 = enc4 = ""; |
| 258 | |
| 259 | } while (i < input.length); |
| 260 | |
| 261 | return output; |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | /* jshint ignore:end */ |
| 266 | }); |