From 0fcc0c5c81f1ec4426a6386b1a5cfe07c079b960 Mon Sep 17 00:00:00 2001 From: fbonhomm Date: Mon, 26 Aug 2019 20:01:29 +0200 Subject: [PATCH] feat: add jwt middleware --- source/middlewares/jwt.go | 46 ++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/source/middlewares/jwt.go b/source/middlewares/jwt.go index c39155d..ba62e27 100644 --- a/source/middlewares/jwt.go +++ b/source/middlewares/jwt.go @@ -8,16 +8,21 @@ package middlewares import ( "fmt" - jwt "github.com/dgrijalva/jwt-go" + "github.com/dgrijalva/jwt-go" "github.com/fbonhomm/api-go/source/libs" "github.com/fbonhomm/api-go/source/services" "github.com/gin-gonic/gin" "net/http" - "os" ) func Auth(c *gin.Context) { - tokenString := libs.GetToken(c) + tokenString, err := libs.GetToken(c) + + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{ "error": err }) + c.Abort() + return + } token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok { @@ -28,14 +33,45 @@ func Auth(c *gin.Context) { }) if err != nil { - c.JSON(http.StatusUnauthorized, gin.H{ "error": err }) + c.JSON(http.StatusUnauthorized, gin.H{ "error": "Token not conform." }) c.Abort() + return } if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { c.Set("Token", claims) } else { - c.JSON(http.StatusUnauthorized, gin.H{ "error": err }) + c.JSON(http.StatusUnauthorized, gin.H{ "error": "Token not conform." }) + c.Abort() + } +} + +func AuthRefresh(c *gin.Context) { + tokenString := c.PostForm("refresh_token") + + token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { + if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok { + return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) + } + + return services.PublicKeyRefresh, nil + }) + + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{ "error": "Token not conform." }) + c.Abort() + return + } + + if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { + var decode = make(map[string]string, 10) + + for key, value := range claims { + decode[key] = fmt.Sprintf("%v", value) + } + c.Set("Token", decode) + } else { + c.JSON(http.StatusUnauthorized, gin.H{ "error": "Token not conform." }) c.Abort() } }