router

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: Apache-2.0 Imports: 10 Imported by: 4

Documentation

Index

Constants

View Source
const DefaultExpiration = 30 * time.Second

Variables

This section is empty.

Functions

func ExtractToken

func ExtractToken(headerValue string) (string, error)

ExtractToken extracts the token from the Authorization header.

func RegisterAuthErrCode added in v0.10.0

func RegisterAuthErrCode(scenario AuthErrorScenario, errCode string)

RegisterAuthErrCode allows the registration of an error code for a specific AuthErrorScenario.

func RegisterAuthMsgID added in v0.10.0

func RegisterAuthMsgID(scenario AuthErrorScenario, msgID int)

RegisterAuthMsgID allows the registration of a message ID for a specific AuthErrorScenario.

func RegisterMiddlewareErrCode added in v0.11.0

func RegisterMiddlewareErrCode(scenario MiddlewareErrorScenario, errCode string)

func RegisterMiddlewareMsgID added in v0.11.0

func RegisterMiddlewareMsgID(scenario MiddlewareErrorScenario, msgID int)

func RequestLoggerMiddleware added in v0.4.0

func RequestLoggerMiddleware(logger logger.Logger) gin.HandlerFunc

func SetDefaultErrCode added in v0.10.0

func SetDefaultErrCode(errCode string)

SetDefaultErrCode allows external code to set a custom default error code.

func SetDefaultMsgID added in v0.10.0

func SetDefaultMsgID(msgID int)

SetDefaultMsgID allows external code to set a custom default message ID.

func SetupRouter

func SetupRouter(useOIDCAuth bool, l logger.Logger, authMiddleware *AuthMiddleware) (*gin.Engine, error)

func TimeoutMiddleware added in v0.11.0

func TimeoutMiddleware(timeout time.Duration) gin.HandlerFunc

Types

type AuthErrorScenario added in v0.10.0

type AuthErrorScenario string

AuthErrorScenario defines a set of constants representing different error scenarios that can occur within the AuthMiddleware. These scenarios are used to map specific error conditions to message IDs and error codes.

const (
	// TokenMissing indicates an error scenario where the expected authentication token is missing from the request.
	TokenMissing AuthErrorScenario = "TokenMissing"
	// TokenCacheFailed indicates an error scenario where an operation related to caching the token fails.
	TokenCacheFailed AuthErrorScenario = "TokenCacheFailed"
	// TokenVerificationFailed indicates an error scenario where the authentication token fails verification.
	TokenVerificationFailed AuthErrorScenario = "TokenVerificationFailed"
)

type AuthMiddleware

type AuthMiddleware struct {
	Verifier *oidc.IDTokenVerifier
	Cache    TokenCache
	Logger   logger.Logger
}

func LoadAuthMiddleware

func LoadAuthMiddleware(clientID string, providerURL string, cache TokenCache, l logger.Logger) (*AuthMiddleware, error)

func NewAuthMiddleware

func NewAuthMiddleware(clientID string, provider *oidc.Provider, cache TokenCache, logger logger.Logger) (*AuthMiddleware, error)

func (*AuthMiddleware) MiddlewareFunc

func (a *AuthMiddleware) MiddlewareFunc() gin.HandlerFunc

MiddlewareFunc returns a gin.HandlerFunc (middleware) that checks for a valid token.

type GinContext

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

GinContext is an adapter that implements the Context interface for Gin.

func (*GinContext) Bind

func (gc *GinContext) Bind(obj any) error

Bind binds the request body to obj.

func (*GinContext) BindJSON

func (gc *GinContext) BindJSON(obj any) error

BindJSON binds the JSON request body into obj.

func (*GinContext) JSON

func (gc *GinContext) JSON(code int, obj any)

JSON sends a JSON response.

func (*GinContext) Request

func (gc *GinContext) Request() *http.Request

Request returns the underlying http.Request.

type GinRouter

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

GinRouter implements the Router interface for Gin framework.

func NewGinRouter

func NewGinRouter() *GinRouter

NewGinRouter is a constructor for GinRouter.

func (*GinRouter) Serve

func (gr *GinRouter) Serve(address string) error

Serve starts the HTTP server at the specified address.

type MiddlewareErrorScenario added in v0.11.0

type MiddlewareErrorScenario string
const (
	RequestTimeout MiddlewareErrorScenario = "RequestTimeout"
)

type RedisTokenCache

type RedisTokenCache struct {
	Client     *redis.Client
	Ctx        context.Context
	Expiration time.Duration
}

RedisTokenCache is a Redis implementation of TokenCache.

func (*RedisTokenCache) Get

func (r *RedisTokenCache) Get(token string) (bool, error)

Get gets a token from the cache.

func (*RedisTokenCache) Set

func (r *RedisTokenCache) Set(token string) error

Set sets a token in the cache.

type TokenCache

type TokenCache interface {
	Get(token string) (bool, error)
	Set(token string) error
}

TokenCache is an interface for caching tokens.

func NewRedisTokenCache

func NewRedisTokenCache(addr string, password string, db int, expiration time.Duration) TokenCache

Jump to

Keyboard shortcuts

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