Documentation ¶
Overview ¶
Package middleware defines multiple gin middlewares
Index ¶
- Constants
- Variables
- func Context() gin.HandlerFunc
- func Cors() gin.HandlerFunc
- func GetDefaultLogFormatterWithRequestID() gin.LogFormatter
- func GetLoggerConfig(formatter gin.LogFormatter, output io.Writer, skipPaths []string) gin.LoggerConfig
- func GetRequestIDFromContext(c *gin.Context) string
- func GetRequestIDFromHeaders(c *gin.Context) string
- func KeyExpired(expires int64) bool
- func Limit(maxEventsPerSec float64, maxBurstSize int) gin.HandlerFunc
- func Logger() gin.HandlerFunc
- func LoggerWithConfig(conf gin.LoggerConfig) gin.HandlerFunc
- func LoggerWithFormatter(f gin.LogFormatter) gin.HandlerFunc
- func LoggerWithWriter(out io.Writer, notlogged ...string) gin.HandlerFunc
- func NoCache(c *gin.Context)
- func Options(c *gin.Context)
- func Publish() gin.HandlerFunc
- func RequestID() gin.HandlerFunc
- func Secure(c *gin.Context)
- func Validation() gin.HandlerFunc
- type AuthInterface
- type AuthMiddleware
- type CacheAuthInterface
- type JWTAuthInterface
Constants ¶
const (
// AuthzAudience defines the value of jwt audience field.
AuthzAudience = "iam.authz.marmotedu.com"
)
const (
// XRequestIDKey defines X-Request-ID key string.
XRequestIDKey = "X-Request-ID"
)
Variables ¶
var ( ErrMissingKID = errors.New("Invalid token format: missing kid field in claims") ErrMissingSecret = errors.New("Can not obtain secret information from cache") )
Defined errors.
var ErrorLimitExceeded = errors.New("Limit exceeded")
ErrorLimitExceeded defines Limit exceeded error.
var Middlewares = defaultMiddlewares()
Middlewares store registered middlewares.
Functions ¶
func Context ¶
func Context() gin.HandlerFunc
Context is a middleware that injects common prefix fields to gin.Context.
func GetDefaultLogFormatterWithRequestID ¶
func GetDefaultLogFormatterWithRequestID() gin.LogFormatter
GetDefaultLogFormatterWithRequestID returns gin.LogFormatter with 'RequestID'.
func GetLoggerConfig ¶
func GetLoggerConfig(formatter gin.LogFormatter, output io.Writer, skipPaths []string) gin.LoggerConfig
GetLoggerConfig return gin.LoggerConfig which will write the logs to specified io.Writer with given gin.LogFormatter. By default gin.DefaultWriter = os.Stdout reference: https://github.com/gin-gonic/gin#custom-log-format
func GetRequestIDFromContext ¶
GetRequestIDFromContext returns 'RequestID' from the given context if present.
func GetRequestIDFromHeaders ¶
GetRequestIDFromHeaders returns 'RequestID' from the headers if present.
func KeyExpired ¶
KeyExpired checks if a key has expired, if the value of user.SessionState.Expires is 0, it will be ignored.
func Limit ¶
func Limit(maxEventsPerSec float64, maxBurstSize int) gin.HandlerFunc
Limit drops (HTTP status 429) the request if the limit is reached.
func Logger ¶
func Logger() gin.HandlerFunc
Logger instances a Logger middleware that will write the logs to gin.DefaultWriter. By default gin.DefaultWriter = os.Stdout.
func LoggerWithConfig ¶
func LoggerWithConfig(conf gin.LoggerConfig) gin.HandlerFunc
LoggerWithConfig instance a Logger middleware with config.
func LoggerWithFormatter ¶
func LoggerWithFormatter(f gin.LogFormatter) gin.HandlerFunc
LoggerWithFormatter instance a Logger middleware with the specified log format function.
func LoggerWithWriter ¶
func LoggerWithWriter(out io.Writer, notlogged ...string) gin.HandlerFunc
LoggerWithWriter instance a Logger middleware with the specified writer buffer. Example: os.Stdout, a file opened in write mode, a socket...
func NoCache ¶
NoCache is a middleware function that appends headers to prevent the client from caching the HTTP response.
func Options ¶
Options is a middleware function that appends headers for options requests and aborts then exits the middleware chain and ends the request.
func Publish ¶
func Publish() gin.HandlerFunc
Publish publish a redis event to specified redis channel when some action occurred.
func RequestID ¶
func RequestID() gin.HandlerFunc
RequestID is a middleware that injects a 'X-Request-ID' into the context and request/response header of each request.
func Validation ¶
func Validation() gin.HandlerFunc
Validation make sure users have the right resource permission and operation.
Types ¶
type AuthInterface ¶
type AuthInterface interface { BasicAuth() gin.HandlerFunc JWTAuth() JWTAuthInterface }
AuthInterface defines interface with basic and jwt authentication method.
type AuthMiddleware ¶
type AuthMiddleware struct { JWT *ginjwt.GinJWTMiddleware // contains filtered or unexported fields }
AuthMiddleware defines authentication middleware struct.
func NewAuthMiddleware ¶
func NewAuthMiddleware(auth AuthInterface, cacheClient CacheAuthInterface) (*AuthMiddleware, error)
NewAuthMiddleware returns a new authentication middleware.
func (*AuthMiddleware) AuthCacheMiddlewareFunc ¶
func (a *AuthMiddleware) AuthCacheMiddlewareFunc() gin.HandlerFunc
AuthCacheMiddlewareFunc defines authentication middleware form authzserver.
func (*AuthMiddleware) AuthMiddlewareFunc ¶
func (a *AuthMiddleware) AuthMiddlewareFunc() gin.HandlerFunc
AuthMiddlewareFunc defines authentication middleware which can deal username/password and jwt at the same time.
type CacheAuthInterface ¶
type CacheAuthInterface interface {
GetSecret(secretID string) (*pb.SecretInfo, error)
}
CacheAuthInterface authentication interface for authzserver.
type JWTAuthInterface ¶
type JWTAuthInterface interface { Realm() string Key() []byte Timeout() time.Duration MaxRefresh() time.Duration Authenticator() func(c *gin.Context) (interface{}, error) LoginResponse() func(*gin.Context, int, string, time.Time) LogoutResponse() func(c *gin.Context, code int) PayloadFunc() func(data interface{}) ginjwt.MapClaims IdentityHandler() func(*gin.Context) interface{} Authorizator() func(data interface{}, c *gin.Context) bool }
JWTAuthInterface defines jwt authentication interface.