✨ init
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package middles
|
||||
|
||||
import (
|
||||
"frank/gin-login-register/config"
|
||||
"frank/gin-login-register/controllers"
|
||||
"frank/gin-login-register/models"
|
||||
jwt "github.com/appleboy/gin-jwt/v2"
|
||||
"github.com/gin-contrib/requestid"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InitAuthMiddlewares(controllers controllers.UserControllers) (*jwt.GinJWTMiddleware, error) {
|
||||
return jwt.New(&jwt.GinJWTMiddleware{
|
||||
IdentityKey: "id",
|
||||
Realm: "test-jwt",
|
||||
SigningAlgorithm: "HS256",
|
||||
Key: []byte(config.GetConfig().Jwt.Key),
|
||||
Timeout: time.Hour * time.Duration(config.GetConfig().Jwt.AccessAge),
|
||||
MaxRefresh: time.Hour * time.Duration(config.GetConfig().Jwt.RefreshAge),
|
||||
TokenLookup: "header: Authorization",
|
||||
TokenHeadName: "Bearer",
|
||||
TimeFunc: time.Now,
|
||||
Authenticator: controllers.Login,
|
||||
Authorizator: authorizedFunc,
|
||||
PayloadFunc: payloadFunc,
|
||||
LoginResponse: loginResponse,
|
||||
LogoutResponse: logoutResponse,
|
||||
Unauthorized: unauthorizedFunc,
|
||||
IdentityHandler: identityHandler,
|
||||
})
|
||||
}
|
||||
|
||||
func authorizedFunc(data interface{}, c *gin.Context) bool {
|
||||
claims := jwt.ExtractClaims(c)
|
||||
return data == claims["id"]
|
||||
}
|
||||
|
||||
func identityHandler(c *gin.Context) interface{} {
|
||||
claims := jwt.ExtractClaims(c)
|
||||
return claims["id"]
|
||||
}
|
||||
func payloadFunc(data interface{}) jwt.MapClaims {
|
||||
return jwt.MapClaims{
|
||||
"id": data.(*models.UserInfo).ID,
|
||||
"username": data.(*models.UserInfo).Username,
|
||||
}
|
||||
}
|
||||
|
||||
func unauthorizedFunc(c *gin.Context, code int, message string) {
|
||||
c.JSON(http.StatusUnauthorized, models.Resp{
|
||||
RequestId: requestid.Get(c),
|
||||
Code: code,
|
||||
Msg: message,
|
||||
})
|
||||
}
|
||||
func loginResponse(c *gin.Context, code int, token string, expires time.Time) {
|
||||
c.JSON(http.StatusOK, models.Resp{
|
||||
RequestId: requestid.Get(c),
|
||||
Code: code,
|
||||
Data: models.Token{
|
||||
Token: "Bearer " + token,
|
||||
Expired: expires,
|
||||
},
|
||||
Msg: "SUCCESS",
|
||||
})
|
||||
}
|
||||
|
||||
func logoutResponse(c *gin.Context, code int) {
|
||||
c.JSON(http.StatusOK, models.Resp{
|
||||
RequestId: requestid.Get(c),
|
||||
Code: code,
|
||||
Data: nil,
|
||||
Msg: "SUCCESS",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package middles
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func AddCors() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(204)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user