fix(19.7): RC-WR-01 a backend grant acts only as the administrator it was consented for

pact.MCPGrant gains BackendUserID, the backend account the consent page
showed. pier gives an OAuth identity a backend subject (on resolution and
after a link event) only while the user's link names that account; a link
to anyone else, or no recorded account, gives none, so a link changed after
the consent never re-targets the grant.
This commit is contained in:
2026-10-23 20:51:57 +02:00
parent 0b0cae5b53
commit c37c6ce50f
9 changed files with 112 additions and 43 deletions

View File

@@ -57,6 +57,10 @@ type Identity struct {
// credential is the admin personal token row stored next to the
// backend principal on a dispatched call.
credential any
// consentedBackend is the backend account an OAuth grant was consented
// for (pact.MCPGrant.BackendUserID); the backend subject exists only
// while the user's link names it.
consentedBackend uint
}
// subjectFor returns the subject acting under guard, nil when the identity
@@ -208,11 +212,12 @@ func (r *identityResolver) resolveOAuth(ctx context.Context, raw string) (Identi
}
scopes := append([]string(nil), grant.Scopes...)
id := Identity{
Scopes: scopes,
Binding: "oauth:" + grant.ClientID + ":" + strconv.FormatUint(uint64(grant.UserID), 10),
ExpiresAt: grant.ExpiresAt,
TokenKind: TokenKindOAuth,
ceiling: true,
Scopes: scopes,
Binding: "oauth:" + grant.ClientID + ":" + strconv.FormatUint(uint64(grant.UserID), 10),
ExpiresAt: grant.ExpiresAt,
TokenKind: TokenKindOAuth,
ceiling: true,
consentedBackend: grant.BackendUserID,
}
if r.frontendGuard != "" {
id.Frontend = &pact.MCPSubject{
@@ -223,20 +228,32 @@ func (r *identityResolver) resolveOAuth(ctx context.Context, raw string) (Identi
TokenID: grant.TokenID,
}
}
if slices.Contains(scopes, "backend") {
if link, ok := r.linkedBackend(ctx, grant.UserID); ok {
id.Backend = &pact.MCPSubject{
Guard: BackendGuard,
UserID: link.ID,
Scopes: scopes,
ClientID: grant.ClientID,
TokenID: grant.TokenID,
}
}
}
id.Backend = r.consentedBackendSubject(ctx, grant.UserID, grant.ClientID, grant.TokenID, scopes, grant.BackendUserID)
return id, nil
}
// consentedBackendSubject is the backend subject of an OAuth grant of
// userID: the linked administrator, when the grant carries the backend
// scope and the link names consented, the account the grant was consented
// for. A link to anyone else, no link or no consented account gives none,
// so a link changed after the consent never re-targets the grant.
func (r *identityResolver) consentedBackendSubject(ctx context.Context, userID uint, clientID string, tokenID uint, scopes []string, consented uint) *pact.MCPSubject {
if consented == 0 || !slices.Contains(scopes, "backend") {
return nil
}
link, ok := r.linkedBackend(ctx, userID)
if !ok || link.ID != consented {
return nil
}
return &pact.MCPSubject{
Guard: BackendGuard,
UserID: link.ID,
Scopes: scopes,
ClientID: clientID,
TokenID: tokenID,
}
}
func (r *identityResolver) resolveBackendToken(ctx context.Context, raw string) (Identity, error) {
principal, row, scopes, err := r.backendTokens(ctx, r.app, raw)
if err != nil || principal == nil || row == nil {