Initial commit: ozon-mock Go server
- Mock Ozon Seller API endpoints - Products, Postings, Finance, Returns, Analytics - JSON fixtures for testing - Docker support - Gitea Actions CI/CD
This commit is contained in:
38
internal/middleware/auth.go
Normal file
38
internal/middleware/auth.go
Normal file
@ -0,0 +1,38 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Auth(expectedClientID, expectedAPIKey string, next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
clientID := r.Header.Get("Client-Id")
|
||||
apiKey := r.Header.Get("Api-Key")
|
||||
|
||||
if clientID == "" || apiKey == "" {
|
||||
writeError(w, 401, "missing credentials: Client-Id and Api-Key headers are required")
|
||||
return
|
||||
}
|
||||
|
||||
if expectedClientID != "" && clientID != expectedClientID {
|
||||
writeError(w, 403, "invalid client id")
|
||||
return
|
||||
}
|
||||
if expectedAPIKey != "" && apiKey != expectedAPIKey {
|
||||
writeError(w, 403, "invalid api key")
|
||||
return
|
||||
}
|
||||
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, code int, msg string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(map[string]any{
|
||||
"code": code,
|
||||
"message": msg,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user