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