Skip to content

Commit

Permalink
fixed critical issue with auth filtering and id ban
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowbluesky committed May 17, 2019
1 parent 8e47579 commit a54387e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class PixivArtProviderDefines {
public static final String APP_VERSION = "6.9.0";
public static final String APP_USER_AGENT =
"PixivIOSApp/6.7.1 (iOS 10.3.1; iPhone8,1)";
public static final String CLIENT_ID = "bYGKuGVw91e0NMfPGp44euvGt59s";
public static final String CLIENT_SECRET = "HP3RmkgAmEGro0gn1x9ioawQE8WMfvLXDz3ZqxpK";
public static final String CLIENT_ID = "MOBrBDS8blbauoSck0ZfDbtuzpyT";
public static final String CLIENT_SECRET = "lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj";

// urls
public static final String PIXIV_HOST = "https://www.pixiv.net";
Expand Down
29 changes: 18 additions & 11 deletions app/src/main/java/com/antony/muzei/pixiv/PixivArtWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import androidx.work.Constraints;
import androidx.work.ExistingWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
Expand Down Expand Up @@ -54,10 +55,12 @@ static void enqueueLoad()
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
WorkRequest request = new OneTimeWorkRequest.Builder(PixivArtWorker.class)
OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(PixivArtWorker.class)
.addTag(LOG_TAG)
.setConstraints(constraints)
.build();
manager.enqueue(request);
//manager.enqueue(request);
manager.enqueueUniqueWork(LOG_TAG, ExistingWorkPolicy.KEEP, request);
}

// Acquires an access token and refresh token from a username / password pair
Expand All @@ -83,12 +86,14 @@ private Response authLogin(String loginId, String loginPassword) throws IOExcept
// It is up to the caller to handle any errors
private Response authRefreshToken(String refreshToken) throws IOException, JSONException
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

Uri authQuery = new Uri.Builder()
.appendQueryParameter("get_secure_url", Integer.toString(1))
.appendQueryParameter("client_id", PixivArtProviderDefines.CLIENT_ID)
.appendQueryParameter("client_secret", PixivArtProviderDefines.CLIENT_SECRET)
.appendQueryParameter("grant_type", "refresh_token")
.appendQueryParameter("refresh_token", refreshToken)
.appendQueryParameter("refresh_token", sharedPrefs.getString("refreshToken", ""))
.build();

Response response = sendPostRequest(PixivArtProviderDefines.OAUTH_URL, authQuery);
Expand All @@ -104,7 +109,6 @@ private void storeTokens(JSONObject tokens) throws JSONException
editor.putLong("accessTokenIssueTime", (System.currentTimeMillis() / 1000));
editor.putString("refreshToken", tokens.getString("refresh_token"));
editor.putString("userId", tokens.getJSONObject("user").getString("id"));
editor.putString("deviceToken", tokens.getString("device_token"));
// Not yet tested, but I believe that this needs to be a commit() and not an apply()
// Muzei queues up many picture requests at one. Almost all of them will not have an access token to use
editor.commit();
Expand All @@ -119,7 +123,8 @@ private String getAccessToken()
// If we possess an access token, AND it has not expired, instantly return it
// Must be a divide by 1000, cannot be subtract 3600 * 1000
String accessToken = sharedPrefs.getString("accessToken", "");
long accessTokenIssueTime = sharedPrefs.getLong("accessTokenIssueTime", 0);
//long accessTokenIssueTime = sharedPrefs.getLong("accessTokenIssueTime", 0);
long accessTokenIssueTime = 1;
if (!accessToken.isEmpty() && accessTokenIssueTime > (System.currentTimeMillis() / 1000) - 3600)
{
Log.i(LOG_TAG, "Existing access token found");
Expand All @@ -142,10 +147,12 @@ private String getAccessToken()
}
else
{
Log.d(LOG_TAG, "using refresh token");
String refreshToken = sharedPrefs.getString("refreshToken", "");
response = authRefreshToken(refreshToken);
}
JSONObject authResponseBody = new JSONObject(response.body().string());
Log.d(LOG_TAG, authResponseBody.toString());
response.close();

if (authResponseBody.has("has_error"))
Expand Down Expand Up @@ -322,7 +329,7 @@ private JSONObject filterPictureFeedBookmark(JSONArray illusts) throws JSONExcep
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean showManga = sharedPrefs.getBoolean("pref_showManga", false);
int nsfwFilteringLevel = Integer.parseInt(sharedPrefs.getString("pref_nsfwFilteringLevel", "0"));
int nsfwFilteringLevel = Integer.parseInt(sharedPrefs.getString("pref_nsfwFilterLevel", "0"));
Log.i(LOG_TAG, "NSFW filter level set to: " + nsfwFilteringLevel);
Random random = new Random();

Expand Down Expand Up @@ -376,7 +383,7 @@ private JSONObject filterPictureRanking(JSONArray contents) throws JSONException
Log.d(LOG_TAG, "Selecting ranking");
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean showManga = sharedPrefs.getBoolean("pref_showManga", false);
int nsfwFilteringLevel = Integer.parseInt(sharedPrefs.getString("pref_nsfwFilteringLevel", "0"));
int nsfwFilteringLevel = Integer.parseInt(sharedPrefs.getString("pref_nsfwFilterLevel", "0"));
JSONObject pictureMetadata;
Random random = new Random();

Expand All @@ -392,7 +399,7 @@ private JSONObject filterPictureRanking(JSONArray contents) throws JSONException
}
}
// If user does not want NSFW images to show
if (nsfwFilterLevel < 4)
if (nsfwFilteringLevel < 4)
{
Log.d(LOG_TAG, "Checking NSFW level of pulled picture");
while (pictureMetadata.getJSONObject("illust_content_type").getInt("sexual") != 0)
Expand Down Expand Up @@ -437,7 +444,7 @@ private Artwork getPictureFeedOrBookmark(String mode, String accessToken) throws
{
Log.d(LOG_TAG, "Getting feed or bookmark");
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String updateUri = getUpdateUriInfo(mode, sharedPrefs.getString("userId", ""))
String updateUri = getUpdateUriInfo(mode, sharedPrefs.getString("userId", ""));
Response rankingResponse = sendGetRequest(updateUri, accessToken);

JSONObject overallJson = new JSONObject((rankingResponse.body().string()));
Expand Down Expand Up @@ -498,8 +505,8 @@ public Result doWork()
{
// TODO somehow make this Log be a toast message
Log.i(LOG_TAG, "Authentication failed, switching update mode to daily ranking");
sharedPrefs.edit().putString("pref_updateMode", "daily_rank").apply();
mode = "daily_ranking";
// sharedPrefs.edit().putString("pref_updateMode", "daily_rank").apply();
// mode = "daily_ranking";
}
}

Expand Down

0 comments on commit a54387e

Please sign in to comment.