feat: add auth jwt middleware

master
fbonhomm 2019-08-25 17:16:46 +02:00
parent 461047e61c
commit d6c847117f
1 changed files with 41 additions and 0 deletions

41
source/middlewares/jwt.go Normal file
View File

@ -0,0 +1,41 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
*/
package middlewares
import (
"fmt"
jwt "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)
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.PublicKeyAccess, nil
})
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{ "error": err })
c.Abort()
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
c.Set("Token", claims)
} else {
c.JSON(http.StatusUnauthorized, gin.H{ "error": err })
c.Abort()
}
}