|
@ -1,6 +1,7 @@ |
|
|
package gr.auth.databases.flavours.session; |
|
|
package gr.auth.databases.flavours.session; |
|
|
|
|
|
|
|
|
import android.content.SharedPreferences; |
|
|
import android.content.SharedPreferences; |
|
|
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar; |
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar; |
|
|
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; |
|
|
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; |
|
@ -44,8 +45,9 @@ public class SessionManager { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Generic constants
|
|
|
//Generic constants
|
|
|
public static final HttpUrl indexUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?theme=4"); |
|
|
private static final HttpUrl loginUrl = HttpUrl.parse("http://83.212.109.171:8181/api/rest-auth/login/"); |
|
|
private static final HttpUrl loginUrl = HttpUrl.parse("https://www.thmmy.gr/smf/index.php?action=login2"); |
|
|
private static final HttpUrl signupUrl = HttpUrl.parse("http://83.212.109.171:8181/api/rest-auth/registration/"); |
|
|
|
|
|
private static final HttpUrl logoutUrl = HttpUrl.parse("http://83.212.109.171:8181/api/rest-auth/logout/"); |
|
|
|
|
|
|
|
|
// Client & Cookies
|
|
|
// Client & Cookies
|
|
|
private final OkHttpClient client; |
|
|
private final OkHttpClient client; |
|
@ -57,7 +59,6 @@ public class SessionManager { |
|
|
private static final String USERNAME = "Username"; |
|
|
private static final String USERNAME = "Username"; |
|
|
private static final String USER_ID = "UserID"; |
|
|
private static final String USER_ID = "UserID"; |
|
|
private static final String USER_TYPE = "USER_TYPE"; |
|
|
private static final String USER_TYPE = "USER_TYPE"; |
|
|
private static final String LOGOUT_LINK = "LogoutLink"; |
|
|
|
|
|
private static final String LOGGED_IN = "LoggedIn"; |
|
|
private static final String LOGGED_IN = "LoggedIn"; |
|
|
private static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; |
|
|
private static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; |
|
|
|
|
|
|
|
@ -73,63 +74,78 @@ public class SessionManager { |
|
|
public int login(String... strings) { |
|
|
public int login(String... strings) { |
|
|
//Builds the login request for each case
|
|
|
//Builds the login request for each case
|
|
|
Request request; |
|
|
Request request; |
|
|
if (strings.length == 2) { |
|
|
|
|
|
clearSessionData(); |
|
|
clearSessionData(); |
|
|
|
|
|
|
|
|
String loginName = strings[0]; |
|
|
String username = strings[0]; |
|
|
String password = strings[1]; |
|
|
String password = strings[1]; |
|
|
|
|
|
|
|
|
RequestBody formBody = new FormBody.Builder() |
|
|
RequestBody formBody = new FormBody.Builder() |
|
|
.add("user", loginName) |
|
|
.add("username", username) |
|
|
.add("passwrd", password) |
|
|
.add("password", password) |
|
|
.add("cookielength", "-1") //-1 is forever
|
|
|
|
|
|
.build(); |
|
|
.build(); |
|
|
request = new Request.Builder() |
|
|
request = new Request.Builder() |
|
|
.url(loginUrl) |
|
|
.url(loginUrl) |
|
|
.post(formBody) |
|
|
.post(formBody) |
|
|
.build(); |
|
|
.build(); |
|
|
} else { |
|
|
|
|
|
request = new Request.Builder() |
|
|
|
|
|
.url(loginUrl) |
|
|
|
|
|
.build(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
//Makes request & handles response
|
|
|
//Makes request & handles response
|
|
|
Response response = client.newCall(request).execute(); |
|
|
Response response = client.newCall(request).execute(); |
|
|
|
|
|
|
|
|
if (validateRetrievedCookies()) { |
|
|
setPersistentCookieSession(loginUrl); //Store cookies
|
|
|
setPersistentCookieSession(); //Store cookies
|
|
|
|
|
|
|
|
|
|
|
|
//Edit SharedPreferences, save session's data
|
|
|
//Edit SharedPreferences, save session's data
|
|
|
SharedPreferences.Editor editor = sharedPrefs.edit(); |
|
|
SharedPreferences.Editor editor = sharedPrefs.edit(); |
|
|
setLoginScreenAsDefault(false); |
|
|
setLoginScreenAsDefault(false); |
|
|
editor.putBoolean(LOGGED_IN, true); |
|
|
editor.putBoolean(LOGGED_IN, true); |
|
|
editor.putString(USERNAME, "Username"); |
|
|
editor.putString(USERNAME, username); |
|
|
editor.putInt(USER_ID, 10); |
|
|
editor.putInt(USER_ID, 10); |
|
|
editor.putInt(USER_TYPE, UserType.USER.getId()); |
|
|
editor.putInt(USER_TYPE, UserType.USER.getId()); |
|
|
editor.apply(); |
|
|
editor.apply(); |
|
|
|
|
|
|
|
|
return 0; |
|
|
return 0; |
|
|
} else { |
|
|
} catch (Exception e) { |
|
|
//Investigates login failure
|
|
|
return 2; |
|
|
/*if (error.size() > 0) { //Wrong username
|
|
|
|
|
|
return WRONG_USER; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (error.size() > 0) { //Wrong password
|
|
|
|
|
|
return WRONG_PASSWORD; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (error.size() > 0) { //User is banned
|
|
|
public int signup(String... strings) { |
|
|
return BANNED_USER; |
|
|
//Builds the signup request
|
|
|
}*/ |
|
|
Request request; |
|
|
|
|
|
|
|
|
|
|
|
clearSessionData(); |
|
|
|
|
|
|
|
|
//Other error e.g. session was reset server-side
|
|
|
String email = strings[0]; |
|
|
clearSessionData(); //Clear invalid saved data
|
|
|
String username = strings[1]; |
|
|
return 1; |
|
|
String password = strings[2]; |
|
|
} |
|
|
String birthday = strings[3]; |
|
|
//Handles exceptions
|
|
|
|
|
|
|
|
|
RequestBody formBody = new FormBody.Builder() |
|
|
|
|
|
.add("email", email) |
|
|
|
|
|
.add("username", username) |
|
|
|
|
|
.add("password1", password) |
|
|
|
|
|
.add("password2", password) |
|
|
|
|
|
.build(); |
|
|
|
|
|
request = new Request.Builder() |
|
|
|
|
|
.url(signupUrl) |
|
|
|
|
|
.post(formBody) |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
//Makes request & handles response
|
|
|
|
|
|
Response response = client.newCall(request).execute(); |
|
|
|
|
|
|
|
|
|
|
|
setPersistentCookieSession(signupUrl); //Store cookies
|
|
|
|
|
|
|
|
|
|
|
|
//Edit SharedPreferences, save session's data
|
|
|
|
|
|
SharedPreferences.Editor editor = sharedPrefs.edit(); |
|
|
|
|
|
setLoginScreenAsDefault(false); |
|
|
|
|
|
editor.putBoolean(LOGGED_IN, true); |
|
|
|
|
|
editor.putString(USERNAME, username); |
|
|
|
|
|
editor.putInt(USER_ID, 10); |
|
|
|
|
|
editor.putInt(USER_TYPE, UserType.USER.getId()); |
|
|
|
|
|
editor.apply(); |
|
|
|
|
|
return 0; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
return 2; |
|
|
return 2; |
|
|
} |
|
|
} |
|
@ -149,7 +165,7 @@ public class SessionManager { |
|
|
|
|
|
|
|
|
public int logout() { |
|
|
public int logout() { |
|
|
Request request = new Request.Builder() |
|
|
Request request = new Request.Builder() |
|
|
.url(sharedPrefs.getString(LOGOUT_LINK, "LogoutLink")) |
|
|
.url(logoutUrl) |
|
|
.build(); |
|
|
.build(); |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
@ -177,15 +193,6 @@ public class SessionManager { |
|
|
return UserType.fromInteger(sharedPrefs.getInt(USER_TYPE, -1)); |
|
|
return UserType.fromInteger(sharedPrefs.getInt(USER_TYPE, -1)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Cookie getThmmyCookie() { |
|
|
|
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(indexUrl); |
|
|
|
|
|
for (Cookie cookie : cookieList) { |
|
|
|
|
|
if (cookie.name().equals("THMMYgrC00ki3")) |
|
|
|
|
|
return cookie; |
|
|
|
|
|
} |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean isLoggedIn() { |
|
|
public boolean isLoggedIn() { |
|
|
return sharedPrefs.getBoolean(LOGGED_IN, false); |
|
|
return sharedPrefs.getBoolean(LOGGED_IN, false); |
|
|
} |
|
|
} |
|
@ -194,18 +201,8 @@ public class SessionManager { |
|
|
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); |
|
|
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private boolean validateRetrievedCookies() { |
|
|
private void setPersistentCookieSession(HttpUrl forUrl) { |
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(indexUrl); |
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(forUrl); |
|
|
for (Cookie cookie : cookieList) { |
|
|
|
|
|
if (cookie.name().equals("THMMYgrC00ki3")) |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Call validateRetrievedCookies() first
|
|
|
|
|
|
private void setPersistentCookieSession() { |
|
|
|
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(indexUrl); |
|
|
|
|
|
Cookie.Builder builder = new Cookie.Builder(); |
|
|
Cookie.Builder builder = new Cookie.Builder(); |
|
|
builder.name(cookieList.get(1).name()) |
|
|
builder.name(cookieList.get(1).name()) |
|
|
.value(cookieList.get(1).value()) |
|
|
.value(cookieList.get(1).value()) |
|
|