2026-10-13 12:35:22 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestRequireBearerToken(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
authorization string
|
|
|
|
|
wantStatus int
|
|
|
|
|
}{
|
|
|
|
|
{name: "missing header", authorization: "", wantStatus: http.StatusUnauthorized},
|
|
|
|
|
{name: "wrong token", authorization: "Bearer guess", wantStatus: http.StatusUnauthorized},
|
|
|
|
|
{name: "token prefix only", authorization: "Bearer s3cre", wantStatus: http.StatusUnauthorized},
|
|
|
|
|
{name: "basic scheme", authorization: "Basic czNjcmV0", wantStatus: http.StatusUnauthorized},
|
|
|
|
|
{name: "empty bearer", authorization: "Bearer ", wantStatus: http.StatusUnauthorized},
|
|
|
|
|
{name: "valid token", authorization: "Bearer s3cret", wantStatus: http.StatusNoContent},
|
|
|
|
|
{name: "scheme is case-insensitive", authorization: "bearer s3cret", wantStatus: http.StatusNoContent},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
})
|
|
|
|
|
handler := requireBearerToken("s3cret")(next)
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2026-10-13 13:37:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPut, "/api/hosted/npm/request/2.88.3", nil)
|
2026-10-13 12:35:22 +02:00
|
|
|
if tt.authorization != "" {
|
|
|
|
|
req.Header.Set("Authorization", tt.authorization)
|
|
|
|
|
}
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
handler.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
|
|
if rec.Code != tt.wantStatus {
|
|
|
|
|
t.Fatalf("status = %d, want %d", rec.Code, tt.wantStatus)
|
|
|
|
|
}
|
|
|
|
|
if tt.wantStatus != http.StatusUnauthorized {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if got := rec.Header().Get("WWW-Authenticate"); got != `Bearer realm="proxy"` {
|
|
|
|
|
t.Errorf("WWW-Authenticate = %q, want Bearer realm=\"proxy\"", got)
|
|
|
|
|
}
|
|
|
|
|
var body ErrorResponse
|
|
|
|
|
if err := json.NewDecoder(rec.Body).Decode(&body); err != nil {
|
|
|
|
|
t.Fatalf("decoding error body: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if body.Code != ErrCodeUnauthorized {
|
|
|
|
|
t.Errorf("error code = %q, want %q", body.Code, ErrCodeUnauthorized)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|