You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by al...@apache.org on 2015/11/10 20:03:10 UTC

ambari git commit: AMBARI-13819. Ambari needs to support an explicit login URL for local users

Repository: ambari
Updated Branches:
  refs/heads/trunk 7b758e87e -> 65629e789


AMBARI-13819. Ambari needs to support an explicit login URL for local users


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/65629e78
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/65629e78
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/65629e78

Branch: refs/heads/trunk
Commit: 65629e789e0f305f88aeab927585db342e46030c
Parents: 7b758e8
Author: Alex Antonenko <hi...@gmail.com>
Authored: Tue Nov 10 20:56:43 2015 +0200
Committer: Alex Antonenko <hi...@gmail.com>
Committed: Tue Nov 10 21:03:06 2015 +0200

----------------------------------------------------------------------
 ambari-web/app/router.js       | 37 ++++++++++++++++++++--
 ambari-web/test/router_test.js | 61 +++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/65629e78/ambari-web/app/router.js
----------------------------------------------------------------------
diff --git a/ambari-web/app/router.js b/ambari-web/app/router.js
index 59e2251..d3343c8 100644
--- a/ambari-web/app/router.js
+++ b/ambari-web/app/router.js
@@ -56,6 +56,7 @@ App.Router = Em.Router.extend({
   backBtnForHigherStep: false,
   transitionInProgress: false,
 
+  localUserAuthUrl: '/login/local',
 
   /**
    * Is true, if cluster.provisioning_state is equal to 'INSTALLED'
@@ -112,7 +113,6 @@ App.Router = Em.Router.extend({
    * @param wizardType one of <code>installer</code>, <code>addHost</code>, <code>addServices</code>
    */
   getWizardCurrentStep: function (wizardType) {
-    var loginName = this.getLoginName();
     var currentStep = App.db.getWizardCurrentStep(wizardType);
     if (!currentStep) {
       currentStep = wizardType === 'installer' ? '0' : '1';
@@ -186,9 +186,24 @@ App.Router = Em.Router.extend({
     }
   },
 
+  /**
+
+   * If authentication failed, need to check for jwt auth url
+   * and redirect user if current location is not <code>localUserAuthUrl</code>
+   *
+   * @param {?object} data
+   */
   onAuthenticationError: function (data) {
     if (data.status === 403) {
-      this.setAuthenticated(false);
+      try {
+        var responseJson = JSON.parse(data.responseText);
+        if (responseJson.jwtProviderUrl && this.get('location.lastSetURL') !== this.get('localUserAuthUrl')) {
+          this.redirectByURL(responseJson.jwtProviderUrl + encodeURIComponent(this.getCurrentLocationUrl()));
+        }
+      } catch (e) {
+      } finally {
+        this.setAuthenticated(false);
+      }
     }
   },
 
@@ -549,6 +564,14 @@ App.Router = Em.Router.extend({
     }
   },
 
+  redirectByURL: function(url) {
+    window.location.href = url;
+  },
+
+  getCurrentLocationUrl: function() {
+    return window.location.href;
+  },
+
   root: Em.Route.extend({
     index: Em.Route.extend({
       route: '/',
@@ -560,7 +583,7 @@ App.Router = Em.Router.extend({
     },
 
     login: Em.Route.extend({
-      route: '/login',
+      route: '/login:suffix',
 
       /**
        *  If the user is already logged in, redirect to where the user was previously
@@ -584,6 +607,14 @@ App.Router = Em.Router.extend({
       connectOutlets: function (router, context) {
         $('title').text(Em.I18n.t('app.name'));
         router.get('applicationController').connectOutlet('login');
+      },
+
+      serialize: function(router, context) {
+        // check for login/local hash
+        var location = router.get('location.location.hash');
+        return {
+          suffix: location === '#' + router.get('localUserAuthUrl') ? '/local' : ''
+        };
       }
     }),
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/65629e78/ambari-web/test/router_test.js
----------------------------------------------------------------------
diff --git a/ambari-web/test/router_test.js b/ambari-web/test/router_test.js
index 4539525..cbdf4ea 100644
--- a/ambari-web/test/router_test.js
+++ b/ambari-web/test/router_test.js
@@ -446,4 +446,65 @@ describe('App.Router', function () {
       expect(router.transitionToViews.calledOnce).to.be.true;
     });
   });
+
+  describe("#getAuthenticated", function() {
+    [
+      {
+        lastSetURL: '/login/local',
+        isResolved: false,
+        responseData: {
+          responseText: "",
+          status: 403
+        },
+        redirectCalled: false,
+        m: 'no jwtProviderUrl in auth response, no redirect'
+      },
+      {
+        lastSetURL: '/main/dashboard',
+        isResolved: false,
+        responseData: {
+          responseText: JSON.stringify({ jwtProviderUrl: 'http://some.com?originalUrl=' }),
+          status: 403
+        },
+        redirectCalled: true,
+        m: 'jwtProviderUrl is present, current location not local login url, redirect according to jwtProviderUrl value'
+      },
+      {
+        lastSetURL: '/login/local',
+        isResolved: false,
+        responseData: {
+          responseText: JSON.stringify({ jwtProviderUrl: 'http://some.com?originalUrl=' }),
+          status: 403
+        },
+        redirectCalled: false,
+        m: 'jwtProviderUrl is present, current location is local login url, no redirect'
+      }
+    ].forEach(function(test) {
+      it(test.m, function() {
+        var router = App.Router.create();
+        var mockCurrentUrl = 'http://localhost:3333/#/some/hash';
+        router.set('location.lastSetURL', test.lastSetURL);
+        sinon.stub(App.ajax, 'send', function() {
+          if (!test.isResolved) {
+            router.onAuthenticationError(test.responseData);
+          }
+          return {
+            complete: function() {}
+          };
+        });
+        sinon.stub(router, 'getCurrentLocationUrl').returns(mockCurrentUrl);
+        sinon.stub(router, 'redirectByURL', Em.K);
+        router.getAuthenticated();
+        expect(router.redirectByURL.calledOnce).to.be.eql(test.redirectCalled);
+        if (test.redirectCalled) {
+          expect(router.redirectByURL.args[0][0]).to.be.eql(JSON.parse(test.responseData.responseText).jwtProviderUrl + encodeURIComponent(mockCurrentUrl));
+        }
+        App.ajax.send.restore();
+        router.getCurrentLocationUrl.restore();
+        router.redirectByURL.restore();
+      });
+    });
+
+  });
+
 });