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 ¶
- type Gatekeeper
- func (s *Gatekeeper[T]) ConsumeAuthCode(ctx context.Context, code string) (userId T, err error)
- func (s *Gatekeeper[T]) CreateAccessToken(ctx context.Context, code string, payload ...[]byte) (tok string, cookie bool, err error)
- func (s *Gatekeeper[T]) CreateAuthCode(ctx context.Context, userId T, expiry time.Duration, cookie bool) (code string, err error)
- func (s *Gatekeeper[T]) CreateTokenWithId(id hexid.ID, payload ...[]byte) (string, error)
- func (g *Gatekeeper[T]) OptionalPermTag() bool
- func (g *Gatekeeper[T]) PreRequest(c *fasthttp.RequestCtx) (err error)
- func (s *Gatekeeper[T]) SecurityRequirement(perm security.Permission) openapi.SecurityRequirement
- func (s *Gatekeeper[T]) SecurityScheme() openapi.SecurityScheme
- func (s *Gatekeeper[T]) UserRoles(c *fasthttp.RequestCtx) (roles []string, err error)
- type GatekeeperOptions
- type OneTimeCode
- type Secret
- func (s Secret) AppendBinary(b []byte) ([]byte, error)
- func (s Secret) AppendText(b []byte) ([]byte, error)
- func (s *Secret) FromString(str string) error
- func (t Secret) MarshalBinary() (data []byte, err error)
- func (s Secret) MarshalText() (text []byte, err error)
- func (s Secret) String() string
- func (s *Secret) UnmarshalBinary(data []byte) error
- func (s *Secret) UnmarshalText(text []byte) (err error)
- type Store
- type Token
- func (t Token) AppendBinary(b []byte) ([]byte, error)
- func (t Token) AppendText(b []byte) ([]byte, error)
- func (t *Token) FromString(str string) error
- func (t Token) Id() hexid.ID
- func (t Token) IsZero() bool
- func (t Token) MarshalBinary() (data []byte, err error)
- func (t Token) MarshalText() (text []byte, err error)
- func (t Token) Payload() [24]byte
- func (t Token) String() string
- func (t *Token) UnmarshalBinary(data []byte) error
- func (t *Token) UnmarshalText(text []byte) (err error)
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 (*Gatekeeper[T]) CreateTokenWithId ¶ added in v0.13.0
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 SecretFromString ¶
func (*Secret) FromString ¶
func (Secret) MarshalBinary ¶
func (Secret) MarshalText ¶
func (*Secret) UnmarshalBinary ¶
func (*Secret) UnmarshalText ¶
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) FromString ¶
func (Token) MarshalBinary ¶
func (Token) MarshalText ¶
func (*Token) UnmarshalBinary ¶
func (*Token) UnmarshalText ¶
Click to show internal directories.
Click to hide internal directories.