forked from github/wulkanowy-mirror
Create api package (#4)
Create api package and move login's implementation to it
This commit is contained in:
parent
4b425f9b39
commit
3a7cb3b904
@ -1,19 +0,0 @@
|
|||||||
package io.github.wulkanowy.activity.main;
|
|
||||||
|
|
||||||
import org.jsoup.nodes.Document;
|
|
||||||
import org.jsoup.nodes.Element;
|
|
||||||
|
|
||||||
class CheckPass {
|
|
||||||
|
|
||||||
private Document document;
|
|
||||||
|
|
||||||
CheckPass(Document doc) {
|
|
||||||
document = doc;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isLogged() {
|
|
||||||
Element messageAlert = document.select(".ErrorMessage").first();
|
|
||||||
|
|
||||||
return null == messageAlert;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,154 +0,0 @@
|
|||||||
package io.github.wulkanowy.activity.main;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.ProgressDialog;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.jsoup.Connection;
|
|
||||||
import org.jsoup.Jsoup;
|
|
||||||
import org.jsoup.nodes.Document;
|
|
||||||
import org.jsoup.select.Elements;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import io.github.wulkanowy.R;
|
|
||||||
import io.github.wulkanowy.activity.dashboard.DashboardActivity;
|
|
||||||
|
|
||||||
public class Login extends AsyncTask<Void, Void, Void> {
|
|
||||||
|
|
||||||
String email;
|
|
||||||
String password;
|
|
||||||
String county;
|
|
||||||
|
|
||||||
Map<String, String> loginCookies;
|
|
||||||
|
|
||||||
Activity activity;
|
|
||||||
String userMesage;
|
|
||||||
|
|
||||||
String urlForStepOne;
|
|
||||||
String urlForStepTwo;
|
|
||||||
String urlForStepThree;
|
|
||||||
|
|
||||||
ProgressDialog progress;
|
|
||||||
|
|
||||||
public Login(String emailT, String passwordT, String countyT, Activity mainAC){
|
|
||||||
|
|
||||||
activity = mainAC;
|
|
||||||
progress = new ProgressDialog(activity);
|
|
||||||
|
|
||||||
if (countyT.equals("Debug")){
|
|
||||||
urlForStepOne = activity.getString(R.string.urlStepOneDebug);
|
|
||||||
urlForStepTwo = activity.getString(R.string.urlStepTwoDebug);
|
|
||||||
urlForStepThree = activity.getString(R.string.urlStepThreeDebug);
|
|
||||||
county = activity.getString(R.string.countyDebug);
|
|
||||||
email = emailT;
|
|
||||||
password = passwordT;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
urlForStepOne = activity.getString(R.string.urlStepOneRelease);
|
|
||||||
urlForStepTwo = activity.getString(R.string.urlStepTwoRelease);
|
|
||||||
urlForStepThree = activity.getString(R.string.urlStepThreeRelease);
|
|
||||||
county = countyT;
|
|
||||||
email = emailT;
|
|
||||||
password = passwordT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPreExecute() {
|
|
||||||
super.onPreExecute();
|
|
||||||
|
|
||||||
progress.setTitle(activity.getText(R.string.login_title));
|
|
||||||
progress.setMessage(activity.getText(R.string.please_wait));
|
|
||||||
progress.setCancelable(false);
|
|
||||||
progress.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Void doInBackground(Void... params) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!stepOne()) {
|
|
||||||
userMesage = activity.getString(R.string.login_bad_credentials);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
Document certificate = stepTwo();
|
|
||||||
|
|
||||||
Connection.Response step3 = stepThree(certificate);
|
|
||||||
Document dashboardHtml = step3.parse();
|
|
||||||
|
|
||||||
String helloText = dashboardHtml.getElementsByClass("welcome").text();
|
|
||||||
|
|
||||||
if (helloText.equals("Dzień dobry!")) {
|
|
||||||
userMesage = activity.getString(R.string.login_accepted);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
userMesage = activity.getString(R.string.login_denied);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (IOException e){
|
|
||||||
userMesage = e.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean stepOne() throws IOException {
|
|
||||||
Connection.Response initial = Jsoup
|
|
||||||
.connect(urlForStepOne)
|
|
||||||
.data("LoginName", email)
|
|
||||||
.data("Password", password)
|
|
||||||
.method(Connection.Method.POST)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
loginCookies = initial.cookies();
|
|
||||||
|
|
||||||
Document document = initial.parse();
|
|
||||||
|
|
||||||
return new CheckPass(document).isLogged();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Document stepTwo() throws IOException {
|
|
||||||
urlForStepTwo = urlForStepTwo.replace("{locationID}", county);
|
|
||||||
|
|
||||||
return Jsoup.connect(urlForStepTwo)
|
|
||||||
.cookies(loginCookies)
|
|
||||||
.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Connection.Response stepThree(Document certificate) throws IOException {
|
|
||||||
Elements wresultsInput = certificate.select("input[name=wresult]");
|
|
||||||
String wresults = wresultsInput.attr("value");
|
|
||||||
|
|
||||||
Elements waInput = certificate.select("input[name=wa]");
|
|
||||||
String wa = waInput.attr("value");
|
|
||||||
|
|
||||||
urlForStepThree = urlForStepThree.replace("{locationID}", county);
|
|
||||||
|
|
||||||
return Jsoup.connect(urlForStepThree)
|
|
||||||
.data("wa", wa)
|
|
||||||
.data("wresult", wresults)
|
|
||||||
.cookies(loginCookies)
|
|
||||||
.followRedirects(true)
|
|
||||||
.method(Connection.Method.POST)
|
|
||||||
.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void onPostExecute(Void result) {
|
|
||||||
super.onPostExecute(result);
|
|
||||||
progress.dismiss();
|
|
||||||
if (!userMesage.isEmpty()){
|
|
||||||
Toast.makeText(activity, userMesage , Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (userMesage.equals(activity.getString(R.string.login_accepted))){
|
|
||||||
Intent intent = new Intent(activity,DashboardActivity.class);
|
|
||||||
activity.startActivity(intent);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,67 @@
|
|||||||
|
package io.github.wulkanowy.activity.main;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import io.github.wulkanowy.R;
|
||||||
|
import io.github.wulkanowy.activity.dashboard.DashboardActivity;
|
||||||
|
import io.github.wulkanowy.api.Cookies;
|
||||||
|
import io.github.wulkanowy.api.login.*;
|
||||||
|
|
||||||
|
public class LoginTask extends AsyncTask<String, Integer, Integer> {
|
||||||
|
|
||||||
|
private Activity activity;
|
||||||
|
|
||||||
|
private ProgressDialog progress;
|
||||||
|
|
||||||
|
public LoginTask(Activity context) {
|
||||||
|
activity = context;
|
||||||
|
progress = new ProgressDialog(activity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPreExecute() {
|
||||||
|
super.onPreExecute();
|
||||||
|
|
||||||
|
progress.setTitle(activity.getText(R.string.login_title));
|
||||||
|
progress.setMessage(activity.getText(R.string.please_wait));
|
||||||
|
progress.setCancelable(false);
|
||||||
|
progress.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Integer doInBackground(String... credentials) {
|
||||||
|
Cookies cookies = new Cookies();
|
||||||
|
Login login = new Login(cookies);
|
||||||
|
|
||||||
|
try {
|
||||||
|
login.login(credentials[0], credentials[1], credentials[2]);
|
||||||
|
} catch (BadCredentialsException e) {
|
||||||
|
return R.string.login_bad_credentials;
|
||||||
|
} catch (AccountPermissionException e) {
|
||||||
|
return R.string.login_bad_account_permission;
|
||||||
|
} catch (LoginErrorException e) {
|
||||||
|
return R.string.login_denied;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Map<String, String> cookiesList = login.getJar();
|
||||||
|
|
||||||
|
return R.string.login_accepted;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onPostExecute(Integer messageID) {
|
||||||
|
super.onPostExecute(messageID);
|
||||||
|
|
||||||
|
progress.dismiss();
|
||||||
|
|
||||||
|
Toast.makeText(activity, activity.getString(messageID), Toast.LENGTH_LONG).show();
|
||||||
|
|
||||||
|
if (messageID == R.string.login_accepted){
|
||||||
|
Intent intent = new Intent(activity, DashboardActivity.class);
|
||||||
|
activity.startActivity(intent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -65,12 +65,10 @@ public class MainActivity extends Activity {
|
|||||||
county = map.get(county);
|
county = map.get(county);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!email.isEmpty() || !password.isEmpty() || !county.isEmpty()){
|
if (!email.isEmpty() && !password.isEmpty() && !county.isEmpty()){
|
||||||
new Login(email, password, county, this).execute();
|
new LoginTask(this).execute(email, password, county);
|
||||||
}
|
} else {
|
||||||
else if (password.isEmpty() || email.isEmpty() || county.isEmpty()) {
|
|
||||||
Toast.makeText(this, R.string.data_text, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.data_text, Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
app/src/main/java/io/github/wulkanowy/api/Cookies.java
Normal file
16
app/src/main/java/io/github/wulkanowy/api/Cookies.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package io.github.wulkanowy.api;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class Cookies {
|
||||||
|
|
||||||
|
private Map<String, String> cookies;
|
||||||
|
|
||||||
|
public void setItems(Map<String, String> items) {
|
||||||
|
cookies = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getAll() {
|
||||||
|
return cookies;
|
||||||
|
}
|
||||||
|
}
|
20
app/src/main/java/io/github/wulkanowy/api/Vulcan.java
Normal file
20
app/src/main/java/io/github/wulkanowy/api/Vulcan.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package io.github.wulkanowy.api;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class Vulcan {
|
||||||
|
|
||||||
|
private Cookies cookies;
|
||||||
|
|
||||||
|
public Vulcan(Cookies cookies) {
|
||||||
|
this.cookies = cookies;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getJar() {
|
||||||
|
return cookies.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCookies(Map<String, String> items) {
|
||||||
|
cookies.setItems(items);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package io.github.wulkanowy.api.login;
|
||||||
|
|
||||||
|
public class AccountPermissionException extends Exception {
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package io.github.wulkanowy.api.login;
|
||||||
|
|
||||||
|
public class BadCredentialsException extends Exception {
|
||||||
|
}
|
88
app/src/main/java/io/github/wulkanowy/api/login/Login.java
Normal file
88
app/src/main/java/io/github/wulkanowy/api/login/Login.java
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package io.github.wulkanowy.api.login;
|
||||||
|
|
||||||
|
import org.jsoup.Connection;
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
import org.jsoup.nodes.Document;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import io.github.wulkanowy.api.Cookies;
|
||||||
|
import io.github.wulkanowy.api.Vulcan;
|
||||||
|
|
||||||
|
public class Login extends Vulcan {
|
||||||
|
|
||||||
|
private String loginPageUrl = "https://cufs.vulcan.net.pl/{locationID}/Account/LogOn";
|
||||||
|
|
||||||
|
private String certificatePageUrl = "https://cufs.vulcan.net.pl/{locationID}/FS/LS?wa=wsignin1.0&wtrealm=https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx&wctx=https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx";
|
||||||
|
|
||||||
|
private String loginEndpointPageUrl = "https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx";
|
||||||
|
|
||||||
|
public Login(Cookies cookies) {
|
||||||
|
super(cookies);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean login(String email, String password, String county) throws BadCredentialsException, LoginErrorException, AccountPermissionException {
|
||||||
|
try {
|
||||||
|
sendCredentials(email, password, county);
|
||||||
|
String[] certificate = getCertificateData(county);
|
||||||
|
sendCertificate(certificate[0], certificate[1], county);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new LoginErrorException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendCredentials(String email, String password, String county) throws IOException, BadCredentialsException {
|
||||||
|
loginPageUrl = loginPageUrl.replace("{locationID}", county);
|
||||||
|
|
||||||
|
Connection.Response response = Jsoup.connect(loginPageUrl)
|
||||||
|
.data("LoginName", email)
|
||||||
|
.data("Password", password)
|
||||||
|
.method(Connection.Method.POST)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
setCookies(response.cookies());
|
||||||
|
Document document = response.parse();
|
||||||
|
|
||||||
|
if (null != document.select(".ErrorMessage").first()) {
|
||||||
|
throw new BadCredentialsException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String[] getCertificateData(String county) throws IOException {
|
||||||
|
certificatePageUrl = certificatePageUrl.replace("{locationID}", county);
|
||||||
|
|
||||||
|
Document certificatePage = Jsoup.connect(certificatePageUrl)
|
||||||
|
.cookies(getJar())
|
||||||
|
.get();
|
||||||
|
|
||||||
|
return new String[] {
|
||||||
|
certificatePage.select("input[name=wa]").attr("value"),
|
||||||
|
certificatePage.select("input[name=wresult]").attr("value")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendCertificate(String protocolVersion, String certificate, String county) throws IOException, LoginErrorException, AccountPermissionException {
|
||||||
|
loginEndpointPageUrl = loginEndpointPageUrl.replace("{locationID}", county);
|
||||||
|
|
||||||
|
Connection.Response response = Jsoup.connect(loginEndpointPageUrl)
|
||||||
|
.data("wa", protocolVersion)
|
||||||
|
.data("wresult", certificate)
|
||||||
|
.cookies(getJar())
|
||||||
|
.followRedirects(true)
|
||||||
|
.method(Connection.Method.POST)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
setCookies(response.cookies());
|
||||||
|
Document html = response.parse();
|
||||||
|
|
||||||
|
if(html.getElementsByTag("title").text().equals("Logowanie")) {
|
||||||
|
throw new AccountPermissionException();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!html.select(".welcome").text().equals("Dzień dobry!")) {
|
||||||
|
throw new LoginErrorException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package io.github.wulkanowy.api.login;
|
||||||
|
|
||||||
|
public class LoginErrorException extends Exception {
|
||||||
|
}
|
@ -12,6 +12,7 @@
|
|||||||
<string name="error_feature_text">Funkcja którą chciałeś uruchomić nie działa</string>
|
<string name="error_feature_text">Funkcja którą chciałeś uruchomić nie działa</string>
|
||||||
<string name="login_accepted">Pomyślnie zalogowano</string>
|
<string name="login_accepted">Pomyślnie zalogowano</string>
|
||||||
<string name="login_bad_credentials">Zła nazwa użytkownika lub hasło</string>
|
<string name="login_bad_credentials">Zła nazwa użytkownika lub hasło</string>
|
||||||
|
<string name="login_bad_account_permission">Brak uprawnień do otwarcia dziennika. Sprawdź wprowadzoną nazwę powiatu</string>
|
||||||
<string name="login_denied">Logowanie nie powiodło się</string>
|
<string name="login_denied">Logowanie nie powiodło się</string>
|
||||||
<string name="please_wait">Proszę czekać…</string>
|
<string name="please_wait">Proszę czekać…</string>
|
||||||
<string name="title_activity_dashboard">Aktywność dashboard</string>
|
<string name="title_activity_dashboard">Aktywność dashboard</string>
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation">
|
|
||||||
<string name="urlStepOneRelease" translatable="false">https://cufs.vulcan.net.pl/Default/Account/LogOn</string>
|
|
||||||
<string name="urlStepTwoRelease" translatable="false">https://cufs.vulcan.net.pl/{locationID}/FS/LS?wa=wsignin1.0&wtrealm=https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx&wctx=https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx</string>
|
|
||||||
<string name="urlStepThreeRelease" translatable="false">https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx"</string>
|
|
||||||
<string name="urlStepOneDebug" translatable="false">https://cufsdemo.vulcan.net.pl/Default/Account/LogOn</string>
|
|
||||||
<string name="urlStepTwoDebug" translatable="false">https://cufsdemo.vulcan.net.pl/{locationID}/FS/LS?wa=wsignin1.0&wtrealm=https://uonetplusdemo.vulcan.net.pl/{locationID}/LoginEndpoint.aspx&wctx=https://uonetplusdemo.vulcan.net.pl/{locationID}/LoginEndpoint.aspx</string>
|
|
||||||
<string name="urlStepThreeDebug" translatable="false">https://uonetplusdemo.vulcan.net.pl/{locationID}/LoginEndpoint.aspx"</string>
|
|
||||||
<string name="countyDebug" translatable="false">demouonetplus</string>
|
|
||||||
</resources>
|
|
@ -12,6 +12,7 @@
|
|||||||
<string name="error_feature_text">The function you wanted to run does not work</string>
|
<string name="error_feature_text">The function you wanted to run does not work</string>
|
||||||
<string name="login_accepted">Login is successful</string>
|
<string name="login_accepted">Login is successful</string>
|
||||||
<string name="login_bad_credentials">Bad username or password</string>
|
<string name="login_bad_credentials">Bad username or password</string>
|
||||||
|
<string name="login_bad_account_permission">No permission to open log. Check entered county name</string>
|
||||||
<string name="login_denied">Login is failed</string>
|
<string name="login_denied">Login is failed</string>
|
||||||
<string name="please_wait">Please wait…</string>
|
<string name="please_wait">Please wait…</string>
|
||||||
<string name="title_activity_dashboard">Dashboard Activity</string>
|
<string name="title_activity_dashboard">Dashboard Activity</string>
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
package io.github.wulkanowy.activity.main;
|
|
||||||
|
|
||||||
import org.jsoup.Jsoup;
|
|
||||||
import org.jsoup.nodes.Document;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
public class CheckPassTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testFailureLogin() throws Exception {
|
|
||||||
String html = "<div class='ErrorMessage center'>Zła nazwa użytkownika lub hasło</div>";
|
|
||||||
Document doc = Jsoup.parse(html);
|
|
||||||
CheckPass obj = new CheckPass(doc);
|
|
||||||
|
|
||||||
assertFalse(obj.isLogged());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSuccessfulLogin() throws Exception {
|
|
||||||
String html = "<title>Working...</title>";
|
|
||||||
Document doc = Jsoup.parse(html);
|
|
||||||
CheckPass check = new CheckPass(doc);
|
|
||||||
|
|
||||||
assertTrue(check.isLogged());
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,13 @@
|
|||||||
|
package io.github.wulkanowy.api.login;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class LoginTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void login() throws Exception {
|
||||||
|
assertEquals(4, 2 + 2);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user