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
+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)
}