Skip to content

Commit

Permalink
Merge pull request #8 from formancehq/fix/missing-email-in-claims
Browse files Browse the repository at this point in the history
feat: fix missing email on claims
  • Loading branch information
flemzord authored Nov 8, 2022
2 parents 2076156 + 376ef2b commit 986cce4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/oidc/authorize_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ func authorizeCallbackHandler(
panic(err)
}

userInfos, err := rp.Userinfo(tokens.AccessToken, "Bearer", tokens.IDTokenClaims.GetSubject(), relyingParty)
if err != nil {
panic(err)
}

user, err := storage.FindUserBySubject(r.Context(), tokens.IDTokenClaims.GetSubject())
if err != nil {
user = &auth.User{
ID: uuid.NewString(),
Subject: tokens.IDTokenClaims.GetSubject(),
Email: tokens.IDTokenClaims.GetEmail(),
Subject: userInfos.GetSubject(),
Email: userInfos.GetEmail(),
}
if err := storage.SaveUser(r.Context(), *user); err != nil {
panic(err)
Expand Down
27 changes: 27 additions & 0 deletions pkg/oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/rand"
"crypto/rsa"
"encoding/json"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -33,6 +34,24 @@ func init() {
os.Setenv(op.OidcDevMode, "true")
}

type user struct {
*mockoidc.MockUser
}

func (u *user) Userinfo(scope []string) ([]byte, error) {
encoded, err := u.MockUser.Userinfo(scope)
if err != nil {
return nil, err
}

m := make(map[string]any)
if err := json.Unmarshal(encoded, &m); err != nil {
return nil, err
}
m["sub"] = u.Subject
return json.Marshal(m)
}

func withServer(t *testing.T, fn func(m *mockoidc.MockOIDC, storage *sqlstorage.Storage, provider op.OpenIDProvider)) {
// Create a mock OIDC server which will always return a default user
mockOIDC, err := mockoidc.Run()
Expand Down Expand Up @@ -116,6 +135,10 @@ func Test3LeggedFlow(t *testing.T) {
clientRelyingParty, err := rp.NewRelyingPartyOIDC(provider.Issuer(), client.Id, clear, client.RedirectURIs[0], []string{"openid", "email"})
require.NoError(t, err)

m.QueueUser(&user{
MockUser: mockoidc.DefaultUser(),
})

// Trigger an authentication request
authUrl := rp.AuthURL("", clientRelyingParty)
if testing.Verbose() {
Expand Down Expand Up @@ -145,6 +168,10 @@ func Test3LeggedFlow(t *testing.T) {
introspection, err := rs.Introspect(context.TODO(), resourceServer, tokens.AccessToken)
require.NoError(t, err)
require.True(t, introspection.IsActive())

user, err := storage.FindUser(context.TODO(), tokens.IDTokenClaims.GetSubject())
require.NoError(t, err)
require.NotEmpty(t, user.Email)
default:
require.Fail(t, "code was expected")
}
Expand Down

0 comments on commit 986cce4

Please sign in to comment.