Browse Source

Minor auth and activity improvements

pull/24/head
Ezerous 7 years ago
parent
commit
99f6f9a9c1
  1. 2
      app/src/main/AndroidManifest.xml
  2. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java
  3. 34
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

2
app/src/main/AndroidManifest.xml

@ -61,7 +61,7 @@
android:windowSoftInputMode="adjustPan" /> android:windowSoftInputMode="adjustPan" />
<activity <activity
android:name=".activities.AboutActivity" android:name=".activities.AboutActivity"
android:launchMode="singleInstance" android:launchMode="singleTop"
android:parentActivityName=".activities.main.MainActivity" android:parentActivityName=".activities.main.MainActivity"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<meta-data <meta-data

6
app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java

@ -16,6 +16,7 @@ import gr.thmmy.mthmmy.activities.main.MainActivity;
import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.base.BaseActivity;
import timber.log.Timber; import timber.log.Timber;
import static gr.thmmy.mthmmy.session.SessionManager.BANNED_USER;
import static gr.thmmy.mthmmy.session.SessionManager.CONNECTION_ERROR; import static gr.thmmy.mthmmy.session.SessionManager.CONNECTION_ERROR;
import static gr.thmmy.mthmmy.session.SessionManager.EXCEPTION; import static gr.thmmy.mthmmy.session.SessionManager.EXCEPTION;
import static gr.thmmy.mthmmy.session.SessionManager.FAILURE; import static gr.thmmy.mthmmy.session.SessionManager.FAILURE;
@ -175,6 +176,11 @@ public class LoginActivity extends BaseActivity {
"Wrong password!", Toast.LENGTH_LONG).show(); "Wrong password!", Toast.LENGTH_LONG).show();
inputPassword.requestFocus(); inputPassword.requestFocus();
break; break;
case BANNED_USER:
Toast.makeText(getApplicationContext(),
"You are banned!", Toast.LENGTH_LONG).show();
inputPassword.requestFocus();
break;
case FAILURE: case FAILURE:
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
"Login failed...", Toast.LENGTH_LONG).show(); "Login failed...", Toast.LENGTH_LONG).show();

34
app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

@ -47,6 +47,7 @@ public class SessionManager {
public static final int CANCELLED = 4; public static final int CANCELLED = 4;
public static final int CONNECTION_ERROR = 5; public static final int CONNECTION_ERROR = 5;
public static final int EXCEPTION = 6; public static final int EXCEPTION = 6;
public static final int BANNED_USER = 7;
// Client & Cookies // Client & Cookies
private final OkHttpClient client; private final OkHttpClient client;
@ -114,8 +115,8 @@ public class SessionManager {
setPersistentCookieSession(); //Store cookies setPersistentCookieSession(); //Store cookies
//Edit SharedPreferences, save session's data //Edit SharedPreferences, save session's data
setLoginScreenAsDefault(false);
sharedPrefs.edit().putBoolean(LOGGED_IN, true).apply(); sharedPrefs.edit().putBoolean(LOGGED_IN, true).apply();
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, false).apply();
sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply();
String avatar = extractAvatarLink(document); String avatar = extractAvatarLink(document);
if (avatar != null) { if (avatar != null) {
@ -133,17 +134,23 @@ public class SessionManager {
//Investigate login failure //Investigate login failure
Elements error = document.select("b:contains(That username does not exist.)"); Elements error = document.select("b:contains(That username does not exist.)");
if (error.size() == 1) { //Wrong username if (error.size() > 0) { //Wrong username
Timber.i("Wrong Username"); Timber.i("Wrong Username");
return WRONG_USER; return WRONG_USER;
} }
error = document.select("body:contains(Password incorrect)"); error = document.select("body:contains(Password incorrect)");
if (error.size() == 1) { //Wrong password if (error.size() > 0) { //Wrong password
Timber.i("Wrong Password"); Timber.i("Wrong Password");
return WRONG_PASSWORD; return WRONG_PASSWORD;
} }
error = document.select("body:contains(you are banned from using this forum!),body:contains(έχετε αποκλειστεί από αυτή τη δημόσια συζήτηση!)");
if (error.size() > 0) { //User is banned
Timber.i("User is banned");
return BANNED_USER;
}
//Other error e.g. session was reset server-side //Other error e.g. session was reset server-side
clearSessionData(); //Clear invalid saved data clearSessionData(); //Clear invalid saved data
return FAILURE; return FAILURE;
@ -180,7 +187,7 @@ public class SessionManager {
} else if (isLoginScreenDefault()) } else if (isLoginScreenDefault())
return; return;
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, true).apply(); setLoginScreenAsDefault(true);
clearSessionData(); clearSessionData();
} }
@ -190,7 +197,7 @@ public class SessionManager {
public void guestLogin() { public void guestLogin() {
Timber.i("Continuing as a guest, as chosen by the user."); Timber.i("Continuing as a guest, as chosen by the user.");
clearSessionData(); clearSessionData();
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, false).apply(); setLoginScreenAsDefault(false);
} }
@ -219,7 +226,7 @@ public class SessionManager {
return FAILURE; return FAILURE;
} }
} catch (IOException e) { } catch (IOException e) {
Timber.w("Logout IOException", e); Timber.w(e, "Logout IOException");
return CONNECTION_ERROR; return CONNECTION_ERROR;
} catch (Exception e) { } catch (Exception e) {
Timber.e(e, "Logout Exception"); Timber.e(e, "Logout Exception");
@ -253,16 +260,17 @@ public class SessionManager {
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true);
} }
public String getCookieHeader() {
return cookiePersistor.loadAll().get(0).toString();
}
//--------------------------------------GETTERS END--------------------------------------------- //--------------------------------------GETTERS END---------------------------------------------
//------------------------------------OTHER FUNCTIONS------------------------------------------- //------------------------------------OTHER FUNCTIONS-------------------------------------------
private boolean validateRetrievedCookies() { private boolean validateRetrievedCookies() {
List<Cookie> cookieList = cookieJar.loadForRequest(indexUrl); List<Cookie> cookieList = cookieJar.loadForRequest(indexUrl);
return (cookieList.size() == 2) && (cookieList.get(0).name().equals("THMMYgrC00ki3")) && (cookieList.get(1).name().equals("PHPSESSID")); for(Cookie cookie: cookieList)
{
if(cookie.name().equals("THMMYgrC00ki3"))
return true;
}
return false;
} }
// Call validateRetrievedCookies() first // Call validateRetrievedCookies() first
@ -288,6 +296,10 @@ public class SessionManager {
Timber.i("Session data cleared."); Timber.i("Session data cleared.");
} }
private void setLoginScreenAsDefault(boolean b){
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, b).apply();
}
@NonNull @NonNull
private String extractUserName(@NonNull Document doc) { private String extractUserName(@NonNull Document doc) {
//Scribbles2 Theme //Scribbles2 Theme

Loading…
Cancel
Save