fix(pier): end only the revoked token's or client's MCP sessions and log the client address (B-WR-04, B-IN-04)
- pact.MCPSubjectChanged and pier.Event gain TokenID and ClientID; a narrowed revoked event closes only matching sessions and stateless servers - cabana fires the admin token revoke with the token row id - towel.WithClientIP/ClientIP; pier puts the address (surf applies http.trusted_proxies) on the resolve context and ResolveBackendToken logs it
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/towel"
|
||||
)
|
||||
|
||||
// resolverTokenPath is the path logTokenAuthAt records for a token check that
|
||||
@@ -23,32 +24,35 @@ const resolverTokenPath = "mcp"
|
||||
// admin and an application without cabana activated all answer
|
||||
// ErrUnauthenticated; the audit line is logged under path "mcp".
|
||||
func ResolveBackendToken(ctx context.Context, app *backpack.App, raw string) (*bouncer.Principal, *BackendAPIToken, []string, error) {
|
||||
// The caller's client address, when it put one on the context (pier
|
||||
// does), so a failed attempt is traceable to an address.
|
||||
remote, _ := towel.ClientIP(ctx)
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
guard := publishedResolverGuard(app)
|
||||
if guard == nil || !wellFormedAPIToken(raw) || guard.store == nil {
|
||||
logTokenAuthAt("", resolverTokenPath, "", "failed", 0, 0)
|
||||
logTokenAuthAt("", resolverTokenPath, remote, "failed", 0, 0)
|
||||
return nil, nil, nil, ErrUnauthenticated
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
hash := hashAPIToken(raw)
|
||||
row, err := guard.store.FindUsableByHash(ctx, hash, now)
|
||||
if err != nil || row == nil {
|
||||
logTokenAuthAt("", resolverTokenPath, "", "failed", 0, 0)
|
||||
logTokenAuthAt("", resolverTokenPath, remote, "failed", 0, 0)
|
||||
return nil, nil, nil, ErrUnauthenticated
|
||||
}
|
||||
if subtle.ConstantTimeCompare([]byte(row.TokenHash), []byte(hash)) != 1 {
|
||||
logTokenAuthAt("", resolverTokenPath, "", "failed", 0, row.ID)
|
||||
logTokenAuthAt("", resolverTokenPath, remote, "failed", 0, row.ID)
|
||||
return nil, nil, nil, ErrUnauthenticated
|
||||
}
|
||||
if guard.users == nil {
|
||||
logTokenAuthAt("", resolverTokenPath, "", "failed", 0, row.ID)
|
||||
logTokenAuthAt("", resolverTokenPath, remote, "failed", 0, row.ID)
|
||||
return nil, nil, nil, ErrUnauthenticated
|
||||
}
|
||||
principal, err := guard.users.FindByID(ctx, row.BackendUserID)
|
||||
if err != nil || principal == nil {
|
||||
logTokenAuthAt("", resolverTokenPath, "", "failed", row.BackendUserID, row.ID)
|
||||
logTokenAuthAt("", resolverTokenPath, remote, "failed", row.BackendUserID, row.ID)
|
||||
return nil, nil, nil, ErrUnauthenticated
|
||||
}
|
||||
stored := row.Scopes.Get()
|
||||
@@ -58,7 +62,7 @@ func ResolveBackendToken(ctx context.Context, app *backpack.App, raw string) (*b
|
||||
// Personal API tokens keep the request locale (see authenticateToken).
|
||||
principal.PreferredLocale = ""
|
||||
stampLastUsed(ctx, guard.stamps, guard.store, row.ID, now)
|
||||
logTokenAuthAt("", resolverTokenPath, "", "success", principal.ID, row.ID)
|
||||
logTokenAuthAt("", resolverTokenPath, remote, "success", principal.ID, row.ID)
|
||||
return principal, row, append([]string(nil), stored...), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user