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

     1  package jwt
     2  
     3  import "sync"
     4  
     5  // TokenOptionSet is a bit flag containing per-token options.
     6  type TokenOptionSet uint64
     7  
     8  var defaultOptions TokenOptionSet
     9  var defaultOptionsMu sync.RWMutex
    10  
    11  // TokenOption describes a single token option that can be set on
    12  // the per-token option set (TokenOptionSet)
    13  type TokenOption uint64
    14  
    15  const (
    16  	// FlattenAudience option controls whether the "aud" claim should be flattened
    17  	// to a single string upon the token being serialized to JSON.
    18  	//
    19  	// This is sometimes important when a JWT consumer does not understand that
    20  	// the "aud" claim can actually take the form of an array of strings.
    21  	// (We have been notified by users that AWS Cognito has manifested this behavior
    22  	// at some point)
    23  	//
    24  	// Unless the global option is set using `jwt.Settings()`, the default value is
    25  	// `disabled`, which means that "aud" claims are always rendered as a arrays of
    26  	// strings when serialized to JSON.
    27  	FlattenAudience TokenOption = 1 << iota
    28  
    29  	// MaxPerTokenOption is a marker to denote the last value that an option can take.
    30  	// This value has no meaning other than to be used as a marker.
    31  	MaxPerTokenOption
    32  )
    33  
    34  // Value returns the uint64 value of a single option
    35  func (o TokenOption) Value() uint64 {
    36  	return uint64(o)
    37  }
    38  
    39  // Value returns the uint64 bit flag value of an option set
    40  func (o TokenOptionSet) Value() uint64 {
    41  	return uint64(o)
    42  }
    43  
    44  // DefaultOptionSet creates a new TokenOptionSet using the default
    45  // option set. This may differ depending on if/when functions that
    46  // change the global state has been called, such as `jwt.Settings`
    47  func DefaultOptionSet() TokenOptionSet {
    48  	return TokenOptionSet(defaultOptions.Value())
    49  }
    50  
    51  // Clear sets all bits to zero, effectively disabling all options
    52  func (o *TokenOptionSet) Clear() {
    53  	*o = TokenOptionSet(uint64(0))
    54  }
    55  
    56  // Set sets the value of this option set, effectively *replacing*
    57  // the entire option set with the new value. This is NOT the same
    58  // as Enable/Disable.
    59  func (o *TokenOptionSet) Set(s TokenOptionSet) {
    60  	*o = s
    61  }
    62  
    63  // Enable sets the appropriate value to enable the option in the
    64  // option set
    65  func (o *TokenOptionSet) Enable(flag TokenOption) {
    66  	*o = TokenOptionSet(o.Value() | uint64(flag))
    67  }
    68  
    69  // Enable sets the appropriate value to disable the option in the
    70  // option set
    71  func (o *TokenOptionSet) Disable(flag TokenOption) {
    72  	*o = TokenOptionSet(o.Value() & ^uint64(flag))
    73  }
    74  
    75  // IsEnabled returns true if the given bit on the option set is enabled.
    76  func (o TokenOptionSet) IsEnabled(flag TokenOption) bool {
    77  	return (uint64(o)&uint64(flag) == uint64(flag))
    78  }