-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
46 lines (34 loc) · 1.18 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package middlewares
import (
"github.com/lcoutinho/luizalabs-client-api/controllers"
"github.com/lcoutinho/luizalabs-client-api/services/auth"
"github.com/patrickmn/go-cache"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
func JwtFilter(cacheClient *cache.Cache) gin.HandlerFunc {
return func(c *gin.Context) {
tokenJwt := c.Request.Header.Get("Authorization")
if len(tokenJwt) == 0 {
c.AbortWithStatusJSON(401, controllers.Response(false, false, 0, "Missing Authorization header"))
return
}
validateJwt(tokenJwt, c, cacheClient)
}
}
func validateJwt(myToken string, c *gin.Context, cacheClient *cache.Cache) {
authService, _ := auth.GetAuthStrategyService(cacheClient)
token, err := authService.ValidateToken(myToken)
if err == nil && token.Valid {
currentResource := c.MustGet("resource").(string)
allowedResource := token.Claims.(jwt.MapClaims)["resource"]
if !strings.Contains(allowedResource.(string), currentResource) {
c.AbortWithStatusJSON(401, controllers.Response(false, false, 0, "Not allowed this resource"))
return
}
c.Next()
} else {
c.AbortWithStatusJSON(403, controllers.Response(false, false, 0, err.Error()))
}
}