Exemplo n.º 1
0
  public boolean login(String username, String password) {
    // Login
    List<AbstractMap.SimpleEntry<String, String>> params = new ArrayList<>();
    AbstractMap.SimpleEntry<String, String> entryUsername =
        new AbstractMap.SimpleEntry<>("username", username);
    AbstractMap.SimpleEntry<String, String> entryPassword =
        new AbstractMap.SimpleEntry<>("password", password);
    params.add(entryUsername);
    params.add(entryPassword);

    String response = App.HTMLPOST(App.Prefs().getString("domain", "") + "/API/login/", params);

    if (response == null) return false;
    else {
      // Split the string and save the username, the token, and the admin state
      // It doesn't matter if the user spoofs the admin login, server checks for rights every
      // request anyway.
      try {
        String[] data = response.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);
        if (data[0] != null & data[1] != null) {
          return this.login(username, data[0], data[1], data[2]);
        } else return false;
      } catch (Exception e) {
        return false;
      }
    }
  }
Exemplo n.º 2
0
  public boolean logout() {
    try {
      SharedPreferences preferences =
          PreferenceManager.getDefaultSharedPreferences(App.getAppContext());
      List<AbstractMap.SimpleEntry<String, String>> params = new ArrayList<>();
      AbstractMap.SimpleEntry<String, String> username =
          new AbstractMap.SimpleEntry<>("username", preferences.getString("username", ""));
      AbstractMap.SimpleEntry<String, String> token =
          new AbstractMap.SimpleEntry<>("token", preferences.getString("token", ""));
      params.add(username);
      params.add(token);
      String response = App.HTMLPOST(App.Prefs().getString("domain", "") + "/API/logout", params);
      // TODO: Redo, this whole thing is flawed.
      if (response.equals("Logout successful")) {
        final SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
        return true;
      }
      return false;

    } catch (Exception e) {
      return false;
    }
  }