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

     1  // Package openid provides a specialized token that provides utilities
     2  // to work with OpenID JWT tokens.
     3  //
     4  // In order to use OpenID claims, you specify the token to use in the
     5  // jwt.Parse method
     6  //
     7  //	jwt.Parse(data, jwt.WithToken(openid.New())
     8  package openid
     9  
    10  import (
    11  	"fmt"
    12  
    13  	"github.com/lestrrat-go/jwx/v2/internal/json"
    14  	"github.com/lestrrat-go/jwx/v2/jwt"
    15  )
    16  
    17  var registry = json.NewRegistry()
    18  
    19  func (t *stdToken) Clone() (jwt.Token, error) {
    20  	var dst jwt.Token = New()
    21  
    22  	for _, pair := range t.makePairs() {
    23  		//nolint:forcetypeassert
    24  		key := pair.Key.(string)
    25  		if err := dst.Set(key, pair.Value); err != nil {
    26  			return nil, fmt.Errorf(`failed to set %s: %w`, key, err)
    27  		}
    28  	}
    29  	return dst, nil
    30  }
    31  
    32  // RegisterCustomField allows users to specify that a private field
    33  // be decoded as an instance of the specified type. This option has
    34  // a global effect.
    35  //
    36  // For example, suppose you have a custom field `x-birthday`, which
    37  // you want to represent as a string formatted in RFC3339 in JSON,
    38  // but want it back as `time.Time`.
    39  //
    40  // In that case you would register a custom field as follows
    41  //
    42  //	jwt.RegisterCustomField(`x-birthday`, timeT)
    43  //
    44  // Then `token.Get("x-birthday")` will still return an `interface{}`,
    45  // but you can convert its type to `time.Time`
    46  //
    47  //	bdayif, _ := token.Get(`x-birthday`)
    48  //	bday := bdayif.(time.Time)
    49  func RegisterCustomField(name string, object interface{}) {
    50  	registry.Register(name, object)
    51  }