1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
package openid
import (
"fmt"
"strconv"
"github.com/lestrrat-go/jwx/v2/internal/json"
"github.com/lestrrat-go/jwx/v2/internal/pool"
)
const (
AddressFormattedKey = "formatted"
AddressStreetAddressKey = "street_address"
AddressLocalityKey = "locality"
AddressRegionKey = "region"
AddressPostalCodeKey = "postal_code"
AddressCountryKey = "country"
)
// AddressClaim is the address claim as described in https://openid.net/specs/openid-connect-core-1_0.html#AddressClaim
type AddressClaim struct {
formatted *string // https://openid.net/specs/openid-connect-core-1_0.html#AddressClaim
streetAddress *string // https://openid.net/specs/openid-connect-core-1_0.html#AddressClaim
locality *string // https://openid.net/specs/openid-connect-core-1_0.html#AddressClaim
region *string // https://openid.net/specs/openid-connect-core-1_0.html#AddressClaim
postalCode *string // https://openid.net/specs/openid-connect-core-1_0.html#AddressClaim
country *string // https://openid.net/specs/openid-connect-core-1_0.html#AddressClaim
}
type addressClaimMarshalProxy struct {
Xformatted *string `json:"formatted,omitempty"`
XstreetAddress *string `json:"street_address,omitempty"`
Xlocality *string `json:"locality,omitempty"`
Xregion *string `json:"region,omitempty"`
XpostalCode *string `json:"postal_code,omitempty"`
Xcountry *string `json:"country,omitempty"`
}
func NewAddress() *AddressClaim {
return &AddressClaim{}
}
// Formatted is a convenience function to retrieve the corresponding value store in the token
// if there is a problem retrieving the value, the zero value is returned. If you need to differentiate between existing/non-existing values, use `Get` instead
func (t AddressClaim) Formatted() string {
if t.formatted == nil {
return ""
}
return *(t.formatted)
}
// StreetAddress is a convenience function to retrieve the corresponding value store in the token
// if there is a problem retrieving the value, the zero value is returned. If you need to differentiate between existing/non-existing values, use `Get` instead
func (t AddressClaim) StreetAddress() string {
if t.streetAddress == nil {
return ""
}
return *(t.streetAddress)
}
// Locality is a convenience function to retrieve the corresponding value store in the token
// if there is a problem retrieving the value, the zero value is returned. If you need to differentiate between existing/non-existing values, use `Get` instead
func (t AddressClaim) Locality() string {
if t.locality == nil {
return ""
}
return *(t.locality)
}
// Region is a convenience function to retrieve the corresponding value store in the token
// if there is a problem retrieving the value, the zero value is returned. If you need to differentiate between existing/non-existing values, use `Get` instead
func (t AddressClaim) Region() string {
if t.region == nil {
return ""
}
return *(t.region)
}
// PostalCode is a convenience function to retrieve the corresponding value store in the token
// if there is a problem retrieving the value, the zero value is returned. If you need to differentiate between existing/non-existing values, use `Get` instead
func (t AddressClaim) PostalCode() string {
if t.postalCode == nil {
return ""
}
return *(t.postalCode)
}
// Country is a convenience function to retrieve the corresponding value store in the token
// if there is a problem retrieving the value, the zero value is returned. If you need to differentiate between existing/non-existing values, use `Get` instead
func (t AddressClaim) Country() string {
if t.country == nil {
return ""
}
return *(t.country)
}
func (t *AddressClaim) Get(s string) (interface{}, bool) {
switch s {
case AddressFormattedKey:
if t.formatted == nil {
return nil, false
}
return *(t.formatted), true
case AddressStreetAddressKey:
if t.streetAddress == nil {
return nil, false
}
return *(t.streetAddress), true
case AddressLocalityKey:
if t.locality == nil {
return nil, false
}
return *(t.locality), true
case AddressRegionKey:
if t.region == nil {
return nil, false
}
return *(t.region), true
case AddressPostalCodeKey:
if t.postalCode == nil {
return nil, false
}
return *(t.postalCode), true
case AddressCountryKey:
if t.country == nil {
return nil, false
}
return *(t.country), true
}
return nil, false
}
func (t *AddressClaim) Set(key string, value interface{}) error {
switch key {
case AddressFormattedKey:
v, ok := value.(string)
if ok {
t.formatted = &v
return nil
}
return fmt.Errorf(`invalid type for key 'formatted': %T`, value)
case AddressStreetAddressKey:
v, ok := value.(string)
if ok {
t.streetAddress = &v
return nil
}
return fmt.Errorf(`invalid type for key 'streetAddress': %T`, value)
case AddressLocalityKey:
v, ok := value.(string)
if ok {
t.locality = &v
return nil
}
return fmt.Errorf(`invalid type for key 'locality': %T`, value)
case AddressRegionKey:
v, ok := value.(string)
if ok {
t.region = &v
return nil
}
return fmt.Errorf(`invalid type for key 'region': %T`, value)
case AddressPostalCodeKey:
v, ok := value.(string)
if ok {
t.postalCode = &v
return nil
}
return fmt.Errorf(`invalid type for key 'postalCode': %T`, value)
case AddressCountryKey:
v, ok := value.(string)
if ok {
t.country = &v
return nil
}
return fmt.Errorf(`invalid type for key 'country': %T`, value)
default:
return fmt.Errorf(`invalid key for address claim: %s`, key)
}
}
func (t *AddressClaim) Accept(v interface{}) error {
switch v := v.(type) {
case AddressClaim:
*t = v
return nil
case *AddressClaim:
*t = *v
return nil
case map[string]interface{}:
for key, value := range v {
if err := t.Set(key, value); err != nil {
return fmt.Errorf(`failed to set header: %w`, err)
}
}
return nil
default:
return fmt.Errorf(`invalid type for AddressClaim: %T`, v)
}
}
// MarshalJSON serializes the token in JSON format.
func (t AddressClaim) MarshalJSON() ([]byte, error) {
buf := pool.GetBytesBuffer()
defer pool.ReleaseBytesBuffer(buf)
buf.WriteByte('{')
prev := buf.Len()
if v := t.country; v != nil {
buf.WriteString(`"country":`)
buf.WriteString(strconv.Quote(*v))
}
if v := t.formatted; v != nil {
if buf.Len() > prev {
buf.WriteByte(',')
}
prev = buf.Len()
buf.WriteString(`"formatted":`)
buf.WriteString(strconv.Quote(*v))
}
if v := t.locality; v != nil {
if buf.Len() > prev {
buf.WriteByte(',')
}
prev = buf.Len()
buf.WriteString(`"locality":`)
buf.WriteString(strconv.Quote(*v))
}
if v := t.postalCode; v != nil {
if buf.Len() > prev {
buf.WriteByte(',')
}
prev = buf.Len()
buf.WriteString(`"postal_code":`)
buf.WriteString(strconv.Quote(*v))
}
if v := t.region; v != nil {
if buf.Len() > prev {
buf.WriteByte(',')
}
prev = buf.Len()
buf.WriteString(`"region":`)
buf.WriteString(strconv.Quote(*v))
}
if v := t.streetAddress; v != nil {
if buf.Len() > prev {
buf.WriteByte(',')
}
buf.WriteString(`"street_address":`)
buf.WriteString(strconv.Quote(*v))
}
buf.WriteByte('}')
ret := make([]byte, buf.Len())
copy(ret, buf.Bytes())
return ret, nil
}
// UnmarshalJSON deserializes data from a JSON data buffer into a AddressClaim
func (t *AddressClaim) UnmarshalJSON(data []byte) error {
var proxy addressClaimMarshalProxy
if err := json.Unmarshal(data, &proxy); err != nil {
return fmt.Errorf(`failed to unmarshasl address claim: %w`, err)
}
t.formatted = proxy.Xformatted
t.streetAddress = proxy.XstreetAddress
t.locality = proxy.Xlocality
t.region = proxy.Xregion
t.postalCode = proxy.XpostalCode
t.country = proxy.Xcountry
return nil
}
|