/
server.go
99 lines (88 loc) · 2.69 KB
/
server.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
package main
import (
"time"
"github.com/henrylee2cn/faygo"
"github.com/henrylee2cn/faygo/ext/middleware/jwt"
)
func helloHandler(c *faygo.Context) error {
claims := jwt.ExtractClaims(c)
return c.JSON(200, map[string]interface{}{
"userID": claims["id"],
"text": "Hello World.",
})
}
func main() {
r := faygo.New("jwt-test")
// the jwt middleware
authMiddleware := &jwt.FaygoJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Minute,
MaxRefresh: time.Minute * 3,
//登录响应
LoginResponse: func(c *faygo.Context, code int, token string, expire time.Time) error {
faygo.Debug("LoginResponse")
return c.JSON(code, faygo.Map{
"code": code,
"token": token,
"expire": expire.Format(time.RFC3339),
"custom": "custom info ",
})
},
/*PayloadFunc: func(data interface{}) map[string]interface{} {
return faygo.Map{
"custom2": "custom info2 ",
}
},*/
//认证
Authenticator: func(userId string, password string, c *faygo.Context) (interface{}, bool) {
faygo.Debug("Authenticator认证")
if (userId == "admin" && password == "admin") || (userId == "test" && password == "test") {
return userId, true
}
return userId, false
},
//授权
Authorizator: func(userId interface{}, c *faygo.Context) bool {
faygo.Debug("Authorizator 授权")
if userId == "admin" {
return true
}
return false
},
Unauthorized: func(c *faygo.Context, code int, message string) {
faygo.Debug("Unauthorized 未授权")
c.JSON(code, map[string]interface{}{
"code": code,
"message": message,
})
},
// TokenLookup is a string in the form of "<source>:<name>" that is used
// to extract token from the request.
// Optional. Default value "header:Authorization".
// Possible values:
// - "header:<name>"
// - "query:<name>"
// - "cookie:<name>"
TokenLookup: "header:token",
// TokenLookup: "query:token",
// TokenLookup: "cookie:token",
// TokenHeadName is a string in the header. Default value is "Bearer" 持票人
TokenHeadName: "",
// TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
TimeFunc: time.Now,
}
//调用初始设置函数,必须的。
err := authMiddleware.MiddlewareInit()
if err != nil {
faygo.Error(err)
}
r.POST("/login", faygo.HandlerFunc(authMiddleware.LoginHandler))
auth := r.Group("/auth")
//auth.Use(authMiddleware.MiddlewareFunc())
{
auth.GET("/hello", faygo.HandlerFunc(helloHandler)).Use(authMiddleware.MiddlewareFunc())
auth.GET("/refreshtoken", faygo.HandlerFunc(authMiddleware.RefreshHandler))
}
r.Run()
}