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