-
Notifications
You must be signed in to change notification settings - Fork 2
/
echo_jwt.go
120 lines (106 loc) · 3.92 KB
/
echo_jwt.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package utils
import (
"fmt"
"time"
"github.com/golang-jwt/jwt/v4"
echojwt "github.com/labstack/echo-jwt/v4"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// EchoJWT utility instance
var EchoJWT EchoJWTUtil
// EchoJWTUtil is a utility struct that provides methods
// for working with JWT tokens in the context of the Echo web framework
type EchoJWTUtil struct {
Config *EchoJWTConfig // The configuration for EchoJWTUtil
echoJWTConfig echojwt.Config // The configuration for the echojwt library
}
// JWTToken is a helper struct for returning signed JWT tokens
type JWTToken struct {
SignedString string // The signed token as a string
Claims jwt.RegisteredClaims // The claims included in the token
}
// EchoJWTConfig is the configuration struct for EchoJWTUtil
type EchoJWTConfig struct {
SigningKey string // The signing key used to sign JWT tokens
ExpiresTTL time.Duration // The duration until which the token should be valid
BeforeSuccessFunc func(token *jwt.Token, c echo.Context) error // A callback function to execute before a successful authentication
}
// New creates and returns a new instance of EchoJWTUtil
func (EchoJWTUtil) New(config *EchoJWTConfig) *EchoJWTUtil {
echoJWTUtil := &EchoJWTUtil{
Config: config,
echoJWTConfig: echojwt.Config{
SigningKey: []byte(config.SigningKey),
},
}
echoJWTUtil.echoJWTConfig.ParseTokenFunc = echoJWTUtil.ParseTokenFunc
return echoJWTUtil
}
// CreateToken creates and returns a new JWTToken
func (eJWT EchoJWTUtil) CreateToken(claims jwt.RegisteredClaims) JWTToken {
if claims.ID == "" {
claims.ID = String.UUID()
}
if claims.IssuedAt == nil {
claims.IssuedAt = jwt.NewNumericDate(time.Now())
}
if claims.ExpiresAt == nil {
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(eJWT.Config.ExpiresTTL))
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedToken, _ := token.SignedString([]byte(eJWT.Config.SigningKey))
return JWTToken{
SignedString: signedToken,
Claims: claims,
}
}
// KeyFunc is a helper function used by ParseToken
// to extract the signing key from the EchoJWTConfig object
func (eJWT EchoJWTUtil) KeyFunc(token *jwt.Token) (any, error) {
if token.Method.Alg() != jwt.SigningMethodHS256.Name {
return nil, fmt.Errorf("unexpected jwt signing method=%v", token.Header["alg"])
}
return []byte(eJWT.Config.SigningKey), nil
}
// ParseToken is a helper function used to parse
// and validate JWT tokens using the echo-jwt library
func (eJWT EchoJWTUtil) ParseToken(signedToken string) (*jwt.Token, error) {
token, err := jwt.ParseWithClaims(signedToken, &jwt.RegisteredClaims{}, eJWT.KeyFunc)
if err != nil {
return nil, err
}
return token, nil
}
// ParseTokenFunc is a callback function used to parse
// and validate JWT tokens within the context of the echo-jwt middleware
func (eJWT EchoJWTUtil) ParseTokenFunc(c echo.Context, auth string) (any, error) {
token, err := eJWT.ParseToken(auth)
if err != nil {
return nil, err
}
if eJWT.Config.BeforeSuccessFunc != nil {
if err := eJWT.Config.BeforeSuccessFunc(token, c); err != nil {
return nil, err
}
}
return token, nil
}
// JWTAuth returns a new instance of the echo-jwt middleware,
// configured with the current EchoJWTConfig object
func (eJWT EchoJWTUtil) JWTAuth() echo.MiddlewareFunc {
return echojwt.WithConfig(eJWT.echoJWTConfig)
}
// GetClaims retrieves and validates JWT claims.
// It takes a JWT token and returns the converted claims
func (eJWT EchoJWTUtil) GetClaims(token *jwt.Token) (*jwt.RegisteredClaims, error) {
claims, ok := token.Claims.(*jwt.RegisteredClaims)
if !ok {
return nil, fmt.Errorf("invalid token claims")
}
return claims, nil
}
// SetSkipper sets the middleware skipper function for the EchoJWTUtil instance.
func (eJWT *EchoJWTUtil) SetSkipper(skipper middleware.Skipper) {
eJWT.echoJWTConfig.Skipper = skipper
}