From: karim hamidou Date: Wed, 14 May 2014 09:51:00 +0000 (+0200) Subject: multiple user auth X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=5da00b2d2cddcb36ad4a08b95801ae4d8496ce75;p=kite.git multiple user auth --- diff --git a/src/back/kite/server.py b/src/back/kite/server.py index 9afeed3..feccdf2 100755 --- a/src/back/kite/server.py +++ b/src/back/kite/server.py @@ -82,7 +82,6 @@ def index(user): @get('/kite//mail/') @with_valid_token def index(user, id): - # FIXME: input sanitization check perms try: threads_index = DatetimeCabinet("/home/kite/threads.db") thread = None @@ -98,7 +97,7 @@ def index(user, id): if thread == None: abort(404, "Thread not found.") - thr["unread"] = False + thread["unread"] = False threads_index.sync() response.content_type = "application/json" @@ -113,6 +112,6 @@ def index(user, id): for mail_id in thread["messages"]: ret_json["messages"].append(get_email(mdir, mail_id)) - return serialize_json(ret_json) + return serialize_json(ret_json, protection=False) bottle.run(host='localhost', port='8080', reloader=True) diff --git a/src/front/app/js/app.js b/src/front/app/js/app.js index d4e0391..2320d08 100644 --- a/src/front/app/js/app.js +++ b/src/front/app/js/app.js @@ -3,13 +3,36 @@ // Declare app level module which depends on filters, and services angular.module('KiteMail', ['ngRoute', 'ngCookies', 'KiteMail.filters', 'KiteMail.services', 'KiteMail.directives', 'KiteMail.controllers']). - config(['$routeProvider', function($routeProvider) { + config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) { $routeProvider.when('/mail', {templateUrl: 'partials/mailbox.html', controller: 'InboxesListController'}); $routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'LoginController'}); $routeProvider.when('/thread/:id', {templateUrl: 'partials/thread.html', controller: 'ThreadController'}); $routeProvider.otherwise({redirectTo: '/login'}); + + var interceptor = ['$rootScope', '$q', '$location', function (scope, $q, $location) { + function success(response) { + var status = response.status; + // user is unauthorized. + if (status == 401) { + //window.location = "/account/login?redirectUrl=" + Base64.encode(document.URL); + $location.path("/login"); + return; + } + + return response; + } + function error(response) { + // otherwise + return $q.reject(response); + } + return function (promise) { + return promise.then(success, error); + } + }]; + $httpProvider.responseInterceptors.push(interceptor); + }]).run(['$rootScope', '$location', 'Auth', function($rootScope, $location, Auth) { - $rootScope.$on( "$routeChangeStart", function(event, next, current) { +/* $rootScope.$on( "$routeChangeStart", function(event, next, current) { console.log(Auth.loggedIn()); if (!Auth.loggedIn()) { // no logged user, redirect to /login @@ -22,4 +45,5 @@ angular.module('KiteMail', ['ngRoute', 'ngCookies', 'KiteMail.filters', 'KiteMai } } }); +*/ }]); diff --git a/src/front/app/js/controllers.js b/src/front/app/js/controllers.js index 5526b05..e19c5d1 100644 --- a/src/front/app/js/controllers.js +++ b/src/front/app/js/controllers.js @@ -2,7 +2,7 @@ /* Controllers */ -angular.module('KiteMail.controllers', []). +angular.module('KiteMail.controllers', ['KiteMail.services']). controller('InboxesListController', function($scope) { $scope.inboxes = [ {"name": "Inbox", }, @@ -10,7 +10,8 @@ angular.module('KiteMail.controllers', []). {"name": "Spam", },]; }).controller('MailsListController', ['$scope', 'Emails', 'Auth', function($scope, Emails, Auth) { - $scope.threads = Emails.threads({username: "testuser"}); + console.log(Auth.username()); + $scope.threads = Emails.threads({username: Auth.username()}); }]).controller('LoginController', ['$scope', '$location', 'Auth', function($scope, $location, Auth) { $scope.doLogin = function(isValid) { if(isValid) { @@ -23,11 +24,10 @@ angular.module('KiteMail.controllers', []). }; Auth.doLogin($scope.username, $scope.password, success, failure); - console.log("PASS" + $scope.password); } } -}]).controller('ThreadController', ['$scope', '$routeParams', '$sce', 'Emails', function($scope, $routeParams, $sce, Emails) { - $scope.thread = Emails.thread({username: "testuser", "id": $routeParams.id}); +}]).controller('ThreadController', ['$scope', '$routeParams', '$sce', 'Emails', 'Auth', function($scope, $routeParams, $sce, Emails, Auth) { + $scope.thread = Emails.thread({username: Auth.username(), "id": $routeParams.id}); $scope.trustHTML = function(html_code) { return $sce.trustAsHtml(html_code); }; diff --git a/src/front/app/js/services.js b/src/front/app/js/services.js index 5934cc0..48c2d14 100644 --- a/src/front/app/js/services.js +++ b/src/front/app/js/services.js @@ -43,17 +43,17 @@ factory('Emails', ['$resource', }).factory('Auth', ['$cookies', '$http', function($cookies, $http) { var _isAuth = false; - var _username = null + var _username = null; var _cookies = $cookies; return { loggedIn: function() { return _isAuth; }, - username: _username, + username: function() { return _username; }, + doLogout: function() { _isAuth = false; }, doLogin: function(username, password, success, failure) { - _username = username; $http({url: '/kite/auth', method:'POST', data: {"username": username, "password": password}}). success(function(data, status, headers, config) { + _username = username; _isAuth = true; - $http.defaults.headers.common["X-XSRF-TOKEN"] = "BLAH"; success(data); }); },