openid

package
v1.2.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2022 License: MIT Imports: 17 Imported by: 12

Documentation

Overview

Package openid provides a specialized token that provides utilities to work with OpenID JWT tokens.

In order to use OpenID claims, you specify the token to use in the jwt.Parse method

jwt.Parse(data, jwt.WithToken(openid.New())

Index

Constants

View Source
const (
	AddressFormattedKey     = "formatted"
	AddressStreetAddressKey = "street_address"
	AddressLocalityKey      = "locality"
	AddressRegionKey        = "region"
	AddressPostalCodeKey    = "postal_code"
	AddressCountryKey       = "country"
)
View Source
const (
	AddressKey             = "address"
	AudienceKey            = "aud"
	BirthdateKey           = "birthdate"
	EmailKey               = "email"
	EmailVerifiedKey       = "email_verified"
	ExpirationKey          = "exp"
	FamilyNameKey          = "family_name"
	GenderKey              = "gender"
	GivenNameKey           = "given_name"
	IssuedAtKey            = "iat"
	IssuerKey              = "iss"
	JwtIDKey               = "jti"
	LocaleKey              = "locale"
	MiddleNameKey          = "middle_name"
	NameKey                = "name"
	NicknameKey            = "nickname"
	NotBeforeKey           = "nbf"
	PhoneNumberKey         = "phone_number"
	PhoneNumberVerifiedKey = "phone_number_verified"
	PictureKey             = "picture"
	PreferredUsernameKey   = "preferred_username"
	ProfileKey             = "profile"
	SubjectKey             = "sub"
	UpdatedAtKey           = "updated_at"
	WebsiteKey             = "website"
	ZoneinfoKey            = "zoneinfo"
)

Variables

This section is empty.

Functions

func RegisterCustomField added in v1.1.2

func RegisterCustomField(name string, object interface{})

RegisterCustomField allows users to specify that a private field be decoded as an instance of the specified type. This option has a global effect.

For example, suppose you have a custom field `x-birthday`, which you want to represent as a string formatted in RFC3339 in JSON, but want it back as `time.Time`.

In that case you would register a custom field as follows

jwt.RegisterCustomField(`x-birthday`, timeT)

Then `token.Get("x-birthday")` will still return an `interface{}`, but you can convert its type to `time.Time`

bdayif, _ := token.Get(`x-birthday`)
bday := bdayif.(time.Time)

Types

type AddressClaim

type AddressClaim struct {
	// contains filtered or unexported fields
}

AddressClaim is the address claim as described in https://openid.net/specs/openid-connect-core-1_0.html#AddressClaim

func NewAddress

func NewAddress() *AddressClaim

func (*AddressClaim) Accept

func (t *AddressClaim) Accept(v interface{}) error

func (AddressClaim) Country

func (t AddressClaim) Country() string

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 (AddressClaim) Formatted

func (t AddressClaim) Formatted() string

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 (*AddressClaim) Get

func (t *AddressClaim) Get(s string) (interface{}, bool)

func (AddressClaim) Locality

func (t AddressClaim) Locality() string

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 (AddressClaim) MarshalJSON

func (t AddressClaim) MarshalJSON() ([]byte, error)

MarshalJSON serializes the token in JSON format.

func (AddressClaim) PostalCode

func (t AddressClaim) PostalCode() string

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 (AddressClaim) Region

func (t AddressClaim) Region() string

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 (*AddressClaim) Set

func (t *AddressClaim) Set(key string, value interface{}) error

func (AddressClaim) StreetAddress

func (t AddressClaim) StreetAddress() string

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 (*AddressClaim) UnmarshalJSON

func (t *AddressClaim) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes data from a JSON data buffer into a AddressClaim

type BirthdateClaim

type BirthdateClaim struct {
	// contains filtered or unexported fields
}

func (*BirthdateClaim) Accept

func (b *BirthdateClaim) Accept(v interface{}) error

Accepts a value read from JSON, and converts it to a BirthdateClaim. This method DOES NOT verify the correctness of a date. Consumers should check for validity of dates such as Apr 31 et al

func (BirthdateClaim) Day

func (b BirthdateClaim) Day() int

func (BirthdateClaim) MarshalText

func (b BirthdateClaim) MarshalText() ([]byte, error)

func (BirthdateClaim) Month

func (b BirthdateClaim) Month() int

func (BirthdateClaim) String

func (b BirthdateClaim) String() string

func (*BirthdateClaim) UnmarshalJSON

func (b *BirthdateClaim) UnmarshalJSON(data []byte) error

func (BirthdateClaim) Year

func (b BirthdateClaim) Year() int

type Builder added in v1.2.13

type Builder struct {
	// contains filtered or unexported fields
}

Builder is a convenience wrapper around the New() constructor and the Set() methods to assign values to Token claims. Users can successively call Claim() on the Builder, and have it construct the Token when Build() is called. This alleviates the need for the user to check for the return value of every single Set() method call. Note that each call to Claim() overwrites the value set from the previous call.

func NewBuilder added in v1.2.13

func NewBuilder() *Builder

func (*Builder) Address added in v1.2.13

func (b *Builder) Address(v *AddressClaim) *Builder

func (*Builder) Audience added in v1.2.13

func (b *Builder) Audience(v []string) *Builder

func (*Builder) Birthdate added in v1.2.13

func (b *Builder) Birthdate(v *BirthdateClaim) *Builder

func (*Builder) Build added in v1.2.13

func (b *Builder) Build() (Token, error)

Build creates a new token based on the claims that the builder has received so far. If a claim cannot be set, then the method returns a nil Token with a en error as a second return value

func (*Builder) Claim added in v1.2.13

func (b *Builder) Claim(name string, value interface{}) *Builder

func (*Builder) Email added in v1.2.13

func (b *Builder) Email(v string) *Builder

func (*Builder) EmailVerified added in v1.2.13

func (b *Builder) EmailVerified(v bool) *Builder

func (*Builder) Expiration added in v1.2.13

func (b *Builder) Expiration(v time.Time) *Builder

func (*Builder) FamilyName added in v1.2.13

func (b *Builder) FamilyName(v string) *Builder

func (*Builder) Gender added in v1.2.13

func (b *Builder) Gender(v string) *Builder

func (*Builder) GivenName added in v1.2.13

func (b *Builder) GivenName(v string) *Builder

func (*Builder) IssuedAt added in v1.2.13

func (b *Builder) IssuedAt(v time.Time) *Builder

func (*Builder) Issuer added in v1.2.13

func (b *Builder) Issuer(v string) *Builder

func (*Builder) JwtID added in v1.2.13

func (b *Builder) JwtID(v string) *Builder

func (*Builder) Locale added in v1.2.13

func (b *Builder) Locale(v string) *Builder

func (*Builder) MiddleName added in v1.2.13

func (b *Builder) MiddleName(v string) *Builder

func (*Builder) Name added in v1.2.13

func (b *Builder) Name(v string) *Builder

func (*Builder) Nickname added in v1.2.13

func (b *Builder) Nickname(v string) *Builder

func (*Builder) NotBefore added in v1.2.13

func (b *Builder) NotBefore(v time.Time) *Builder

func (*Builder) PhoneNumber added in v1.2.13

func (b *Builder) PhoneNumber(v string) *Builder

func (*Builder) PhoneNumberVerified added in v1.2.13

func (b *Builder) PhoneNumberVerified(v bool) *Builder

func (*Builder) Picture added in v1.2.13

func (b *Builder) Picture(v string) *Builder

func (*Builder) PreferredUsername added in v1.2.13

func (b *Builder) PreferredUsername(v string) *Builder

func (*Builder) Profile added in v1.2.13

func (b *Builder) Profile(v string) *Builder

func (*Builder) Subject added in v1.2.13

func (b *Builder) Subject(v string) *Builder

func (*Builder) UpdatedAt added in v1.2.13

func (b *Builder) UpdatedAt(v time.Time) *Builder

func (*Builder) Website added in v1.2.13

func (b *Builder) Website(v string) *Builder

func (*Builder) Zoneinfo added in v1.2.13

func (b *Builder) Zoneinfo(v string) *Builder

type ClaimPair

type ClaimPair = mapiter.Pair

type DecodeCtx added in v1.2.1

type DecodeCtx = json.DecodeCtx

type Iterator

type Iterator = mapiter.Iterator

type Token

type Token interface {
	Address() *AddressClaim
	Audience() []string
	Birthdate() *BirthdateClaim
	Email() string
	EmailVerified() bool
	Expiration() time.Time
	FamilyName() string
	Gender() string
	GivenName() string
	IssuedAt() time.Time
	Issuer() string
	JwtID() string
	Locale() string
	MiddleName() string
	Name() string
	Nickname() string
	NotBefore() time.Time
	PhoneNumber() string
	PhoneNumberVerified() bool
	Picture() string
	PreferredUsername() string
	Profile() string
	Subject() string
	UpdatedAt() time.Time
	Website() string
	Zoneinfo() string
	PrivateClaims() map[string]interface{}
	Get(string) (interface{}, bool)
	Set(string, interface{}) error
	Remove(string) error
	Clone() (jwt.Token, error)
	Iterate(context.Context) Iterator
	Walk(context.Context, Visitor) error
	AsMap(context.Context) (map[string]interface{}, error)
}

func New

func New() Token

New creates a standard token, with minimal knowledge of possible claims. Standard claims include"address", "aud", "birthdate", "email", "email_verified", "exp", "family_name", "gender", "given_name", "iat", "iss", "jti", "locale", "middle_name", "name", "nickname", "nbf", "phone_number", "phone_number_verified", "picture", "preferred_username", "profile", "sub", "updated_at", "website" and "zoneinfo". Convenience accessors are provided for these standard claims

type TokenWithDecodeCtx added in v1.2.1

type TokenWithDecodeCtx = json.DecodeCtxContainer

type Visitor

type Visitor = iter.MapVisitor

type VisitorFunc

type VisitorFunc = iter.MapVisitorFunc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL