fix: oidc userinfo can UnmarshalJSON with address

This commit is contained in:
wener 2021-10-08 00:16:04 +08:00
parent 0de2cc1d24
commit 55e2b05fd8
2 changed files with 28 additions and 1 deletions

View file

@ -360,6 +360,7 @@ func (i *userinfo) MarshalJSON() ([]byte, error) {
func (i *userinfo) UnmarshalJSON(data []byte) error { func (i *userinfo) UnmarshalJSON(data []byte) error {
type Alias userinfo type Alias userinfo
a := &struct { a := &struct {
Address *userInfoAddress `json:"address,omitempty"`
*Alias *Alias
UpdatedAt int64 `json:"update_at,omitempty"` UpdatedAt int64 `json:"update_at,omitempty"`
}{ }{
@ -368,7 +369,7 @@ func (i *userinfo) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &a); err != nil { if err := json.Unmarshal(data, &a); err != nil {
return err return err
} }
i.Address = a.Address
i.UpdatedAt = Time(time.Unix(a.UpdatedAt, 0).UTC()) i.UpdatedAt = Time(time.Unix(a.UpdatedAt, 0).UTC())
if err := json.Unmarshal(data, &i.claims); err != nil { if err := json.Unmarshal(data, &i.claims); err != nil {

26
pkg/oidc/userinfo_test.go Normal file
View file

@ -0,0 +1,26 @@
package oidc
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)
func TestUserInfoMarshal(t *testing.T) {
userinfo := NewUserInfo()
userinfo.SetSubject("test")
userinfo.SetAddress(NewUserInfoAddress("Test 789\nPostfach 2", "", "", "", "", ""))
userinfo.SetEmail("test", true)
userinfo.SetPhone("0791234567", true)
userinfo.SetName("Test")
userinfo.AppendClaims("private_claim", "test")
marshal, err := json.Marshal(userinfo)
out := NewUserInfo()
assert.NoError(t, err)
assert.NoError(t, json.Unmarshal(marshal, out))
assert.Equal(t, userinfo.GetAddress(), out.GetAddress())
expected, err := json.Marshal(out)
assert.NoError(t, err)
assert.Equal(t, expected, marshal)
}