forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticated_middlesare.go
More file actions
36 lines (30 loc) · 1 KB
/
Copy pathauthenticated_middlesare.go
File metadata and controls
36 lines (30 loc) · 1 KB
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
package middlewares
import (
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/gofiber/fiber/v3"
)
const (
authHeaderBearer = "Authorization"
authHeaderAPIKey = "x-api-key"
bearerScheme = "Bearer"
)
const (
// ContextKeyAuthUserID is the context key used to store the ID of an authenticated user
ContextKeyAuthUserID = "auth.user.id"
)
// Authenticated checks if the request is authenticated
func Authenticated(tracer telemetry.Tracer) fiber.Handler {
return func(c fiber.Ctx) error {
_, span := tracer.StartFromFiberCtx(c, "middlewares.Authenticated")
defer span.End()
if tokenUser, ok := c.Locals(ContextKeyAuthUserID).(entities.AuthContext); !ok || tokenUser.IsNoop() {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"status": "error",
"message": "You are not authorized to carry out this request.",
"data": "Make sure your API key is set in the [x-api-key] header in the request",
})
}
return c.Next()
}
}