CamScanner.apk(点击下载) / AuthActivity.java


package com.dropbox.core.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import com.dropbox.core.DbxAuthFinish;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxHost;
import com.dropbox.core.DbxPKCEManager;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxRequestUtil;
import com.dropbox.core.IncludeGrantedScopes;
import com.dropbox.core.TokenAccessType;
import com.facebook.appevents.AppEventsConstants;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.scribe.model.OAuthConstants;

public class AuthActivity extends Activity {
    public static final String ACTION_AUTHENTICATE_V1 = "com.dropbox.android.AUTHENTICATE_V1";
    public static final String ACTION_AUTHENTICATE_V2 = "com.dropbox.android.AUTHENTICATE_V2";
    public static final String AUTH_PATH_CONNECT = "/connect";
    public static final int AUTH_VERSION = 1;
    private static final String DEFAULT_WEB_HOST = "www.dropbox.com";
    public static final String EXTRA_ACCESS_SECRET = "ACCESS_SECRET";
    public static final String EXTRA_ACCESS_TOKEN = "ACCESS_TOKEN";
    public static final String EXTRA_ALREADY_AUTHED_UIDS = "ALREADY_AUTHED_UIDS";
    public static final String EXTRA_AUTH_QUERY_PARAMS = "AUTH_QUERY_PARAMS";
    public static final String EXTRA_AUTH_STATE = "AUTH_STATE";
    public static final String EXTRA_CALLING_CLASS = "CALLING_CLASS";
    public static final String EXTRA_CALLING_PACKAGE = "CALLING_PACKAGE";
    public static final String EXTRA_CONSUMER_KEY = "CONSUMER_KEY";
    public static final String EXTRA_CONSUMER_SIG = "CONSUMER_SIG";
    public static final String EXTRA_DESIRED_UID = "DESIRED_UID";
    public static final String EXTRA_EXPIRES_AT = "EXPIRES_AT";
    public static final String EXTRA_REFRESH_TOKEN = "REFRESH_TOKEN";
    public static final String EXTRA_SCOPE = "SCOPE";
    public static final String EXTRA_SESSION_ID = "SESSION_ID";
    public static final String EXTRA_UID = "UID";
    private static final String SIS_KEY_AUTH_STATE_NONCE = "SIS_KEY_AUTH_STATE_NONCE";
    private static final String SIS_KEY_PKCE_CODE_VERIFIER = "SIS_KEY_PKCE_CODE_VERIFIER";
    private static final String TAG = "com.dropbox.core.android.AuthActivity";
    public static Intent result = null;
    private static String[] sAlreadyAuthedUids;
    private static String sApiType;
    private static String sAppKey;
    private static String sDesiredUid;
    private static DbxHost sHost;
    private static IncludeGrantedScopes sIncludeGrantedScopes;
    private static DbxRequestConfig sRequestConfig;
    private static String sScope;
    private static SecurityProvider sSecurityProvider = new SecurityProvider() {
        /* class com.dropbox.core.android.AuthActivity.AnonymousClass1 */

        @Override // com.dropbox.core.android.AuthActivity.SecurityProvider
        public SecureRandom getSecureRandom() {
            return FixedSecureRandom.get();
        }
    };
    private static final Object sSecurityProviderLock = new Object();
    private static String sSessionId;
    private static TokenAccessType sTokenAccessType;
    private boolean mActivityDispatchHandlerPosted = false;
    private String[] mAlreadyAuthedUids;
    private String mApiType;
    private String mAppKey;
    private String mAuthStateNonce = null;
    private String mDesiredUid;
    private DbxHost mHost;
    private IncludeGrantedScopes mIncludeGrantedScopes;
    private DbxPKCEManager mPKCEManager;
    private DbxRequestConfig mRequestConfig;
    private String mScope;
    private String mSessionId;
    private TokenAccessType mTokenAccessType;

    public interface SecurityProvider {
        SecureRandom getSecureRandom();
    }

    private class TokenRequestAsyncTask extends AsyncTask<Void, Void, DbxAuthFinish> {
        private final String code;

