auth

package
v0.0.3-rc4 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

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 FromId

func FromId(ctx context.Context) string

func FromMetadata

func FromMetadata(ctx context.Context) metadata.Metadata

func FromScopes

func FromScopes(ctx context.Context) []string

func FromSubject

func FromSubject(ctx context.Context) int64

func FromType

func FromType(ctx context.Context) string

func Marshal

func Marshal(message proto.Message) (string, error)

Marshal converts a protobuf message to a URL legal string.

func MustFromSubject

func MustFromSubject(ctx context.Context) int64

func NewContext

func NewContext(ctx context.Context, acc *Account) context.Context

NewContext put auth info into context

func Subject

func Subject(c *gin.Context) string

func Unmarshal

func Unmarshal(s string, message proto.Message) error

Unmarshal decodes a protobuf message.

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"`
}

func FromContext

func FromContext(ctx context.Context) (acc *Account, ok bool)

FromContext extract auth info from context

type Auth

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

Auth provides a Json-Web-Token authentication implementation.

func New

func New(p Provider, c Config) *Auth

New auth with Config

func (*Auth) ExtractToken

func (sf *Auth) ExtractToken(r *http.Request) (string, error)

ExtractToken extract token from http request

func (*Auth) GenerateRefreshToken

func (sf *Auth) GenerateRefreshToken(id string, acc *Account) (string, time.Time, error)

GenerateRefreshToken generate refresh token

func (*Auth) GenerateToken

func (sf *Auth) GenerateToken(id string, acc *Account) (string, time.Time, error)

GenerateToken generate token

func (*Auth) MaxTimeout

func (sf *Auth) MaxTimeout() time.Duration

MaxTimeout refresh timeout

func (*Auth) Middleware

func (sf *Auth) Middleware(opts ...Option) gin.HandlerFunc

func (*Auth) ParseFromRequest

func (sf *Auth) ParseFromRequest(r *http.Request) (*Account, error)

ParseFromRequest parse token to account from http request

func (*Auth) ParseToken

func (sf *Auth) ParseToken(token string) (*Account, error)

ParseToken parse token

func (*Auth) Timeout

func (sf *Auth) Timeout() time.Duration

Timeout token valid time

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 (sf *JwtProvider) GenerateRefreshToken(id string, acc *Account, timeout time.Duration) (string, time.Time, error)

func (*JwtProvider) GenerateToken

func (sf *JwtProvider) GenerateToken(id string, acc *Account, timeout time.Duration) (string, time.Time, error)

func (*JwtProvider) ParseToken

func (sf *JwtProvider) ParseToken(tokenString string) (*Account, error)

type Option

type Option func(*options)

Option is Middleware option.

func WithSkip

func WithSkip(f func(c *gin.Context) bool) Option

WithSkip set skip func

func WithUnauthorizedFallback

func WithUnauthorizedFallback(f func(c *gin.Context, err error)) Option

WithUnauthorizedFallback sets the fallback handler when requests are unauthorized.

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

func NewJwtProvider(c JwtConfig) (Provider, error)

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

Jump to

Keyboard shortcuts

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