Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Allow users to sign out when there is only one oauth provider configured#44803

Merged
miveronese merged 20 commits into
mainfrom
mv/bugfix-signout
Nov 29, 2022
Merged

Allow users to sign out when there is only one oauth provider configured#44803
miveronese merged 20 commits into
mainfrom
mv/bugfix-signout

Conversation

@miveronese

Copy link
Copy Markdown
Contributor

Issue: #31192

This PR fixes a signout bug that happens when there's only one auth provider configured.

Bug:

  • if a user had only one auth provider and clicked the SignOut link, the user was immediately signed back (sso login)
  • it was reported that this behavior caused issues in the past
  • this flow also created an annoying experience for the user, especially if they want to log in with a different account.

With this PR:

  • if a user has only one auth provider and clicks the SignOut link, they will be redirected to the Sourcegraph login page.
  • The session expiration behavior for only one auth provider was preserved. If the session expires, the user will still be signed back immediately.

Test plan

  • Added + updated unit tests.
  • Manually tested the new behavior in a local instance.

Before vs after demos:

@sourcegraph-bot

sourcegraph-bot commented Nov 25, 2022

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff ca2c501...830a72f.

Notify File(s)
@unknwon enterprise/cmd/frontend/internal/auth/githuboauth/middleware_test.go
enterprise/cmd/frontend/internal/auth/gitlaboauth/middleware_test.go
enterprise/cmd/frontend/internal/auth/oauth/middleware.go
enterprise/cmd/frontend/internal/auth/openidconnect/middleware.go
enterprise/cmd/frontend/internal/auth/openidconnect/middleware_test.go
enterprise/cmd/frontend/internal/auth/saml/middleware.go
enterprise/cmd/frontend/internal/auth/saml/middleware_test.go

@miveronese miveronese requested a review from a team November 25, 2022 00:39
Comment thread cmd/frontend/internal/app/sign_out.go Outdated
Comment thread enterprise/cmd/frontend/internal/auth/common/utils.go Outdated
Comment thread enterprise/cmd/frontend/internal/auth/oauth/middleware.go Outdated
Comment thread enterprise/cmd/frontend/internal/auth/openidconnect/middleware.go Outdated
Comment thread enterprise/cmd/frontend/internal/auth/saml/middleware.go Outdated
Comment thread enterprise/cmd/frontend/internal/auth/saml/middleware.go Outdated
@miveronese

Copy link
Copy Markdown
Contributor Author

@mrnugget , thanks for the review! I've addressed your suggestions, so it's ready for another look!

@miveronese miveronese requested review from a team and mrnugget November 25, 2022 17:34
Comment thread cmd/frontend/auth/sign_out_cookie.go
Comment thread cmd/frontend/internal/app/sign_out.go Outdated
Comment thread enterprise/cmd/frontend/internal/auth/oauth/middleware.go Outdated
Comment thread enterprise/cmd/frontend/internal/auth/saml/middleware.go Outdated
Comment on lines -85 to -94
if got, want := resp.Header.Get("Location"), "/.auth/github/login?"; !strings.Contains(got, want) {
t.Errorf("got redirect URL %v, want contains %v", got, want)
}
redirectURL, err := url.Parse(resp.Header.Get("Location"))
if err != nil {
t.Fatal(err)
}
if got, want := redirectURL.Query().Get("redirect"), "/page"; got != want {
t.Errorf("got return-to URL %v, want %v", got, want)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did these redirect checks get removed?

@miveronese miveronese Nov 28, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The redirects were there to cover the previous flow where:

  • user clicks signout
  • user is redirected to the sso signin flow (a 302 was expected)

now, the behavior is different:

  • user clicks signout
  • there's a get request to the SG sign in page (a 200 is expected) instead of a redirect to the sso flow as before

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's not the flow that this test tests. Reading the test it says:

  • User is not signed in
  • User tries to navigate to "http://example.com/page" sub-page
  • User gets redirected to GitHub OAuth sign-in flow

This is still valid and desired behaviour. Similar goes for the test above it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you are correct!! thank you!!!

Comment thread cmd/frontend/internal/app/sign_out.go Outdated
Comment thread enterprise/cmd/frontend/internal/auth/oauth/middleware.go Outdated
@miveronese miveronese requested a review from pjlast November 28, 2022 20:18

@pjlast pjlast left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@miveronese

Copy link
Copy Markdown
Contributor Author

:shipit:

I was waiting until the build finish to ping you, @pjlast , but you were faster :D
Thanks for insisting on the test changes 🙏

@miveronese miveronese merged commit dcfb092 into main Nov 29, 2022
@miveronese miveronese deleted the mv/bugfix-signout branch November 29, 2022 16:25

@unknwon unknwon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


func RemoveSignOutCookieIfSet(r *http.Request, w http.ResponseWriter) {
if HasSignOutCookie(r) {
http.SetCookie(w, &http.Cookie{Name: SignoutCookie, Value: "", MaxAge: -1})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: It is always safe to clear a cookie regardless of its existence, browser handles it. The opposite version of "upsert".

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants