aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_gui/static/app/services/partner/authentication.service.js
blob: b6d3f36db45067a7cbc0dd23f9da57ea41228601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
 * @author Samy Abdallah
 */

(function() {

    'use strict';

    angular
        .module('moon')
        .factory('authenticationService', authenticationService);

    authenticationService.$inject = ['$resource', 'REST_URI', '$sessionStorage', '$http', '$location'];

    function authenticationService($resource, REST_URI, $sessionStorage, $http, $location) {

        return {
            data: $resource(REST_URI.KEYSTONE + 'auth/tokens', {}, {
                login: { method: 'POST' ,
                    /**
                     * Transform Response is needed to add headers into the response object
                     * @param data
                     * @param headersGetter
                     * @returns {{}}
                     */
                  transformResponse : function (data, headersGetter) {
                        var response = {};
                        response.data =  angular.fromJson(data) ;
                        response.headers = headersGetter();
                        return response;
                    }
                },
                logout: { method: 'DELETE' }
            }),

            /**
             *
             * @param credentials object : {username : '', password : ''}
             * @param callbackSuccess
             * @param callbackError
             * @constructor
             */
            Login : function (credentials, callbackSuccess, callbackError){
                var requestData = {
                    auth:{
                        identity:{
                            methods:[
                                'password'
                            ],
                            password:{
                                user:{
                                    name: credentials.username,
                                    domain:{
                                        name:'Default'
                                    },
                                    password: credentials.password
                                }
                            }
                        },
                        scope: {
                            project: {
                                name:'admin',
                                domain:{
                                    name:'Default'
                                }
                            }
                        }
                    }
                };
                this.data.login({}, requestData, function (response){
                    $sessionStorage.currentUser =  response.data;
                    $sessionStorage.currentUser.connectionToken = response.headers['x-subject-token'];
                    SetTokenHeader(response.headers['x-subject-token']);
                    callbackSuccess();
                }, callbackError);
            },
            IsConnected : IsConnected,
            SetTokenHeader : SetTokenHeader,
            GetTokenHeader : GetTokenHeader,
            GetUser : GetUser,
            Logout : Logout
        };

        function IsConnected(){
            return _.has($sessionStorage, 'currentUser');
        }

        function Logout(){
            delete $sessionStorage.currentUser;
            $http.defaults.headers.common['X-Auth-Token'] = '';
            $location.path('/');
        }

        function GetUser(){
            return $sessionStorage.currentUser;
        }

        function GetTokenHeader(){
            return $sessionStorage.currentUser.connectionToken;
        }

        function SetTokenHeader(token){
            $http.defaults.headers.common['X-Auth-Token'] = token;
        }
    }
})();