mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-19 07:27:43 +08:00
feat: Introduce DetermineCaller for auth without account pooling and make wide_input_strict_output configurable.
This commit is contained in:
@@ -83,6 +83,26 @@ func (r *Resolver) Determine(req *http.Request) (*RequestAuth, error) {
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// DetermineCaller resolves caller identity without acquiring any pooled account.
|
||||
// Use this for local-cache lookup routes that only need tenant isolation.
|
||||
func (r *Resolver) DetermineCaller(req *http.Request) (*RequestAuth, error) {
|
||||
callerKey := extractCallerToken(req)
|
||||
if callerKey == "" {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
callerID := callerTokenID(callerKey)
|
||||
a := &RequestAuth{
|
||||
UseConfigToken: false,
|
||||
CallerID: callerID,
|
||||
resolver: r,
|
||||
TriedAccounts: map[string]bool{},
|
||||
}
|
||||
if r == nil || r.Store == nil || !r.Store.HasAPIKey(callerKey) {
|
||||
a.DeepSeekToken = callerKey
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func WithAuth(ctx context.Context, a *RequestAuth) context.Context {
|
||||
return context.WithValue(ctx, authCtxKey, a)
|
||||
}
|
||||
|
||||
@@ -66,6 +66,26 @@ func TestDetermineWithXAPIKeyManagedKeyAcquiresAccount(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetermineCallerWithManagedKeySkipsAccountAcquire(t *testing.T) {
|
||||
r := newTestResolver(t)
|
||||
req, _ := http.NewRequest(http.MethodGet, "/v1/responses/resp_1", nil)
|
||||
req.Header.Set("x-api-key", "managed-key")
|
||||
|
||||
a, err := r.DetermineCaller(req)
|
||||
if err != nil {
|
||||
t.Fatalf("determine caller failed: %v", err)
|
||||
}
|
||||
if a.CallerID == "" {
|
||||
t.Fatalf("expected caller id to be populated")
|
||||
}
|
||||
if a.UseConfigToken {
|
||||
t.Fatalf("expected no config-token lease for caller-only auth")
|
||||
}
|
||||
if a.AccountID != "" {
|
||||
t.Fatalf("expected empty account id, got %q", a.AccountID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallerTokenIDStable(t *testing.T) {
|
||||
a := callerTokenID("token-a")
|
||||
b := callerTokenID("token-a")
|
||||
@@ -93,3 +113,16 @@ func TestDetermineMissingToken(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetermineCallerMissingToken(t *testing.T) {
|
||||
r := newTestResolver(t)
|
||||
req, _ := http.NewRequest(http.MethodGet, "/v1/responses/resp_1", nil)
|
||||
|
||||
_, err := r.DetermineCaller(req)
|
||||
if err == nil {
|
||||
t.Fatal("expected unauthorized error")
|
||||
}
|
||||
if err != ErrUnauthorized {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user