Documentation
¶
Index ¶
- Variables
- func FromCookie(r *http.Request, key string) (string, error)
- func FromHeader(r *http.Request, key, prefix string) (string, error)
- func FromQuery(r *http.Request, key string) (string, error)
- func Marshal(v any) (string, error)
- func NewContext[T any](ctx context.Context, claims *Claims[T]) context.Context
- func Unmarshal(s string, v any) error
- type ArgumentExtractor
- type Auth
- func (a *Auth[T]) ExtractToken(r *http.Request) (string, error)
- func (a *Auth[T]) GenerateRefreshToken(val *Claims[T]) (string, time.Time, error)
- func (a *Auth[T]) GenerateToken(val *Claims[T]) (string, time.Time, error)
- func (a *Auth[T]) MaxTimeout() time.Duration
- func (sf *Auth[T]) Middleware(opts ...Option) gin.HandlerFunc
- func (a *Auth[T]) ParseFromRequest(r *http.Request) (*Claims[T], error)
- func (p *Auth[T]) ParseToken(tokenString string) (*Claims[T], error)
- func (a *Auth[T]) Timeout() time.Duration
- type Claims
- type Config
- type CookieExtractor
- type Extractor
- type HeaderExtractor
- type Lookup
- type MultiExtractor
- type Option
- type TokenSubject
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidPubKey indicates the the given public key is invalid ErrInvalidPubKey = errors.New("public key invalid") // ErrInvalidPrivKey indicates that the given private key is invalid ErrInvalidPrivKey = errors.New("private key invalid") // ErrMissingSecretKey indicates Secret key is required ErrMissingSecretKey = errors.New("secret key is required") )
var ErrMissingValue = errors.New("no value present in request")
ErrMissingValue can be thrown by follow if value with a HTTP header, the value header needs to be set if value with URL Query, the query value variable is empty if value with a cookie, the value cookie is empty
Functions ¶
func FromCookie ¶
FromCookie get value from Cookie key is a cookie key
func FromHeader ¶
FromHeader get value from header key is a header key, like "Authorization" prefix is a string in the header, like "Bearer", if it is empty, only will return value.
func NewContext ¶
NewContext put auth info into context
Types ¶
type ArgumentExtractor ¶
type ArgumentExtractor string
ArgumentExtractor extracts a value from request arguments. This includes a POSTed form or GET URL arguments. This extractor calls `ParseMultipartForm` on the request
func (ArgumentExtractor) ExtractToken ¶
func (e ArgumentExtractor) ExtractToken(r *http.Request) (string, error)
type Auth ¶
type Auth[T any] struct { // contains filtered or unexported fields }
Auth provides a Json-Web-Token authentication implementation.
func (*Auth[T]) ExtractToken ¶
ExtractToken extract token from http request
func (*Auth[T]) GenerateRefreshToken ¶
GenerateRefreshToken generate refresh token
func (*Auth[T]) GenerateToken ¶
GenerateToken generate token
func (*Auth[T]) MaxTimeout ¶
MaxTimeout refresh timeout
func (*Auth[T]) Middleware ¶
func (sf *Auth[T]) Middleware(opts ...Option) gin.HandlerFunc
func (*Auth[T]) ParseFromRequest ¶
ParseFromRequest parse token to account from http request
func (*Auth[T]) ParseToken ¶
ParseToken parse token
type Claims ¶
type Claims[T any] struct { jwt.RegisteredClaims Meta T `json:"meta,omitempty"` }
Claims jwt claims
type Config ¶
type Config struct {
// Timeout token valid time
// if timeout <= refreshTimeout, refreshTimeout = timeout + 30 * time.Minute
Timeout time.Duration
// RefreshTimeout refresh token valid time.
RefreshTimeout time.Duration
// Lookup used to extract token from the http request
// lookup is a string in the form of "<source>:<name>[:<prefix>]" that is used
// to extract value from the request.
// use like "header:<name>[:<prefix>],query:<name>,cookie:<name>,param:<name>"
// Optional, Default value "header:Authorization:Bearer" for json web token.
// Possible values:
// - "header:<name>:<prefix>", <prefix> is a special string in the header, Possible value is "Bearer"
// - "query:<name>"
// - "cookie:<name>"
Lookup string
// 支持签名算法: HS256, HS384, HS512, RS256, RS384, RS512, EdDSA
// Optional, Default HS256.
Algorithm string
// Secret key used for signing.
// Required, if Algorithm is one of HS256, HS384, HS512.
Key []byte
// Private key for asymmetric algorithms,
// Public key for asymmetric algorithms
// Required, if Algorithm is one of RS256, RS384, RS512, EdDSA.
PrivKey, PubKey string
// the issuer of the jwt
Issuer string
}
Config Auth config
type CookieExtractor ¶
type CookieExtractor string
CookieExtractor extracts a value from cookie.
func (CookieExtractor) ExtractToken ¶
func (e CookieExtractor) ExtractToken(r *http.Request) (string, error)
type Extractor ¶
Extractor is an interface for extracting a value from an HTTP request. The ExtractToken method should return a value string or an error. If no value is present, you must return ErrMissingValue.
type HeaderExtractor ¶
type HeaderExtractor struct {
// The key of the header
// Required
Key string
// Strips 'Bearer ' prefix from bearer value string.
// Possible value is "Bearer"
// Optional
Prefix string
}
HeaderExtractor is an extractor for finding a value in a header. Looks at each specified header in order until there's a match
func (HeaderExtractor) ExtractToken ¶
func (e HeaderExtractor) ExtractToken(r *http.Request) (string, error)
type Lookup ¶
type Lookup struct {
// contains filtered or unexported fields
}
Lookup is a tool that looks up the value from http request, such as token
func NewLookup ¶
NewLookup new a lookup. lookup is a string in the form of "<source>:<name>[:<prefix>]" that is used to extract value from the request. use like "header:<name>[:<prefix>],query:<name>,cookie:<name>,param:<name>" Optional, Default value "header:Authorization:Bearer" for json web token. Possible values: - "header:<name>:<prefix>", <prefix> is a special string in the header, Possible value is "Bearer" - "query:<name>" - "cookie:<name>"
type MultiExtractor ¶
type MultiExtractor []Extractor
MultiExtractor tries Extractors in order until one returns a value string or an error occurs
func (MultiExtractor) ExtractToken ¶
func (e MultiExtractor) ExtractToken(req *http.Request) (string, error)
type TokenSubject ¶
type TokenSubject struct {
Sub string `json:"Sub,omitempty"`
ConnId string `json:"connId,omitempty"`
}
TokenSubject represents both the subject and connId which is returned as the "sub" claim in the Id Token.