2 * Copyright (c) 2014 Inocybe Technologies, and others. All rights reserved.
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
9 define([], function () {
12 var Auth = function ($http, $window, Base64, ENV) {
14 // Set Authorization header to username + password
15 factory.setBasic = function (user, pw) {
16 $window.sessionStorage.odlUser = user;
17 $window.sessionStorage.odlPass = pw;
20 factory.unsetBasic = function () {
21 if ($http.defaults.headers.common.Authorization !== null) {
22 delete $http.defaults.headers.common.Authorization;
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;';
29 // Return the current user object
30 factory.getUser = function () {
31 var user = $window.sessionStorage.odlUser || null;
35 factory.authorize = function (accessLevel, role) {
36 if (role === undefined) {
37 role = currentUser.role;
39 return accessLevel.bitMask & role.bitMask;
41 factory.isAuthed = function () {
42 var authed = factory.getUser() ? true : false;
45 factory.isLoggedIn = function (user) {
46 if (user === undefined) {
49 return user.role.title === userRoles.user.title || user.role.title === userRoles.admin.title;
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) {
57 .error(function (resp) {
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
70 factory.logout = function (success) {
76 Auth.$inject = ['$http', '$window', 'Base64', 'ENV'];
78 var Base64 = function () {
79 var keyStr = 'ABCDEFGHIJKLMNOP' +
85 encode: function (input) {
87 var chr1, chr2, chr3 = "";
88 var enc1, enc2, enc3, enc4 = "";
92 chr1 = input.charCodeAt(i++);
93 chr2 = input.charCodeAt(i++);
94 chr3 = input.charCodeAt(i++);
97 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
98 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
103 } else if (isNaN(chr3)) {
108 keyStr.charAt(enc1) +
109 keyStr.charAt(enc2) +
110 keyStr.charAt(enc3) +
112 chr1 = chr2 = chr3 = "";
113 enc1 = enc2 = enc3 = enc4 = "";
114 } while (i < input.length);
118 decode: function (input) {
120 var chr1, chr2, chr3 = "";
121 var enc1, enc2, enc3, enc4 = "";
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.");
132 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
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++));
140 chr1 = (enc1 << 2) | (enc2 >> 4);
141 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
142 chr3 = ((enc3 & 3) << 6) | enc4;
144 output = output + String.fromCharCode(chr1);
147 output = output + String.fromCharCode(chr2);
150 output = output + String.fromCharCode(chr3);
153 chr1 = chr2 = chr3 = "";
154 enc1 = enc2 = enc3 = enc4 = "";
156 } while (i < input.length);
163 // Filter to add authorization header if its a nb api call
164 var NbInterceptor = function ($q, $window, Base64) {
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;
177 response: function (response) {
178 return response || $q.when(response);
182 NbInterceptor.$inject = ['$q', '$window', 'Base64'];
187 NbInterceptor: NbInterceptor