-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
50 lines (48 loc) · 1.43 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
package main
import (
"github.com/appleboy/gin-jwt/v2"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"time"
)
func Authenticator(c *gin.Context) (i interface{}, err error) {
var loginValues User
if err := c.ShouldBindBodyWith(&loginValues, binding.JSON); err != nil {
return "", jwt.ErrMissingLoginValues
}
c.Set("user", &loginValues)
return AuthorizeLogin(&loginValues)
}
func PayloadFunc(data interface{}) jwt.MapClaims {
return jwt.MapClaims{
"UserName": data.(*UserPayLoad).UserName,
"id": data.(*UserPayLoad).id,
"Email": data.(*UserPayLoad).Email,
}
}
func AuthMiddleware(key string) (*jwt.GinJWTMiddleware, error) {
authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{
Realm: "Blog-server",
Key: []byte(key),
Authenticator: Authenticator,
LoginResponse: LoginResponse,
PayloadFunc: PayloadFunc,
LogoutResponse: nil,
Authorizator: Authorizator,
RefreshResponse: nil,
IdentityHandler: nil,
TokenLookup: "",
TokenHeadName: "",
Timeout: time.Hour * 24 * 356,
TimeFunc: nil,
HTTPStatusMessageFunc: nil,
SendCookie: false,
SecureCookie: false,
CookieHTTPOnly: false,
CookieDomain: "",
SendAuthorization: false,
DisabledAbort: false,
CookieName: "",
})
return authMiddleware, err
}