|
@ -48,39 +48,38 @@ public class SessionManager { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Generic constants
|
|
|
//Generic constants
|
|
|
private static final String baseServerUrl = "http://83.212.109.171:8181/"; |
|
|
private static String baseServerUrl = "http://0.0.0.0:8181/"; |
|
|
private static final String loginUrl = baseServerUrl + "api/rest-auth/login/"; |
|
|
public static String restaurantsUrl = baseServerUrl + "api/restaurant/"; |
|
|
private static final String signupUrl = baseServerUrl + "api/rest-auth/registration/"; |
|
|
public static String restaurantsUserViewUrl = baseServerUrl + "api/restaurantUserView/"; |
|
|
private static final String logoutUrl = baseServerUrl + "api/rest-auth/logout/"; |
|
|
public static String getUserDietsUrl = baseServerUrl + "api/userDiets/"; |
|
|
private static final String setUserBirthday = baseServerUrl + "api/setUserBirthday/"; |
|
|
public static String rateDrinkUrl = baseServerUrl + "api/userratesdrink/"; |
|
|
public static final String restaurantsUrl = baseServerUrl + "api/restaurant/"; |
|
|
public static String rateFoodUrl = baseServerUrl + "api/userratesfood/"; |
|
|
public static final String restaurantsUserViewUrl = baseServerUrl + "api/restaurantUserView/"; |
|
|
public static String rateRestaurantUrl = baseServerUrl + "api/userratesrestaurant/"; |
|
|
public static final String getUserDietsUrl = baseServerUrl + "api/userDiets/"; |
|
|
public static String foodsUserViewUrl = baseServerUrl + "api/foodUserView/"; |
|
|
public static final String rateDrinkUrl = baseServerUrl + "api/userratesdrink/"; |
|
|
public static String drinksUserViewUrl = baseServerUrl + "api/drinkUserView/"; |
|
|
public static final String rateFoodUrl = baseServerUrl + "api/userratesfood/"; |
|
|
public static String profileUserViewUrl = baseServerUrl + "api/profileUserView/"; |
|
|
public static final String rateRestaurantUrl = baseServerUrl + "api/userratesrestaurant/"; |
|
|
public static String addFoodUrl = baseServerUrl + "api/addFood/"; |
|
|
public static final String foodsUserViewUrl = baseServerUrl + "api/foodUserView/"; |
|
|
public static String addDrinkUrl = baseServerUrl + "api/addDrink/"; |
|
|
public static final String drinksUserViewUrl = baseServerUrl + "api/drinkUserView/"; |
|
|
public static String dietsUserViewUrl = baseServerUrl + "api/dietUserView/"; |
|
|
public static final String profileUserViewUrl = baseServerUrl + "api/profileUserView/"; |
|
|
public static String ingredientsUserViewUrl = baseServerUrl + "api/ingredientUserView/"; |
|
|
public static final String addFoodUrl = baseServerUrl + "api/addFood/"; |
|
|
public static String prohibitIngredientUrl = baseServerUrl + "api/prohibitIngredient/"; |
|
|
public static final String addDrinkUrl = baseServerUrl + "api/addDrink/"; |
|
|
public static String followDietUrl = baseServerUrl + "api/followDiet/"; |
|
|
public static final String dietsUserViewUrl = baseServerUrl + "api/dietUserView/"; |
|
|
public static String addIngredientUrl = baseServerUrl + "api/ingredient/"; |
|
|
public static final String ingredientsUserViewUrl = baseServerUrl + "api/ingredientUserView/"; |
|
|
public static String addDietUrl = baseServerUrl + "api/addDiet/"; |
|
|
public static final String prohibitIngredientUrl = baseServerUrl + "api/prohibitIngredient/"; |
|
|
public static String addIngredientToFoodUrl = baseServerUrl + "api/foodhasingredient/"; |
|
|
public static final String followDietUrl = baseServerUrl + "api/followDiet/"; |
|
|
public static String addIngredientToDrinkUrl = baseServerUrl + "api/drinkhasingredient/"; |
|
|
public static final String addIngredientUrl = baseServerUrl + "api/ingredient/"; |
|
|
public static String acceptRestaurantUrl = baseServerUrl + "api/acceptRestaurant/"; |
|
|
public static final String addDietUrl = baseServerUrl + "api/addDiet/"; |
|
|
public static String acceptFoodUrl = baseServerUrl + "api/acceptFood/"; |
|
|
public static final String addIngredientToFoodUrl = baseServerUrl + "api/foodhasingredient/"; |
|
|
public static String acceptDrinkUrl = baseServerUrl + "api/acceptDrink/"; |
|
|
public static final String addIngredientToDrinkUrl = baseServerUrl + "api/drinkhasingredient/"; |
|
|
public static String acceptDietUrl = baseServerUrl + "api/acceptDiet/"; |
|
|
public static final String acceptRestaurantUrl = baseServerUrl + "api/acceptRestaurant/"; |
|
|
private static String loginUrl = baseServerUrl + "api/rest-auth/login/"; |
|
|
public static final String acceptFoodUrl = baseServerUrl + "api/acceptFood/"; |
|
|
private static String signupUrl = baseServerUrl + "api/rest-auth/registration/"; |
|
|
public static final String acceptDrinkUrl = baseServerUrl + "api/acceptDrink/"; |
|
|
private static String logoutUrl = baseServerUrl + "api/rest-auth/logout/"; |
|
|
public static final String acceptDietUrl = baseServerUrl + "api/acceptDiet/"; |
|
|
private static String setUserBirthday = baseServerUrl + "api/setUserBirthday/"; |
|
|
|
|
|
|
|
|
// Client & Cookies
|
|
|
// Client & Cookies
|
|
|
private final OkHttpClient client; |
|
|
private final OkHttpClient client; |
|
|
private final PersistentCookieJar cookieJar; |
|
|
private final PersistentCookieJar cookieJar; |
|
|
//private final SharedPrefsCookiePersistor cookiePersistor;
|
|
|
|
|
|
|
|
|
|
|
|
//Shared Preferences & its keys
|
|
|
//Shared Preferences & its keys
|
|
|
private final SharedPreferences sharedPrefs; |
|
|
private final SharedPreferences sharedPrefs; |
|
@ -91,54 +90,48 @@ public class SessionManager { |
|
|
private static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; |
|
|
private static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; |
|
|
|
|
|
|
|
|
//Constructor
|
|
|
//Constructor
|
|
|
public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar, SharedPreferences sharedPrefs) { |
|
|
public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar, SharedPreferences sharedPrefs, String baseServerIP) { |
|
|
this.client = client; |
|
|
this.client = client; |
|
|
this.cookieJar = cookieJar; |
|
|
this.cookieJar = cookieJar; |
|
|
this.sharedPrefs = sharedPrefs; |
|
|
this.sharedPrefs = sharedPrefs; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int login(String... strings) { |
|
|
if (baseServerIP != null && !baseServerIP.isEmpty()) { |
|
|
//Builds the login request for each case
|
|
|
setBaseServerUrl(baseServerIP); |
|
|
Request request; |
|
|
} |
|
|
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(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
//Makes request & handles response
|
|
|
|
|
|
Response response = client.newCall(request).execute(); |
|
|
|
|
|
|
|
|
|
|
|
setPersistentCookieSession(loginUrl); //Store cookies
|
|
|
|
|
|
|
|
|
|
|
|
ResponseBody responseBody = response.body(); |
|
|
|
|
|
assert responseBody != null; |
|
|
|
|
|
JSONObject jsonResponse = new JSONObject(responseBody.string()); |
|
|
|
|
|
JSONObject jsonUser = jsonResponse.getJSONObject("user"); |
|
|
|
|
|
int extractedUserId = jsonUser.getInt("id"); |
|
|
|
|
|
int extractedRole = jsonUser.getInt("role"); |
|
|
|
|
|
|
|
|
|
|
|
//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, extractedUserId); |
|
|
|
|
|
editor.putInt(USER_TYPE, extractedRole); |
|
|
|
|
|
editor.apply(); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
return 2; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void setBaseServerUrl(String baseServerIP) { |
|
|
|
|
|
// Sets base url
|
|
|
|
|
|
SessionManager.baseServerUrl = "http://" + baseServerIP + ":8181/"; |
|
|
|
|
|
|
|
|
|
|
|
// Sets all other api point urls
|
|
|
|
|
|
loginUrl = baseServerUrl + "api/rest-auth/login/"; |
|
|
|
|
|
signupUrl = baseServerUrl + "api/rest-auth/registration/"; |
|
|
|
|
|
logoutUrl = baseServerUrl + "api/rest-auth/logout/"; |
|
|
|
|
|
setUserBirthday = baseServerUrl + "api/setUserBirthday/"; |
|
|
|
|
|
restaurantsUrl = baseServerUrl + "api/restaurant/"; |
|
|
|
|
|
restaurantsUserViewUrl = baseServerUrl + "api/restaurantUserView/"; |
|
|
|
|
|
getUserDietsUrl = baseServerUrl + "api/userDiets/"; |
|
|
|
|
|
rateDrinkUrl = baseServerUrl + "api/userratesdrink/"; |
|
|
|
|
|
rateFoodUrl = baseServerUrl + "api/userratesfood/"; |
|
|
|
|
|
rateRestaurantUrl = baseServerUrl + "api/userratesrestaurant/"; |
|
|
|
|
|
foodsUserViewUrl = baseServerUrl + "api/foodUserView/"; |
|
|
|
|
|
drinksUserViewUrl = baseServerUrl + "api/drinkUserView/"; |
|
|
|
|
|
profileUserViewUrl = baseServerUrl + "api/profileUserView/"; |
|
|
|
|
|
addFoodUrl = baseServerUrl + "api/addFood/"; |
|
|
|
|
|
addDrinkUrl = baseServerUrl + "api/addDrink/"; |
|
|
|
|
|
dietsUserViewUrl = baseServerUrl + "api/dietUserView/"; |
|
|
|
|
|
ingredientsUserViewUrl = baseServerUrl + "api/ingredientUserView/"; |
|
|
|
|
|
prohibitIngredientUrl = baseServerUrl + "api/prohibitIngredient/"; |
|
|
|
|
|
followDietUrl = baseServerUrl + "api/followDiet/"; |
|
|
|
|
|
addIngredientUrl = baseServerUrl + "api/ingredient/"; |
|
|
|
|
|
addDietUrl = baseServerUrl + "api/addDiet/"; |
|
|
|
|
|
addIngredientToFoodUrl = baseServerUrl + "api/foodhasingredient/"; |
|
|
|
|
|
addIngredientToDrinkUrl = baseServerUrl + "api/drinkhasingredient/"; |
|
|
|
|
|
acceptRestaurantUrl = baseServerUrl + "api/acceptRestaurant/"; |
|
|
|
|
|
acceptFoodUrl = baseServerUrl + "api/acceptFood/"; |
|
|
|
|
|
acceptDrinkUrl = baseServerUrl + "api/acceptDrink/"; |
|
|
|
|
|
acceptDietUrl = baseServerUrl + "api/acceptDiet/"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public int signup(String... strings) { |
|
|
public int signup(String... strings) { |
|
@ -206,18 +199,6 @@ public class SessionManager { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void validateSession() { |
|
|
|
|
|
if (isLoggedIn()) { |
|
|
|
|
|
int loginResult = login(); |
|
|
|
|
|
if (loginResult != 1) |
|
|
|
|
|
return; |
|
|
|
|
|
} else if (isLoginScreenDefault()) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
setLoginScreenAsDefault(true); |
|
|
|
|
|
clearSessionData(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int logout() { |
|
|
public int logout() { |
|
|
RequestBody requestBody = RequestBody.create(null, new byte[]{}); |
|
|
RequestBody requestBody = RequestBody.create(null, new byte[]{}); |
|
|
|
|
|
|
|
@ -252,6 +233,51 @@ public class SessionManager { |
|
|
return UserType.fromInteger(sharedPrefs.getInt(USER_TYPE, -1)); |
|
|
return UserType.fromInteger(sharedPrefs.getInt(USER_TYPE, -1)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int login(String... strings) { |
|
|
|
|
|
//Builds the login request for each case
|
|
|
|
|
|
Request request; |
|
|
|
|
|
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(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
//Makes request & handles response
|
|
|
|
|
|
Response response = client.newCall(request).execute(); |
|
|
|
|
|
|
|
|
|
|
|
setPersistentCookieSession(loginUrl); //Store cookies
|
|
|
|
|
|
|
|
|
|
|
|
ResponseBody responseBody = response.body(); |
|
|
|
|
|
assert responseBody != null; |
|
|
|
|
|
JSONObject jsonResponse = new JSONObject(responseBody.string()); |
|
|
|
|
|
JSONObject jsonUser = jsonResponse.getJSONObject("user"); |
|
|
|
|
|
int extractedUserId = jsonUser.getInt("id"); |
|
|
|
|
|
int extractedRole = jsonUser.getInt("role"); |
|
|
|
|
|
|
|
|
|
|
|
//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, extractedUserId); |
|
|
|
|
|
editor.putInt(USER_TYPE, extractedRole); |
|
|
|
|
|
editor.apply(); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return 2; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public boolean isLoggedIn() { |
|
|
public boolean isLoggedIn() { |
|
|
return sharedPrefs.getBoolean(LOGGED_IN, false); |
|
|
return sharedPrefs.getBoolean(LOGGED_IN, false); |
|
|
} |
|
|
} |
|
@ -264,9 +290,21 @@ public class SessionManager { |
|
|
HttpUrl httpUrl = HttpUrl.parse(forUrl); |
|
|
HttpUrl httpUrl = HttpUrl.parse(forUrl); |
|
|
assert httpUrl != null; |
|
|
assert httpUrl != null; |
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(httpUrl); |
|
|
List<Cookie> cookieList = cookieJar.loadForRequest(httpUrl); |
|
|
|
|
|
String csrfToken = ""; |
|
|
|
|
|
int maxLength = 0; |
|
|
|
|
|
|
|
|
SharedPreferences.Editor editor = sharedPrefs.edit(); |
|
|
SharedPreferences.Editor editor = sharedPrefs.edit(); |
|
|
editor.putString(CSRF_TOKEN, cookieList.get(0).value()); |
|
|
|
|
|
|
|
|
for (int cookieIndex = 0; cookieIndex < cookieList.size(); ++cookieIndex) { |
|
|
|
|
|
Cookie cookie = cookieList.get(cookieIndex); |
|
|
|
|
|
if (!cookie.value().contains("Successfully signed in") && cookie.value().length() > maxLength) { |
|
|
|
|
|
// The longest one is the CSRF token
|
|
|
|
|
|
csrfToken = cookie.value(); |
|
|
|
|
|
maxLength = csrfToken.length(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
editor.putString(CSRF_TOKEN, csrfToken); |
|
|
editor.apply(); |
|
|
editor.apply(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|