Search in sources :

Example 1 with LiveConnectSession

use of com.microsoft.services.msa.LiveConnectSession in project keepass2android by PhilippC.

the class MyMSAAuthenticator method login.

/**
 * Starts an interactive login.
 * @param emailAddressHint The hint for the email address during the interactive login.
 * @return The account info.
 * @throws ClientException An exception occurs if the login was unable to complete for any reason.
 */
@Override
public synchronized IAccountInfo login(final String emailAddressHint) throws ClientException {
    if (!mInitialized) {
        throw new IllegalStateException("init must be called");
    }
    mLogger.logDebug("Starting login");
    final AtomicReference<ClientException> error = new AtomicReference<>();
    final SimpleWaiter waiter = new SimpleWaiter();
    final LiveAuthListener listener = new LiveAuthListener() {

        @Override
        public void onAuthComplete(final LiveStatus liveStatus, final LiveConnectSession liveConnectSession, final Object o) {
            if (liveStatus == LiveStatus.NOT_CONNECTED) {
                mLogger.logDebug("Received invalid login failure from silent authentication with MSA, ignoring.");
            } else {
                mLogger.logDebug("Successful interactive login");
                waiter.signal();
            }
        }

        @Override
        public void onAuthError(final LiveAuthException e, final Object o) {
            OneDriveErrorCodes code = OneDriveErrorCodes.AuthenticationFailure;
            if (e.getError().equals(SIGN_IN_CANCELLED_MESSAGE)) {
                code = OneDriveErrorCodes.AuthenticationCancelled;
            }
            error.set(new ClientAuthenticatorException("Unable to login with MSA", e, code));
            mLogger.logError(error.get().getMessage(), error.get());
            waiter.signal();
        }
    };
    mActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            mAuthClient.login(mActivity, /* scopes */
            null, /* user object */
            null, emailAddressHint, listener);
        }
    });
    mLogger.logDebug("Waiting for MSA callback");
    waiter.waitForSignal();
    final ClientException exception = error.get();
    if (exception != null) {
        throw exception;
    }
    final String userId;
    if (emailAddressHint != null) {
        userId = emailAddressHint;
    } else {
        userId = DEFAULT_USER_ID;
    }
    mUserId.set(userId);
    final SharedPreferences prefs = getSharedPreferences();
    prefs.edit().putString(USER_ID_KEY, mUserId.get()).putInt(VERSION_CODE_KEY, BuildConfig.VERSION_CODE).apply();
    return getAccountInfo();
}
Also used : OneDriveErrorCodes(com.onedrive.sdk.core.OneDriveErrorCodes) SharedPreferences(android.content.SharedPreferences) LiveConnectSession(com.microsoft.services.msa.LiveConnectSession) LiveAuthException(com.microsoft.services.msa.LiveAuthException) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveAuthListener(com.microsoft.services.msa.LiveAuthListener) LiveStatus(com.microsoft.services.msa.LiveStatus) ClientAuthenticatorException(com.onedrive.sdk.authentication.ClientAuthenticatorException) SimpleWaiter(com.onedrive.sdk.concurrency.SimpleWaiter) ClientException(com.onedrive.sdk.core.ClientException)

Example 2 with LiveConnectSession

use of com.microsoft.services.msa.LiveConnectSession in project keepass2android by PhilippC.

the class MyMSAAuthenticator method logout.

/**
 * Log the current user out.
 * @throws ClientException An exception occurs if the logout was unable to complete for any reason.
 */
