diff --git a/gin.go b/gin.go index 287129f..ccbaea3 100644 --- a/gin.go +++ b/gin.go @@ -20,6 +20,7 @@ func New(setters ...options.Option) gin.HandlerFunc { return toGinHandler(oidcHandler.ParseToken, setters...) } +// onError is called when there's an error. func onError(c *gin.Context, errorHandler options.ErrorHandler, statusCode int, description options.ErrorDescription, err error) { c.AbortWithStatusJSON(statusCode, gin.H{"error": err.Error()}) } @@ -28,32 +29,32 @@ func onError(c *gin.Context, errorHandler options.ErrorHandler, statusCode int, func toGinHandler(parseToken oidc.ParseTokenFunc, setters ...options.Option) gin.HandlerFunc { opts := options.New(setters...) - return func(c *gin.Context) { - ctx := c.Request.Context() + var errs []ginerror.Error + return func(c *gin.Context) { tokenString, err := oidc.GetTokenString(c.Request.Header.Get, opts.TokenString) if err != nil { if !opts.Permissive { onError(c, opts.ErrorHandler, http.StatusBadRequest, options.GetTokenErrorDescription, err) return } else { - c.Set(string(opts.ErrorsContextKeyName), ginerror.Error{ + errs = append(errs, ginerror.Error{ Description: string(options.GetTokenErrorDescription), Error: err, }) - + c.Set(string(opts.ErrorsContextKeyName), errs) c.Next() return } } - token, err := parseToken(ctx, tokenString) + token, err := parseToken(c, tokenString) if err != nil { onError(c, opts.ErrorHandler, http.StatusUnauthorized, options.ParseTokenErrorDescription, err) return } - tokenClaims, err := token.AsMap(ctx) + tokenClaims, err := token.AsMap(c) if err != nil { onError(c, opts.ErrorHandler, http.StatusUnauthorized, options.ConvertTokenErrorDescription, err) return