Login improved
This commit is contained in:
@@ -44,6 +44,7 @@ import de.sebse.fuplanner.services.canteen.types.CanteenListener;
|
||||
import de.sebse.fuplanner.services.fulogin.AccountGeneral;
|
||||
import de.sebse.fuplanner.services.kvv.KVV;
|
||||
import de.sebse.fuplanner.services.kvv.KVVListener;
|
||||
import de.sebse.fuplanner.services.kvv.Login;
|
||||
import de.sebse.fuplanner.services.kvv.sync.KVVContentProvider;
|
||||
import de.sebse.fuplanner.services.kvv.types.LoginTokenKVV;
|
||||
import de.sebse.fuplanner.services.kvv.types.Modules;
|
||||
@@ -124,7 +125,7 @@ public class MainActivity extends AppCompatActivity
|
||||
if (!mAccountManager.hasAccounts(AccountGeneral.ACCOUNT_TYPE)) {
|
||||
desiredPage = getDefaultFragmentAfterLogout();
|
||||
desiredData = "";
|
||||
mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, null);
|
||||
mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, null, null);
|
||||
updateNavigation();
|
||||
changeFragment(desiredPage, desiredData);
|
||||
} else {
|
||||
@@ -132,9 +133,9 @@ public class MainActivity extends AppCompatActivity
|
||||
changeFragment(FRAGMENT_STARTUP);
|
||||
int targetPage = desiredPage;
|
||||
String targetData = desiredData;
|
||||
getKVV().account().restoreOnlineLogin(isRestored -> {
|
||||
getKVV().account().restoreOnlineLogin(restoreResult -> {
|
||||
updateNavigation();
|
||||
if (isRestored)
|
||||
if (restoreResult != Login.RESTORE_STATUS_INVALID_PASSWORD)
|
||||
changeFragment(targetPage, targetData);
|
||||
else
|
||||
changeFragment(getDefaultFragmentAfterLogout());
|
||||
@@ -159,11 +160,11 @@ public class MainActivity extends AppCompatActivity
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (isPaused) {
|
||||
getKVV().account().restoreOnlineLogin(isRestored -> {
|
||||
getKVV().account().restoreOnlineLogin(restoreResult -> {
|
||||
updateNavigation();
|
||||
if (isRestored && !isLoggedInBeforePause)
|
||||
if (restoreResult == Login.RESTORE_STATUS_SUCCESS && !isLoggedInBeforePause)
|
||||
changeFragment(getDefaultFragmentAfterLogin());
|
||||
else if (!isRestored && isLoggedInBeforePause) {
|
||||
else if (restoreResult == Login.RESTORE_STATUS_INVALID_PASSWORD && isLoggedInBeforePause) {
|
||||
getKVV().account().logout(false);
|
||||
changeFragment(getDefaultFragmentAfterLogout());
|
||||
}
|
||||
@@ -348,7 +349,7 @@ public class MainActivity extends AppCompatActivity
|
||||
setRefreshFailedBanner(false);
|
||||
updateNavigation();
|
||||
changeFragment(getDefaultFragmentAfterLogout());
|
||||
mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, null);
|
||||
mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, null, null);
|
||||
}
|
||||
|
||||
private void toLoginState(String fullName, String email, int newFragment) {
|
||||
@@ -512,7 +513,7 @@ public class MainActivity extends AppCompatActivity
|
||||
if (drawer.isDrawerOpen(GravityCompat.START)) {
|
||||
drawer.closeDrawer(GravityCompat.START);
|
||||
}
|
||||
mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, null);
|
||||
mAccountManager.getTokenByType(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, null, null);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,13 @@ public class FUAuthenticator extends AbstractAccountAuthenticator {
|
||||
if (password != null) {
|
||||
try {
|
||||
authToken = new UserLoginTask(account.name, password, authTokenType, mContext).execute((Void) null).get();
|
||||
// Login when auth/re-auth
|
||||
if (!TextUtils.isEmpty(authToken) && authToken.startsWith("Error: ")) {
|
||||
// if password is not wrong -> Networking error
|
||||
if (!authToken.contains("100143") && !authToken.contains("100243"))
|
||||
throw new NetworkErrorException(authToken);
|
||||
else authToken = null;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
|
||||
@@ -62,6 +62,7 @@ public class UserLoginTask extends AsyncTask<Void, Void, String> {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
AtomicReference<String> login = new AtomicReference<>();
|
||||
NetworkErrorCallback errorFunc = error -> {
|
||||
login.set("Error: "+String.valueOf(error.getCode()));
|
||||
log.e(error);
|
||||
latch.countDown();
|
||||
};
|
||||
|
||||
@@ -19,20 +19,24 @@ import de.sebse.fuplanner.tools.network.NetworkError;
|
||||
import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
|
||||
|
||||
public class Login extends HTTPService {
|
||||
public static final int RESTORE_STATUS_SUCCESS = 1;
|
||||
public static final int RESTORE_STATUS_ERROR = 2;
|
||||
public static final int RESTORE_STATUS_INVALID_PASSWORD = 3;
|
||||
private final KVVListener mListener;
|
||||
@Nullable private LoginTokenKVV mTokenKVV;
|
||||
@Nullable private LoginTokenBB mTokenBB;
|
||||
private boolean mLoginPending = false;
|
||||
private final NetworkCallbackCollector<Pair<LoginTokenKVV, LoginTokenBB>> mRefreshCallbacks = new NetworkCallbackCollector<>();
|
||||
private final NetworkCallbackCollector<Integer> mRestoreCallbacks = new NetworkCallbackCollector<>();
|
||||
|
||||
Login(KVVListener listener, Context context) {
|
||||
super(context);
|
||||
this.mListener = listener;
|
||||
}
|
||||
|
||||
public void restoreOnlineLogin(BooleanInterface callback) {
|
||||
public void restoreOnlineLogin(IntegerInterface callback) {
|
||||
mRestoreCallbacks.add(new Pair<>(callback::run, null));
|
||||
if (mLoginPending) {
|
||||
callback.run(false);
|
||||
return;
|
||||
}
|
||||
mLoginPending = true;
|
||||
@@ -40,17 +44,17 @@ public class Login extends HTTPService {
|
||||
LoginTokenBB.load(mListener.getAccountManager(), tokenBB -> {
|
||||
boolean result = setToken(tokenKVV, tokenBB);
|
||||
mLoginPending = false;
|
||||
callback.run(result);
|
||||
});
|
||||
});
|
||||
mRestoreCallbacks.responseResponse(result ? RESTORE_STATUS_SUCCESS : RESTORE_STATUS_INVALID_PASSWORD);
|
||||
}, e -> mRestoreCallbacks.responseResponse(RESTORE_STATUS_ERROR));
|
||||
}, e -> mRestoreCallbacks.responseResponse(RESTORE_STATUS_ERROR));
|
||||
}
|
||||
|
||||
public void isOfflineStoredAvailable(BooleanInterface callback) {
|
||||
LoginTokenKVV.load(mListener.getAccountManager(), tokenKVV -> {
|
||||
LoginTokenBB.load(mListener.getAccountManager(), tokenBB -> {
|
||||
callback.run(tokenKVV != null && tokenBB != null);
|
||||
});
|
||||
});
|
||||
}, e -> callback.run(false));
|
||||
}, e -> callback.run(false));
|
||||
}
|
||||
|
||||
public boolean logout(boolean delete) {
|
||||
@@ -114,12 +118,14 @@ public class Login extends HTTPService {
|
||||
CustomAccountManager manager = mListener.getAccountManager();
|
||||
manager.doInvalidateToken(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_KVV, ignored -> {
|
||||
manager.doInvalidateToken(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_BLACKBOARD, ignored2 -> {
|
||||
restoreOnlineLogin(isRestored -> {
|
||||
if (isRestored)
|
||||
restoreOnlineLogin(restoreResult -> {
|
||||
if (restoreResult == RESTORE_STATUS_SUCCESS)
|
||||
testLoginToken(mRefreshCallbacks::responseResponse, mRefreshCallbacks::responseError);
|
||||
else {
|
||||
else if (restoreResult == RESTORE_STATUS_INVALID_PASSWORD) {
|
||||
logout(true);
|
||||
mRefreshCallbacks.responseError(new NetworkError(100180, 403, "Re-login failed!"));
|
||||
mRefreshCallbacks.responseError(new NetworkError(100180, 403, "Re-login failed (password)!"));
|
||||
} else {
|
||||
mRefreshCallbacks.responseError(new NetworkError(100181, 403, "Re-login failed (error)!"));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -150,4 +156,8 @@ public class Login extends HTTPService {
|
||||
public interface BooleanInterface {
|
||||
void run(boolean bool);
|
||||
}
|
||||
|
||||
public interface IntegerInterface {
|
||||
void run(int integer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,6 +148,12 @@ public class BBLogin extends HTTPService {
|
||||
body.put("j_password", password);
|
||||
body.put("_eventId_proceed", "");
|
||||
post("https://identity.fu-berlin.de/idp-fub/profile/SAML2/Redirect/SSO?execution=e1s1", cookies, body, response -> {
|
||||
String content = response.getParsed();
|
||||
if (content == null) {
|
||||
errorCallback.onError(new NetworkError(100243, -1, "Error on getting SAML response!"));
|
||||
return;
|
||||
}
|
||||
|
||||
String cookies1 = response.getHeaders().get("Set-Cookie");
|
||||
if (cookies1 ==null) {
|
||||
errorCallback.onError(new NetworkError(100241, -1, "Error on logging in to FU Identity Server!"));
|
||||
@@ -160,12 +166,6 @@ public class BBLogin extends HTTPService {
|
||||
errorCallback.onError(new NetworkError(100242, -1, "Error on logging in to FU Identity Server!"));
|
||||
return;
|
||||
}
|
||||
|
||||
String content = response.getParsed();
|
||||
if (content == null) {
|
||||
errorCallback.onError(new NetworkError(100243, -1, "Error on getting SAML response!"));
|
||||
return;
|
||||
}
|
||||
Pattern pattern = Pattern.compile("name=\"SAMLResponse\" value=\"([0-9a-zA-Z+]+=*)");
|
||||
Matcher matcher = pattern.matcher(content);
|
||||
if (!matcher.find()) {
|
||||
|
||||
@@ -147,6 +147,12 @@ public class KVVLogin extends HTTPService {
|
||||
body.put("j_password", password);
|
||||
body.put("_eventId_proceed", "");
|
||||
post("https://identity.fu-berlin.de/idp-fub/profile/SAML2/Redirect/SSO?execution=e1s1", cookies, body, response -> {
|
||||
String content = response.getParsed();
|
||||
if (content == null) {
|
||||
errorCallback.onError(new NetworkError(100143, -1, "Error on getting SAML response!"));
|
||||
return;
|
||||
}
|
||||
|
||||
String cookies1 = response.getHeaders().get("Set-Cookie");
|
||||
if (cookies1 ==null) {
|
||||
errorCallback.onError(new NetworkError(100141, -1, "Error on logging in to FU Identity Server!"));
|
||||
@@ -159,12 +165,6 @@ public class KVVLogin extends HTTPService {
|
||||
errorCallback.onError(new NetworkError(100142, -1, "Error on logging in to FU Identity Server!"));
|
||||
return;
|
||||
}
|
||||
|
||||
String content = response.getParsed();
|
||||
if (content == null) {
|
||||
errorCallback.onError(new NetworkError(100143, -1, "Error on getting SAML response!"));
|
||||
return;
|
||||
}
|
||||
Pattern pattern = Pattern.compile("name=\"SAMLResponse\" value=\"([0-9a-zA-Z+]+=*)");
|
||||
Matcher matcher = pattern.matcher(content);
|
||||
if (!matcher.find()) {
|
||||
|
||||
@@ -28,7 +28,7 @@ public class LoginTokenBB {
|
||||
this.session_id = session_id;
|
||||
}
|
||||
|
||||
public static void load(CustomAccountManager manager, LoginTokenInterface callback) {
|
||||
public static void load(CustomAccountManager manager, LoginTokenInterface callback, CustomAccountManager.ExceptionInterface errorCallback) {
|
||||
if (!manager.hasAccounts(AccountGeneral.ACCOUNT_TYPE)) {
|
||||
callback.run(null);
|
||||
return;
|
||||
@@ -39,7 +39,7 @@ public class LoginTokenBB {
|
||||
return;
|
||||
}
|
||||
callback.run(LoginTokenBB.fromJsonString(tokenString));
|
||||
});
|
||||
}, errorCallback);
|
||||
}
|
||||
|
||||
public void delete(CustomAccountManager manager) {
|
||||
|
||||
@@ -26,7 +26,7 @@ public class LoginTokenKVV {
|
||||
this.JSESSIONID = JSESSIONID;
|
||||
}
|
||||
|
||||
public static void load(CustomAccountManager manager, LoginTokenInterface callback) {
|
||||
public static void load(CustomAccountManager manager, LoginTokenInterface callback, CustomAccountManager.ExceptionInterface errorCallback) {
|
||||
if (!manager.hasAccounts(AccountGeneral.ACCOUNT_TYPE)) {
|
||||
callback.run(null);
|
||||
return;
|
||||
@@ -37,7 +37,7 @@ public class LoginTokenKVV {
|
||||
return;
|
||||
}
|
||||
callback.run(LoginTokenKVV.fromJsonString(tokenString));
|
||||
});
|
||||
}, errorCallback);
|
||||
}
|
||||
|
||||
public void delete(CustomAccountManager manager) {
|
||||
|
||||
@@ -9,6 +9,8 @@ import android.app.Activity;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -77,7 +79,7 @@ public class CustomAccountManager {
|
||||
}
|
||||
}
|
||||
|
||||
public void getTokenByType(String accountType, String authTokenType, @Nullable StringInterface callback) {
|
||||
public void getTokenByType(String accountType, String authTokenType, @Nullable StringInterface callback, @Nullable ExceptionInterface errorCallback) {
|
||||
Activity activity = mActivityInterface.get();
|
||||
AccountManagerCallback<Bundle> cb = (accountManagerFuture -> {
|
||||
try {
|
||||
@@ -85,16 +87,11 @@ public class CustomAccountManager {
|
||||
final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
|
||||
if (callback != null)
|
||||
callback.run(authtoken);
|
||||
return;
|
||||
} catch (AuthenticatorException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (OperationCanceledException e) {
|
||||
} catch (AuthenticatorException | IOException | OperationCanceledException e) {
|
||||
e.printStackTrace();
|
||||
if (errorCallback != null)
|
||||
errorCallback.run(e);
|
||||
}
|
||||
if (callback != null)
|
||||
callback.run(null);
|
||||
});
|
||||
if (activity != null) {
|
||||
mAccountManager.getAuthTokenByFeatures(accountType, authTokenType, null, mActivityInterface.get(), null, null, cb, null);
|
||||
@@ -138,4 +135,8 @@ public class CustomAccountManager {
|
||||
public interface StringInterface {
|
||||
void run(@Nullable String string);
|
||||
}
|
||||
|
||||
public interface ExceptionInterface {
|
||||
void run(@NotNull Exception string);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@ import android.util.Pair;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import de.sebse.fuplanner.tools.network.NetworkCallback;
|
||||
import de.sebse.fuplanner.tools.network.NetworkError;
|
||||
import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
|
||||
|
||||
public class NetworkCallbackCollector<T> extends HashSet<Pair<NetworkCallback<T>, NetworkErrorCallback>> {
|
||||
public void add(NetworkCallback<T> success, NetworkErrorCallback error) {
|
||||
public void add(@Nullable NetworkCallback<T> success, @Nullable NetworkErrorCallback error) {
|
||||
add(new Pair<>(success, error));
|
||||
}
|
||||
|
||||
@@ -18,7 +19,8 @@ public class NetworkCallbackCollector<T> extends HashSet<Pair<NetworkCallback<T>
|
||||
Iterator<Pair<NetworkCallback<T>, NetworkErrorCallback>> i;
|
||||
for (i = this.iterator(); i.hasNext();) {
|
||||
Pair<NetworkCallback<T>, NetworkErrorCallback> pair = i.next();
|
||||
pair.second.onError(error);
|
||||
if (pair.second != null)
|
||||
pair.second.onError(error);
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
@@ -27,7 +29,8 @@ public class NetworkCallbackCollector<T> extends HashSet<Pair<NetworkCallback<T>
|
||||
Iterator<Pair<NetworkCallback<T>, NetworkErrorCallback>> i;
|
||||
for (i = this.iterator(); i.hasNext();) {
|
||||
Pair<NetworkCallback<T>, NetworkErrorCallback> pair = i.next();
|
||||
pair.first.onResponse(success);
|
||||
if (pair.first != null)
|
||||
pair.first.onResponse(success);
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user