token

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Example (Auth)
s, err := GenerateSecret()

if err != nil {
	panic(err)
}

a := auth{secret: s}
tok, err := a.CreateToken()

if err != nil {
	panic(err)
}

fmt.Println(tok)
fmt.Println(a.ValidateToken(tok))
fmt.Println(tok.Payload())
Output:

TODO

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gatekeeper added in v0.13.0

type Gatekeeper[T any] struct {
	// contains filtered or unexported fields
}

func NewGatekeeper added in v0.13.0

func NewGatekeeper[T any](secret Secret, store Store[T], opt ...GatekeeperOptions) *Gatekeeper[T]

func (*Gatekeeper[T]) ConsumeAuthCode added in v0.16.0

func (s *Gatekeeper[T]) ConsumeAuthCode(ctx context.Context, code string) (userId T, err error)

Consumes an auth code and returns the user ID associated with the auth code

func (*Gatekeeper[T]) CreateAccessToken added in v0.13.0

func (s *Gatekeeper[T]) CreateAccessToken(ctx context.Context, code string, payload ...[]byte) (tok string, cookie bool, err error)

Create a token with an optional payload that will be stored in the token. The payload cannot exceed 24 bytes, and will be padded with random bytes.

func (*Gatekeeper[T]) CreateAuthCode added in v0.13.0

func (s *Gatekeeper[T]) CreateAuthCode(ctx context.Context, userId T, expiry time.Duration, cookie bool) (code string, err error)

func (*Gatekeeper[T]) CreateTokenWithId added in v0.13.0

func (s *Gatekeeper[T]) CreateTokenWithId(id hexid.ID, payload ...[]byte) (string, error)

Create a token with a specific ID and an optional payload (e.g. a user ID) that will be stored in the token. The payload cannot exceed 24 bytes, and will be padded with random bytes.

func (*Gatekeeper[T]) OptionalPermTag added in v0.13.0

func (g *Gatekeeper[T]) OptionalPermTag() bool

OptionalPermTag implements security.Gatekeeper.

func (*Gatekeeper[T]) PreRequest added in v0.15.0

func (g *Gatekeeper[T]) PreRequest(c *fasthttp.RequestCtx) (err error)

PreRequest implements security.Gatekeeper.

func (*Gatekeeper[T]) SecurityRequirement added in v0.13.0

func (s *Gatekeeper[T]) SecurityRequirement(perm security.Permission) openapi.SecurityRequirement

OperationSecurityDocs implements security.Gatekeeper.

func (*Gatekeeper[T]) SecurityScheme added in v0.13.0

func (s *Gatekeeper[T]) SecurityScheme() openapi.SecurityScheme

SecurityDocs implements security.Gatekeeper.

func (*Gatekeeper[T]) UserRoles added in v0.13.0

func (s *Gatekeeper[T]) UserRoles(c *fasthttp.RequestCtx) (roles []string, err error)

type GatekeeperOptions added in v0.20.3

type GatekeeperOptions struct {
	PreRequest      func(c *fasthttp.RequestCtx, tok Token) error
	OptionalPermTag bool
}

type OneTimeCode

type OneTimeCode [20]byte
Example
fmt.Println(CreateOneTimeCode())
Output:

TODO

func CreateOneTimeCode

func CreateOneTimeCode() (otc OneTimeCode, err error)

func (*OneTimeCode) FromString

func (t *OneTimeCode) FromString(str string) error

func (OneTimeCode) MarshalText

func (t OneTimeCode) MarshalText() (text []byte, err error)

func (OneTimeCode) String

func (t OneTimeCode) String() string

func (*OneTimeCode) UnmarshalText

func (t *OneTimeCode) UnmarshalText(text []byte) (err error)

type Secret

type Secret [secretLen]byte
Example
s, err := GenerateSecret()

if err != nil {
	panic(err)
}

fmt.Println(s)
Output:

TODO

func GenerateSecret

func GenerateSecret() (s Secret, err error)

func SecretFromString

func SecretFromString(str string) (s Secret, err error)

func (Secret) AppendBinary

func (s Secret) AppendBinary(b []byte) ([]byte, error)

func (Secret) AppendText

func (s Secret) AppendText(b []byte) ([]byte, error)

func (*Secret) FromString

func (s *Secret) FromString(str string) error

func (Secret) MarshalBinary

func (t Secret) MarshalBinary() (data []byte, err error)

func (Secret) MarshalText

func (s Secret) MarshalText() (text []byte, err error)

func (Secret) String

func (s Secret) String() string

func (*Secret) UnmarshalBinary

func (s *Secret) UnmarshalBinary(data []byte) error

func (*Secret) UnmarshalText

func (s *Secret) UnmarshalText(text []byte) (err error)

type Store added in v0.13.0

type Store[T any] interface {

	// Looks up a token in the underlying token store, and returns its corresponding user roles.
	// A user can have 0+ roles. If the token doesn't exist in store and/or has been revoked, it MUST
	// return an error. The ctx MIGHT be a *papi.RequestCtx.
	UserRoles(ctx context.Context, tokId uint64) (roles []string, err error)

	// Consume an authentication code and returns its corresponding details.
	ConsumeAuthCode(ctx context.Context, code string) (userId T, cookie bool, err error)

	// Save an authentication code to a storage.
	SaveAuthCode(ctx context.Context, userId T, code string, expiry time.Time, cookie bool) error

	// Save an access token to a storage. Only the ID of the token should be saved, not the whole token.
	SaveAccessToken(ctx context.Context, userId T, tokId uint64, cookie bool) error
}

type Token

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

func (Token) AppendBinary

func (t Token) AppendBinary(b []byte) ([]byte, error)

func (Token) AppendText

func (t Token) AppendText(b []byte) ([]byte, error)

func (*Token) FromString

func (t *Token) FromString(str string) error

func (Token) Id

func (t Token) Id() hexid.ID

func (Token) IsZero added in v0.20.4

func (t Token) IsZero() bool

func (Token) MarshalBinary

func (t Token) MarshalBinary() (data []byte, err error)

func (Token) MarshalText

func (t Token) MarshalText() (text []byte, err error)

func (Token) Payload

func (t Token) Payload() [24]byte

The payload can be any additional data you have stored, e.g. a user ID.

func (Token) String

func (t Token) String() string

func (*Token) UnmarshalBinary

func (t *Token) UnmarshalBinary(data []byte) error

func (*Token) UnmarshalText

func (t *Token) UnmarshalText(text []byte) (err error)

Jump to

Keyboard shortcuts

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