/
jwt.go
69 lines (57 loc) · 1.56 KB
/
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
package conf
import (
"os"
"time"
"github.com/JosephJoshua/shin-psmapi/forms"
"github.com/JosephJoshua/shin-psmapi/models"
"github.com/JosephJoshua/shin-psmapi/utils"
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/gin-gonic/gin"
)
var JWTMiddleware *jwt.GinJWTMiddleware
func InitJWTMiddleware() (*jwt.GinJWTMiddleware, error) {
JWTMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{
Realm: utils.JWTRealm,
Key: []byte(os.Getenv("JWT_KEY")),
Timeout: time.Hour * 24,
MaxRefresh: time.Hour,
IdentityKey: utils.JWTIdentityKey,
Authenticator: authenticator,
Authorizator: authorizator,
Unauthorized: unauthorized,
PayloadFunc: payloadFunc,
LoginResponse: loginResponse,
TimeFunc: time.Now,
SendCookie: true,
TokenLookup: utils.JWTTokenLookup,
TokenHeadName: utils.JWTTokenHeadName,
})
return JWTMiddleware, err
}
func authenticator(c *gin.Context) (interface{}, error) {
var form forms.LoginForm
if err := c.ShouldBindJSON(&form); err != nil {
return nil, err
}
return models.AuthenticateUser(form)
}
func authorizator(data interface{}, c *gin.Context) bool {
return true
}
func unauthorized(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"message": message,
})
}
func payloadFunc(data interface{}) jwt.MapClaims {
if user, ok := data.(models.User); ok {
return jwt.MapClaims{utils.JWTIdentityKey: user.ID}
}
return jwt.MapClaims{}
}
func loginResponse(c *gin.Context, code int, token string, expire time.Time) {
c.JSON(code, gin.H{
"expire": expire,
"token": token,
})
}