mirror of
https://github.com/Alvin-Zilverstand/Twitter-X-Media-Batch-Downloader.git
synced 2026-12-01 02:27:54 +01:00
v4.5
This commit is contained in:
1 parent
84b067ebd4
commit
e67d09a248
47 files changed
+3688
-958
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/zalando/go-keyring"
|
||||
)
|
||||
|
||||
const authTokenKeyringService = "TwitterXMediaBatchDownloader"
|
||||
|
||||
func normalizeAuthTokenSlot(slot string) (string, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(slot)) {
|
||||
case "public":
|
||||
return "public", nil
|
||||
case "private":
|
||||
return "private", nil
|
||||
default:
|
||||
return "", fmt.Errorf("invalid auth token slot: %q", slot)
|
||||
}
|
||||
}
|
||||
|
||||
func authTokenKeyringUser(slot string) string {
|
||||
return "auth-token-" + slot
|
||||
}
|
||||
|
||||
func GetStoredAuthToken(slot string) (string, error) {
|
||||
normalizedSlot, err := normalizeAuthTokenSlot(slot)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
token, err := keyring.Get(authTokenKeyringService, authTokenKeyringUser(normalizedSlot))
|
||||
if errors.Is(err, keyring.ErrNotFound) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func SetStoredAuthToken(slot, token string) error {
|
||||
normalizedSlot, err := normalizeAuthTokenSlot(slot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if strings.TrimSpace(token) == "" {
|
||||
return ClearStoredAuthToken(normalizedSlot)
|
||||
}
|
||||
|
||||
return keyring.Set(authTokenKeyringService, authTokenKeyringUser(normalizedSlot), token)
|
||||
}
|
||||
|
||||
func ClearStoredAuthToken(slot string) error {
|
||||
normalizedSlot, err := normalizeAuthTokenSlot(slot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = keyring.Delete(authTokenKeyringService, authTokenKeyringUser(normalizedSlot))
|
||||
if errors.Is(err, keyring.ErrNotFound) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user