|
|
@ -1,6 +1,7 @@ |
|
|
|
package gr.auth.databases.flavours.session; |
|
|
|
|
|
|
|
import android.content.SharedPreferences; |
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar; |
|
|
|
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; |
|
|
@ -44,8 +45,9 @@ public class SessionManager { |
|
|
|
} |
|
|
|
|
|
|
|
//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("https://www.thmmy.gr/smf/index.php?action=login2"); |
|
|
|
private static final HttpUrl loginUrl = HttpUrl.parse("http://83.212.109.171:8181/api/rest-auth/login/"); |
|
|
|
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
|
|
|
|
private final OkHttpClient client; |
|
|
@ -57,7 +59,6 @@ public class SessionManager { |
|
|
|
private static final String USERNAME = "Username"; |
|
|
|
private static final String USER_ID = "UserID"; |
|
|
|
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 LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; |
|
|
|
|
|
|
@ -73,63 +74,78 @@ public class SessionManager { |
|
|
|
public int login(String... strings) { |
|
|
|
//Builds the login request for each case
|
|
|
|
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]; |
|
|
|
|
|
|
|
RequestBody formBody = new FormBody.Builder() |
|
|
|
.add("user", loginName) |
|
|
|
.add("passwrd", password) |
|
|
|
.add("cookielength", "-1") //-1 is forever
|
|
|
|
.build(); |
|
|
|
request = new Request.Builder() |
|
|
|
.url(loginUrl) |
|
|
|
.post(formBody) |
|
|
|
.build(); |
|
|
|
} else { |
|
|
|
request = new Request.Builder() |
|
|
|
.url(loginUrl) |
|
|
|
.build(); |
|
|
|
|
|
|
|
try { |
|
|
|
//Makes request & handles response
|
|
|
|
Response response = client.newCall(request).execute(); |
|
|
|
|
|
|
|
setPersistentCookieSession(loginUrl); //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) { |
|
|
|
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 { |
|
|
|
//Makes request & handles response
|
|
|
|
Response response = client.newCall(request).execute(); |
|
|
|
|
|
|
|
if (validateRetrievedCookies()) { |
|
|
|
setPersistentCookieSession(); //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; |
|
|
|
} 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
|
|
|
|
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) { |
|
|
|
return 2; |
|
|
|
} |
|
|
@ -149,7 +165,7 @@ public class SessionManager { |
|
|
|
|
|
|
|
public int logout() { |
|
|
|
Request request = new Request.Builder() |
|
|
|
.url(sharedPrefs.getString(LOGOUT_LINK, "LogoutLink")) |
|
|
|
.url(logoutUrl) |
|
|
|
.build(); |
|
|
|
|
|
|
|
try { |
|
|
@ -177,15 +193,6 @@ public class SessionManager { |
|
|
|
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() { |
|
|
|
return sharedPrefs.getBoolean(LOGGED_IN, false); |
|
|
|
} |
|
|
@ -194,18 +201,8 @@ public class SessionManager { |
|
|
|
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); |
|
|
|
} |
|
|
|
|
|
|
|
private boolean validateRetrievedCookies() { |
|
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(indexUrl); |
|
|
|
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); |
|
|
|
private void setPersistentCookieSession(HttpUrl forUrl) { |
|
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(forUrl); |
|
|
|
Cookie.Builder builder = new Cookie.Builder(); |
|
|
|
builder.name(cookieList.get(1).name()) |
|
|
|
.value(cookieList.get(1).value()) |
|
|
|