Перенести WithUser в httpx
This commit is contained in:
@@ -282,6 +282,31 @@ func TestUserIDRoundTripAndAccessLogAttribution(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithUserPassesTheIdentifiedCaller(t *testing.T) {
|
||||
actor := uuid.MustParse("22222222-2222-4222-8222-222222222222")
|
||||
e := newEngine(t, nil)
|
||||
e.GET("/actor",
|
||||
func(c *gin.Context) { httpx.SetUserID(c, actor) },
|
||||
httpx.WithUser(func(c *gin.Context, got uuid.UUID) {
|
||||
httpx.JSON(c, http.StatusOK, gin.H{"id": got.String()})
|
||||
}))
|
||||
rec, _ := do(t, e, http.MethodGet, "/actor", "", nil)
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), actor.String()) {
|
||||
t.Fatalf("handler did not receive the caller: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Reaching WithUser without an authentication middleware in front is a
|
||||
// wiring mistake, so it must not be blamed on the client with a 401.
|
||||
func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) {
|
||||
e := newEngine(t, nil)
|
||||
e.GET("/actor", httpx.WithUser(func(c *gin.Context, _ uuid.UUID) {
|
||||
httpx.JSON(c, http.StatusOK, gin.H{})
|
||||
}))
|
||||
rec, eb := do(t, e, http.MethodGet, "/actor", "", nil)
|
||||
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
|
||||
}
|
||||
|
||||
func logEntry(t *testing.T, logs *bytes.Buffer) map[string]any {
|
||||
t.Helper()
|
||||
var entry map[string]any
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const keyUserID = "messenger.user_id"
|
||||
|
||||
// SetUserID records the identified caller for the handlers behind the
|
||||
// identification middleware and for the access log. It lives here rather
|
||||
// than in the auth package because the access log needs it and auth already
|
||||
// depends on this package.
|
||||
// SetUserID records the authenticated caller for the handlers behind the
|
||||
// authentication middleware and for the access log. It lives here rather
|
||||
// than in the auth package because the access log needs it and the business
|
||||
// modules must not depend on auth.
|
||||
func SetUserID(c *gin.Context, id uuid.UUID) {
|
||||
c.Set(keyUserID, id)
|
||||
}
|
||||
@@ -23,3 +25,20 @@ func UserID(c *gin.Context) (uuid.UUID, bool) {
|
||||
id, ok := v.(uuid.UUID)
|
||||
return id, ok
|
||||
}
|
||||
|
||||
// UserHandler is a handler that needs the authenticated caller.
|
||||
type UserHandler func(c *gin.Context, actor uuid.UUID)
|
||||
|
||||
// WithUser adapts a UserHandler to gin. Reaching it without the
|
||||
// authentication middleware in front is a wiring mistake, so it is reported
|
||||
// as an internal error rather than blamed on the client with a 401.
|
||||
func WithUser(h UserHandler) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
actor, ok := UserID(c)
|
||||
if !ok {
|
||||
Error(c, errors.New("route is missing the authentication middleware"))
|
||||
return
|
||||
}
|
||||
h(c, actor)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user