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

[#50] Handle all 4xx errors as "user not found" #52

Merged
merged 2 commits into from
Jan 13, 2020
Merged
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
17 changes: 15 additions & 2 deletions plugins/auth/oidc/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/gob"
"fmt"
"net/http"
"regexp"
"strings"

"golang.org/x/oauth2"
Expand All @@ -23,6 +24,8 @@ const (
userIDMethodSubject = "subject"
)

var http4xxErrorResponse = regexp.MustCompile(`^(4[0-9]{2}) (.*)`)

type AuthOIDC struct {
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
Expand Down Expand Up @@ -224,10 +227,20 @@ func (a *AuthOIDC) getOAuthConfig() *oauth2.Config {
func (a *AuthOIDC) getUserFromToken(ctx context.Context, token *oauth2.Token) (string, error) {
ui, err := a.provider.UserInfo(ctx, oauth2.StaticTokenSource(token))
if err != nil {
if strings.Contains(err.Error(), "401 Unauthorized") {
// Handle Unauthorized as no user found instead of generic error
if http4xxErrorResponse.MatchString(err.Error()) {
/*
* Server answered with any 4xx error
*
* Google OIDC: 401 Unauthorized => Token expired
* Wordpress OIDC plugin: 400 Bad Request => Token expired
*
* As long as they can't agree on ONE status for that we need to
* handle all 4xx as "token expired" and therefore "no valid user"
*/
return "", plugins.ErrNoValidUserFound
}

// Other error: Report the error
return "", errors.Wrap(err, "Unable to fetch user info")
}

Expand Down