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


package com.intsig.webstorage.googleaccount;

import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import com.intsig.account.AccountAPKUtil;
import com.intsig.account.AccountUtil;
import com.intsig.account.IRefreshListener;
import com.intsig.log.LogUtils;
import com.intsig.utils.net.OkHttpUtil;
import com.intsig.webstorage.exception.LoginException;
import com.intsig.webstorage.util.CloudServiceUtils;
import com.lzy.okgo.cache.CacheEntity;
import com.microsoft.aad.adal.AuthenticationConstants;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import okhttp3.Response;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class GoogleAuthUtil {

    /* renamed from: a  reason: collision with root package name */
    public static String f41067a = "179807108306.apps.googleusercontent.com";

    /* renamed from: b  reason: collision with root package name */
    private static String f41068b = "com.googleusercontent.apps.179807108306:/oauth2redirect";

    /* renamed from: c  reason: collision with root package name */
    public static int f41069c;

    public static boolean a(Context context, String str) {
        context.startActivity(AuthorizationManagementActivity.K5(context, c(str), str));
        return false;
    }

    public static void b(Context context, String str) {
        SharedPreferences.Editor edit = context.getSharedPreferences("camscanner", 0).edit();
        SharedPreferences.Editor putString = edit.putString(str + "ZYL_19910501", "");
        SharedPreferences.Editor putString2 = putString.putString(str + "dd2vr", "");
        SharedPreferences.Editor putString3 = putString2.putString(str + "d32r4v", "");
        SharedPreferences.Editor putLong = putString3.putLong(str + "d2r34r", 0);
        SharedPreferences.Editor putLong2 = putLong.putLong(str + "dy6y", 0);
        putLong2.putBoolean(str + "dy56y", true).apply();
    }

    private static String c(String str) {
        try {
            return AuthenticationConstants.Broker.REDIRECT_SSL_PREFIX + "accounts.google.com" + "/o/oauth2/auth" + "?" + e("scope") + "=" + e(str) + "&" + e("redirect_uri") + "=" + e(f41068b) + "&" + e("response_type") + "=" + e("code") + "&" + e("client_id") + "=" + e(f41067a);
        } catch (UnsupportedEncodingException e10) {
            CloudServiceUtils.d("GoogleAuthUtil", e10);
            return null;
        }
    }

    private static InputStream d(byte[] bArr) throws LoginException {
        try {
            URL url = new URL("https://accounts.google.com/o/oauth2/token");
            CloudServiceUtils.a("GoogleAuthUtil", "post url = " + url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setInstanceFollowRedirects(true);
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpURLConnection.connect();
            DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
            dataOutputStream.write(bArr);
            dataOutputStream.flush();
            dataOutputStream.close();
            return httpURLConnection.getInputStream();
        } catch (FileNotFoundException e10) {
            CloudServiceUtils.c("GoogleAuthUtil", "doAuthPost FileNotFoundException", e10);
            throw new LoginException();
        } catch (IOException e11) {
            CloudServiceUtils.d("GoogleAuthUtil", e11);
            return null;
        }
    }

    private static String e(String str) throws UnsupportedEncodingException {
        return URLEncoder.encode(str, "utf-8");
    }

    private static InputStream f(String str) throws LoginException {
        StringBuilder sb2 = new StringBuilder();
        sb2.append("grant_type=authorization_code");
        sb2.append("&code=" + str);
        sb2.append("&client_id=" + f41067a);
        sb2.append("&redirect_uri=" + f41068b);
        return d(sb2.toString().getBytes());
    }

    public static String g(String str) {
        Exception e10;
        String str2 = null;
        try {
            CloudServiceUtils.a("GoogleAuthUtil", "listDriveFile url = https://www.googleapis.com/oauth2/v2/userinfo");
            HashMap hashMap = new HashMap();
            hashMap.put("Authorization", "Bearer " + str);
            HashMap hashMap2 = new HashMap();
            hashMap2.put(CacheEntity.KEY, f41067a);
            Response e11 = OkHttpUtil.t().e("https://www.googleapis.com/oauth2/v2/userinfo", hashMap2, hashMap, true);
            if (e11.z()) {
                JSONObject jSONObject = (JSONObject) new JSONParser().parse(new InputStreamReader(e11.e().byteStream()));
                String str3 = (String) jSONObject.get("name");
                try {
                    str2 = TextUtils.isEmpty(str3) ? (String) jSONObject.get("email") : str3;
                } catch (Exception e12) {
                    e10 = e12;
                    str2 = str3;
                    CloudServiceUtils.c("GoogleAuthUtil", "", e10);
                    CloudServiceUtils.a("GoogleAuthUtil", "fetchUserInfo " + str2);
                    return str2;
                }
                CloudServiceUtils.a("GoogleAuthUtil", "fetchUserInfo " + str2);
                return str2;
            }
        } catch (Exception e13) {
            e10 = e13;
            CloudServiceUtils.c("GoogleAuthUtil", "", e10);
            CloudServiceUtils.a("GoogleAuthUtil", "fetchUserInfo " + str2);
            return str2;
        }
        CloudServiceUtils.a("GoogleAuthUtil", "fetchUserInfo " + str2);
        return str2;
    }

    public static void h(String str, Context context, String str2) {
        InputStream inputStream;
        try {
            inputStream = f(str);
        } catch (LoginException e10) {
            CloudServiceUtils.c("GoogleAuthUtil", "getAccessToken LoginException", e10);
            inputStream = null;
        }
        if (inputStream != null) {
            try {
                JSONObject jSONObject = (JSONObject) new JSONParser().parse(new InputStreamReader(inputStream));
                String str3 = (String) jSONObject.get("access_token");
                Long l10 = (Long) jSONObject.get("expires_in");
                String str4 = (String) jSONObject.get("refresh_token");
                CloudServiceUtils.a("GoogleAuthUtil", "Get Token: " + str3 + ", " + l10 + ", " + ((String) jSONObject.get("token_type")) + ", " + str4);
                SharedPreferences.Editor edit = context.getSharedPreferences("camscanner", 0).edit();
                StringBuilder sb2 = new StringBuilder();
                sb2.append(str2);
                sb2.append("dd2vr");
                SharedPreferences.Editor putString = edit.putString(sb2.toString(), str3);
                StringBuilder sb3 = new StringBuilder();
                sb3.append(str2);
                sb3.append("d32r4v");
                putString.putString(sb3.toString(), str4).putLong(str2 + "d2r34r", l10.longValue()).putLong(str2 + "dy6y", System.currentTimeMillis()).apply();
            } catch (IOException e11) {
                CloudServiceUtils.d("GoogleAuthUtil", e11);
            } catch (ParseException e12) {
                CloudServiceUtils.d("GoogleAuthUtil", e12);
            }
        }
    }

    public static String i(Context context, String str) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("camscanner", 0);
        return sharedPreferences.getString(str + "dd2vr", "");
    }

    public static int j(Context context, String str) {
        int i10 = 0;
        SharedPreferences sharedPreferences = context.getSharedPreferences("camscanner", 0);
        String string = sharedPreferences.getString(str + "d32r4v", "");
        String string2 = sharedPreferences.getString(str + "dd2vr", "");
        if (TextUtils.isEmpty(string) && !TextUtils.isEmpty(string2)) {
            CloudServiceUtils.e("GoogleAuthUtil", "refreshAccessTokenByAccountManager");
            i10 = 1;
        } else if (!TextUtils.isEmpty(string)) {
            CloudServiceUtils.e("GoogleAuthUtil", "refreshAccessTokenByOnline");
        } else {
            CloudServiceUtils.e("GoogleAuthUtil", "accessToken is empty, not login");
            i10 = -8;
        }
        CloudServiceUtils.e("GoogleAuthUtil", "refreshAccessToken res = " + i10);
        return i10;
    }

    public static String k(Context context, String str) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("camscanner", 0);
        return sharedPreferences.getString(str + "ZYL_19910501", null);
    }

    public static void l(Context context) {
        try {
            ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 128);
            f41067a = applicationInfo.metaData.getString("com.intsig.camscanner.driveclientid");
            f41068b = applicationInfo.metaData.getString("com.intsig.camscanner.drive.scheme") + ":/oauth2redirect";
            LogUtils.a("GoogleAuthUtil", "initClientId metaValue " + f41067a + " REDIRECT_URI=" + f41068b);
        } catch (Exception e10) {
            LogUtils.c("GoogleAuthUtil", e10.getMessage());
        }
    }

    public static boolean m(Context context, String str) {
        boolean z10 = false;
        SharedPreferences sharedPreferences = context.getSharedPreferences("camscanner", 0);
        String string = sharedPreferences.getString(str + "dd2vr", "");
        long j2 = sharedPreferences.getLong(str + "d2r34r", 0);
        long j10 = sharedPreferences.getLong(str + "dy6y", 0);
        if (!TextUtils.isEmpty(string)) {
            long currentTimeMillis = System.currentTimeMillis() - j10;
            if (currentTimeMillis > 0 && currentTimeMillis < (j2 - 20) * 1000) {
                CloudServiceUtils.b("GoogleAuthUtil", "isAuthTokenExpired = " + z10);
                return z10;
            }
        }
        z10 = true;
        CloudServiceUtils.b("GoogleAuthUtil", "isAuthTokenExpired = " + z10);
        return z10;
    }

    public static int n(Context context, String str) throws LoginException {
        int i10;
        SharedPreferences sharedPreferences = context.getSharedPreferences("camscanner", 0);
        String string = sharedPreferences.getString(str + "d32r4v", "");
        String string2 = sharedPreferences.getString(str + "dd2vr", "");
        if (TextUtils.isEmpty(string) && !TextUtils.isEmpty(string2)) {
            CloudServiceUtils.e("GoogleAuthUtil", "refreshAccessTokenByAccountManager");
            i10 = o(context, str, string2);
        } else if (!TextUtils.isEmpty(string)) {
            CloudServiceUtils.e("GoogleAuthUtil", "refreshAccessTokenByOnline");
            i10 = p(context, str);
        } else {
            CloudServiceUtils.e("GoogleAuthUtil", "accessToken is empty");
            i10 = -8;
        }
        CloudServiceUtils.e("GoogleAuthUtil", "refreshAccessToken res = " + i10);
        return i10;
    }

    private static int o(final Context context, final String str, String str2) {
        if (AccountAPKUtil.a(context) != AccountAPKUtil.f9798a) {
            return -8;
        }
        if (TextUtils.isEmpty(str2)) {
            return -8;
        }
        final AnonymousClass1 r02 = new IRefreshListener.Stub() {
            /* class com.intsig.webstorage.googleaccount.GoogleAuthUtil.AnonymousClass1 */

            @Override // com.intsig.account.IRefreshListener
            public void F(int i10) throws RemoteException {
                CloudServiceUtils.b("GoogleAuthUtil", "refreshError errCode = " + i10);
                GoogleAuthUtil.f41069c = 2;
            }

            @Override // com.intsig.account.IRefreshListener
            public void H(String str) throws RemoteException {
                GoogleAuthUtil.r(context, str, str, AuthenticationConstants.DEFAULT_EXPIRATION_TIME_SEC);
                GoogleAuthUtil.f41069c = 0;
                CloudServiceUtils.b("GoogleAuthUtil", "refreshOK token = " + str);
            }
        };
        AccountUtil.a(context, str, str2, new ServiceConnection() {
            /* class com.intsig.webstorage.googleaccount.GoogleAuthUtil.AnonymousClass2 */

            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                AccountUtil.b(IRefreshListener.this);
                CloudServiceUtils.b("GoogleAuthUtil", "onServiceDisconnected ComponentName = " + componentName.getClass().getName());
            }

            public void onServiceDisconnected(ComponentName componentName) {
                AccountUtil.c(IRefreshListener.this);
            }
        });
        CloudServiceUtils.b("GoogleAuthUtil", "waiting refreshAccessTokenByAccountManager");
        f41069c = 1;
        while (f41069c == 1) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e10) {
                CloudServiceUtils.d("GoogleAuthUtil", e10);
            }
        }
        CloudServiceUtils.b("GoogleAuthUtil", "unbindFromService IS_WAITING_ACCOUNT_AUTH = " + f41069c);
        AccountUtil.d(context);
        return f41069c == 2 ? -8 : 0;
    }

    private static int p(Context context, String str) throws LoginException {
        JSONObject q10;
        SharedPreferences sharedPreferences = context.getSharedPreferences("camscanner", 0);
        String string = sharedPreferences.getString(str + "d32r4v", "");
        if (TextUtils.isEmpty(string) || (q10 = q(string)) == null || !q10.containsKey("access_token")) {
            return -4;
        }
        String str2 = (String) q10.get("access_token");
        long longValue = ((Long) q10.get("expires_in")).longValue();
        long currentTimeMillis = System.currentTimeMillis();
        CloudServiceUtils.a("GoogleAuthUtil", "refresh access token at " + currentTimeMillis);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        SharedPreferences.Editor putString = edit.putString(str + "dd2vr", str2);
        SharedPreferences.Editor putLong = putString.putLong(str + "d2r34r", longValue);
        putLong.putLong(str + "dy6y", currentTimeMillis).apply();
        return 0;
    }

    private static JSONObject q(String str) throws LoginException {
        StringBuilder sb2 = new StringBuilder();
        sb2.append("grant_type=refresh_token");
        sb2.append("&refresh_token=" + str);
        sb2.append("&client_id=" + f41067a);
        InputStream d10 = d(sb2.toString().getBytes());
        if (d10 != null) {
            try {
                return (JSONObject) new JSONParser().parse(new InputStreamReader(d10));
            } catch (IOException e10) {
                CloudServiceUtils.d("GoogleAuthUtil", e10);
            } catch (ParseException e11) {
                CloudServiceUtils.d("GoogleAuthUtil", e11);
            }
        }
        return null;
    }

    public static void r(Context context, String str, String str2, int i10) {
        SharedPreferences.Editor edit = context.getSharedPreferences("camscanner", 0).edit();
        SharedPreferences.Editor putString = edit.putString(str + "dd2vr", str2);
        SharedPreferences.Editor putString2 = putString.putString(str + "d32r4v", "");
        SharedPreferences.Editor putLong = putString2.putLong(str + "d2r34r", (long) i10);
        putLong.putLong(str + "dy6y", System.currentTimeMillis()).apply();
    }

    public static void s(Context context, String str, String str2) {
        SharedPreferences.Editor edit = context.getSharedPreferences("camscanner", 0).edit();
        edit.putString(str + "ZYL_19910501", str2).apply();
    }
}