fixing port number
This commit is contained in:
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"permissions": {
|
||||||
|
"allow": [
|
||||||
|
"Read(//home/david/go/**)",
|
||||||
|
"Read(//home/david/**)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
-11
@@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -17,7 +18,6 @@ import (
|
|||||||
const (
|
const (
|
||||||
authURL = "https://twitter.com/i/oauth2/authorize"
|
authURL = "https://twitter.com/i/oauth2/authorize"
|
||||||
tokenURL = "https://api.twitter.com/2/oauth2/token"
|
tokenURL = "https://api.twitter.com/2/oauth2/token"
|
||||||
redirectURI = "http://localhost:8080/callback"
|
|
||||||
scopes = "tweet.read tweet.write users.read"
|
scopes = "tweet.read tweet.write users.read"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,8 +25,17 @@ type Result struct {
|
|||||||
AccessToken string
|
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
|
// 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.
|
// authorization code for a user access token.
|
||||||
func TwitterOAuth2(ctx context.Context, clientID, clientSecret string) (*Result, error) {
|
func TwitterOAuth2(ctx context.Context, clientID, clientSecret string) (*Result, error) {
|
||||||
verifier, challenge, state, err := generatePKCE()
|
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)
|
return nil, fmt.Errorf("generating PKCE values: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ln, redirectURI, err := listenOnFreePort()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
params := url.Values{
|
params := url.Values{
|
||||||
"response_type": {"code"},
|
"response_type": {"code"},
|
||||||
"client_id": {clientID},
|
"client_id": {clientID},
|
||||||
@@ -49,7 +63,7 @@ func TwitterOAuth2(ctx context.Context, clientID, clientSecret string) (*Result,
|
|||||||
errCh := make(chan error, 1)
|
errCh := make(chan error, 1)
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
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) {
|
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
|
||||||
q := r.URL.Query()
|
q := r.URL.Query()
|
||||||
@@ -75,7 +89,7 @@ func TwitterOAuth2(ctx context.Context, clientID, clientSecret string) (*Result,
|
|||||||
})
|
})
|
||||||
|
|
||||||
go func() {
|
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)
|
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 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{
|
data := url.Values{
|
||||||
"grant_type": {"authorization_code"},
|
"grant_type": {"authorization_code"},
|
||||||
"code": {code},
|
"code": {code},
|
||||||
@@ -148,6 +162,11 @@ func LinkedInOAuth2(ctx context.Context, clientID, clientSecret string) (*Result
|
|||||||
return nil, fmt.Errorf("generating state: %w", err)
|
return nil, fmt.Errorf("generating state: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ln, redirectURI, err := listenOnFreePort()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
params := url.Values{
|
params := url.Values{
|
||||||
"response_type": {"code"},
|
"response_type": {"code"},
|
||||||
"client_id": {clientID},
|
"client_id": {clientID},
|
||||||
@@ -161,7 +180,7 @@ func LinkedInOAuth2(ctx context.Context, clientID, clientSecret string) (*Result
|
|||||||
errCh := make(chan error, 1)
|
errCh := make(chan error, 1)
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
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) {
|
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
|
||||||
q := r.URL.Query()
|
q := r.URL.Query()
|
||||||
@@ -187,7 +206,7 @@ func LinkedInOAuth2(ctx context.Context, clientID, clientSecret string) (*Result
|
|||||||
})
|
})
|
||||||
|
|
||||||
go func() {
|
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)
|
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 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{
|
data := url.Values{
|
||||||
"grant_type": {"authorization_code"},
|
"grant_type": {"authorization_code"},
|
||||||
"code": {code},
|
"code": {code},
|
||||||
@@ -269,4 +288,3 @@ func generateState() (string, error) {
|
|||||||
}
|
}
|
||||||
return base64.RawURLEncoding.EncodeToString(b), nil
|
return base64.RawURLEncoding.EncodeToString(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user