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

id token: allow to set claims #329

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@ func (i *IDToken) Claims(v interface{}) error {
return json.Unmarshal(i.claims, v)
}

// WithClaims returns a new IDToken that's a clone of i, but using
// provided claims. This is only intended for test cases or very
// specific use cases
func (i *IDToken) WithClaims(claims []byte) *IDToken {
i2 := new(IDToken)
*i2 = *i
i2.claims = claims
return i2
}

// VerifyAccessToken verifies that the hash of the access token that corresponds to the iD token
// matches the hash in the id token. It returns an error if the hashes don't match.
// It is the caller's responsibility to ensure that the optional access token hash is present for the ID token
Expand Down
12 changes: 12 additions & 0 deletions oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -519,3 +520,14 @@ func TestUserInfoEndpoint(t *testing.T) {
}

}

func TestIDTokenWithClaims(t *testing.T) {
idToken := IDToken{
Issuer: "accounts.google.com",
claims: []byte(`{"iss":"accounts.google.com"}`),
}
idTokenWithClaims := idToken.WithClaims([]byte(`{"iss":"accounts.google.com","aud":"client1"}`))
assert.Equal(t, idToken.Issuer, idTokenWithClaims.Issuer)
assert.Equal(t, []byte(`{"iss":"accounts.google.com"}`), idToken.claims)
assert.Equal(t, []byte(`{"iss":"accounts.google.com","aud":"client1"}`), idTokenWithClaims.claims)
}