@Override
public synchronized void logout() throws ClientException {
    if (!mInitialized) {
        throw new IllegalStateException("init must be called");
    }
    mLogger.logDebug("Starting logout");
    final SimpleWaiter logoutWaiter = new SimpleWaiter();
    final AtomicReference<ClientException> error = new AtomicReference<>();
    mAuthClient.logout(new LiveAuthListener() {

        @Override
        public void onAuthComplete(final LiveStatus liveStatus, final LiveConnectSession liveConnectSession, final Object o) {
            mLogger.logDebug("Logout completed");
            logoutWaiter.signal();
        }

        @Override
        public void onAuthError(final LiveAuthException e, final Object o) {
            error.set(new ClientAuthenticatorException("MSA Logout failed", e, OneDriveErrorCodes.AuthenticationFailure));
            mLogger.logError(error.get().getMessage(), error.get());
            logoutWaiter.signal();
        }
    });
    mLogger.logDebug("Waiting for logout to complete");
    logoutWaiter.waitForSignal();
    mLogger.logDebug("Clearing all MSA Authenticator shared preferences");
    final SharedPreferences prefs = getSharedPreferences();
    prefs.edit().clear().putInt(VERSION_CODE_KEY, BuildConfig.VERSION_CODE).apply();
    mUserId.set(null);
    final ClientException exception = error.get();
    if (exception != null) {
        throw exception;
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) LiveConnectSession(com.microsoft.services.msa.LiveConnectSession) LiveAuthException(com.microsoft.services.msa.LiveAuthException) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveAuthListener(com.microsoft.services.msa.LiveAuthListener) LiveStatus(com.microsoft.services.msa.LiveStatus) ClientAuthenticatorException(com.onedrive.sdk.authentication.ClientAuthenticatorException) SimpleWaiter(com.onedrive.sdk.concurrency.SimpleWaiter) ClientException(com.onedrive.sdk.core.ClientException)

Example 3 with LiveConnectSession

use of com.microsoft.services.msa.LiveConnectSession in project keepass2android by PhilippC.

the class MyMSAAuthenticator method loginSilent.

/**
 * Starts a silent login.
 * @return The account info.
 * @throws ClientException An exception occurs if the login was unable to complete for any reason.
 */
@Override
public synchronized IAccountInfo loginSilent() throws ClientException {
    if (!mInitialized) {
        throw new IllegalStateException("init must be called");
    }
    mLogger.logDebug("Starting login silent");
    final int userIdStoredMinVersion = 10112;
    if (getSharedPreferences().getInt(VERSION_CODE_KEY, 0) >= userIdStoredMinVersion && mUserId.get() == null) {
        mLogger.logDebug("No login information found for silent authentication");
        return null;
    }
    final SimpleWaiter loginSilentWaiter = new SimpleWaiter();
    final AtomicReference<ClientException> error = new AtomicReference<>();
    final boolean waitForCallback = mAuthClient.loginSilent(new LiveAuthListener() {

        @Override
        public void onAuthComplete(final LiveStatus liveStatus, final LiveConnectSession liveConnectSession, final Object o) {
            if (liveStatus == LiveStatus.NOT_CONNECTED) {
                error.set(new ClientAuthenticatorException("Failed silent login, interactive login required", OneDriveErrorCodes.AuthenticationFailure));
                mLogger.logError(error.get().getMessage(), error.get());
            } else {
                mLogger.logDebug("Successful silent login");
            }
            loginSilentWaiter.signal();
        }

        @Override
        public void onAuthError(final LiveAuthException e, final Object o) {
            OneDriveErrorCodes code = OneDriveErrorCodes.AuthenticationFailure;
            if (e.getError().equals(SIGN_IN_CANCELLED_MESSAGE)) {
                code = OneDriveErrorCodes.AuthenticationCancelled;
            }
            error.set(new ClientAuthenticatorException("Login silent authentication error", e, code));
            mLogger.logError(error.get().getMessage(), error.get());
            loginSilentWaiter.signal();
        }
    });
    if (!waitForCallback) {
        mLogger.logDebug("MSA silent auth fast-failed");
        return null;
    }
    mLogger.logDebug("Waiting for MSA callback");
    loginSilentWaiter.waitForSignal();
    final ClientException exception = error.get();
    if (exception != null) {
        throw exception;
    }
    return getAccountInfo();
}
Also used : OneDriveErrorCodes(com.onedrive.sdk.core.OneDriveErrorCodes) LiveConnectSession(com.microsoft.services.msa.LiveConnectSession) LiveAuthException(com.microsoft.services.msa.LiveAuthException) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveAuthListener(com.microsoft.services.msa.LiveAuthListener) LiveStatus(com.microsoft.services.msa.LiveStatus) ClientAuthenticatorException(com.onedrive.sdk.authentication.ClientAuthenticatorException) SimpleWaiter(com.onedrive.sdk.concurrency.SimpleWaiter) ClientException(com.onedrive.sdk.core.ClientException)

Aggregations

LiveAuthException (com.microsoft.services.msa.LiveAuthException)3 LiveAuthListener (com.microsoft.services.msa.LiveAuthListener)3 LiveConnectSession (com.microsoft.services.msa.LiveConnectSession)3 LiveStatus (com.microsoft.services.msa.LiveStatus)3 ClientAuthenticatorException (com.onedrive.sdk.authentication.ClientAuthenticatorException)3 SimpleWaiter (com.onedrive.sdk.concurrency.SimpleWaiter)3 ClientException (com.onedrive.sdk.core.ClientException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 SharedPreferences (android.content.SharedPreferences)2 OneDriveErrorCodes (com.onedrive.sdk.core.OneDriveErrorCodes)2