Restore user token precedence and rename GetPixivToken

This commit restores the ability to use a user-provided token when
making API requests and gives it precedence over the default token
provided by tokenManager.

Additionally, the function GetPixivToken has been renamed to
GetUserToken for clarity.
This commit is contained in:
perennial
2024-09-22 02:20:12 +10:00
parent 901286d98e
commit a8aaefef37
13 changed files with 39 additions and 28 deletions
+1 -1
View File
@@ -232,7 +232,7 @@ func GetRelatedArtworks(r *http.Request, id string) ([]ArtworkBrief, error) {
}
func GetArtworkByID(r *http.Request, id string, full bool) (*Illust, error) {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
var illust struct {
Illust
+2 -2
View File
@@ -10,7 +10,7 @@ import (
)
func GetDiscoveryArtwork(r *http.Request, mode string) ([]ArtworkBrief, error) {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
URL := GetDiscoveryURL(mode, 100)
@@ -35,7 +35,7 @@ func GetDiscoveryArtwork(r *http.Request, mode string) ([]ArtworkBrief, error) {
}
func GetDiscoveryNovels(r *http.Request, mode string) ([]NovelBrief, error) {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
URL := GetDiscoveryNovelURL(mode, 100)
+1 -1
View File
@@ -7,7 +7,7 @@ import (
)
func GetNewestArtworks(r *http.Request, worktype string, r18 string) ([]ArtworkBrief, error) {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
URL := GetNewestArtworksURL(worktype, r18, "0")
var body struct {
+2 -2
View File
@@ -116,7 +116,7 @@ func GetNovelSeriesByID(r *http.Request, id string) (NovelSeries, error) {
func GetNovelSeriesContentByID(r *http.Request, id string, page int, perPage int, useToken bool) ([]NovelSeriesContent, error) {
var token string
if useToken {
token = session.GetPixivToken(r)
token = session.GetUserToken(r)
} else {
token = ""
}
@@ -163,7 +163,7 @@ func GetNovelSeriesContentByID(r *http.Request, id string, page int, perPage int
func GetNovelSeriesContentTitlesByID(r *http.Request, id int, useToken bool) ([]NovelSeriesContentTitle, error) {
var token string
if useToken {
token = session.GetPixivToken(r)
token = session.GetUserToken(r)
} else {
token = ""
}
+1 -1
View File
@@ -8,7 +8,7 @@ import (
)
func GetNewestFromFollowing(r *http.Request, mode, page string) ([]ArtworkBrief, error) {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
URL := GetNewestFromFollowingURL(mode, page)
var body struct {
+1 -1
View File
@@ -38,7 +38,7 @@ var selector_img = cascadia.MustCompile("img")
// so the funny thing about Pixiv is that they will return this month's data for a request of a future date
// is it a bug or a feature?
func GetRankingCalendar(r *http.Request, mode string, year, month int) (HTML, error) {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
URL := GetRankingCalendarURL(mode, year, month)
resp, err := API_GET(r.Context(), URL, token)
+19 -10
View File
@@ -35,12 +35,17 @@ func init() {
}
// retryRequest performs a request with automatic retries and token management
func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*retryablehttp.Request, error)) (SimpleHTTPResponse, error) {
func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*retryablehttp.Request, error), userToken string) (SimpleHTTPResponse, error) {
var lastErr error
tokenManager := config.GlobalConfig.TokenManager
for i := 0; i < config.GlobalConfig.APIMaxRetries; i++ {
token := tokenManager.GetToken()
var token *token_manager.Token
if userToken != "" {
token = &token_manager.Token{Value: userToken}
} else {
token = tokenManager.GetToken()
}
if token == nil {
return SimpleHTTPResponse{}, errors.New("All tokens are timed out")
}
@@ -55,7 +60,9 @@ func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*r
end := time.Now()
if err == nil && resp.StatusCode == http.StatusOK {
tokenManager.MarkTokenStatus(token, token_manager.Good)
if userToken == "" {
tokenManager.MarkTokenStatus(token, token_manager.Good)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
@@ -72,7 +79,9 @@ func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*r
lastErr = fmt.Errorf("HTTP status code: %d", resp.StatusCode)
}
tokenManager.MarkTokenStatus(token, token_manager.TimedOut)
if userToken == "" {
tokenManager.MarkTokenStatus(token, token_manager.TimedOut)
}
audit.LogAPIRoundTrip(audit.APIRequestSpan{
RequestId: request_context.GetFromContext(ctx).RequestId,
@@ -97,7 +106,7 @@ func retryRequest(ctx context.Context, reqFunc func(context.Context, string) (*r
}
// API_GET performs a GET request to the Pixiv API with automatic retries
func API_GET(ctx context.Context, url string, _ string) (SimpleHTTPResponse, error) {
func API_GET(ctx context.Context, url string, userToken string) (SimpleHTTPResponse, error) {
return retryRequest(ctx, func(ctx context.Context, token string) (*retryablehttp.Request, error) {
req, err := retryablehttp.NewRequest("GET", url, nil)
if err != nil {
@@ -111,12 +120,12 @@ func API_GET(ctx context.Context, url string, _ string) (SimpleHTTPResponse, err
Value: token,
})
return req, nil
})
}, userToken)
}
// API_GET_UnwrapJson performs a GET request and unwraps the JSON response
func API_GET_UnwrapJson(ctx context.Context, url string, _ string) (string, error) {
resp, err := API_GET(ctx, url, "")
func API_GET_UnwrapJson(ctx context.Context, url string, userToken string) (string, error) {
resp, err := API_GET(ctx, url, userToken)
if err != nil {
return "", err
}
@@ -139,7 +148,7 @@ func API_GET_UnwrapJson(ctx context.Context, url string, _ string) (string, erro
}
// API_POST performs a POST request to the Pixiv API with automatic retries
func API_POST(ctx context.Context, url, payload, _, csrf string, isJSON bool) error {
func API_POST(ctx context.Context, url, payload, userToken, csrf string, isJSON bool) error {
_, err := retryRequest(ctx, func(ctx context.Context, token string) (*retryablehttp.Request, error) {
req, err := retryablehttp.NewRequest("POST", url, bytes.NewBuffer([]byte(payload)))
if err != nil {
@@ -159,7 +168,7 @@ func API_POST(ctx context.Context, url, payload, _, csrf string, isJSON bool) er
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
}
return req, nil
})
}, userToken)
return err
}
+1 -1
View File
@@ -250,7 +250,7 @@ func GetUserArtworksID(r *http.Request, id string, category UserArtCategory, pag
func GetUserArtwork(r *http.Request, id string, category UserArtCategory, page int, getTags bool) (User, error) {
var user User
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
URL := GetUserInformationURL(id)
+3 -3
View File
@@ -11,7 +11,7 @@ import (
)
func AddBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
csrf := session.GetCookie(r, session.Cookie_CSRF)
if token == "" || csrf == "" {
@@ -39,7 +39,7 @@ func AddBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
}
func DeleteBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
csrf := session.GetCookie(r, session.Cookie_CSRF)
if token == "" || csrf == "" {
@@ -63,7 +63,7 @@ func DeleteBookmarkRoute(w http.ResponseWriter, r *http.Request) error {
}
func LikeRoute(w http.ResponseWriter, r *http.Request) error {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
csrf := session.GetCookie(r, session.Cookie_CSRF)
if token == "" || csrf == "" {
+1 -1
View File
@@ -10,7 +10,7 @@ import (
func IndexPage(w http.ResponseWriter, r *http.Request) error {
// If token is set, do the landing request...
if token := session.GetPixivToken(r); token != "" {
if token := session.GetUserToken(r); token != "" {
mode := GetQueryParam(r, "mode", "all")
works, err := core.GetLanding(r, mode)
+3 -3
View File
@@ -20,7 +20,7 @@ func PromptUserToLoginPage(w http.ResponseWriter, r *http.Request) error {
}
func LoginUserPage(w http.ResponseWriter, r *http.Request) error {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
if token == "" {
return PromptUserToLoginPage(w, r)
@@ -34,7 +34,7 @@ func LoginUserPage(w http.ResponseWriter, r *http.Request) error {
}
func LoginBookmarkPage(w http.ResponseWriter, r *http.Request) error {
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
if token == "" {
return PromptUserToLoginPage(w, r)
}
@@ -47,7 +47,7 @@ func LoginBookmarkPage(w http.ResponseWriter, r *http.Request) error {
}
func FollowingWorksPage(w http.ResponseWriter, r *http.Request) error {
if token := session.GetPixivToken(r); token == "" {
if token := session.GetUserToken(r); token == "" {
return PromptUserToLoginPage(w, r)
}
+3 -1
View File
@@ -11,7 +11,9 @@ import (
"codeberg.org/vnpower/pixivfe/v2/config"
)
func GetPixivToken(r *http.Request) string {
// GetUserToken retrieves the authentication token for the Pixiv API from the 'pixivfe-Token' cookie.
// This token takes precedence over the default one provided by tokenManager.
func GetUserToken(r *http.Request) string {
return GetCookie(r, Cookie_Token)
}
+1 -1
View File
@@ -60,7 +60,7 @@ func RenderInner[T any](w io.Writer, variables jet.VarMap, data T) error {
func GetTemplatingVariables(r *http.Request) jet.VarMap {
// Pass in values that we want to be available to all pages here
token := session.GetPixivToken(r)
token := session.GetUserToken(r)
baseURL := utils.Origin(r)
pageURL := r.URL.String()