github.com/lestrrat-go/jwx/v2@v2.0.21/jwt/openid/address.go (about)

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