-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
46 lines (37 loc) · 1.04 KB
/
middleware.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 http
import (
"net/http"
"github.com/google/uuid"
"github.com/guilhermealvess/guicpay/internal/token"
"github.com/labstack/echo/v4"
)
const PayloadToken = "account"
type Payload struct {
AccountID uuid.UUID `json:"account_id"`
AccountType string `json:"account_type"`
}
func validateTokenMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
tokenString := c.Request().Header.Get("Authorization")
if tokenString == "" {
return echo.NewHTTPError(http.StatusForbidden)
}
var payload Payload
if err := token.Middle(tokenString, &payload); err != nil {
return echo.NewHTTPError(http.StatusForbidden)
}
c.Set(PayloadToken, &payload)
return next(c)
}
}
func validatePermission(accountType string) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
payload := c.Get(PayloadToken).(*Payload)
if payload.AccountType != accountType {
return echo.NewHTTPError(http.StatusForbidden)
}
return next(c)
}
}
}