-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmiddleware.go
52 lines (45 loc) · 1.12 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package echoserver
import (
"github.com/labstack/echo/v4"
)
// Config for middleware
type Config struct {
// keys stored in the context
TokenKey string
// defines a function to skip middleware.Returning true skips processing
// the middleware.
Skipper func(echo.Context) bool
}
var (
// DefaultConfig is the default middleware config.
DefaultConfig = Config{
TokenKey: "token",
Skipper: func(_ echo.Context) bool {
return false
},
}
)
// TokenHandler gets the token from request using default config
func TokenHandler() echo.MiddlewareFunc {
return TokenHandlerWithConfig(&DefaultConfig)
}
// TokenHandlerWithConfig gets the token from request with given config
func TokenHandlerWithConfig(cfg *Config) echo.MiddlewareFunc {
tokenKey := cfg.TokenKey
if tokenKey == "" {
tokenKey = DefaultConfig.TokenKey
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if cfg.Skipper != nil && cfg.Skipper(c) {
return next(c)
}
ti, err := eServer.ValidationBearerToken(c.Request())
if err != nil {
return err
}
c.Set(tokenKey, ti)
return next(c)
}
}
}