|
@ -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 username = strings[0]; |
|
|
|
|
|
String password = strings[1]; |
|
|
|
|
|
|
|
|
|
|
|
RequestBody formBody = new FormBody.Builder() |
|
|
|
|
|
.add("username", username) |
|
|
|
|
|
.add("password", password) |
|
|
|
|
|
.build(); |
|
|
|
|
|
request = new Request.Builder() |
|
|
|
|
|
.url(loginUrl) |
|
|
|
|
|
.post(formBody) |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
String loginName = strings[0]; |
|
|
|
|
|
String password = strings[1]; |
|
|
try { |
|
|
|
|
|
//Makes request & handles response
|
|
|
RequestBody formBody = new FormBody.Builder() |
|
|
Response response = client.newCall(request).execute(); |
|
|
.add("user", loginName) |
|
|
|
|
|
.add("passwrd", password) |
|
|
setPersistentCookieSession(loginUrl); //Store cookies
|
|
|
.add("cookielength", "-1") //-1 is forever
|
|
|
|
|
|
.build(); |
|
|
//Edit SharedPreferences, save session's data
|
|
|
request = new Request.Builder() |
|
|
SharedPreferences.Editor editor = sharedPrefs.edit(); |
|
|
.url(loginUrl) |
|
|
setLoginScreenAsDefault(false); |
|
|
.post(formBody) |
|
|
editor.putBoolean(LOGGED_IN, true); |
|
|
.build(); |
|
|
editor.putString(USERNAME, username); |
|
|
} else { |
|
|
editor.putInt(USER_ID, 10); |
|
|
request = new Request.Builder() |
|
|
editor.putInt(USER_TYPE, UserType.USER.getId()); |
|
|
.url(loginUrl) |
|
|
editor.apply(); |
|
|
.build(); |
|
|
return 0; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
return 2; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int signup(String... strings) { |
|
|
|
|
|
//Builds the signup request
|
|
|
|
|
|
Request request; |
|
|
|
|
|
|
|
|
|
|
|
clearSessionData(); |
|
|
|
|
|
|
|
|
|
|
|
String email = strings[0]; |
|
|
|
|
|
String username = strings[1]; |
|
|
|
|
|
String password = strings[2]; |
|
|
|
|
|
String birthday = strings[3]; |
|
|
|
|
|
|
|
|
|
|
|
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 { |
|
|
try { |
|
|
//Makes request & handles response
|
|
|
//Makes request & handles response
|
|
|
Response response = client.newCall(request).execute(); |
|
|
Response response = client.newCall(request).execute(); |
|
|
|
|
|
|
|
|
if (validateRetrievedCookies()) { |
|
|
setPersistentCookieSession(signupUrl); //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 { |
|
|
|
|
|
//Investigates login failure
|
|
|
|
|
|
/*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
|
|
|
|
|
|
return BANNED_USER; |
|
|
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
|
|
//Other error e.g. session was reset server-side
|
|
|
|
|
|
clearSessionData(); //Clear invalid saved data
|
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
//Handles exceptions
|
|
|
|
|
|
} 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()) |
|
|