package com.sina.weibo.sdk.auth; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import com.huawei.hms.support.api.entity.common.CommonConstant; import com.huawei.hms.support.hianalytics.HiAnalyticsConstant; import com.igexin.assist.sdk.AssistPushConsts; import com.sina.weibo.sdk.WbSdk; import com.sina.weibo.sdk.WeiboAppManager; import com.sina.weibo.sdk.net.WeiboParameters; import com.sina.weibo.sdk.sso.WeiboSsoManager; import com.sina.weibo.sdk.utils.LogUtil; import com.sina.weibo.sdk.utils.NetworkHelper; import com.sina.weibo.sdk.utils.SecurityHelper; import com.sina.weibo.sdk.utils.UIUtils; import com.sina.weibo.sdk.utils.Utility; import com.sina.weibo.sdk.web.WebRequestType; import com.sina.weibo.sdk.web.WeiboCallbackManager; import com.sina.weibo.sdk.web.WeiboSdkWebActivity; import com.sina.weibo.sdk.web.param.AuthWebViewRequestParam; import com.xiaomi.mipush.sdk.PushMessageHelper; public class BaseSsoHandler { public static final String OAUTH2_BASE_URL = "https://open.weibo.cn/oauth2/authorize?"; private static final String TAG = "BaseSsoHandler"; public final int SSO_TYPE_INVALID = 3; public WbAuthListener authListener; public Context mAuthActivity; public int ssoRequestCode = -1; public int ssoRequestType = 3; public enum AuthType { ALL, SsoOnly, WebOnly } public BaseSsoHandler(Activity activity) { this.mAuthActivity = activity; WeiboSsoManager.getInstance().init(activity, WbSdk.getAuthInfo().getAppKey()); } public void authorize(WbAuthListener wbAuthListener) { authorize(32973, wbAuthListener, AuthType.ALL); } public void authorizeCallBack(int i10, int i14, Intent intent) { if (32973 != i10) { return; } if (i14 == -1) { Context context = this.mAuthActivity; if (!SecurityHelper.checkResponseAppLegal(context, WeiboAppManager.getInstance(context).getWbAppInfo(), intent)) { this.authListener.onFailure(new WbConnectErrorMessage("your install weibo app is counterfeit", "8001")); return; } String safeString = Utility.safeString(intent.getStringExtra("error")); String safeString2 = Utility.safeString(intent.getStringExtra(PushMessageHelper.ERROR_TYPE)); String safeString3 = Utility.safeString(intent.getStringExtra("error_description")); LogUtil.d(TAG, "error: " + safeString + ", error_type: " + safeString2 + ", error_description: " + safeString3); if (TextUtils.isEmpty(safeString) && TextUtils.isEmpty(safeString2) && TextUtils.isEmpty(safeString3)) { Oauth2AccessToken parseAccessToken = Oauth2AccessToken.parseAccessToken(intent.getExtras()); if (parseAccessToken != null && parseAccessToken.isSessionValid()) { LogUtil.d(TAG, "Login Success! " + parseAccessToken.toString()); AccessTokenKeeper.writeAccessToken(this.mAuthActivity, parseAccessToken); this.authListener.onSuccess(parseAccessToken); } } else if ("access_denied".equals(safeString) || "OAuthAccessDeniedException".equals(safeString)) { LogUtil.d(TAG, "Login canceled by user."); this.authListener.cancel(); } else { LogUtil.d(TAG, "Login failed: " + safeString); this.authListener.onFailure(new WbConnectErrorMessage(safeString2, safeString3)); } } else if (i14 == 0) { this.authListener.cancel(); } } public void authorizeClientSso(WbAuthListener wbAuthListener) { authorize(32973, wbAuthListener, AuthType.SsoOnly); } public void authorizeWeb(WbAuthListener wbAuthListener) { authorize(32973, wbAuthListener, AuthType.WebOnly); } public void fillExtraIntent(Intent intent, int i10) { } @Deprecated public boolean isWbAppInstalled() { return WbSdk.isWbInstall(this.mAuthActivity); } public void resetIntentFillData() { this.ssoRequestCode = 32973; } public void startClientAuth(int i10) { try { WbAppInfo wbAppInfo = WeiboAppManager.getInstance(this.mAuthActivity).getWbAppInfo(); Intent intent = new Intent(); intent.setClassName(wbAppInfo.getPackageName(), wbAppInfo.getAuthActivityName()); intent.putExtras(WbSdk.getAuthInfo().getAuthBundle()); intent.putExtra("_weibo_command_type", 3); intent.putExtra("_weibo_transaction", String.valueOf(System.currentTimeMillis())); intent.putExtra("aid", Utility.getAid(this.mAuthActivity, WbSdk.getAuthInfo().getAppKey())); if (!SecurityHelper.validateAppSignatureForIntent(this.mAuthActivity, intent)) { this.authListener.onFailure(new WbConnectErrorMessage("your install weibo app is counterfeit", "8001")); return; } fillExtraIntent(intent, i10); try { ((Activity) this.mAuthActivity).startActivityForResult(intent, this.ssoRequestCode); } catch (Exception unused) { WbAuthListener wbAuthListener = this.authListener; if (wbAuthListener != null) { wbAuthListener.onFailure(new WbConnectErrorMessage()); } } } catch (Exception unused2) { } } public void startWebAuth() { String str; AuthInfo authInfo = WbSdk.getAuthInfo(); WeiboParameters weiboParameters = new WeiboParameters(authInfo.getAppKey()); weiboParameters.put("client_id", authInfo.getAppKey()); weiboParameters.put(CommonConstant.ReqAccessTokenParam.REDIRECT_URI, authInfo.getRedirectUrl()); weiboParameters.put("scope", authInfo.getScope()); weiboParameters.put(CommonConstant.ReqAccessTokenParam.RESPONSE_TYPE, "code"); weiboParameters.put(HiAnalyticsConstant.HaKey.BI_KEY_VERSION, "0041005000"); weiboParameters.put("luicode", "10000360"); Oauth2AccessToken readAccessToken = AccessTokenKeeper.readAccessToken(this.mAuthActivity); if (readAccessToken != null && !TextUtils.isEmpty(readAccessToken.getToken())) { weiboParameters.put("trans_token", readAccessToken.getToken()); weiboParameters.put("trans_access_token", readAccessToken.getToken()); } weiboParameters.put("lfid", AssistPushConsts.OPPO_PREFIX + authInfo.getAppKey()); String aid = Utility.getAid(this.mAuthActivity, authInfo.getAppKey()); if (!TextUtils.isEmpty(aid)) { weiboParameters.put("aid", aid); } weiboParameters.put("packagename", authInfo.getPackageName()); weiboParameters.put("key_hash", authInfo.getKeyHash()); String str2 = OAUTH2_BASE_URL + weiboParameters.encodeUrl(); if (!NetworkHelper.hasInternetPermission(this.mAuthActivity)) { UIUtils.showAlert(this.mAuthActivity, "Error", "Application requires permission to access the Internet"); return; } if (this.authListener != null) { WeiboCallbackManager instance = WeiboCallbackManager.getInstance(); String genCallbackKey = instance.genCallbackKey(); instance.setWeiboAuthListener(genCallbackKey, this.authListener); str = genCallbackKey; } else { str = null; } AuthWebViewRequestParam authWebViewRequestParam = new AuthWebViewRequestParam(authInfo, WebRequestType.AUTH, str, "微博登录", str2, this.mAuthActivity); Intent intent = new Intent(this.mAuthActivity, WeiboSdkWebActivity.class); Bundle bundle = new Bundle(); authWebViewRequestParam.fillBundle(bundle); intent.putExtras(bundle); this.mAuthActivity.startActivity(intent); } private void authorize(int i10, WbAuthListener wbAuthListener, AuthType authType) { resetIntentFillData(); if (wbAuthListener != null) { this.authListener = wbAuthListener; if (authType == AuthType.WebOnly) { startWebAuth(); return; } boolean z14 = false; if (authType == AuthType.SsoOnly) { z14 = true; } WbAppInfo wbAppInfo = WeiboAppManager.getInstance(this.mAuthActivity).getWbAppInfo(); if (isWbAppInstalled() && wbAppInfo != null) { startClientAuth(i10); } else if (z14) { this.authListener.onFailure(new WbConnectErrorMessage()); } else { startWebAuth(); } } else { throw new RuntimeException("please set auth listener"); } } public BaseSsoHandler(Context context) { this.mAuthActivity = context; WeiboSsoManager.getInstance().init(context, WbSdk.getAuthInfo().getAppKey()); } }