MCPcopy Create free account
hub / github.com/basecamp/hey-cli / exchangeCode

Function exchangeCode

internal/auth/oauth.go:27–68  ·  view source on GitHub ↗

exchangeCode exchanges an authorization code for tokens using PKCE.

(ctx context.Context, httpClient *http.Client, tokenEndpoint, code, redirectURI, clientID, codeVerifier, installID string)

Source from the content-addressed store, hash-verified

25
26// exchangeCode exchanges an authorization code for tokens using PKCE.
27func exchangeCode(ctx context.Context, httpClient *http.Client, tokenEndpoint, code, redirectURI, clientID, codeVerifier, installID string) (*OAuthToken, error) {
28 data := url.Values{
29 "grant_type": {"authorization_code"},
30 "client_id": {clientID},
31 "code": {code},
32 "redirect_uri": {redirectURI},
33 "code_verifier": {codeVerifier},
34 "install_id": {installID},
35 }
36
37 req, err := http.NewRequestWithContext(ctx, "POST", tokenEndpoint, strings.NewReader(data.Encode()))
38 if err != nil {
39 return nil, fmt.Errorf("creating token request: %w", err)
40 }
41 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
42
43 resp, err := httpClient.Do(req)
44 if err != nil {
45 return nil, fmt.Errorf("token exchange request failed: %w", err)
46 }
47 defer resp.Body.Close()
48
49 body, err := io.ReadAll(io.LimitReader(resp.Body, 64<<10))
50 if err != nil {
51 return nil, fmt.Errorf("reading token response: %w", err)
52 }
53
54 if resp.StatusCode != http.StatusOK {
55 return nil, fmt.Errorf("token exchange failed (status %d): %s", resp.StatusCode, string(body))
56 }
57
58 var token OAuthToken
59 if err := json.Unmarshal(body, &token); err != nil {
60 return nil, fmt.Errorf("parsing token response: %w", err)
61 }
62
63 if token.ExpiresIn > 0 {
64 token.ExpiresAt = time.Now().Add(time.Duration(token.ExpiresIn) * time.Second)
65 }
66
67 return &token, nil
68}
69
70// refreshOAuthToken refreshes an access token using a refresh token.
71func refreshOAuthToken(ctx context.Context, httpClient *http.Client, tokenEndpoint, refreshTok, clientID, installID string) (*OAuthToken, error) {

Callers 1

LoginMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected