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.localStorage.odlUser = user;
 
  17       $window.localStorage.odlPass = pw;
 
  18       // for backward compatibility
 
  19       $window.sessionStorage.odlUser = user;
 
  20       $window.sessionStorage.odlPass = pw;
 
  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;
 
  29     factory.unsetBasic = function () {
 
  30       if ($http.defaults.headers.common.Authorization !== null) {
 
  31         delete $http.defaults.headers.common.Authorization;
 
  33       $window.localStorage.clear();
 
  34       $window.sessionStorage.clear();
 
  35       document.cookie = 'JSESSIONID=; Path=/restconf; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
 
  38     // Return the current user object
 
  39     factory.getUser = function () {
 
  40       var user = $window.localStorage.odlUser || null;
 
  44     factory.authorize = function (accessLevel, role) {
 
  45       if (role === undefined) {
 
  46         role = currentUser.role;
 
  48       return accessLevel.bitMask & role.bitMask;
 
  50     factory.isAuthed = function () {
 
  51       var authed = factory.getUser() ? true : false;
 
  54     factory.isLoggedIn = function (user) {
 
  55       if (user === undefined) {
 
  58       return user.role.title === userRoles.user.title || user.role.title === userRoles.admin.title;
 
  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) {
 
  66         .error(function (resp) {
 
  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
 
  79     factory.logout = function (success) {
 
  85   Auth.$inject = ['$http', '$window', 'Base64', 'ENV'];
 
  87   var Base64 = function () {
 
  88     var keyStr = 'ABCDEFGHIJKLMNOP' +
 
  94       encode: function (input) {
 
  96         var chr1, chr2, chr3 = "";
 
  97         var enc1, enc2, enc3, enc4 = "";
 
 101           chr1 = input.charCodeAt(i++);
 
 102           chr2 = input.charCodeAt(i++);
 
 103           chr3 = input.charCodeAt(i++);
 
 106           enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
 
 107           enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
 
 112           } else if (isNaN(chr3)) {
 
 117             keyStr.charAt(enc1) +
 
 118             keyStr.charAt(enc2) +
 
 119             keyStr.charAt(enc3) +
 
 121           chr1 = chr2 = chr3 = "";
 
 122           enc1 = enc2 = enc3 = enc4 = "";
 
 123         } while (i < input.length);
 
 127       decode: function (input) {
 
 129         var chr1, chr2, chr3 = "";
 
 130         var enc1, enc2, enc3, enc4 = "";
 
 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.");
 
 141         input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
 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++));
 
 149           chr1 = (enc1 << 2) | (enc2 >> 4);
 
 150           chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
 
 151           chr3 = ((enc3 & 3) << 6) | enc4;
 
 153           output = output + String.fromCharCode(chr1);
 
 156             output = output + String.fromCharCode(chr2);
 
 159             output = output + String.fromCharCode(chr3);
 
 162           chr1 = chr2 = chr3 = "";
 
 163           enc1 = enc2 = enc3 = enc4 = "";
 
 165         } while (i < input.length);
 
 172   // Filter to add authorization header if its a nb api call
 
 173   var NbInterceptor = function ($q, $window, Base64) {
 
 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;
 
 186       response: function (response) {
 
 187         return response || $q.when(response);
 
 191   NbInterceptor.$inject = ['$q', '$window', 'Base64'];
 
 196     NbInterceptor: NbInterceptor