Skip to content

Commit d7bd7d8

Browse files
add debug level logs
1 parent 49c8a46 commit d7bd7d8

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
"program": "${workspaceFolder}/main.go",
1010
"env": {
1111
"DEV_ENV": "true",
12+
"DEBUG": "true",
1213
"JWT_SECRET": "35nhvjfosfklgmfg56+fdsgzvfnjksacvbhfksfkgofadsjfgjkoldsdkfvpl'jbgio;dfsjgkl;'XZFVMifobd;dlgjv[sfvjmiodfkvs2fh;fhk44gfdhksdkfdffk",
1314
"REFRESH_JWT_SECRET": "35c7b3eb969db065bfa3c66b38e4323e8f73113f3965dfb55c6bc585dcb0ba62bd399e2588fdc8f709ae0b63fb24be32590f134506ca1d7a4314339f11b8045a",
1415
"CONNECTION_TOKEN": "memphis",
1516
"ROOT_USER": "root",
16-
"MEMPHIS_HOST":"localhost",
17+
"MEMPHIS_HOST": "localhost",
1718
"HTTP_PORT": "4444",
1819
"CLIENT_CERT_PATH": "",
1920
"CLIENT_KEY_PATH": "",

conf/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Configuration struct {
2020
ROOT_CA_PATH string
2121
USER_PASS_BASED_AUTH bool
2222
ROOT_PASSWORD string
23+
DEBUG bool
2324
}
2425

2526
func GetConfig() Configuration {

middlewares/auth.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"rest-gateway/models"
99
"strings"
1010

11-
"github.com/golang-jwt/jwt/v4"
1211
"github.com/gofiber/fiber/v2"
12+
"github.com/golang-jwt/jwt/v4"
1313
)
1414

1515
var configuration = conf.GetConfig()
@@ -74,6 +74,9 @@ func Authenticate(c *fiber.Ctx) error {
7474
tokenString = c.Query("authorization")
7575
if tokenString == "" { // fallback - get the token from the query params
7676
log.Warnf("Authentication error - jwt token is missing")
77+
if configuration.DEBUG {
78+
fmt.Printf("Method: %s, Path: %s, IP: %s\nBody: %s\n", c.Method(), c.Path(), c.IP(), string(c.Body()))
79+
}
7780
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
7881
"message": "Unauthorized",
7982
})
@@ -82,6 +85,9 @@ func Authenticate(c *fiber.Ctx) error {
8285
err = verifyToken(tokenString, configuration.JWT_SECRET)
8386
if err != nil {
8487
log.Warnf("Authentication error - jwt token validation has failed")
88+
if configuration.DEBUG {
89+
fmt.Printf("Method: %s, Path: %s, IP: %s\nBody: %s\n", c.Method(), c.Path(), c.IP(), string(c.Body()))
90+
}
8591
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
8692
"message": "Unauthorized",
8793
})
@@ -90,13 +96,19 @@ func Authenticate(c *fiber.Ctx) error {
9096
var body models.RefreshTokenSchema
9197
if err := c.BodyParser(&body); err != nil {
9298
log.Errorf("Authenticate: %s", err.Error())
99+
if configuration.DEBUG {
100+
fmt.Printf("Method: %s, Path: %s, IP: %s\nBody: %s\n", c.Method(), c.Path(), c.IP(), string(c.Body()))
101+
}
93102
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
94103
"message": err.Error(),
95104
})
96105
}
97106

98107
if body.JwtRefreshToken == "" {
99108
log.Warnf("Authentication error - refresh token is missing")
109+
if configuration.DEBUG {
110+
fmt.Printf("Method: %s, Path: %s, IP: %s\nBody: %s\n", c.Method(), c.Path(), c.IP(), string(c.Body()))
111+
}
100112
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
101113
"message": "Unauthorized",
102114
})
@@ -105,6 +117,9 @@ func Authenticate(c *fiber.Ctx) error {
105117
err := verifyToken(body.JwtRefreshToken, configuration.REFRESH_JWT_SECRET)
106118
if err != nil {
107119
log.Warnf("Authentication error - refresh token validation has failed")
120+
if configuration.DEBUG {
121+
fmt.Printf("Method: %s, Path: %s, IP: %s\nBody: %s\n", c.Method(), c.Path(), c.IP(), string(c.Body()))
122+
}
108123
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
109124
"message": "Unauthorized",
110125
})

0 commit comments

Comments
 (0)