16101: Handle logout without sso-provider.
authorTom Clegg <tom@tomclegg.ca>
Mon, 17 Feb 2020 18:55:27 +0000 (13:55 -0500)
committerTom Clegg <tom@tomclegg.ca>
Mon, 17 Feb 2020 18:55:27 +0000 (13:55 -0500)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@tomclegg.ca>

13 files changed:
lib/controller/federation/conn.go
lib/controller/federation/login_test.go
lib/controller/handler.go
lib/controller/handler_test.go
lib/controller/localdb/conn.go
lib/controller/localdb/login.go
lib/controller/localdb/login_test.go
lib/controller/router/router.go
lib/controller/rpc/conn.go
lib/controller/rpc/conn_test.go
sdk/go/arvados/api.go
sdk/go/arvados/login.go
sdk/go/arvadostest/api.go

index 42083cb83df10918cdbc9f14869d0c2e49e51442..56f117ee781682aeb09119b448494f263d34046f 100644 (file)
@@ -222,6 +222,32 @@ func (conn *Conn) Login(ctx context.Context, options arvados.LoginOptions) (arva
        }
 }
 
+func (conn *Conn) Logout(ctx context.Context, options arvados.LogoutOptions) (arvados.LogoutResponse, error) {
+       // If the logout request comes with an API token from a known
+       // remote cluster, redirect to that cluster's logout handler
+       // so it has an opportunity to clear sessions, expire tokens,
+       // etc. Otherwise use the local endpoint.
+       reqauth, ok := auth.FromContext(ctx)
+       if !ok || len(reqauth.Tokens) == 0 || len(reqauth.Tokens[0]) < 8 || !strings.HasPrefix(reqauth.Tokens[0], "v2/") {
+               return conn.local.Logout(ctx, options)
+       }
+       id := reqauth.Tokens[0][3:8]
+       if id == conn.cluster.ClusterID {
+               return conn.local.Logout(ctx, options)
+       }
+       remote, ok := conn.remotes[id]
+       if !ok {
+               return conn.local.Logout(ctx, options)
+       }
+       baseURL := remote.BaseURL()
+       target, err := baseURL.Parse(arvados.EndpointLogout.Path)
+       if err != nil {
+               return arvados.LogoutResponse{}, fmt.Errorf("internal error getting redirect target: %s", err)
+       }
+       target.RawQuery = url.Values{"return_to": {options.ReturnTo}}.Encode()
+       return arvados.LogoutResponse{RedirectLocation: target.String()}, nil
+}
+
 func (conn *Conn) CollectionGet(ctx context.Context, options arvados.GetOptions) (arvados.Collection, error) {
        if len(options.UUID) == 27 {
                // UUID is really a UUID
index ab39619c79703ff683a4adc4e16396cde2d3ebc5..3cc5cb842c4907a1b5ac530113489144d636063c 100644 (file)
@@ -10,6 +10,7 @@ import (
 
        "git.arvados.org/arvados.git/sdk/go/arvados"
        "git.arvados.org/arvados.git/sdk/go/arvadostest"
+       "git.arvados.org/arvados.git/sdk/go/auth"
        check "gopkg.in/check.v1"
 )
 
@@ -38,3 +39,32 @@ func (s *LoginSuite) TestDeferToLoginCluster(c *check.C) {
                c.Check(remotePresent, check.Equals, remote != "")
        }
 }
+
+func (s *LoginSuite) TestLogout(c *check.C) {
+       s.cluster.Login.GoogleClientID = "zzzzzzzzzzzzzz"
+       s.addHTTPRemote(c, "zhome", &arvadostest.APIStub{})
+       s.cluster.Login.LoginCluster = "zhome"
+
+       returnTo := "https://app.example.com/foo?bar"
+       for _, trial := range []struct {
+               token  string
+               target string
+       }{
+               {token: "", target: returnTo},
+               {token: "zzzzzzzzzzzzzzzzzzzzz", target: returnTo},
+               {token: "v2/zzzzz-aaaaa-aaaaaaaaaaaaaaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", target: returnTo},
+               {token: "v2/zhome-aaaaa-aaaaaaaaaaaaaaa/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", target: "http://" + s.cluster.RemoteClusters["zhome"].Host + "/logout?" + url.Values{"return_to": {returnTo}}.Encode()},
+       } {
+               c.Logf("trial %#v", trial)
+               ctx := context.Background()
+               if trial.token != "" {
+                       ctx = auth.NewContext(ctx, &auth.Credentials{Tokens: []string{trial.token}})
+               }
+               resp, err := s.fed.Logout(ctx, arvados.LogoutOptions{ReturnTo: returnTo})
+               c.Assert(err, check.IsNil)
+               c.Logf("  RedirectLocation %q", resp.RedirectLocation)
+               target, err := url.Parse(resp.RedirectLocation)
+               c.Check(err, check.IsNil)
+               c.Check(target.String(), check.Equals, trial.target)
+       }
+}
index 935a1b6cb683a57a2da99b4d4f7285b1bc1ae7f5..e3869261a160b4e42d147574410f15b9641b4e9d 100644 (file)
@@ -86,6 +86,7 @@ func (h *Handler) setup() {
                mux.Handle("/arvados/v1/users", rtr)
                mux.Handle("/arvados/v1/users/", rtr)
                mux.Handle("/login", rtr)
+               mux.Handle("/logout", rtr)
        }
 
        hs := http.NotFoundHandler()
index d54f50cf1ff2a8b9f52c3149548e74257443dbb8..6b1fa1a0b6ef05b539b8113c67d6f18b96c1905b 100644 (file)
@@ -180,6 +180,32 @@ func (s *HandlerSuite) TestProxyRedirect(c *check.C) {
        c.Check(resp.Header().Get("Location"), check.Matches, `(https://0.0.0.0:1)?/auth/joshid\?return_to=%2Cfoo&?`)
 }
 
+func (s *HandlerSuite) TestLogoutSSO(c *check.C) {
+       s.cluster.Login.ProviderAppID = "test"
+       req := httptest.NewRequest("GET", "https://0.0.0.0:1/logout?return_to=https://example.com/foo", nil)
+       resp := httptest.NewRecorder()
+       s.handler.ServeHTTP(resp, req)
+       if !c.Check(resp.Code, check.Equals, http.StatusFound) {
+               c.Log(resp.Body.String())
+       }
+       c.Check(resp.Header().Get("Location"), check.Equals, "http://localhost:3002/users/sign_out?"+url.Values{"redirect_uri": {"https://example.com/foo"}}.Encode())
+}
+
+func (s *HandlerSuite) TestLogoutGoogle(c *check.C) {
+       if s.cluster.ForceLegacyAPI14 {
+               // Google login N/A
+               return
+       }
+       s.cluster.Login.GoogleClientID = "test"
+       req := httptest.NewRequest("GET", "https://0.0.0.0:1/logout?return_to=https://example.com/foo", nil)
+       resp := httptest.NewRecorder()
+       s.handler.ServeHTTP(resp, req)
+       if !c.Check(resp.Code, check.Equals, http.StatusFound) {
+               c.Log(resp.Body.String())
+       }
+       c.Check(resp.Header().Get("Location"), check.Equals, "https://example.com/foo")
+}
+
 func (s *HandlerSuite) TestValidateV1APIToken(c *check.C) {
        req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
        user, ok, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveToken)
index 4139b270c51dac1f51d48dde4880c7e201e2d879..ac092382d42a20c6ec4caad4033ed4a5679d7632 100644 (file)
@@ -29,6 +29,15 @@ func NewConn(cluster *arvados.Cluster) *Conn {
        }
 }
 
