| 1 |
package oidc |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
) |
| 6 |
|
| 7 |
type Bool bool |
| 8 |
|
| 9 |
// UnmarshalJSON handles both standard JSON boolean values and string representations. |
| 10 |
// This is necessary because some OIDC providers (notably AWS Cognito) incorrectly return |
| 11 |
// boolean fields like email_verified and phone_number_verified as strings ("true"/"false") |
| 12 |
// instead of proper JSON booleans, violating the OIDC specification. |
| 13 |
// |
| 14 |
// The method first attempts standard boolean unmarshaling, and falls back to string |
| 15 |
// parsing if that fails, making it compatible with both compliant and non-compliant providers. |
| 16 |
// |
| 17 |
// Ref: |
| 18 |
// - https://openid.net/specs/openid-connect-basic-1_0.html#StandardClaims |
| 19 |
// - https://docs.aws.amazon.com/cognito/latest/developerguide/userinfo-endpoint.html |
| 20 |
func (bs *Bool) UnmarshalJSON(data []byte) error { |
| 21 |
s := string(data) |
| 22 |
switch s { |
| 23 |
case "true", `"true"`: |
| 24 |
*bs = true |
| 25 |
case "false", `"false"`: |
| 26 |
*bs = false |
| 27 |
default: |
| 28 |
return fmt.Errorf("cannot unmarshal %s into Bool", s) |
| 29 |
} |
| 30 |
return nil |
| 31 |
} |
| 32 |
|
| 33 |
// UserInfo implements OpenID Connect Core 1.0, section 5.1. |
| 34 |
// https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims. |
| 35 |
type UserInfo struct { |
| 36 |
Subject string `json:"sub,omitempty"` |
| 37 |
UserInfoProfile |
| 38 |
UserInfoEmail |
| 39 |
UserInfoPhone |
| 40 |
Address *UserInfoAddress `json:"address,omitempty"` |
| 41 |
|
| 42 |
Claims map[string]any `json:"-"` |
| 43 |
} |
| 44 |
|
| 45 |
func (u *UserInfo) AppendClaims(k string, v any) { |
| 46 |
if u.Claims == nil { |
| 47 |
u.Claims = make(map[string]any) |
| 48 |
} |
| 49 |
|
| 50 |
u.Claims[k] = v |
| 51 |
} |
| 52 |
|
| 53 |
// GetAddress is a safe getter that takes |
| 54 |
// care of a possible nil value. |
| 55 |
func (u *UserInfo) GetAddress() *UserInfoAddress { |
| 56 |
if u.Address == nil { |
| 57 |
return new(UserInfoAddress) |
| 58 |
} |
| 59 |
return u.Address |
| 60 |
} |
| 61 |
|
| 62 |
// GetSubject implements [rp.SubjectGetter] |
| 63 |
func (u *UserInfo) GetSubject() string { |
| 64 |
return u.Subject |
| 65 |
} |
| 66 |
|
| 67 |
type uiAlias UserInfo |
| 68 |
|
| 69 |
func (u *UserInfo) MarshalJSON() ([]byte, error) { |
| 70 |
return mergeAndMarshalClaims((*uiAlias)(u), u.Claims) |
| 71 |
} |
| 72 |
|
| 73 |
func (u *UserInfo) UnmarshalJSON(data []byte) error { |
| 74 |
return unmarshalJSONMulti(data, (*uiAlias)(u), &u.Claims) |
| 75 |
} |
| 76 |
|
| 77 |
type UserInfoProfile struct { |
| 78 |
Name string `json:"name,omitempty"` |
| 79 |
GivenName string `json:"given_name,omitempty"` |
| 80 |
FamilyName string `json:"family_name,omitempty"` |
| 81 |
MiddleName string `json:"middle_name,omitempty"` |
| 82 |
Nickname string `json:"nickname,omitempty"` |
| 83 |
Profile string `json:"profile,omitempty"` |
| 84 |
Picture string `json:"picture,omitempty"` |
| 85 |
Website string `json:"website,omitempty"` |
| 86 |
Gender Gender `json:"gender,omitempty"` |
| 87 |
Birthdate string `json:"birthdate,omitempty"` |
| 88 |
Zoneinfo string `json:"zoneinfo,omitempty"` |
| 89 |
Locale *Locale `json:"locale,omitempty"` |
| 90 |
UpdatedAt Time `json:"updated_at,omitempty"` |
| 91 |
PreferredUsername string `json:"preferred_username,omitempty"` |
| 92 |
} |
| 93 |
|
| 94 |
type UserInfoEmail struct { |
| 95 |
Email string `json:"email,omitempty"` |
| 96 |
|
| 97 |
EmailVerified Bool `json:"email_verified,omitempty"` |
| 98 |
} |
| 99 |
|
| 100 |
type UserInfoPhone struct { |
| 101 |
PhoneNumber string `json:"phone_number,omitempty"` |
| 102 |
PhoneNumberVerified Bool `json:"phone_number_verified,omitempty"` |
| 103 |
} |
| 104 |
|
| 105 |
type UserInfoAddress struct { |
| 106 |
Formatted string `json:"formatted,omitempty"` |
| 107 |
StreetAddress string `json:"street_address,omitempty"` |
| 108 |
Locality string `json:"locality,omitempty"` |
| 109 |
Region string `json:"region,omitempty"` |
| 110 |
PostalCode string `json:"postal_code,omitempty"` |
| 111 |
Country string `json:"country,omitempty"` |
| 112 |
} |
| 113 |
|
| 114 |
type UserInfoRequest struct { |
| 115 |
AccessToken string `schema:"access_token"` |
| 116 |
} |