-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauth.go
54 lines (48 loc) · 1.41 KB
/
auth.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
package main
import (
"time"
"github.com/gin-gonic/gin"
jwt "gopkg.in/appleboy/gin-jwt.v2"
)
// ClientJWTMiddleware ...
func ClientJWTMiddleware() *jwt.GinJWTMiddleware {
authMiddleware := &jwt.GinJWTMiddleware{
Realm: `Dolores Client Zone`,
Key: []byte(`DoloresKey`),
Timeout: 24 * time.Hour, // token 有效期一天
MaxRefresh: 7 * 24 * time.Hour, // 一周以内可以刷新
Authenticator: func(userID string, password string, c *gin.Context) (string, bool) {
id, err := org.AuthMember(userID, password)
return id, err == nil
},
TokenHeadName: `Dolores`,
Unauthorized: func(c *gin.Context, code int, msg string) {
c.JSON(code, map[string]string{
`message`: msg,
})
},
}
return authMiddleware
}
// ServerJWTMiddleware ...
func ServerJWTMiddleware() *jwt.GinJWTMiddleware {
authMiddleware := &jwt.GinJWTMiddleware{
Realm: `Dolores Admin Zone`,
Key: []byte(`DoloresKey`),
Timeout: 24 * time.Hour, // token 有效期一天
MaxRefresh: 7 * 24 * time.Hour, // 一周以内可以刷新
Authenticator: func(userID string, password string, c *gin.Context) (string, bool) {
if userID == `admin` && password == `dolores` {
return ``, true
}
return ``, false
},
TokenHeadName: `Dolores`,
Unauthorized: func(c *gin.Context, code int, msg string) {
c.JSON(code, map[string]string{
`message`: msg,
})
},
}
return authMiddleware
}