Skip to content

Commit

Permalink
Merge 76f6dec into 75894bb
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 committed Jul 11, 2019
2 parents 75894bb + 76f6dec commit a667100
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
15 changes: 5 additions & 10 deletions middleware/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,16 @@ func MakeHTTPOIDCTokenValidationMW(keycloakClient KeycloakClient, audienceRequir
return
}

var matched, _ = regexp.MatchString(`^[Bb]earer *`, authorizationHeader)

if !matched {
var r = regexp.MustCompile(`^[Bb]earer +([^ ]+)$`)
var match = r.FindStringSubmatch(authorizationHeader)
if match == nil {
logger.Log("Authorization Error", "Missing bearer token")
httpErrorHandler(context.TODO(), http.StatusForbidden, fmt.Errorf("Missing bearer token"), w)
return
}

var splitToken = strings.Split(authorizationHeader, "Bearer ")

if len(splitToken) < 2 {
splitToken = strings.Split(authorizationHeader, "bearer ")
}

var accessToken = splitToken[1]
// match[0] is the global matched group. match[1] is the first captured group
var accessToken = match[1]

payload, _, err := jwt.Parse(accessToken)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions middleware/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ func TestHTTPOIDCTokenValidationMW(t *testing.T) {
assert.Equal(t, 403, result.StatusCode)
}

req.Header.Set("Authorization", "Bearer AB CD")

// Invalid bearer token.
{
var w = httptest.NewRecorder()
mockLogger.EXPECT().Log("Authorization Error", "Missing bearer token").Return(nil).Times(1)
m.ServeHTTP(w, req)
var result = w.Result()
assert.Equal(t, 403, result.StatusCode)
}

req.Header.Set("Authorization", "Bearer "+tokenAudString)

// Valid authorization token.
Expand Down

0 comments on commit a667100

Please sign in to comment.