-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
62 lines (52 loc) · 1.57 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
55
56
57
58
59
60
61
62
package middleware
import(
"github.com/gin-gonic/gin"
"fmt"
"github.com/appleboy/gin-jwt"
"time"
)
func InitJwtMiddleware(c *gin.Context) *jwt.GinJWTMiddleware{
return &jwt.GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour,
Authenticator: func(userID string, password string, c *gin.Context) (string, bool) {
if (userID == "admin" && password == "admin") || (userID == "test" && password == "test") {
return userID, true
}
return userID, false
},
Authorizator: func(userID string, c *gin.Context) bool {
if userID == "admin" {
return true
}
return false
},
Unauthorized: func(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"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:Authorization",
// TokenLookup: "query:token",
// TokenLookup: "cookie:token",
// TokenHeadName is a string in the header. Default value is "Bearer"
TokenHeadName: "Bearer",
// 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,
}
}
func Auth(c *gin.Context) {
InitJwtMiddleware(c).LoginHandler(c)
fmt.Println("In auth middleware")
c.Next()
}