79 lines
1.6 KiB
Go
Executable File
79 lines
1.6 KiB
Go
Executable File
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
|
|
}
|