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