85 lines
3.1 KiB
Go
85 lines
3.1 KiB
Go
package rp_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
tu "github.com/zitadel/oidc/v2/internal/testutil"
|
|
"github.com/zitadel/oidc/v2/pkg/client/rp"
|
|
"github.com/zitadel/oidc/v2/pkg/oidc"
|
|
)
|
|
|
|
// MyCustomClaims extends the TokenClaims base,
|
|
// so it implments the oidc.Claims interface.
|
|
// Instead of carying a map, we add needed fields
|
|
// to the struct for type safe access.
|
|
type MyCustomClaims struct {
|
|
oidc.TokenClaims
|
|
NotBefore oidc.Time `json:"nbf,omitempty"`
|
|
AccessTokenHash string `json:"at_hash,omitempty"`
|
|
Foo string `json:"foo,omitempty"`
|
|
Bar *Nested `json:"bar,omitempty"`
|
|
}
|
|
|
|
// GetAccessTokenHash is required to implement
|
|
// the oidc.IDClaims interface.
|
|
func (c *MyCustomClaims) GetAccessTokenHash() string {
|
|
return c.AccessTokenHash
|
|
}
|
|
|
|
// Nested struct types are also possible.
|
|
type Nested struct {
|
|
Count int `json:"count,omitempty"`
|
|
Tags []string `json:"tags,omitempty"`
|
|
}
|
|
|
|
/*
|
|
idToken caries the following claims. foo and bar are custom claims
|
|
|
|
{
|
|
"acr": "something",
|
|
"amr": [
|
|
"foo",
|
|
"bar"
|
|
],
|
|
"at_hash": "GKlH62ujLglHjxdM6ezzyQ",
|
|
"aud": [
|
|
"unit",
|
|
"test",
|
|
"555666"
|
|
],
|
|
"auth_time": 1678096954,
|
|
"azp": "555666",
|
|
"bar": {
|
|
"count": 22,
|
|
"tags": [
|
|
"some",
|
|
"tags"
|
|
]
|
|
},
|
|
"client_id": "555666",
|
|
"exp": 4802234675,
|
|
"foo": "Hello, World!",
|
|
"iat": 1678097014,
|
|
"iss": "local.com",
|
|
"jti": "9876",
|
|
"nbf": 1678097014,
|
|
"nonce": "12345",
|
|
"sub": "tim@local.com"
|
|
}
|
|
*/
|
|
const idToken = `eyJhbGciOiJSUzI1NiIsImtpZCI6IjE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F_9HQUAkmd750OIxkAHxAMvEPQcpbLHenVvX_Q0QMrzClVrxehn5TVMfmkYYg7ocr876Bq9xQGPNHAcrwvVIJqdg5uMUA38L3HC2BEueG6furZGvc7-qDWAT1VR9liM5ieKpPg`
|
|
|
|
func ExampleVerifyTokens_customClaims() {
|
|
v := rp.NewIDTokenVerifier("local.com", "555666", tu.KeySet{})
|
|
|
|
// VerifyAccessToken can be called with the *MyCustomClaims.
|
|
claims, err := rp.VerifyTokens[*MyCustomClaims](context.TODO(), accessToken, idToken, v)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
// Here we have typesafe access to the custom claims
|
|
fmt.Println(claims.Foo, claims.Bar.Count, claims.Bar.Tags)
|
|
// Output: Hello, World! 22 [some tags]
|
|
}
|