Commit 963c0b2999

963c0b2999bd0ae9ee5baddaa50bd769cbc531c7

parent: 933f7cf466

Unsigned

Wraient <wraient.911@gmail.com> · 2026-08-02 21:21 UTC

Fix spurious AniList re-auth when tracking with MyAnimeList

With TrackingRemote=myanimelist (or both), user.Token holds the MyAnimeList
JWT. AniList GraphQL calls such as GetAnimeDataByID passed that token to
graphql.anilist.co, which rejects it with "Invalid token"; the app misread
that as AniList expiry and forced a browser OAuth re-auth on almost every run.

Resolve the real AniList token from anilist_token.json for all AniList API
requests, and only open the re-auth browser when the failing token is actually
the stored AniList token.

Layout: unified · split

internal/anilist.go +7
@@ -141,6 +141,7 @@ func min3(a, b, c int) int {
141141}
142142
143143func doAniListSearchRequest(url string, requestBody []byte, token string) ([]byte, error) {
144 token = anilistTokenForAPI(nil, token)
144145 for authAttempt := 0; authAttempt < 2; authAttempt++ {
145146 body, err := doAniListSearchRequestAttempt(url, requestBody, token)
146147 if err == nil {
@@ -929,6 +930,12 @@ func makePostRequest(url, query string, variables map[string]interface{}, header
929930 token = strings.TrimPrefix(authHeader, "Bearer ")
930931 }
931932 }
933 token = anilistTokenForAPI(nil, token)
934 if headers == nil && token != "" {
935 headers = map[string]string{"Authorization": "Bearer " + token}
936 } else if headers != nil && token != "" {
937 headers["Authorization"] = "Bearer " + token
938 }
932939
933940 for authAttempt := 0; authAttempt < 2; authAttempt++ {
934941 response, err := makePostRequestAttempt(url, requestBody, headers)
internal/auth_token.go +27
@@ -28,6 +28,24 @@ func anilistTokenPath(config *CurdConfig) string {
2828 return filepath.Join(os.ExpandEnv(config.StoragePath), "anilist_token.json")
2929}
3030
31// anilistTokenForAPI resolves the real AniList access token for GraphQL calls.
32// Callers pass the tracker token (user.Token), which is the MyAnimeList token
33// when tracking is configured as myanimelist/both; AniList rejects that with
34// "Invalid token", which the app misread as expiry and forced a browser
35// re-auth on every run. Prefer the token persisted at anilist_token.json and
36// fall back to the caller's token only when none is stored.
37func anilistTokenForAPI(config *CurdConfig, current string) string {
38 if config == nil {
39 config = GetGlobalConfig()
40 }
41 if config != nil && strings.TrimSpace(config.StoragePath) != "" {
42 if stored, err := GetTokenFromFile(anilistTokenPath(config)); err == nil && strings.TrimSpace(stored) != "" {
43 return stored
44 }
45 }
46 return current
47}
48
3149func ReauthenticateAniList(config *CurdConfig, user *User, reason string) (string, error) {
3250 if config == nil {
3351 config = GetGlobalConfig()
@@ -69,6 +87,15 @@ func tryRenewAniListTokenForAPI(currentToken string, reason string) (string, boo
6987 return currentToken, false, nil
7088 }
7189
90 // The failing token may be a tracker token (e.g. the MyAnimeList token when
91 // tracking is myanimelist/both) rather than the AniList token. Don't force a
92 // browser OAuth re-auth in that case; just use the stored AniList token.
93 if stored, err := GetTokenFromFile(anilistTokenPath(config)); err == nil && strings.TrimSpace(stored) != "" {
94 if currentToken == "" || currentToken != stored {
95 return stored, true, nil
96 }
97 }
98
7299 newToken, err := ReauthenticateAniList(config, GetGlobalUser(), reason)
73100 if err != nil {
74101 return currentToken, false, err
internal/auth_token_test.go added +109
@@ -0,0 +1,109 @@
1package internal
2
3import (
4 "path/filepath"
5 "testing"
6 "time"
7)
8
9func writeAnilistTokenFileForTest(t *testing.T, dir, token string) {
10 t.Helper()
11 tok := &AnilistToken{
12 AccessToken: token,
13 TokenType: "Bearer",
14 ExpiresIn: 31536000,
15 ExpiresAt: time.Now().Add(24 * time.Hour),
16 }
17 if err := saveToken(filepath.Join(dir, "anilist_token.json"), tok); err != nil {
18 t.Fatalf("saveToken: %v", err)
19 }
20}
21
22func withGlobalConfig(t *testing.T, config *CurdConfig) {
23 t.Helper()
24 previous := GetGlobalConfig()
25 SetGlobalConfig(config)
26 t.Cleanup(func() { SetGlobalConfig(previous) })
27}
28
29func TestAnilistTokenForAPIPrefersStoredToken(t *testing.T) {
30 dir := t.TempDir()
31 writeAnilistTokenFileForTest(t, dir, "stored-anilist-token")
32 withGlobalConfig(t, &CurdConfig{StoragePath: dir})
33
34 // The tracker token (user.Token) may be a MyAnimeList token; the AniList
35 // call must use the real AniList token from disk instead.
36 if got := anilistTokenForAPI(nil, "mal-jwt-token"); got != "stored-anilist-token" {
37 t.Fatalf("anilistTokenForAPI = %q, want stored-anilist-token", got)
38 }
39}
40
41func TestAnilistTokenForAPIFallsBackToCurrentWhenNoStoredToken(t *testing.T) {
42 dir := t.TempDir()
43 withGlobalConfig(t, &CurdConfig{StoragePath: dir})
44
45 if got := anilistTokenForAPI(nil, "some-token"); got != "some-token" {
46 t.Fatalf("anilistTokenForAPI = %q, want some-token", got)
47 }
48}
49
50func TestAnilistTokenForAPIFallsBackWhenNoGlobalConfig(t *testing.T) {
51 withGlobalConfig(t, nil)
52
53 if got := anilistTokenForAPI(nil, "some-token"); got != "some-token" {
54 t.Fatalf("anilistTokenForAPI = %q, want some-token", got)
55 }
56}
57
58func TestTryRenewUsesStoredTokenInsteadOfBrowserReauth(t *testing.T) {
59 dir := t.TempDir()
60 writeAnilistTokenFileForTest(t, dir, "stored-anilist-token")
61 withGlobalConfig(t, &CurdConfig{StoragePath: dir})
62
63 // A MyAnimeList tracker token must not trigger a browser re-auth: the real
64 // AniList token is simply swapped in and the request retried.
65 newToken, renewed, err := tryRenewAniListTokenForAPI("mal-jwt-token", "invalid token")
66 if err != nil {
67 t.Fatalf("tryRenewAniListTokenForAPI error: %v", err)
68 }
69 if !renewed {
70 t.Fatal("expected renewed=true")
71 }
72 if newToken != "stored-anilist-token" {
73 t.Fatalf("renewed token = %q, want stored-anilist-token", newToken)
74 }
75}
76
77func TestTryRenewEmptyTokenUsesStoredToken(t *testing.T) {
78 dir := t.TempDir()
79 writeAnilistTokenFileForTest(t, dir, "stored-anilist-token")
80 withGlobalConfig(t, &CurdConfig{StoragePath: dir})
81
82 newToken, renewed, err := tryRenewAniListTokenForAPI("", "invalid token")
83 if err != nil {
84 t.Fatalf("tryRenewAniListTokenForAPI error: %v", err)
85 }
86 if !renewed {
87 t.Fatal("expected renewed=true")
88 }
89 if newToken != "stored-anilist-token" {
90 t.Fatalf("renewed token = %q, want stored-anilist-token", newToken)
91 }
92}
93
94func TestTryRenewNoGlobalConfigFallsThrough(t *testing.T) {
95 withGlobalConfig(t, nil)
96
97 // Without global config there is no stored token to compare against, so the
98 // function must not panic and simply report no renewal.
99 newToken, renewed, err := tryRenewAniListTokenForAPI("token", "invalid token")
100 if err != nil {
101 t.Fatalf("tryRenewAniListTokenForAPI error: %v", err)
102 }
103 if renewed {
104 t.Fatal("expected renewed=false when no global config")
105 }
106 if newToken != "token" {
107 t.Fatalf("renewed token = %q, want original token", newToken)
108 }
109}