errors have to be an array after all

This commit is contained in:
Darko Luketic 2021-11-17 11:12:26 +01:00
parent 448ee700c8
commit ef64b6cff8

13
gin.go
View File

@ -20,6 +20,7 @@ func New(setters ...options.Option) gin.HandlerFunc {
return toGinHandler(oidcHandler.ParseToken, setters...) 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) { func onError(c *gin.Context, errorHandler options.ErrorHandler, statusCode int, description options.ErrorDescription, err error) {
c.AbortWithStatusJSON(statusCode, gin.H{"error": 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 { func toGinHandler(parseToken oidc.ParseTokenFunc, setters ...options.Option) gin.HandlerFunc {
opts := options.New(setters...) opts := options.New(setters...)
return func(c *gin.Context) { var errs []ginerror.Error
ctx := c.Request.Context()
return func(c *gin.Context) {
tokenString, err := oidc.GetTokenString(c.Request.Header.Get, opts.TokenString) tokenString, err := oidc.GetTokenString(c.Request.Header.Get, opts.TokenString)
if err != nil { if err != nil {
if !opts.Permissive { if !opts.Permissive {
onError(c, opts.ErrorHandler, http.StatusBadRequest, options.GetTokenErrorDescription, err) onError(c, opts.ErrorHandler, http.StatusBadRequest, options.GetTokenErrorDescription, err)
return return
} else { } else {
c.Set(string(opts.ErrorsContextKeyName), ginerror.Error{ errs = append(errs, ginerror.Error{
Description: string(options.GetTokenErrorDescription), Description: string(options.GetTokenErrorDescription),
Error: err, Error: err,
}) })
c.Set(string(opts.ErrorsContextKeyName), errs)
c.Next() c.Next()
return return
} }
} }
token, err := parseToken(ctx, tokenString) token, err := parseToken(c, tokenString)
if err != nil { if err != nil {
onError(c, opts.ErrorHandler, http.StatusUnauthorized, options.ParseTokenErrorDescription, err) onError(c, opts.ErrorHandler, http.StatusUnauthorized, options.ParseTokenErrorDescription, err)
return return
} }
tokenClaims, err := token.AsMap(ctx) tokenClaims, err := token.AsMap(c)
if err != nil { if err != nil {
onError(c, opts.ErrorHandler, http.StatusUnauthorized, options.ConvertTokenErrorDescription, err) onError(c, opts.ErrorHandler, http.StatusUnauthorized, options.ConvertTokenErrorDescription, err)
return return