MCPcopy Create free account
hub / github.com/Mnexa-AI/e2a / issueOAuthCodeWithNewAgent

Method issueOAuthCodeWithNewAgent

internal/agent/oauth_handlers.go:993–1033  ·  view source on GitHub ↗
(ctx context.Context, w http.ResponseWriter, r *http.Request, ar fosite.AuthorizeRequester, userID, agentEmail, scope string)

Source from the content-addressed store, hash-verified

991}
992
993func (a *API) issueOAuthCodeWithNewAgent(ctx context.Context, w http.ResponseWriter, r *http.Request, ar fosite.AuthorizeRequester, userID, agentEmail, scope string) error {
994 pool := a.oauthStorage.Pool()
995 tx, err := pool.BeginTx(ctx, pgx.TxOptions{})
996 if err != nil {
997 return fmt.Errorf("begin tx: %w", err)
998 }
999 // Safe to defer unconditionally: Rollback is a no-op after Commit
1000 // in pgx v5.
1001 defer func() { _ = tx.Rollback(ctx) }()
1002 txCtx := oauth.WithTx(ctx, tx)
1003
1004 // Agent insert via the identity package — same tx, same context.
1005 if _, err := a.store.CreateAgentTx(txCtx, tx, agentEmail, a.sharedDomain, "", "", "", userID); err != nil {
1006 if isUniqueViolation(err) {
1007 http.Error(w, "that slug is already taken; pick another", http.StatusConflict)
1008 return nil
1009 }
1010 return fmt.Errorf("create agent: %w", err)
1011 }
1012
1013 // Code issue via fosite. Storage.db(txCtx) finds the tx on the
1014 // context, so the INSERT into oauth_auth_codes runs in the same tx.
1015 sess := &oauth.Session{
1016 UserID: userID,
1017 AgentEmail: agentEmail,
1018 Subject: userID,
1019 }
1020 ar.SetSession(sess)
1021 grantConsentedScope(ar, scope)
1022 resp, err := a.oauthProvider.NewAuthorizeResponse(txCtx, ar, sess)
1023 if err != nil {
1024 return err
1025 }
1026
1027 if err := tx.Commit(ctx); err != nil {
1028 return fmt.Errorf("commit tx: %w", err)
1029 }
1030
1031 a.writeAuthorizeRedirect(w, r, ar, resp)
1032 return nil
1033}
1034
1035// writeAuthorizeRedirect emits the 303 redirect to the client's
1036// redirect_uri with fosite's parameters + the RFC 9207 issuer. We

Callers 1

handleOAuthConsentMethod · 0.95

Calls 9

WithTxFunction · 0.92
isUniqueViolationFunction · 0.85
grantConsentedScopeFunction · 0.85
PoolMethod · 0.80
RollbackMethod · 0.80
CreateAgentTxMethod · 0.80
CommitMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected