Commit 963c0b2999
Unsigned
Layout: unified · split
internal/anilist.go +7
| @@ -141,6 +141,7 @@ func min3(a, b, c int) int { | |||
| 141 | } | 141 | } |
| 142 | 142 | ||
| 143 | func doAniListSearchRequest(url string, requestBody []byte, token string) ([]byte, error) { | 143 | func doAniListSearchRequest(url string, requestBody []byte, token string) ([]byte, error) { |
| 144 | token = anilistTokenForAPI(nil, token) | ||
| 144 | for authAttempt := 0; authAttempt < 2; authAttempt++ { | 145 | for authAttempt := 0; authAttempt < 2; authAttempt++ { |
| 145 | body, err := doAniListSearchRequestAttempt(url, requestBody, token) | 146 | body, err := doAniListSearchRequestAttempt(url, requestBody, token) |
| 146 | if err == nil { | 147 | if err == nil { |
| @@ -929,6 +930,12 @@ func makePostRequest(url, query string, variables map[string]interface{}, header | |||
| 929 | token = strings.TrimPrefix(authHeader, "Bearer ") | 930 | token = strings.TrimPrefix(authHeader, "Bearer ") |
| 930 | } | 931 | } |
| 931 | } | 932 | } |
| 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 | } | ||
| 932 | 939 | ||
| 933 | for authAttempt := 0; authAttempt < 2; authAttempt++ { | 940 | for authAttempt := 0; authAttempt < 2; authAttempt++ { |
| 934 | response, err := makePostRequestAttempt(url, requestBody, headers) | 941 | response, err := makePostRequestAttempt(url, requestBody, headers) |
internal/auth_token.go +27
| @@ -28,6 +28,24 @@ func anilistTokenPath(config *CurdConfig) string { | |||
| 28 | return filepath.Join(os.ExpandEnv(config.StoragePath), "anilist_token.json") | 28 | return filepath.Join(os.ExpandEnv(config.StoragePath), "anilist_token.json") |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 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. | ||
| 37 | func 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 | |||
| 31 | func ReauthenticateAniList(config *CurdConfig, user *User, reason string) (string, error) { | 49 | func ReauthenticateAniList(config *CurdConfig, user *User, reason string) (string, error) { |
| 32 | if config == nil { | 50 | if config == nil { |
| 33 | config = GetGlobalConfig() | 51 | config = GetGlobalConfig() |
| @@ -69,6 +87,15 @@ func tryRenewAniListTokenForAPI(currentToken string, reason string) (string, boo | |||
| 69 | return currentToken, false, nil | 87 | return currentToken, false, nil |
| 70 | } | 88 | } |
| 71 | 89 | ||
| 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 | |||
| 72 | newToken, err := ReauthenticateAniList(config, GetGlobalUser(), reason) | 99 | newToken, err := ReauthenticateAniList(config, GetGlobalUser(), reason) |
| 73 | if err != nil { | 100 | if err != nil { |
| 74 | return currentToken, false, err | 101 | return currentToken, false, err |
internal/auth_token_test.go added +109
| @@ -0,0 +1,109 @@ | |||
| 1 | package internal | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "path/filepath" | ||
| 5 | "testing" | ||
| 6 | "time" | ||
| 7 | ) | ||
| 8 | |||
| 9 | func 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 | |||
| 22 | func withGlobalConfig(t *testing.T, config *CurdConfig) { | ||
| 23 | t.Helper() | ||
| 24 | previous := GetGlobalConfig() | ||
| 25 | SetGlobalConfig(config) | ||
| 26 | t.Cleanup(func() { SetGlobalConfig(previous) }) | ||
| 27 | } | ||
| 28 | |||
| 29 | func 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 | |||
| 41 | func 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 | |||
| 50 | func 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 | |||
| 58 | func 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 | |||
| 77 | func 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 | |||
| 94 | func 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 | } | ||