mintAuthCode drives fosite's authorize flow inline to produce a code for the given user/client. This is what the consent handler will do in slice 5; for slice 4's HTTP test we just need a code to exchange at /token.
(t *testing.T, provider fosite.OAuth2Provider, clientID, userID, redirectURI, challenge string)
| 115 | // in slice 5; for slice 4's HTTP test we just need a code to exchange |
| 116 | // at /token. |
| 117 | func mintAuthCode(t *testing.T, provider fosite.OAuth2Provider, clientID, userID, redirectURI, challenge string) string { |
| 118 | t.Helper() |
| 119 | ctx := context.Background() |
| 120 | q := url.Values{} |
| 121 | q.Set("response_type", "code") |
| 122 | q.Set("client_id", clientID) |
| 123 | q.Set("redirect_uri", redirectURI) |
| 124 | q.Set("scope", "mcp") |
| 125 | q.Set("state", "abc123abc123abc123") |
| 126 | q.Set("code_challenge", challenge) |
| 127 | q.Set("code_challenge_method", "S256") |
| 128 | |
| 129 | authReq, _ := http.NewRequest("GET", "https://test.e2a.dev/oauth2/authorize?"+q.Encode(), nil) |
| 130 | ar, err := provider.NewAuthorizeRequest(ctx, authReq) |
| 131 | if err != nil { |
| 132 | t.Fatalf("NewAuthorizeRequest: %v", err) |
| 133 | } |
| 134 | ar.SetSession(&oauth.Session{UserID: userID, AgentEmail: "agent@example.com", Subject: userID}) |
| 135 | ar.GrantScope("mcp") |
| 136 | |
| 137 | resp, err := provider.NewAuthorizeResponse(ctx, ar, ar.GetSession()) |
| 138 | if err != nil { |
| 139 | t.Fatalf("NewAuthorizeResponse: %v", err) |
| 140 | } |
| 141 | rec := httptest.NewRecorder() |
| 142 | provider.WriteAuthorizeResponse(ctx, rec, ar, resp) |
| 143 | loc, err := url.Parse(rec.Header().Get("Location")) |
| 144 | if err != nil { |
| 145 | t.Fatalf("parse Location: %v", err) |
| 146 | } |
| 147 | code := loc.Query().Get("code") |
| 148 | if code == "" { |
| 149 | t.Fatalf("no code in redirect: %s", rec.Header().Get("Location")) |
| 150 | } |
| 151 | return code |
| 152 | } |
| 153 | |
| 154 | // TestHTTP_Token_AuthCode covers the happy path: POST /oauth2/token |
| 155 | // with an auth_code grant + PKCE verifier yields an access+refresh |
no test coverage detected