Repository navigation
Expand file tree
/
Copy pathissue.go
More file actions
223 lines (204 loc) · 9.58 KB
/
Copy pathissue.go
File metadata and controls
223 lines (204 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package agentauth
import (
"context"
"fmt"
"time"
log "github.com/xraph/go-utils/log"
"github.com/xraph/authsome/account"
"github.com/xraph/authsome/bridge"
"github.com/xraph/authsome/id"
"github.com/xraph/authsome/session"
)
// agentSessionTTL bounds an agent access token, and is a hard ceiling: nothing
// in the engine extends an agent session past it. Three separate guards
// enforce that, each closing a different route to the same failure (a
// session outliving the grant that authorized it):
// - Engine.Refresh (service.go) refuses to rotate an agent-principal
// session at all, so a refresh cannot renew one.
// - roleStampingStore's shouldStamp/shouldRestamp (engine_session_roles.go)
// never re-stamp an agent session, so even if rotation were ever allowed
// it could not restore the delegating human's roles onto it.
// - middleware.SessionActivityMiddleware (middleware/activity.go) skips
// agent sessions entirely, so the sliding session window cannot reset
// ExpiresAt to now + InactivityTimeout on ordinary request traffic.
//
// Short by design: with no renewal path of any kind, this TTL is genuinely
// the only thing standing between a revoked or expired grant and continued
// access, until agentauth grows its own grant-aware refresh.
const agentSessionTTL = 15 * time.Minute
// IssueMeta carries request-scoped metadata onto the minted session. Without
// it the risk plugins this task exists to inform have nothing to score:
// impossibletravel returns immediately on an empty IPAddress and geoip
// returns on a nil lookup, so the hooks would fire but teach those plugins
// nothing. There are no callers yet, so this shape is free to change.
type IssueMeta struct {
IPAddress string
UserAgent string
DeviceID id.DeviceID
}
// IssueAgentSession mints a session for an agent acting under grant.
//
// Issuance deliberately goes through the engine's normal store and hook path
// so BeforeSessionCreate and AfterSignIn fire. That is what puts agent
// traffic in front of riskengine, impossibletravel, ipreputation and the
// rest. The API key plugin builds a synthetic session by hand and fires
// neither, which is why its traffic is invisible to all of them. Do not
// copy that shape here.
//
// riskengine's own OnBeforeSessionCreate is a documented no-op today, so
// nothing actually vetoes an agent session through this hook yet. What this
// function guarantees is the mechanism: an emit that returns an error and
// aborts the mint before anything is persisted, so whichever plugin later
// implements the veto can rely on it working. See
// TestIssueAgentSession_VetoBlocksPersistence.
func (p *Plugin) IssueAgentSession(ctx context.Context, grant *AgentGrant, meta IssueMeta) (*session.Session, error) {
if err := validateGrant(grant); err != nil {
return nil, err
}
now := time.Now()
if !grant.IsActive(now) {
return nil, ErrGrantInactive
}
// Resolved before anything is written. A GetUser failure that happened
// after the store write would leave a persisted session behind that the
// caller believes never got created (it saw an error) and that
// AfterSignIn never fires for — invisible to exactly the plugins this
// task exists to inform.
u, err := p.engine.GetUser(ctx, grant.UserID)
if err != nil {
return nil, fmt.Errorf("agentauth: resolve delegating user: %w", err)
}
ttl := agentSessionTTL
if until := grant.ExpiresAt.Sub(now); until < ttl {
// A session must never outlive the grant that authorized it. Without
// this clamp, a short-lived grant close to expiry would still mint a
// full-length session, and revoking or letting the grant lapse would
// not actually end the agent's access until the session separately
// expired.
ttl = until
}
// Token and RefreshToken are generated exactly the way every other mint
// path generates them: account.NewSession, the same helper
// engine_issue_session.go and the oauth2provider plugin call. Both
// columns carry a non-partial unique index on postgres and sqlite,
// so a session hand-built with empty strings for both inserts fine
// once and then collides with "authsome: conflict" on the second.
sess, err := account.NewSession(grant.AppID, grant.UserID, account.SessionConfig{
TokenTTL: ttl,
RefreshTokenTTL: ttl,
})
if err != nil {
return nil, fmt.Errorf("agentauth: generate session tokens: %w", err)
}
// account.NewSession takes its own time.Now() snapshot to add the ttl
// to, a few instructions after ours. Re-clamping against grant.ExpiresAt
// directly (rather than trusting the duration alone) keeps "never
// outlives the grant" true regardless of that skew.
if sess.ExpiresAt.After(grant.ExpiresAt) {
sess.ExpiresAt = grant.ExpiresAt
}
if sess.RefreshTokenExpiresAt.After(grant.ExpiresAt) {
sess.RefreshTokenExpiresAt = grant.ExpiresAt
}
sess.OrgID = grant.OrgID
sess.PrincipalKind = session.PrincipalKindAgent
sess.AgentID = grant.AgentID
// GrantID must always be set: the postgres CHECK constraint
// authsome_sessions_principal_check requires grant_id <> '' for an
// agent-principal row, and the authorization core in middleware.go
// refuses to honor a session whose grant does not match the session's
// UserID, AgentID and AppID. Leaving it unset would mint a session that
// passes on sqlite/mongo/memory (no such constraint) but fails outright
// on postgres, and would be unusable everywhere regardless.
sess.GrantID = grant.ID
sess.IPAddress = meta.IPAddress
sess.UserAgent = meta.UserAgent
sess.DeviceID = meta.DeviceID
// authsome_sessions.env_id is NOT NULL with a foreign key to
// authsome_environments, and a zero id.EnvironmentID stringifies to "",
// which fails that FK on the very first insert on postgres (sqlite and
// mongo have no such constraint). Best-effort, matching the pattern in
// engine_issue_session.go and plugins/oauth2provider/plugin.go: an app
// with no default environment configured should not block issuance.
if env, envErr := p.engine.Store().GetDefaultEnvironment(ctx, grant.AppID); envErr == nil && env != nil {
sess.EnvID = env.ID
}
// The Emit* methods live on *plugin.Registry, reached through
// engine.Plugins(). engine.Hooks() returns a *hook.Bus, which is a
// different type and has no Emit methods on it.
reg := p.engine.Plugins()
// BeforeSessionCreate returns an error and can veto: an implementer can
// abort the mint before anything is persisted. This must stay ahead of
// the store write below — moving it after would let a vetoed session
// persist anyway, which TestIssueAgentSession_VetoBlocksPersistence
// guards against.
if err := reg.EmitBeforeSessionCreate(ctx, sess); err != nil {
return nil, fmt.Errorf("agentauth: before session create: %w", err)
}
if err := p.engine.Store().CreateSession(ctx, sess); err != nil {
return nil, fmt.Errorf("agentauth: create session: %w", err)
}
reg.EmitAfterSessionCreate(ctx, sess)
// EmitAfterSignIn returns nothing. Notification hooks are fire-and-forget
// by design, so there is no error to handle here.
reg.EmitAfterSignIn(ctx, u, sess)
// A delegated-agent credential minted with no audit trail is the wrong
// default for this feature specifically: it is the record that lets an
// operator answer "what did this agent do, and who let it" after the
// fact. Mirrors the shape Engine.IssueSession writes in
// engine_issue_session.go. Best-effort: an audit-log outage must not
// block issuance, the same trade-off engine.audit already makes.
if p.chronicle != nil {
if aerr := p.chronicle.Record(ctx, &bridge.AuditEvent{
Action: "issue_agent_session",
Resource: "session",
ResourceID: sess.ID.String(),
ActorID: grant.UserID.String(),
Tenant: grant.AppID.String(),
Outcome: bridge.OutcomeSuccess,
Severity: bridge.SeverityInfo,
Category: "auth",
Metadata: map[string]string{
"agent_id": grant.AgentID.String(),
"grant_id": grant.ID.String(),
},
}); aerr != nil {
p.logger.Warn("agentauth: audit record failed", log.Error(aerr))
}
}
// StampLastUsed, not UpdateAgentGrant: UpdateAgentGrant replaces the
// whole row from this function's local grant, which was read before the
// session write above and can be stale by the time this runs. A revoke
// landing in that window would have its RevokedAt undone the moment this
// stale copy was written back — see the Store interface doc comment on
// StampLastUsed. A targeted update that only ever mentions
// last_used_at/updated_at cannot do that, because it never reads or
// writes RevokedAt at all. The caller's grant is only mutated once the
// write actually succeeds, so it never claims a stamp that wasn't
// persisted.
if err := p.store.StampLastUsed(ctx, grant.ID, now); err != nil {
p.logger.Warn("agentauth: could not stamp grant last-used", log.Error(err))
} else {
stamp := now
grant.LastUsedAt = &stamp
grant.UpdatedAt = now
}
return sess, nil
}
// validateGrant rejects a caller-supplied grant this package cannot safely
// mint a session from, before anything about it is dereferenced or trusted.
// Task 8's ruling was that Authorize must not trust the issuer; the
// symmetric obligation is that the issuer validates what it is handed.
// IssueAgentSession is exported, so a nil grant, or a grant missing one of
// the ids Authorize binds sessions to (UserID, AgentID, AppID), is exactly
// the input a careless or malicious caller can supply — and grant.IsActive
// would panic on a nil grant before any of this ran.
func validateGrant(grant *AgentGrant) error {
if grant == nil {
return ErrGrantInactive
}
if grant.UserID.IsNil() || grant.AgentID.IsNil() || grant.AppID.IsNil() {
return ErrGrantInactive
}
return nil
}