You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
730 B

4 years ago
package store
import (
"github.com/gin-gonic/gin"
"recook/internal/back"
"recook/internal/cache"
"strconv"
"strings"
)
func authorize() gin.HandlerFunc {
return func(c *gin.Context) {
path := c.Request.URL.Path
if strings.Contains(path, "login") {
c.Next()
} else {
id, _ := strconv.Atoi(c.Request.Header.Get("X-Recook-ID"))
if id == 0 {
back.Unauthorized(c)
c.Abort()
return
}
token := c.Request.Header.Get("X-Recook-Token")
if len(token) == 0 {
back.Unauthorized(c)
c.Abort()
}
cacheToken := cache.GetStoreLoginCache(uint(id))
if len(cacheToken) > 0 && cacheToken == token {
c.Next()
} else {
back.Unauthorized(c)
c.Abort()
return
}
}
}
}