ginmiddleware

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: MIT Imports: 20 Imported by: 2

Documentation

Index

Constants

View Source
const (
	AuthPayload = "auth-payload"

	ACCESS_TOKEN_SUBJECT  = "access_token"
	REFRESH_TOKEN_SUBJECT = "refresh_token"
)
View Source
const (
	RequestLogChar  string = "RequestLog"
	ResponseLogChar string = "ResponseLog"
	TimeStamp       string = "TimeStamp"
	Method          string = "Method"
	Host            string = "Host"
	Uri             string = "Uri"
	ReqHeader       string = "ReqHeader"
	ReqBody         string = "ReqBody"
	RespHeader      string = "RespHeader"
	RespBody        string = "RespBody"
	StatusCode      string = "StatusCode"
	Latency         string = "Latency"
)
View Source
const (
	ContextKey = "contextMetaKey"
)
View Source
const (
	DebugStack string = "DebugStack"
)

Variables

View Source
var (
	JWTHeaderAuthToken   = HeaderAuthToken{Key: "Authorization", ValueHeader: "bearer"}
	BasicHeaderAuthToken = HeaderAuthToken{Key: "Authorization", ValueHeader: "basic"}
	JWTQueryAuthToken    = QueryAuthToken{Key: "access_token"}
)
View Source
var (
	ErrorHeaderAuthFormat = errors.New("error header auth format")
)
View Source
var HeaderXRequestID string = "X-Request-ID"

Functions

func AddRequestID

func AddRequestID() gin.HandlerFunc

func CtxLogger

func CtxLogger(ctx context.Context) *zerolog.Logger

CtxLogger put the ContextMetaData of incoming context into log

func GetAuthPayload added in v0.0.2

func GetAuthPayload(c *gin.Context) (interface{}, bool)

func InjectContextRequestHeader

func InjectContextRequestHeader(ctx context.Context, req *http.Request)

func NewAuthMiddleware added in v0.0.2

func NewAuthMiddleware(getToken IgetAuthToken, authenticator IAuthenticator) gin.HandlerFunc

func NewContextWithMeta

func NewContextWithMeta(ctx context.Context) context.Context

NewContextWithMeta new a context with incoming context metadata when we want to run a go routine in background, we should use this function and also can get those metadata from output context

func Recovery

func Recovery(writer io.Writer) gin.HandlerFunc

func WithMoreContextMeta

func WithMoreContextMeta(ctx context.Context, data ...ContextMetaData) context.Context

WithMoreContextMeta add extra ContextMetaData to incoming context

Types

type AuthMiddleware

type AuthMiddleware struct {
	GetToken      IgetAuthToken
	Authenticator IAuthenticator
}

func (*AuthMiddleware) ValidateAuth

func (auth *AuthMiddleware) ValidateAuth() gin.HandlerFunc

type CommonJwtAuthenticator added in v0.0.3

type CommonJwtAuthenticator struct {
	Options  []jwt.ParserOption
	JwtParse utils.IJwtParser
}

func (*CommonJwtAuthenticator) Authenticate added in v0.0.3

func (auth *CommonJwtAuthenticator) Authenticate(token string) (interface{}, *Error)

type ContextMetaData

type ContextMetaData struct {
	Key   string
	Value string
}

func GetContextMetaData

func GetContextMetaData(ctx context.Context) []ContextMetaData

GetContextMetaData get GetContextMetaData slice from incoming context

type ContextMetaKey

type ContextMetaKey string

type CustomJwtAuthenticator added in v0.0.3

type CustomJwtAuthenticator struct {
	Audience string
	Issuer   string
	Subject  string
	JwtParse utils.IJwtParser
}

func (*CustomJwtAuthenticator) Authenticate added in v0.0.3

func (auth *CustomJwtAuthenticator) Authenticate(token string) (interface{}, *Error)

type Error

type Error struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func (*Error) Error

func (e *Error) Error() string

type HeaderAuthToken

type HeaderAuthToken struct {
	Key         string
	ValueHeader string
}

func (*HeaderAuthToken) GetAuthToken

func (h *HeaderAuthToken) GetAuthToken(r *http.Request) (string, error)

type IAuthenticator

type IAuthenticator interface {
	Authenticate(token string) (interface{}, *Error)
}

type IgetAuthToken

type IgetAuthToken interface {
	GetAuthToken(*http.Request) (string, error)
}

func NewCustomGetAuthToken added in v0.0.3

func NewCustomGetAuthToken(fun func(*http.Request) (string, error)) IgetAuthToken

type M2mClaims added in v0.0.3

type M2mClaims struct {
	jwt.RegisteredClaims
	Scp string `json:"scp,omitempty"`
}

type M2mJwtAuthenticator added in v0.0.3

type M2mJwtAuthenticator struct {
	Audience string
	Issuer   string
	Scope    string
	JwtParse utils.IJwtParser
}

func (*M2mJwtAuthenticator) Authenticate added in v0.0.3

func (auth *M2mJwtAuthenticator) Authenticate(token string) (interface{}, *Error)

type MyCustomClaims added in v0.0.3

type MyCustomClaims struct {
	Uid string `json:"uid"`
	jwt.RegisteredClaims
}

type PathSettings

type PathSettings struct {
	Path          string
	DisableLogs   bool
	DisableBodies bool
}

type QueryAuthToken

type QueryAuthToken struct {
	Key string
}

func (*QueryAuthToken) GetAuthToken

func (q *QueryAuthToken) GetAuthToken(r *http.Request) (string, error)

type RequestLog

type RequestLog struct {
	Output       io.Writer
	Settings     []PathSettings
	MaskedFields []string
	TimeFormat   string
}

func (*RequestLog) AddRequestLog

func (l *RequestLog) AddRequestLog() gin.HandlerFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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