Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth: Redirect github oauth login attempt back if user cancels #23083

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion enterprise/cmd/frontend/internal/auth/githuboauth/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import (
"net/http"
"net/url"

"github.com/dghubble/gologin"
"github.com/dghubble/gologin/github"
goauth2 "github.com/dghubble/gologin/oauth2"
"github.com/inconshreveable/log15"
"golang.org/x/oauth2"

"github.com/sourcegraph/sourcegraph/cmd/frontend/auth"
"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
"github.com/sourcegraph/sourcegraph/enterprise/cmd/frontend/internal/auth/oauth"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
Expand Down Expand Up @@ -66,12 +70,37 @@ func parseProvider(p *schema.GitHubAuthProvider, db dbutil.DB, sourceCfg schema.
allowSignup: p.AllowSignup,
allowOrgs: p.AllowOrgs,
}, sessionKey),
nil,
http.HandlerFunc(failureHandler),
)
},
}), messages
}

func failureHandler(w http.ResponseWriter, r *http.Request) {
// As a special case wa want to handle `access_denied` errors by redirecting
// back. This case arises when the user decides not to proceed by clicking `cancel`.
if err := r.URL.Query().Get("error"); err != "access_denied" {
// Fall back to default failure handler
gologin.DefaultFailureHandler.ServeHTTP(w, r)
return
}

ctx := r.Context()
encodedState, err := goauth2.StateFromContext(ctx)
if err != nil {
log15.Error("OAuth failed: could not get state from context.", "error", err)
http.Error(w, "Authentication failed. Try signing in again (and clearing cookies for the current site). The error was: could not get OAuth state from context.", http.StatusInternalServerError)
return
}
state, err := oauth.DecodeState(encodedState)
if err != nil {
log15.Error("OAuth failed: could not decode state.", "error", err)
http.Error(w, "Authentication failed. Try signing in again (and clearing cookies for the current site). The error was: could not get decode OAuth state.", http.StatusInternalServerError)
return
}
Comment on lines +88 to +100
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure all of these are actually needed because we know we got "access_denied" exactly here.

My suggestion would be put a warning log saying OAuth flow is failing because of "access_denied" and do the redirect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to get the state so that we can extract the URL we want to redirect back to

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or do you think it's safe to always assume we're redirecting back to the sign in page?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. Assuming you've verified that we always get "state" back even when the user cencals the authorization on GitHub side.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, the state is populated.

http.Redirect(w, r, auth.SafeRedirectURL(state.Redirect), http.StatusFound)
}

var clientIDSecretValidator = lazyregexp.New("^[a-z0-9]*$")

func validateClientIDAndSecret(clientIDOrSecret string) (valid bool) {
Expand Down
2 changes: 1 addition & 1 deletion enterprise/cmd/frontend/internal/auth/oauth/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func SessionIssuer(s SessionIssuerHelper, sessionKey string) http.Handler {
return
}

// Delete state cookie (no longer needed, while be stale if user logs out and logs back in within 120s)
// Delete state cookie (no longer needed, will be stale if user logs out and logs back in within 120s)
defer s.DeleteStateCookie(w)

if state.Op == LoginStateOpCreateCodeHostConnection {
Expand Down