Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6ec83381 authored by daquexian's avatar daquexian Committed by Vincent Breitmoser
Browse files

Request for user's prompt when first logging in

parent 0e436413
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail;

public class OAuth2NeedUserPromptException extends MessagingException {

    public OAuth2NeedUserPromptException() {
        super("Need user's prompt for xoauth2");
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import java.util.List;
import android.app.Activity;

import com.fsck.k9.mail.AuthenticationFailedException;
import com.fsck.k9.mail.OAuth2NeedUserPromptException;


public interface OAuth2TokenProvider {
@@ -50,7 +51,7 @@ public interface OAuth2TokenProvider {
     * @return Token string
     * @throws AuthenticationFailedException
     */
    String getToken(String username, long timeoutMillis) throws AuthenticationFailedException;
    String getToken(String username, long timeoutMillis) throws AuthenticationFailedException, OAuth2NeedUserPromptException;

    /**
     * Invalidate the token for this username.
+2 −1
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.K9MailLib;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.NetworkType;
import com.fsck.k9.mail.OAuth2NeedUserPromptException;
import com.fsck.k9.mail.filter.Base64;
import com.fsck.k9.mail.filter.PeekableInputStream;
import com.fsck.k9.mail.oauth.OAuth2TokenProvider;
@@ -148,7 +149,6 @@ class ImapConnection {

            retrievePathPrefixIfNecessary();
            retrievePathDelimiterIfNecessary();

        } catch (SSLException e) {
            handleSslException(e);
        } catch (ConnectException e) {
@@ -441,6 +441,7 @@ class ImapConnection {
                        handleXOAuthUntaggedResponse(response);
                    }
                });

    }

    private void handleXOAuthUntaggedResponse(ImapResponse response) throws IOException {
+24 −9
Original line number Diff line number Diff line
@@ -8,10 +8,12 @@ import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.fsck.k9.R;
import com.fsck.k9.mail.AuthenticationFailedException;
import com.fsck.k9.mail.OAuth2NeedUserPromptException;
import com.fsck.k9.mail.oauth.AuthorizationException;
import com.fsck.k9.mail.oauth.OAuth2TokenProvider;

@@ -31,11 +33,16 @@ public class AndroidAccountOAuth2TokenStore implements OAuth2TokenProvider {

    private Map<String,String> authTokens = new HashMap<>();
    private AccountManager accountManager;
    private GMailXOauth2PromptRequestHandler promptRequestHandler;

    public AndroidAccountOAuth2TokenStore(Context applicationContext) {
        this.accountManager = AccountManager.get(applicationContext);
    }

    public void setPromptRequestHandler(GMailXOauth2PromptRequestHandler promptRequestHandler) {
        this.promptRequestHandler = promptRequestHandler;
    }

    @Override
    public void authorizeAPI(final String emailAddress, final Activity activity,
                             final OAuth2TokenProviderAuthCallback callback) {
@@ -79,7 +86,8 @@ public class AndroidAccountOAuth2TokenStore implements OAuth2TokenProvider {
    }

    @Override
    public String getToken(String username, long timeoutMillis) throws AuthenticationFailedException {
    public String getToken(String username, long timeoutMillis) throws
            AuthenticationFailedException, OAuth2NeedUserPromptException {
        if(authTokens.get(username) == null) {
            Account account = getAccountFromManager(username);
            if (account == null) {
@@ -101,19 +109,26 @@ public class AndroidAccountOAuth2TokenStore implements OAuth2TokenProvider {
    }

    private void fetchNewAuthToken(String username, Account account, long timeoutMillis)
            throws AuthenticationFailedException {
            throws AuthenticationFailedException, OAuth2NeedUserPromptException {
        try {
            AccountManagerFuture<Bundle> future = accountManager
                    .getAuthToken(account, GMAIL_AUTH_TOKEN_TYPE, false, null, null);
                    .getAuthToken(account, GMAIL_AUTH_TOKEN_TYPE, null, false, null, null);
            Bundle bundle = future.getResult(timeoutMillis, TimeUnit.MILLISECONDS);
            if (bundle == null)
                throw new AuthenticationFailedException("No token provided");
            if (bundle.get(AccountManager.KEY_INTENT) != null) {
                promptRequestHandler.handleIntent((Intent) bundle.get(AccountManager.KEY_INTENT));
                throw new OAuth2NeedUserPromptException();
            } else {
                if (bundle.get(AccountManager.KEY_ACCOUNT_NAME) == null)
                    throw new AuthenticationFailedException("No account information provided");
                if (bundle.get(AccountManager.KEY_ACCOUNT_NAME).equals(username))
                    authTokens.put(username, bundle.get(AccountManager.KEY_AUTHTOKEN).toString());
                else
                    throw new AuthenticationFailedException("Unexpected account information provided");
            }
        } catch (OAuth2NeedUserPromptException e) {
            throw e;
        } catch (Exception e) {
            throw new AuthenticationFailedException(e.getMessage());
        }
+7 −0
Original line number Diff line number Diff line
package com.fsck.k9.account;

import android.content.Intent;

public interface GMailXOauth2PromptRequestHandler {
    void handleIntent(Intent intent);
}