MCPcopy
hub / github.com/appleboy/gin-jwt / main

Function main

_example/redis_simple/main.go:22–161  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

20var identityKey = "id"
21
22func main() {
23 r := gin.Default()
24
25 // Create JWT middleware configuration
26 middleware := &jwt.GinJWTMiddleware{
27 Realm: "test zone",
28 Key: []byte("secret key"),
29 Timeout: time.Hour,
30 MaxRefresh: time.Hour,
31 IdentityKey: identityKey,
32
33 PayloadFunc: func(data any) gojwt.MapClaims {
34 if v, ok := data.(*User); ok {
35 return gojwt.MapClaims{
36 identityKey: v.UserName,
37 }
38 }
39 return gojwt.MapClaims{}
40 },
41
42 IdentityHandler: func(c *gin.Context) any {
43 claims := jwt.ExtractClaims(c)
44 return &User{
45 UserName: claims[identityKey].(string),
46 }
47 },
48
49 Authenticator: func(c *gin.Context) (any, error) {
50 var loginVals User
51 if err := c.ShouldBind(&loginVals); err != nil {
52 return "", jwt.ErrMissingLoginValues
53 }
54 userID := loginVals.UserName
55 password := loginVals.Password
56
57 if (userID == "admin" && password == "admin") ||
58 (userID == "test" && password == "test") {
59 return &User{
60 UserName: userID,
61 LastName: "Bo-Yi",
62 FirstName: "Wu",
63 }, nil
64 }
65
66 return nil, jwt.ErrFailedAuthentication
67 },
68
69 Authorizer: func(c *gin.Context, data any) bool {
70 if v, ok := data.(*User); ok && v.UserName == "admin" {
71 return true
72 }
73 return false
74 },
75
76 Unauthorized: func(c *gin.Context, code int, message string) {
77 c.JSON(code, gin.H{
78 "code": code,
79 "message": message,

Callers

nothing calls this directly

Calls 3

EnableRedisStoreMethod · 0.95
MiddlewareInitMethod · 0.80
MiddlewareFuncMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…