authorization flow work

This commit is contained in:
2026-04-29 13:44:32 -06:00
parent e242cc71d8
commit 64f437f2e5
4 changed files with 106 additions and 26 deletions
+49 -4
View File
@@ -18,11 +18,12 @@ import (
const (
authURL = "https://twitter.com/i/oauth2/authorize"
tokenURL = "https://api.twitter.com/2/oauth2/token"
scopes = "tweet.read tweet.write users.read"
scopes = "tweet.read tweet.write users.read offline.access"
)
type Result struct {
AccessToken string
AccessToken string
RefreshToken string
}
func listenOnFreePort() (net.Listener, string, error) {
@@ -143,7 +144,8 @@ func exchangeCode(ctx context.Context, clientID, clientSecret, code, verifier, r
}
var out struct {
AccessToken string `json:"access_token"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
if err := json.Unmarshal(raw, &out); err != nil {
return nil, err
@@ -151,7 +153,50 @@ func exchangeCode(ctx context.Context, clientID, clientSecret, code, verifier, r
if out.AccessToken == "" {
return nil, fmt.Errorf("no access_token in response: %s", raw)
}
return &Result{AccessToken: out.AccessToken}, nil
return &Result{AccessToken: out.AccessToken, RefreshToken: out.RefreshToken}, nil
}
// RefreshTwitterToken exchanges a refresh token for a new access token.
func RefreshTwitterToken(ctx context.Context, clientID, clientSecret, refreshToken string) (*Result, error) {
data := url.Values{
"grant_type": {"refresh_token"},
"refresh_token": {refreshToken},
}
if clientSecret == "" {
data.Set("client_id", clientID)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if clientSecret != "" {
req.SetBasicAuth(clientID, clientSecret)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
raw, _ := io.ReadAll(res.Body)
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("token refresh HTTP %d: %s", res.StatusCode, raw)
}
var out struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
if err := json.Unmarshal(raw, &out); err != nil {
return nil, err
}
if out.AccessToken == "" {
return nil, fmt.Errorf("no access_token in refresh response: %s", raw)
}
return &Result{AccessToken: out.AccessToken, RefreshToken: out.RefreshToken}, nil
}
// LinkedInOAuth2 runs the standard OAuth 2.0 authorization code flow for
+24 -14
View File
@@ -12,10 +12,11 @@ import (
)
type TwitterCreds struct {
BearerToken string `yaml:"bearer_token"`
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
OAuth2AccessToken string `yaml:"oauth2_access_token"`
BearerToken string `yaml:"bearer_token"`
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
OAuth2AccessToken string `yaml:"oauth2_access_token"`
OAuth2RefreshToken string `yaml:"oauth2_refresh_token"`
}
type YouTubeCreds struct {
@@ -120,24 +121,33 @@ func (c *Config) HasLinkedInPost() bool {
}
var (
oauth2TokenRe = regexp.MustCompile(`oauth2_access_token:\s*"[^"]*"`)
liAccessTokenRe = regexp.MustCompile(`access_token:\s*"[^"]*"`)
liAuthorURNRe = regexp.MustCompile(`author_urn:\s*"[^"]*"`)
liSectionRe = regexp.MustCompile(`^ linkedin:\s*$`)
credentialSectionRe = regexp.MustCompile(`^ [a-z]`)
oauth2AccessTokenRe = regexp.MustCompile(`oauth2_access_token:\s*"[^"]*"`)
oauth2RefreshTokenRe = regexp.MustCompile(`oauth2_refresh_token:\s*"[^"]*"`)
liAccessTokenRe = regexp.MustCompile(`access_token:\s*"[^"]*"`)
liAuthorURNRe = regexp.MustCompile(`author_urn:\s*"[^"]*"`)
liSectionRe = regexp.MustCompile(`^ linkedin:\s*$`)
credentialSectionRe = regexp.MustCompile(`^ [a-z]`)
)
// UpdateOAuth2Token rewrites the oauth2_access_token value in the named
// config file in-place, preserving all other content and formatting.
func UpdateOAuth2Token(path, token string) error {
// UpdateOAuth2Tokens rewrites both oauth2_access_token and oauth2_refresh_token
// in the named config file in-place, preserving all other content and formatting.
// If refreshToken is empty the refresh_token field is left unchanged.
func UpdateOAuth2Tokens(path, accessToken, refreshToken string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
updated := oauth2TokenRe.ReplaceAllLiteralString(string(data), `oauth2_access_token: "`+token+`"`)
if updated == string(data) {
s := string(data)
updated := oauth2AccessTokenRe.ReplaceAllLiteralString(s, `oauth2_access_token: "`+accessToken+`"`)
if updated == s {
return fmt.Errorf("oauth2_access_token field not found in %s", path)
}
if refreshToken != "" {
updated = oauth2RefreshTokenRe.ReplaceAllLiteralString(updated, `oauth2_refresh_token: "`+refreshToken+`"`)
}
return os.WriteFile(path, []byte(updated), 0600)
}