Spaces:
Paused
Paused
ML Intern deploy
Deploy httpSMS gateway to Hugging Face Spaces (Docker SDK): nginx :7860 -> Go API :8000, Nuxt SPA, Android APK, bootstrap-mode entrypoint
83a59ca Download api/pkg/middlewares/bearer_api_key_auth_middleware.go from red-kit/httpsms-gateway: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/main/api/pkg/middlewares/bearer_api_key_auth_middleware.go
- Command line
-
hf download hf://spaces/red-kit/httpsms-gateway/api/pkg/middlewares/bearer_api_key_auth_middleware.go
-
curl -L -o bearer_api_key_auth_middleware.go https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/main/api/pkg/middlewares/bearer_api_key_auth_middleware.go
1.17 kB
| package middlewares | |
| import ( | |
| "fmt" | |
| "strings" | |
| "github.com/NdoleStudio/httpsms/pkg/repositories" | |
| "github.com/NdoleStudio/httpsms/pkg/telemetry" | |
| "github.com/NdoleStudio/stacktrace" | |
| "github.com/gofiber/fiber/v3" | |
| ) | |
| // BearerAPIKeyAuth authenticates an API key using the Bearer header | |
| func BearerAPIKeyAuth(logger telemetry.Logger, tracer telemetry.Tracer, userRepository repositories.UserRepository) fiber.Handler { | |
| logger = logger.WithService("middlewares.APIKeyAuth") | |
| return func(c fiber.Ctx) error { | |
| ctx, span, ctxLogger := tracer.StartFromFiberCtxWithLogger(c, logger, "middlewares.APIKeyAuth") | |
| defer span.End() | |
| apiKey := strings.TrimSpace(strings.Replace(c.Get(authHeaderBearer), bearerScheme, "", 1)) | |
| if len(apiKey) == 0 { | |
| span.AddEvent(fmt.Sprintf("the request header has no [%s] api key", authHeaderAPIKey)) | |
| return c.Next() | |
| } | |
| authUser, err := userRepository.LoadAuthContext(ctx, apiKey) | |
| if err != nil { | |
| ctxLogger.Error(stacktrace.Propagatef(err, "cannot load user with api key [%s] using header [%s]", apiKey, c.Get(authHeaderBearer))) | |
| return c.Next() | |
| } | |
| c.Locals(ContextKeyAuthUserID, authUser) | |
| return c.Next() | |
| } | |
| } | |