        private TokenRequestAsyncTask(String str) {
            this.code = str;
        }

        /* access modifiers changed from: protected */
        public DbxAuthFinish doInBackground(Void... voidArr) {
            try {
                return AuthActivity.this.mPKCEManager.makeTokenRequest(AuthActivity.this.mRequestConfig, this.code, AuthActivity.this.mAppKey, null, AuthActivity.this.mHost);
            } catch (DbxException e10) {
                String unused = AuthActivity.TAG;
                StringBuilder sb2 = new StringBuilder();
                sb2.append("Token Request Failed: ");
                sb2.append(e10.getMessage());
                return null;
            }
        }
    }

    private enum TokenType {
        OAUTH2("oauth2:"),
        OAUTH2CODE("oauth2code:");
        
        private String string;

        private TokenType(String str) {
            this.string = str;
        }

        public String toString() {
            return this.string;
        }
    }

    private void authFinished(Intent intent) {
        result = intent;
        this.mAuthStateNonce = null;
        setAuthParams(null, null, null);
        finish();
    }

    public static boolean checkAppBeforeAuth(Context context, String str, boolean z10) {
        Intent intent = new Intent("android.intent.action.VIEW");
        String str2 = "db-" + str;
        intent.setData(Uri.parse(str2 + "://" + 1 + AUTH_PATH_CONNECT));
        List<ResolveInfo> queryIntentActivities = context.getPackageManager().queryIntentActivities(intent, 0);
        if (queryIntentActivities == null || queryIntentActivities.size() == 0) {
            throw new IllegalStateException("URI scheme in your app's manifest is not set up correctly. You should have a " + AuthActivity.class.getName() + " with the scheme: " + str2);
        } else if (queryIntentActivities.size() > 1) {
            if (z10) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Security alert");
                builder.setMessage("Another app on your phone may be trying to pose as the app you are currently using. The malicious app can't access your account, but linking to Dropbox has been disabled as a precaution. Please contact support@dropbox.com.");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    /* class com.dropbox.core.android.AuthActivity.AnonymousClass2 */

                    public void onClick(DialogInterface dialogInterface, int i10) {
                        dialogInterface.dismiss();
                    }
                });
                builder.show();
            } else {
                StringBuilder sb2 = new StringBuilder();
                sb2.append("There are multiple apps registered for the AuthActivity URI scheme (");
                sb2.append(str2);
                sb2.append(").  Another app may be trying to  impersonate this app, so authentication will be disabled.");
            }
            return false;
        } else {
            ResolveInfo resolveInfo = queryIntentActivities.get(0);
            if (resolveInfo != null && resolveInfo.activityInfo != null && context.getPackageName().equals(resolveInfo.activityInfo.packageName)) {
                return true;
            }
            throw new IllegalStateException("There must be a " + AuthActivity.class.getName() + " within your app's package registered for your URI scheme (" + str2 + "). However, it appears that an activity in a different package is registered for that scheme instead. If you have multiple apps that all want to use the same accesstoken pair, designate one of them to do authentication and have the other apps launch it and then retrieve the token pair from it.");
        }
    }

    private String createExtraQueryParams() {
        if (this.mTokenAccessType != null) {
            Locale locale = Locale.US;
            String format = String.format(locale, "%s=%s&%s=%s&%s=%s&%s=%s", "code_challenge", this.mPKCEManager.getCodeChallenge(), "code_challenge_method", DbxPKCEManager.CODE_CHALLENGE_METHODS, "token_access_type", this.mTokenAccessType.toString(), "response_type", "code");
            if (this.mScope != null) {
                format = format + String.format(locale, "&%s=%s", "scope", this.mScope);
            }
            if (this.mIncludeGrantedScopes == null) {
                return format;
            }
            return format + String.format(locale, "&%s=%s", "include_granted_scopes", this.mIncludeGrantedScopes.toString());
        }
        throw new IllegalStateException("Extra Query Param should only be used in short live token flow.");
    }

    private String createPKCEStateNonce() {
        return String.format(Locale.US, "oauth2code:%s:%s:%s:%s:%s", this.mPKCEManager.getCodeChallenge(), DbxPKCEManager.CODE_CHALLENGE_METHODS, this.mTokenAccessType.toString(), this.mScope, this.mIncludeGrantedScopes);
    }

    private String createStateNonce() {
        byte[] bArr = new byte[16];
        getSecureRandom().nextBytes(bArr);
        StringBuilder sb2 = new StringBuilder();
        sb2.append("oauth2:");
        for (int i10 = 0; i10 < 16; i10++) {
            sb2.append(String.format("%02x", Integer.valueOf(bArr[i10] & 255)));
        }
        return sb2.toString();
    }

    static Intent getOfficialAuthIntent() {
        Intent intent = new Intent(ACTION_AUTHENTICATE_V2);
        intent.setPackage("com.dropbox.android");
        return intent;
    }

    private static SecureRandom getSecureRandom() {
        SecurityProvider securityProvider = getSecurityProvider();
        return securityProvider != null ? securityProvider.getSecureRandom() : new SecureRandom();
    }

    private static SecurityProvider getSecurityProvider() {
        SecurityProvider securityProvider;
        synchronized (sSecurityProviderLock) {
            securityProvider = sSecurityProvider;
        }
        return securityProvider;
    }

    public static Intent makeIntent(Context context, String str, String str2, String str3) {
        return makeIntent(context, str, null, null, null, str2, str3, null, null, null, null, null);
    }

    public static Intent makeIntent(Context context, String str, String str2, String[] strArr, String str3, String str4, String str5) {
        if (str != null) {
            setAuthParams(str, str2, strArr, str3, str4, str5, null, null, null, null, null);
            return new Intent(context, AuthActivity.class);
        }
        throw new IllegalArgumentException("'appKey' can't be null");
    }

    static Intent makeIntent(Context context, String str, String str2, String[] strArr, String str3, String str4, String str5, TokenAccessType tokenAccessType, DbxRequestConfig dbxRequestConfig, DbxHost dbxHost, String str6, IncludeGrantedScopes includeGrantedScopes) {
        if (str != null) {
            setAuthParams(str, str2, strArr, str3, str4, str5, tokenAccessType, dbxRequestConfig, dbxHost, str6, includeGrantedScopes);
            return new Intent(context, AuthActivity.class);
        }
        throw new IllegalArgumentException("'appKey' can't be null");
    }

    static void setAuthParams(String str, String str2, String[] strArr) {
        setAuthParams(str, str2, strArr, null);
    }

    static void setAuthParams(String str, String str2, String[] strArr, String str3) {
        setAuthParams(str, str2, strArr, str3, null, null, null, null, null, null, null);
    }

    static void setAuthParams(String str, String str2, String[] strArr, String str3, String str4) {
        setAuthParams(str, str2, strArr, null, null, null, null, null, null, null, null);
    }

    static void setAuthParams(String str, String str2, String[] strArr, String str3, String str4, String str5, TokenAccessType tokenAccessType, DbxRequestConfig dbxRequestConfig, DbxHost dbxHost, String str6, IncludeGrantedScopes includeGrantedScopes) {
        sAppKey = str;
        sDesiredUid = str2;
        if (strArr == null) {
            strArr = new String[0];
        }
        sAlreadyAuthedUids = strArr;
        sSessionId = str3;
        sApiType = str5;
        sTokenAccessType = tokenAccessType;
        sRequestConfig = dbxRequestConfig;
        if (dbxHost != null) {
            sHost = dbxHost;
        } else if (str4 != null) {
            DbxHost dbxHost2 = DbxHost.DEFAULT;
            sHost = new DbxHost(dbxHost2.getApi(), dbxHost2.getContent(), str4, dbxHost2.getNotify());
        } else {
            sHost = DbxHost.DEFAULT;
        }
        sScope = str6;
        sIncludeGrantedScopes = includeGrantedScopes;
    }

    public static void setSecurityProvider(SecurityProvider securityProvider) {
        synchronized (sSecurityProviderLock) {
            sSecurityProvider = securityProvider;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void startWebAuth(String str) {
        Locale locale = Locale.getDefault();
        Locale locale2 = new Locale(locale.getLanguage(), locale.getCountry());
        String[] strArr = this.mAlreadyAuthedUids;
        ArrayList arrayList = new ArrayList(Arrays.asList("k", this.mAppKey, "n", strArr.length > 0 ? strArr[0] : AppEventsConstants.EVENT_PARAM_VALUE_NO, "api", this.mApiType, "state", str));
        if (this.mTokenAccessType != null) {
            arrayList.add("extra_query_params");
            arrayList.add(createExtraQueryParams());
        }
        startActivity(new Intent("android.intent.action.VIEW", Uri.parse(DbxRequestUtil.buildUrlWithParams(locale2.toString(), this.mHost.getWeb(), "1/connect", (String[]) arrayList.toArray(new String[0])))));
    }

    /* access modifiers changed from: protected */
    public void onCreate(Bundle bundle) {
        this.mAppKey = sAppKey;
        this.mApiType = sApiType;
        this.mDesiredUid = sDesiredUid;
        this.mAlreadyAuthedUids = sAlreadyAuthedUids;
        this.mSessionId = sSessionId;
        this.mTokenAccessType = sTokenAccessType;
        this.mRequestConfig = sRequestConfig;
        this.mHost = sHost;
        this.mScope = sScope;
        this.mIncludeGrantedScopes = sIncludeGrantedScopes;
        if (bundle == null) {
            result = null;
            this.mAuthStateNonce = null;
            this.mPKCEManager = new DbxPKCEManager();
        } else {
            this.mAuthStateNonce = bundle.getString(SIS_KEY_AUTH_STATE_NONCE);
            this.mPKCEManager = new DbxPKCEManager(bundle.getString(SIS_KEY_PKCE_CODE_VERIFIER));
        }
        setTheme(16973840);
        super.onCreate(bundle);
    }

    /* access modifiers changed from: protected */
    /* JADX WARNING: Removed duplicated region for block: B:47:0x008c  */
    /* JADX WARNING: Removed duplicated region for block: B:49:0x0090  */
    public void onNewIntent(Intent intent) {
        String str;
        String str2;
        String str3;
        String str4;
        Intent intent2 = null;
        if (this.mAuthStateNonce == null) {
            authFinished(null);
            return;
        }
        if (intent.hasExtra(EXTRA_ACCESS_TOKEN)) {
            str4 = intent.getStringExtra(EXTRA_ACCESS_TOKEN);
            str3 = intent.getStringExtra(EXTRA_ACCESS_SECRET);
            str2 = intent.getStringExtra(EXTRA_UID);
            str = intent.getStringExtra(EXTRA_AUTH_STATE);
        } else {
            Uri data = intent.getData();
            if (data == null || !AUTH_PATH_CONNECT.equals(data.getPath())) {
                str = null;
                str4 = null;
                str3 = null;
                str2 = null;
            } else {
                try {
                    str4 = data.getQueryParameter(OAuthConstants.TOKEN);
                    try {
                        str3 = data.getQueryParameter(OAuthConstants.TOKEN_SECRET);
                    } catch (UnsupportedOperationException unused) {
                        str3 = null;
                        str2 = str3;
                        str = null;
                        if (this.mAuthStateNonce.equals(str)) {
                        }
                    }
                    try {
                        str2 = data.getQueryParameter("uid");
                        try {
                            str = data.getQueryParameter("state");
                        } catch (UnsupportedOperationException unused2) {
                        }
                    } catch (UnsupportedOperationException unused3) {
                        str2 = null;
                        str = null;
                        if (this.mAuthStateNonce.equals(str)) {
                        }
                    }
                } catch (UnsupportedOperationException unused4) {
                    str4 = null;
                    str3 = null;
                    str2 = str3;
                    str = null;
                    if (this.mAuthStateNonce.equals(str)) {
                    }
                }
            }
        }
        if (str4 != null && !str4.equals("") && str3 != null && !str3.equals("") && str2 != null && !str2.equals("") && str != null && !str.equals("")) {
            if (this.mAuthStateNonce.equals(str)) {
                authFinished(null);
                return;
            } else if (str4.equals(TokenType.OAUTH2.toString())) {
                intent2 = new Intent();
                intent2.putExtra(EXTRA_ACCESS_TOKEN, str4);
                intent2.putExtra(EXTRA_ACCESS_SECRET, str3);
                intent2.putExtra(EXTRA_UID, str2);
            } else if (str4.equals(TokenType.OAUTH2CODE.toString())) {
                try {
                    DbxAuthFinish dbxAuthFinish = (DbxAuthFinish) new TokenRequestAsyncTask(str3).execute(new Void[0]).get();
                    if (dbxAuthFinish != null) {
                        Intent intent3 = new Intent();
                        intent3.putExtra(EXTRA_ACCESS_TOKEN, dbxAuthFinish.getAccessToken());
                        intent3.putExtra(EXTRA_ACCESS_SECRET, dbxAuthFinish.getAccessToken());
                        intent3.putExtra(EXTRA_REFRESH_TOKEN, dbxAuthFinish.getRefreshToken());
                        intent3.putExtra(EXTRA_EXPIRES_AT, dbxAuthFinish.getExpiresAt());
                        intent3.putExtra(EXTRA_UID, dbxAuthFinish.getUserId());
                        intent3.putExtra(EXTRA_CONSUMER_KEY, this.mAppKey);
                        intent3.putExtra(EXTRA_SCOPE, dbxAuthFinish.getScope());
                        intent2 = intent3;
                    }
                } catch (Exception unused5) {
                }
            }
        }
        authFinished(intent2);
    }

    /* access modifiers changed from: protected */
    public void onResume() {
        final String str;
        super.onResume();
        if (!isFinishing()) {
            if (this.mAuthStateNonce != null || this.mAppKey == null) {
                authFinished(null);
                return;
            }
            result = null;
            if (!this.mActivityDispatchHandlerPosted) {
                final Intent officialAuthIntent = getOfficialAuthIntent();
                if (this.mTokenAccessType != null) {
                    str = createPKCEStateNonce();
                    officialAuthIntent.putExtra(EXTRA_AUTH_QUERY_PARAMS, createExtraQueryParams());
                } else {
                    str = createStateNonce();
                }
                officialAuthIntent.putExtra(EXTRA_CONSUMER_KEY, this.mAppKey);
                officialAuthIntent.putExtra(EXTRA_CONSUMER_SIG, "");
                officialAuthIntent.putExtra(EXTRA_CALLING_PACKAGE, getPackageName());
                officialAuthIntent.putExtra(EXTRA_CALLING_CLASS, getClass().getName());
                officialAuthIntent.putExtra(EXTRA_AUTH_STATE, str);
                officialAuthIntent.putExtra(EXTRA_DESIRED_UID, this.mDesiredUid);
                officialAuthIntent.putExtra(EXTRA_ALREADY_AUTHED_UIDS, this.mAlreadyAuthedUids);
                officialAuthIntent.putExtra(EXTRA_SESSION_ID, this.mSessionId);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    /* class com.dropbox.core.android.AuthActivity.AnonymousClass3 */

                    public void run() {
                        String unused = AuthActivity.TAG;
                        try {
                            if (DbxOfficialAppConnector.getDropboxAppPackage(AuthActivity.this, officialAuthIntent) != null) {
                                AuthActivity.this.startActivity(officialAuthIntent);
                            } else {
                                AuthActivity.this.startWebAuth(str);
                            }
                            AuthActivity.this.mAuthStateNonce = str;
                            AuthActivity.setAuthParams(null, null, null);
                        } catch (ActivityNotFoundException unused2) {
                            String unused3 = AuthActivity.TAG;
                            AuthActivity.this.finish();
                        }
                    }
                });
                this.mActivityDispatchHandlerPosted = true;
            }
        }
    }

    /* access modifiers changed from: protected */
    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putString(SIS_KEY_AUTH_STATE_NONCE, this.mAuthStateNonce);
        bundle.putString(SIS_KEY_PKCE_CODE_VERIFIER, this.mPKCEManager.getCodeVerifier());
    }
}