Initial commit

This commit is contained in:
kandrusyak
2023-06-23 02:18:00 +03:00
commit 688ae61bc6
12 changed files with 297 additions and 0 deletions

26
mw/jwt.go Normal file
View File

@@ -0,0 +1,26 @@
package mw
import (
"astra-api-gateway/handler"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo-jwt/v4"
"github.com/labstack/echo/v4"
"strings"
)
var JWTMW = echojwt.WithConfig(echojwt.Config{
SigningKey: []byte(handler.Key),
Skipper: func(c echo.Context) bool {
// Skip authentication for signup and login requests
if strings.Contains(c.Path(), "auth") || c.Path() == "/health" {
return true
}
return false
},
SuccessHandler: func(c echo.Context) {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
c.Request().Header.Set("UserId", claims["id"].(string))
c.Request().Header.Del("Authorization")
},
})

25
mw/token_from_cookies.go Normal file
View File

@@ -0,0 +1,25 @@
package mw
import (
"github.com/labstack/echo/v4"
"net/http"
)
func TokenFromCookies(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
var token, err = c.Request().Cookie("accessToken")
var authToken = c.Request().Header.Get("Authorization")
if err != nil {
return next(c)
}
if authToken != "" {
return c.NoContent(http.StatusBadRequest)
}
c.Request().Header.Set("Authorization", "Bearer "+token.Value)
return next(c)
}
}