Documentation ¶
Index ¶
- Constants
- Variables
- func FromId(ctx context.Context) string
- func FromMetadata(ctx context.Context) metadata.Metadata
- func FromScopes(ctx context.Context) []string
- func FromSubject(ctx context.Context) int64
- func FromType(ctx context.Context) string
- func Marshal(message proto.Message) (string, error)
- func MustFromSubject(ctx context.Context) int64
- func NewContext(ctx context.Context, acc *Account) context.Context
- func Subject(c *gin.Context) string
- func Unmarshal(s string, message proto.Message) error
- type Account
- type Auth
- func (sf *Auth) ExtractToken(r *http.Request) (string, error)
- func (sf *Auth) GenerateRefreshToken(id string, acc *Account) (string, time.Time, error)
- func (sf *Auth) GenerateToken(id string, acc *Account) (string, time.Time, error)
- func (sf *Auth) MaxTimeout() time.Duration
- func (sf *Auth) Middleware(opts ...Option) gin.HandlerFunc
- func (sf *Auth) ParseFromRequest(r *http.Request) (*Account, error)
- func (sf *Auth) ParseToken(token string) (*Account, error)
- func (sf *Auth) Timeout() time.Duration
- type Claims
- type Config
- type JwtConfig
- type JwtProvider
- func (sf *JwtProvider) GenerateRefreshToken(id string, acc *Account, timeout time.Duration) (string, time.Time, error)
- func (sf *JwtProvider) GenerateToken(id string, acc *Account, timeout time.Duration) (string, time.Time, error)
- func (sf *JwtProvider) ParseToken(tokenString string) (*Account, error)
- type Option
- type Provider
- type TokenSubject
- func (*TokenSubject) Descriptor() ([]byte, []int)deprecated
- func (x *TokenSubject) GetConnId() string
- func (x *TokenSubject) GetUserId() string
- func (*TokenSubject) ProtoMessage()
- func (x *TokenSubject) ProtoReflect() protoreflect.Message
- func (x *TokenSubject) Reset()
- func (x *TokenSubject) String() string
Constants ¶
View Source
const TokenUniqueId = "dyn:auth:uniqueId" // nolint: revive
Variables ¶
View Source
var ( // ErrMissingToken can be thrown by follow // if authing with a HTTP header, the Auth header needs to be set // if authing with URL Query, the query token variable is empty // if authing with a cookie, the token cookie is empty ErrMissingToken = errors.New("no token present in request") // ErrInvalidToken indicates auth token is invalid ErrInvalidToken = errors.New("invalid token provided") // ErrTokenExpired indicates auth token is expired ErrTokenExpired = errors.New("token expired") // ErrInvalidToken indicates auth token is invalid ErrTokenParseFail = errors.New("parse JWT token failed") // ErrInvalidSigningAlgorithm indicates signing algorithm is invalid, // needs to be HS256, HS384, HS512, RS256, RS384 or RS512 ErrInvalidSigningAlgorithm = errors.New("invalid signing algorithm") // 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") )
View Source
var File_types_proto protoreflect.FileDescriptor
Functions ¶
func FromScopes ¶
func FromSubject ¶
func MustFromSubject ¶
func NewContext ¶
NewContext put auth info into context
Types ¶
type Account ¶
type Account struct { // UserId of the account UserId string `json:"subject,omitempty"` // Type of the account, client, service, user Type string `json:"type,omitempty"` // Issuer of the account Issuer string `json:"issuer,omitempty"` // Scopes the account has access to Scopes []string `json:"scopes,omitempty"` // Metadata Any other associated metadata Metadata metadata.Metadata `json:"metadata,omitempty"` }
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Auth provides a Json-Web-Token authentication implementation.
func (*Auth) ExtractToken ¶
ExtractToken extract token from http request
func (*Auth) GenerateRefreshToken ¶
GenerateRefreshToken generate refresh token
func (*Auth) GenerateToken ¶
GenerateToken generate token
func (*Auth) Middleware ¶
func (sf *Auth) Middleware(opts ...Option) gin.HandlerFunc
func (*Auth) ParseFromRequest ¶
ParseFromRequest parse token to account from http request
func (*Auth) ParseToken ¶
ParseToken parse token
type Claims ¶
type Claims struct { Type string `json:"type,omitempty"` Scopes []string `json:"scopes,omitempty"` Metadata metadata.Metadata `json:"metadata,omitempty"` jwt.RegisteredClaims }
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 // Issuer of the account Issuer string // Lookup used to extract token from the http request Lookup string }
Config Auth config
type JwtConfig ¶
type JwtConfig struct { // 支持签名算法: HS256, HS384, HS512, RS256, RS384 or RS512 // Optional, Default HS256. Algorithm string // Secret key used for signing. // Required, HS256, HS384, HS512. Key []byte // Private key for asymmetric algorithms, // Public key for asymmetric algorithms // Required, RS256, RS384 or RS512. PrivKey, PubKey string }
type JwtProvider ¶
type JwtProvider struct {
// contains filtered or unexported fields
}
func (*JwtProvider) GenerateRefreshToken ¶
func (*JwtProvider) GenerateToken ¶
func (*JwtProvider) ParseToken ¶
func (sf *JwtProvider) ParseToken(tokenString string) (*Account, error)
type Provider ¶
type Provider interface { GenerateToken(id string, acc *Account, timeout time.Duration) (string, time.Time, error) GenerateRefreshToken(id string, acc *Account, timeout time.Duration) (string, time.Time, error) ParseToken(token string) (*Account, error) }
func NewJwtProvider ¶
type TokenSubject ¶
type TokenSubject struct { UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` ConnId string `protobuf:"bytes,2,opt,name=conn_id,json=connId,proto3" json:"conn_id,omitempty"` // contains filtered or unexported fields }
TokenSubject represents both the subject and connId which is returned as the "sub" claim in the Id Token.
func (*TokenSubject) Descriptor
deprecated
func (*TokenSubject) Descriptor() ([]byte, []int)
Deprecated: Use TokenSubject.ProtoReflect.Descriptor instead.
func (*TokenSubject) GetConnId ¶
func (x *TokenSubject) GetConnId() string
func (*TokenSubject) GetUserId ¶
func (x *TokenSubject) GetUserId() string
func (*TokenSubject) ProtoMessage ¶
func (*TokenSubject) ProtoMessage()
func (*TokenSubject) ProtoReflect ¶
func (x *TokenSubject) ProtoReflect() protoreflect.Message
func (*TokenSubject) Reset ¶
func (x *TokenSubject) Reset()
func (*TokenSubject) String ¶
func (x *TokenSubject) String() string
Click to show internal directories.
Click to hide internal directories.