clear all sessionStorage on Logout
[dlux.git] / modules / common-authentication-resources / src / main / resources / authentification / auth.services.js
1 /*
2  * Copyright (c) 2014 Inocybe Technologies, and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 define([], function () {
10   'use strict';
11
12   var Auth = function ($http, $window, Base64, ENV) {
13     var factory = {};
14     // Set Authorization header to username + password
15     factory.setBasic = function (user, pw) {
16       $window.sessionStorage.odlUser = user;
17       $window.sessionStorage.odlPass = pw;
18     };
19
20     factory.unsetBasic = function () {
21       if ($http.defaults.headers.common.Authorization !== null) {
22         delete $http.defaults.headers.common.Authorization;
23       }
24       $window.sessionStorage.clear();
25       document.cookie = 'JSESSIONID=; Path=/restconf; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
26     };
27
28     // Return the current user object
29     factory.getUser = function () {
30       var user = $window.sessionStorage.odlUser || null;
31       return user;
32     };
33
34     factory.authorize = function (accessLevel, role) {
35       if (role === undefined) {
36         role = currentUser.role;
37       }
38       return accessLevel.bitMask & role.bitMask;
39     };
40     factory.isAuthed = function () {
41       var authed = factory.getUser() ? true : false;
42       return authed;
43     };
44     factory.isLoggedIn = function (user) {
45       if (user === undefined) {
46         user = currentUser;
47       }
48       return user.role.title === userRoles.user.title || user.role.title === userRoles.admin.title;
49     };
50     factory.login = function (user, pw, cb, eb) {
51       factory.setBasic(user, pw);
52       $http.get(ENV.getBaseURL('MD_SAL') + '/restconf/modules')
53         .success(function (data) {
54           cb(data);
55         })
56         .error(function (resp) {
57           if (resp.errors) {
58             var errorDetails = resp.errors.error[0];
59             if (errorDetails && errorDetails['error-tag'] === 'data-missing') {
60               // Authentication succeed, but API does not have data, allow to enter
61               cb(resp);
62               return;
63             }
64           }
65           factory.unsetBasic();
66           eb(resp);
67         });
68     };
69     factory.logout = function (success) {
70       factory.unsetBasic();
71       success();
72     };
73     return factory;
74   };
75   Auth.$inject = ['$http', '$window', 'Base64', 'ENV'];
76
77   var Base64 = function () {
78     var keyStr = 'ABCDEFGHIJKLMNOP' +
79       'QRSTUVWXYZabcdef' +
80       'ghijklmnopqrstuv' +
81       'wxyz0123456789+/' +
82       '=';
83     return {
84       encode: function (input) {
85         var output = "";
86         var chr1, chr2, chr3 = "";
87         var enc1, enc2, enc3, enc4 = "";
88         var i = 0;
89
90         do {
91           chr1 = input.charCodeAt(i++);
92           chr2 = input.charCodeAt(i++);
93           chr3 = input.charCodeAt(i++);
94
95           enc1 = chr1 >> 2;
96           enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
97           enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
98           enc4 = chr3 & 63;
99
100           if (isNaN(chr2)) {
101             enc3 = enc4 = 64;
102           } else if (isNaN(chr3)) {
103             enc4 = 64;
104           }
105
106           output = output +
107             keyStr.charAt(enc1) +
108             keyStr.charAt(enc2) +
109             keyStr.charAt(enc3) +
110             keyStr.charAt(enc4);
111           chr1 = chr2 = chr3 = "";
112           enc1 = enc2 = enc3 = enc4 = "";
113         } while (i < input.length);
114
115         return output;
116       },
117       decode: function (input) {
118         var output = "";
119         var chr1, chr2, chr3 = "";
120         var enc1, enc2, enc3, enc4 = "";
121         var i = 0;
122
123         // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
124         var base64test = /[^A-Za-z0-9\+\/\=]/g;
125         if (base64test.exec(input)) {
126           alert("There were invalid base64 characters in the input text.\n" +
127             "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
128             "Expect errors in decoding.");
129         }
130
131         input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
132
133         do {
134           enc1 = keyStr.indexOf(input.charAt(i++));
135           enc2 = keyStr.indexOf(input.charAt(i++));
136           enc3 = keyStr.indexOf(input.charAt(i++));
137           enc4 = keyStr.indexOf(input.charAt(i++));
138
139           chr1 = (enc1 << 2) | (enc2 >> 4);
140           chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
141           chr3 = ((enc3 & 3) << 6) | enc4;
142
143           output = output + String.fromCharCode(chr1);
144
145           if (enc3 != 64) {
146             output = output + String.fromCharCode(chr2);
147           }
148           if (enc4 != 64) {
149             output = output + String.fromCharCode(chr3);
150           }
151
152           chr1 = chr2 = chr3 = "";
153           enc1 = enc2 = enc3 = enc4 = "";
154
155         } while (i < input.length);
156
157         return output;
158       }
159     };
160   };
161
162   // Filter to add authorization header if its a nb api call
163   var NbInterceptor = function ($q, $window, Base64) {
164     return {
165       request: function (config) {
166         // Use AAA basic authentication
167         if (config.url.indexOf('restconf') !== -1 || config.url.indexOf('apidoc') !== -1) {
168           config.headers = config.headers || {};
169           if ($window.sessionStorage.odlUser && $window.sessionStorage.odlPass) {
170             var encoded = Base64.encode($window.sessionStorage.odlUser + ':' + $window.sessionStorage.odlPass);
171             config.headers.Authorization = 'Basic ' + encoded;
172           }
173         }
174         return config;
175       },
176       response: function (response) {
177         return response || $q.when(response);
178       }
179     };
180   };
181   NbInterceptor.$inject = ['$q', '$window', 'Base64'];
182
183   return {
184     Auth: Auth,
185     Base64: Base64,
186     NbInterceptor: NbInterceptor
187   };
188
189 });