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 $window.sessionStorage.clear();
25 document.cookie = 'JSESSIONID=; Path=/restconf; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
28 // Return the current user object
29 factory.getUser = function () {
30 var user = $window.sessionStorage.odlUser || null;
34 factory.authorize = function (accessLevel, role) {
35 if (role === undefined) {
36 role = currentUser.role;
38 return accessLevel.bitMask & role.bitMask;
40 factory.isAuthed = function () {
41 var authed = factory.getUser() ? true : false;
44 factory.isLoggedIn = function (user) {
45 if (user === undefined) {
48 return user.role.title === userRoles.user.title || user.role.title === userRoles.admin.title;
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) {
56 .error(function (resp) {
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
69 factory.logout = function (success) {
75 Auth.$inject = ['$http', '$window', 'Base64', 'ENV'];
77 var Base64 = function () {
78 var keyStr = 'ABCDEFGHIJKLMNOP' +
84 encode: function (input) {
86 var chr1, chr2, chr3 = "";
87 var enc1, enc2, enc3, enc4 = "";
91 chr1 = input.charCodeAt(i++);
92 chr2 = input.charCodeAt(i++);
93 chr3 = input.charCodeAt(i++);
96 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
97 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
102 } else if (isNaN(chr3)) {
107 keyStr.charAt(enc1) +
108 keyStr.charAt(enc2) +
109 keyStr.charAt(enc3) +
111 chr1 = chr2 = chr3 = "";
112 enc1 = enc2 = enc3 = enc4 = "";
113 } while (i < input.length);
117 decode: function (input) {
119 var chr1, chr2, chr3 = "";
120 var enc1, enc2, enc3, enc4 = "";
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.");
131 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
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++));
139 chr1 = (enc1 << 2) | (enc2 >> 4);
140 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
141 chr3 = ((enc3 & 3) << 6) | enc4;
143 output = output + String.fromCharCode(chr1);
146 output = output + String.fromCharCode(chr2);
149 output = output + String.fromCharCode(chr3);
152 chr1 = chr2 = chr3 = "";
153 enc1 = enc2 = enc3 = enc4 = "";
155 } while (i < input.length);
162 // Filter to add authorization header if its a nb api call
163 var NbInterceptor = function ($q, $window, Base64) {
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;
176 response: function (response) {
177 return response || $q.when(response);
181 NbInterceptor.$inject = ['$q', '$window', 'Base64'];
186 NbInterceptor: NbInterceptor