...

Source file src/github.com/lestrrat-go/jwx/jwt/openid/address.go

Documentation: github.com/lestrrat-go/jwx/jwt/openid

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

View as plain text