diff --git a/static/authentication.js b/static/authentication.js new file mode 100644 index 0000000..395e966 --- /dev/null +++ b/static/authentication.js @@ -0,0 +1,131 @@ +'use strict'; + +angular.module('Authentication') + +.factory('AuthenticationService', + ['Base64', '$http', '$cookieStore', '$rootScope', '$timeout', + function (Base64, $http, $cookieStore, $rootScope, $timeout) { + var service = {}; + + service.Login = function (username, password, callback) { + var authdata = Base64.encode(username + ':' + password); + + $http.get('/api/users/'+username, {headers: {Authorization: 'Basic ' + authdata}}) + .then(function (response) { + service.SetCredentials(username, password); + callback({success: true}); + }).catch(function (response) { + service.ClearCredentials(); + callback({success: false, message: 'Username or password is incorrect'}); + }); + + }; + + service.SetCredentials = function (username, password) { + var authdata = Base64.encode(username + ':' + password); + + $rootScope.globals = { + currentUser: { + username: username, + authdata: authdata + } + }; + + $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; // jshint ignore:line + $cookieStore.put('globals', $rootScope.globals); + }; + + service.ClearCredentials = function () { + $rootScope.globals = {}; + $cookieStore.remove('globals'); + $http.defaults.headers.common.Authorization = 'Basic '; + }; + + return service; + }]) + +.factory('Base64', function () { + /* jshint ignore:start */ + + var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; + + return { + encode: function (input) { + var output = ""; + var chr1, chr2, chr3 = ""; + var enc1, enc2, enc3, enc4 = ""; + var i = 0; + + do { + chr1 = input.charCodeAt(i++); + chr2 = input.charCodeAt(i++); + chr3 = input.charCodeAt(i++); + + enc1 = chr1 >> 2; + enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); + enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); + enc4 = chr3 & 63; + + if (isNaN(chr2)) { + enc3 = enc4 = 64; + } else if (isNaN(chr3)) { + enc4 = 64; + } + + output = output + + keyStr.charAt(enc1) + + keyStr.charAt(enc2) + + keyStr.charAt(enc3) + + keyStr.charAt(enc4); + chr1 = chr2 = chr3 = ""; + enc1 = enc2 = enc3 = enc4 = ""; + } while (i < input.length); + + return output; + }, + + decode: function (input) { + var output = ""; + var chr1, chr2, chr3 = ""; + var enc1, enc2, enc3, enc4 = ""; + var i = 0; + + // remove all characters that are not A-Z, a-z, 0-9, +, /, or = + var base64test = /[^A-Za-z0-9\+\/\=]/g; + if (base64test.exec(input)) { + window.alert("There were invalid base64 characters in the input text.\n" + + "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" + + "Expect errors in decoding."); + } + input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); + + do { + enc1 = keyStr.indexOf(input.charAt(i++)); + enc2 = keyStr.indexOf(input.charAt(i++)); + enc3 = keyStr.indexOf(input.charAt(i++)); + enc4 = keyStr.indexOf(input.charAt(i++)); + + chr1 = (enc1 << 2) | (enc2 >> 4); + chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); + chr3 = ((enc3 & 3) << 6) | enc4; + + output = output + String.fromCharCode(chr1); + + if (enc3 != 64) { + output = output + String.fromCharCode(chr2); + } + if (enc4 != 64) { + output = output + String.fromCharCode(chr3); + } + + chr1 = chr2 = chr3 = ""; + enc1 = enc2 = enc3 = enc4 = ""; + + } while (i < input.length); + + return output; + } + }; + + /* jshint ignore:end */ +}); diff --git a/static/controllers.js b/static/controllers.js new file mode 100644 index 0000000..049a9c0 --- /dev/null +++ b/static/controllers.js @@ -0,0 +1,23 @@ +'use strict'; + +angular.module('Authentication') + +.controller('LoginController', + ['$scope', '$rootScope', '$location', 'AuthenticationService', + function ($scope, $rootScope, $location, AuthenticationService) { + // reset login status + AuthenticationService.ClearCredentials(); + + $scope.login = function () { + $scope.dataLoading = true; + AuthenticationService.Login($scope.username, $scope.password, function (response) { + if (response.success) { + $location.path('/users/'+$scope.username); + } else { + console.log(response); + $scope.error = response.message; + $scope.dataLoading = false; + } + }); + }; + }]); diff --git a/static/http-auth-interceptor.min.js b/static/http-auth-interceptor.min.js new file mode 100644 index 0000000..167529a --- /dev/null +++ b/static/http-auth-interceptor.min.js @@ -0,0 +1 @@ +!function(){"use strict";angular.module("http-auth-interceptor",["http-auth-interceptor-buffer"]).factory("authService",["$rootScope","httpBuffer",function($rootScope,httpBuffer){return{loginConfirmed:function(data,configUpdater){var updater=configUpdater||function(config){return config};$rootScope.$broadcast("event:auth-loginConfirmed",data),httpBuffer.retryAll(updater)},loginCancelled:function(data,reason){httpBuffer.rejectAll(reason),$rootScope.$broadcast("event:auth-loginCancelled",data)}}}]).config(["$httpProvider",function($httpProvider){$httpProvider.interceptors.push(["$rootScope","$q","httpBuffer",function($rootScope,$q,httpBuffer){return{responseError:function(rejection){var config=rejection.config||{};if(!config.ignoreAuthModule)switch(rejection.status){case 401:var deferred=$q.defer(),bufferLength=httpBuffer.append(config,deferred);return 1===bufferLength&&$rootScope.$broadcast("event:auth-loginRequired",rejection),deferred.promise;case 403:$rootScope.$broadcast("event:auth-forbidden",rejection)}return $q.reject(rejection)}}}])}]),angular.module("http-auth-interceptor-buffer",[]).factory("httpBuffer",["$injector",function($injector){function retryHttpRequest(config,deferred){function successCallback(response){deferred.resolve(response)}function errorCallback(response){deferred.reject(response)}$http=$http||$injector.get("$http"),$http(config).then(successCallback,errorCallback)}var $http,buffer=[];return{append:function(config,deferred){return buffer.push({config:config,deferred:deferred})},rejectAll:function(reason){if(reason)for(var i=0;i + + + + LILiK Users + + + + + + + + + + + + + + + + +
+
+ Home + New + + {{item}} + +
+ +
+
+ + diff --git a/static/views/edit.html b/static/views/edit.html new file mode 100644 index 0000000..4cd4345 --- /dev/null +++ b/static/views/edit.html @@ -0,0 +1,20 @@ +
+ +
+

Edit {{user.uid}}

+
+
+ + + + + +

Services

+
+ {{service}} +
+ Save + +
+
+ diff --git a/static/views/login.html b/static/views/login.html new file mode 100644 index 0000000..2c0590b --- /dev/null +++ b/static/views/login.html @@ -0,0 +1,25 @@ +
+ + +
+

Login Form

+
+
+ +
+ +
{{error}}
+
+ + + + + + + + + Login +
+
+
+
diff --git a/static/views/show.html b/static/views/show.html new file mode 100644 index 0000000..142c49b --- /dev/null +++ b/static/views/show.html @@ -0,0 +1,10 @@ +
+ +
+

Hi {{logged_user.uid}} aka {{logged_user.cn}}

+
+
+ +
+
+