fixing port number

This commit is contained in:
2026-04-29 13:18:32 -06:00
parent 0aac727cc1
commit e242cc71d8
3 changed files with 46 additions and 15 deletions
+5
View File
@@ -0,0 +1,5 @@
{
"env": {
"PATH": "/run/host/usr/lib/go/bin:/app/bin:/usr/bin:/home/david/.var/app/com.vscodium.codium/data/node/bin:/home/david/.var/app/com.vscodium.codium/data/cargo/bin:/home/david/.var/app/com.vscodium.codium/data/python/bin"
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"permissions": {
"allow": [
"Read(//home/david/go/**)",
"Read(//home/david/**)"
]
}
}
+30 -12
View File
@@ -8,16 +8,16 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"strings"
"time"
)
const (
authURL = "https://twitter.com/i/oauth2/authorize"
tokenURL = "https://api.twitter.com/2/oauth2/token"
redirectURI = "http://localhost:8080/callback"
scopes = "tweet.read tweet.write users.read"
)
@@ -25,8 +25,17 @@ type Result struct {
AccessToken string
}
func listenOnFreePort() (net.Listener, string, error) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, "", fmt.Errorf("finding free port: %w", err)
}
port := ln.Addr().(*net.TCPAddr).Port
return ln, fmt.Sprintf("http://localhost:%d/callback", port), nil
}
// TwitterOAuth2 runs the OAuth 2.0 PKCE flow for Twitter/X. It opens the
// browser, starts a local callback server on :8080, and exchanges the
// browser, starts a local callback server on a free port, and exchanges the
// authorization code for a user access token.
func TwitterOAuth2(ctx context.Context, clientID, clientSecret string) (*Result, error) {
verifier, challenge, state, err := generatePKCE()
@@ -34,6 +43,11 @@ func TwitterOAuth2(ctx context.Context, clientID, clientSecret string) (*Result,
return nil, fmt.Errorf("generating PKCE values: %w", err)
}
ln, redirectURI, err := listenOnFreePort()
if err != nil {
return nil, err
}
params := url.Values{
"response_type": {"code"},
"client_id": {clientID},
@@ -49,7 +63,7 @@ func TwitterOAuth2(ctx context.Context, clientID, clientSecret string) (*Result,
errCh := make(chan error, 1)
mux := http.NewServeMux()
srv := &http.Server{Addr: ":8080", Handler: mux}
srv := &http.Server{Handler: mux}
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
@@ -75,7 +89,7 @@ func TwitterOAuth2(ctx context.Context, clientID, clientSecret string) (*Result,
})
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
if err := srv.Serve(ln); err != nil && err != http.ErrServerClosed {
errCh <- fmt.Errorf("local callback server: %w", err)
}
}()
@@ -94,10 +108,10 @@ func TwitterOAuth2(ctx context.Context, clientID, clientSecret string) (*Result,
return nil, fmt.Errorf("timed out waiting for authorization")
}
return exchangeCode(ctx, clientID, clientSecret, code, verifier)
return exchangeCode(ctx, clientID, clientSecret, code, verifier, redirectURI)
}
func exchangeCode(ctx context.Context, clientID, clientSecret, code, verifier string) (*Result, error) {
func exchangeCode(ctx context.Context, clientID, clientSecret, code, verifier, redirectURI string) (*Result, error) {
data := url.Values{
"grant_type": {"authorization_code"},
"code": {code},
@@ -148,6 +162,11 @@ func LinkedInOAuth2(ctx context.Context, clientID, clientSecret string) (*Result
return nil, fmt.Errorf("generating state: %w", err)
}
ln, redirectURI, err := listenOnFreePort()
if err != nil {
return nil, err
}
params := url.Values{
"response_type": {"code"},
"client_id": {clientID},
@@ -161,7 +180,7 @@ func LinkedInOAuth2(ctx context.Context, clientID, clientSecret string) (*Result
errCh := make(chan error, 1)
mux := http.NewServeMux()
srv := &http.Server{Addr: ":8080", Handler: mux}
srv := &http.Server{Handler: mux}
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
@@ -187,7 +206,7 @@ func LinkedInOAuth2(ctx context.Context, clientID, clientSecret string) (*Result
})
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
if err := srv.Serve(ln); err != nil && err != http.ErrServerClosed {
errCh <- fmt.Errorf("local callback server: %w", err)
}
}()
@@ -206,10 +225,10 @@ func LinkedInOAuth2(ctx context.Context, clientID, clientSecret string) (*Result
return nil, fmt.Errorf("timed out waiting for authorization")
}
return linkedinExchangeCode(ctx, clientID, clientSecret, code)
return linkedinExchangeCode(ctx, clientID, clientSecret, code, redirectURI)
}
func linkedinExchangeCode(ctx context.Context, clientID, clientSecret, code string) (*Result, error) {
func linkedinExchangeCode(ctx context.Context, clientID, clientSecret, code, redirectURI string) (*Result, error) {
data := url.Values{
"grant_type": {"authorization_code"},
"code": {code},
@@ -269,4 +288,3 @@ func generateState() (string, error) {
}
return base64.RawURLEncoding.EncodeToString(b), nil
}