oauth2-proxy / mockoidc

A Mock OIDC Server for Unit & Integration Tests
MIT License
58 stars 38 forks source link

How to add additional claims to the token? #53

Open BenjamenMeyer opened 4 months ago

BenjamenMeyer commented 4 months ago

Services often need to add their own claims to tokens to manage what users are allowed to do in a system. OIDC providers like Okta, Auth0, and others allow their users to add claims via configuration and integrations to support this. How can this be done with mockoidc? One can decode the access token to get the claims.

I tried building a custom user object:

type myUserObject struct {
    mockoidc.MockUser
    PhoneVerified bool
}

with the Claims() method adding the additional data to the jwt.Claims, but I still only get the original data - nothing in the way of the custom claims I added.

What am I missing?

BenjamenMeyer commented 4 months ago

I created my own jsonClaims type that JSON encodes all the fields based on mockoidc's mockClaims type which adds the additional field which is what is used by my implementation of Claims().

BenjamenMeyer commented 4 months ago

PR #54 has some example code that exemplifies the behavior in a similar pattern to how I was doing it in my other code. The PR creates a custom user with a new attribute (PhoneVerified) and then adds a test to the session to get a token with the custom user type and checks for the attribute being in the claims as one would expect. The new test presently fails.

akosyakov commented 2 months ago

I stumbled over the same, would be nice to have easier way adding new claims.