fixed
This commit is contained in:
1 parent
4194cef934
commit
363127fe9e
3350 files changed
+1284220
-1
No files matched your search
Executable
+78
@@ -0,0 +1,78 @@
|
||||
package appresult
|
||||
|
||||
import (
|
||||
"bankline/internal/config"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UnsignedResponse struct {
|
||||
Message interface{} `json:"message"`
|
||||
}
|
||||
|
||||
type appHandler func(w http.ResponseWriter, r *http.Request) error
|
||||
|
||||
func HeaderContentTypeJson() (string, string) {
|
||||
return "Content-Type", "application/json"
|
||||
}
|
||||
func AccessControlAllow() (string, string) {
|
||||
return "Access-Control-Allow-Origin", "*"
|
||||
}
|
||||
|
||||
func respondWithError(c *gin.Context, code int, message interface{}) {
|
||||
c.AbortWithStatusJSON(code, gin.H{"error": message})
|
||||
}
|
||||
|
||||
func JwtTokenCheck() gin.HandlerFunc {
|
||||
|
||||
return func(c *gin.Context) {
|
||||
token := c.GetHeader("Authorization")
|
||||
|
||||
if token == "" {
|
||||
respondWithError(c, 403, "Authorization header is missing")
|
||||
return
|
||||
}
|
||||
|
||||
cfg := config.GetConfig()
|
||||
claims, err := TokenClaims(token, cfg.JwtKey)
|
||||
if err != nil {
|
||||
respondWithError(c, 401, "Token is expired")
|
||||
return
|
||||
}
|
||||
|
||||
isAdmin := claims["admin"].(bool)
|
||||
if isAdmin == false {
|
||||
respondWithError(c, 403, "Unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("id", fmt.Sprint(claims["id"]))
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func TokenClaims(token, secretKey string) (jwt.MapClaims, error) {
|
||||
decoded, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
|
||||
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(secretKey), nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("err", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
claims, ok := decoded.Claims.(jwt.MapClaims)
|
||||
|
||||
if !ok {
|
||||
// TODO tokenin omrini test etmeli
|
||||
return nil, ErrInternalServer
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
Reference in new issue
Block a user