Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Allow multiple audiences #401

Merged
merged 1 commit into from
Jul 21, 2018
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
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ type userContext struct {
// the id of the user
id string
// the audience for the token
audience string
audiences []string
// whether the context is from a session cookie or authorization header
bearerToken bool
// the claims associated to the token
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (r *oauthProxy) identityHeadersMiddleware(custom []string) func(http.Handle
scope := req.Context().Value(contextScopeName).(*RequestScope)
if scope.Identity != nil {
user := scope.Identity
req.Header.Set("X-Auth-Audience", user.audience)
req.Header.Set("X-Auth-Audience", strings.Join(user.audiences, ","))
req.Header.Set("X-Auth-Email", user.email)
req.Header.Set("X-Auth-ExpiresIn", user.expiresAt.String())
req.Header.Set("X-Auth-Groups", strings.Join(user.groups, ","))
Expand Down
29 changes: 24 additions & 5 deletions user_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ func extractIdentity(token jose.JWT) (*userContext, error) {
if err != nil || !found {
preferredName = identity.Email
}
audience, found, err := claims.StringClaim(claimAudience)
if err != nil || !found {
return nil, ErrNoTokenAudience

var audiences []string
aud, found, err := claims.StringClaim(claimAudience)
if err == nil && found {
audiences = append(audiences, aud)
} else {
if aud, found, err := claims.StringsClaim(claimAudience); err != nil || !found {
return nil, ErrNoTokenAudience
} else {
audiences = aud
}
}

// @step: extract the realm roles
Expand Down Expand Up @@ -74,7 +82,7 @@ func extractIdentity(token jose.JWT) (*userContext, error) {
}

return &userContext{
audience: audience,
audiences: audiences,
claims: claims,
email: identity.Email,
expiresAt: identity.ExpiresAt,
Expand All @@ -87,9 +95,20 @@ func extractIdentity(token jose.JWT) (*userContext, error) {
}, nil
}

// backported from https://github.com/gambol99/go-oidc/blob/master/oidc/verification.go#L28-L37
// I'll raise another PR to make it public in the go-oidc package so we can just use `oidc.ContainsString()`
func containsString(needle string, haystack []string) bool {
for _, v := range haystack {
if v == needle {
return true
}
}
return false
}

// isAudience checks the audience
func (r *userContext) isAudience(aud string) bool {
return r.audience == aud
return containsString(aud, r.audiences)
}

// getRoles returns a list of roles
Expand Down
5 changes: 4 additions & 1 deletion user_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ import (

func TestIsAudience(t *testing.T) {
user := &userContext{
audience: "test",
audiences: []string{"test", "test2"},
}
if !user.isAudience("test") {
t.Error("return should not have been false")
}
if user.isAudience("test1") {
t.Error("return should not have been true")
}
if !user.isAudience("test2") {
t.Error("return should not have been false")
}
}

func TestGetUserRoles(t *testing.T) {
Expand Down