+func (conn *Conn) Logout(ctx context.Context, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
+       if conn.cluster.Login.ProviderAppID != "" {
+               // Proxy to RailsAPI, which hands off to sso-provider.
+               return conn.railsProxy.Logout(ctx, opts)
+       } else {
+               return conn.googleLoginController.Logout(ctx, conn.cluster, conn.railsProxy, opts)
+       }
+}
+
 func (conn *Conn) Login(ctx context.Context, opts arvados.LoginOptions) (arvados.LoginResponse, error) {
        wantGoogle := conn.cluster.Login.GoogleClientID != ""
        wantSSO := conn.cluster.Login.ProviderAppID != ""
index b1ebb27e486f91ecda01341d0f09c2e425df5a71..e96b940ef7cc990c9dd7c98bf950c76fe690fcb4 100644 (file)
@@ -52,6 +52,10 @@ func (ctrl *googleLoginController) getProvider() (*oidc.Provider, error) {
        return ctrl.provider, nil
 }
 
+func (ctrl *googleLoginController) Logout(ctx context.Context, cluster *arvados.Cluster, railsproxy *railsProxy, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
+       return arvados.LogoutResponse{RedirectLocation: opts.ReturnTo}, nil
+}
+
 func (ctrl *googleLoginController) Login(ctx context.Context, cluster *arvados.Cluster, railsproxy *railsProxy, opts arvados.LoginOptions) (arvados.LoginResponse, error) {
        provider, err := ctrl.getProvider()
        if err != nil {
index 9f3267cef0e5c0435a027f73dfa0367afd6d403c..4fb0fbcee18055fbf7f7ac2e6fc688d19fac4cde 100644 (file)
@@ -163,6 +163,12 @@ func (s *LoginSuite) TearDownTest(c *check.C) {
        s.railsSpy.Close()
 }
 
+func (s *LoginSuite) TestGoogleLogout(c *check.C) {
+       resp, err := s.localdb.Logout(context.Background(), arvados.LogoutOptions{ReturnTo: "https://foo.example.com/bar"})
+       c.Check(err, check.IsNil)
+       c.Check(resp.RedirectLocation, check.Equals, "https://foo.example.com/bar")
+}
+
 func (s *LoginSuite) TestGoogleLogin_Start_Bogus(c *check.C) {
        resp, err := s.localdb.Login(context.Background(), arvados.LoginOptions{})
        c.Check(err, check.IsNil)
index c41f103dc464be41be43002b24e11a59a9968c8c..69d707703852b7fc60acfcb6e86d8fa960e7a5c9 100644 (file)
@@ -54,6 +54,13 @@ func (rtr *router) addRoutes() {
                                return rtr.fed.Login(ctx, *opts.(*arvados.LoginOptions))
                        },
                },
+               {
+                       arvados.EndpointLogout,
+                       func() interface{} { return &arvados.LogoutOptions{} },
+                       func(ctx context.Context, opts interface{}) (interface{}, error) {
+                               return rtr.fed.Logout(ctx, *opts.(*arvados.LogoutOptions))
+                       },
+               },
                {
                        arvados.EndpointCollectionCreate,
                        func() interface{} { return &arvados.CreateOptions{} },
index bf6166f44ffd649691d317eed302e31bee35b5cf..4b143b770bbbe093bbec7c668f58e2f0100f81a6 100644 (file)
@@ -145,6 +145,14 @@ func (conn *Conn) Login(ctx context.Context, options arvados.LoginOptions) (arva
        return resp, err
 }
 
+func (conn *Conn) Logout(ctx context.Context, options arvados.LogoutOptions) (arvados.LogoutResponse, error) {
+       ep := arvados.EndpointLogout
+       var resp arvados.LogoutResponse
+       err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
+       resp.RedirectLocation = conn.relativeToBaseURL(resp.RedirectLocation)
+       return resp, err
+}
+
 // If the given location is a valid URL and its origin is the same as
 // conn.baseURL, return it as a relative URL. Otherwise, return it
 // unmodified.
index 83a80e878c06de3af7cc252b142423a462391f46..b97c0f87b85f6e4f8c2c0ee798256bde9fced23c 100644 (file)
@@ -51,6 +51,16 @@ func (s *RPCSuite) TestLogin(c *check.C) {
        c.Check(resp.RedirectLocation, check.Equals, "/auth/joshid?return_to="+url.QueryEscape(","+opts.ReturnTo))
 }
 
+func (s *RPCSuite) TestLogout(c *check.C) {
+       s.ctx = context.Background()
+       opts := arvados.LogoutOptions{
+               ReturnTo: "https://foo.example.com/bar",
+       }
+       resp, err := s.conn.Logout(s.ctx, opts)
+       c.Check(err, check.IsNil)
+       c.Check(resp.RedirectLocation, check.Equals, "http://localhost:3002/users/sign_out?redirect_uri="+url.QueryEscape(opts.ReturnTo))
+}
+
 func (s *RPCSuite) TestCollectionCreate(c *check.C) {
        coll, err := s.conn.CollectionCreate(s.ctx, arvados.CreateOptions{Attrs: map[string]interface{}{
                "owner_uuid":         arvadostest.ActiveUserUUID,
index aa670c53921ff0a4be5f6765347b06ede278d0d1..5c8d4f629a2503ab91e8d3e2af7eae7d956846d5 100644 (file)
@@ -19,6 +19,7 @@ type APIEndpoint struct {
 var (
        EndpointConfigGet                     = APIEndpoint{"GET", "arvados/v1/config", ""}
        EndpointLogin                         = APIEndpoint{"GET", "login", ""}
+       EndpointLogout                        = APIEndpoint{"GET", "logout", ""}
        EndpointCollectionCreate              = APIEndpoint{"POST", "arvados/v1/collections", "collection"}
        EndpointCollectionUpdate              = APIEndpoint{"PATCH", "arvados/v1/collections/{uuid}", "collection"}
        EndpointCollectionGet                 = APIEndpoint{"GET", "arvados/v1/collections/{uuid}", ""}
@@ -140,9 +141,14 @@ type LoginOptions struct {
        State    string `json:"state,omitempty"`  // OAuth2 callback state
 }
 
+type LogoutOptions struct {
+       ReturnTo string `json:"return_to"` // Redirect to this URL after logging out
+}
+
 type API interface {
        ConfigGet(ctx context.Context) (json.RawMessage, error)
        Login(ctx context.Context, options LoginOptions) (LoginResponse, error)
+       Logout(ctx context.Context, options LogoutOptions) (LogoutResponse, error)
        CollectionCreate(ctx context.Context, options CreateOptions) (Collection, error)
        CollectionUpdate(ctx context.Context, options UpdateOptions) (Collection, error)
        CollectionGet(ctx context.Context, options GetOptions) (Collection, error)
index 7107ac57ab76c029ccc28764762ad4b08de42bbe..75ebc81c142b1ee167bafa7792923513af3c5120 100644 (file)
@@ -24,3 +24,12 @@ func (resp LoginResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) {
                w.Write(resp.HTML.Bytes())
        }
 }
+
+type LogoutResponse struct {
+       RedirectLocation string
+}
+
+func (resp LogoutResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) {
+       w.Header().Set("Location", resp.RedirectLocation)
+       w.WriteHeader(http.StatusFound)
+}
index b5cea5c18599afe1ba252e611d7f401ad0c2c622..9019d33cfb8bc167c45cf5e4c1b2fa4107d0c9c3 100644 (file)
@@ -37,6 +37,10 @@ func (as *APIStub) Login(ctx context.Context, options arvados.LoginOptions) (arv
        as.appendCall(as.Login, ctx, options)
        return arvados.LoginResponse{}, as.Error
 }
+func (as *APIStub) Logout(ctx context.Context, options arvados.LogoutOptions) (arvados.LogoutResponse, error) {
+       as.appendCall(as.Logout, ctx, options)
+       return arvados.LogoutResponse{}, as.Error
+}
 func (as *APIStub) CollectionCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Collection, error) {
        as.appendCall(as.CollectionCreate, ctx, options)
        return arvados.Collection{}, as.Error