导航菜单

页面标题

页面副标题

xDrip+ v04633772025.07.16 - AuthorizationService.java 源代码

正在查看: xDrip+ v04633772025.07.16 应用的 AuthorizationService.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package net.openid.appauth;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.TextUtils;
import androidx.browser.customtabs.CustomTabsIntent;
import com.newrelic.agent.android.util.Constants;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.util.Map;
import net.openid.appauth.AuthorizationException;
import net.openid.appauth.IdToken;
import net.openid.appauth.TokenResponse;
import net.openid.appauth.browser.BrowserDescriptor;
import net.openid.appauth.browser.BrowserSelector;
import net.openid.appauth.browser.CustomTabManager;
import net.openid.appauth.connectivity.ConnectionBuilder;
import net.openid.appauth.internal.Logger;
import net.openid.appauth.internal.UriUtil;
import org.json.JSONException;
import org.json.JSONObject;

public class AuthorizationService {
    private final BrowserDescriptor mBrowser;
    private final AppAuthConfiguration mClientConfiguration;
    Context mContext;
    private final CustomTabManager mCustomTabManager;
    private boolean mDisposed;

    public interface TokenResponseCallback {
        void onTokenRequestCompleted(TokenResponse response, AuthorizationException ex);
    }

    public AuthorizationService(Context context, AppAuthConfiguration clientConfiguration) {
        this(context, clientConfiguration, BrowserSelector.select(context, clientConfiguration.getBrowserMatcher()), new CustomTabManager(context));
    }

    AuthorizationService(Context context, AppAuthConfiguration clientConfiguration, BrowserDescriptor browser, CustomTabManager customTabManager) {
        this.mDisposed = false;
        this.mContext = (Context) Preconditions.checkNotNull(context);
        this.mClientConfiguration = clientConfiguration;
        this.mCustomTabManager = customTabManager;
        this.mBrowser = browser;
        if (browser == null || !browser.useCustomTab.booleanValue()) {
            return;
        }
        customTabManager.bind(browser.packageName);
    }

    public CustomTabsIntent.Builder createCustomTabsIntentBuilder(Uri... possibleUris) {
        checkNotDisposed();
        return this.mCustomTabManager.createTabBuilder(possibleUris);
    }

    public void performAuthorizationRequest(AuthorizationRequest request, PendingIntent completedIntent, PendingIntent canceledIntent) {
        performAuthorizationRequest(request, completedIntent, canceledIntent, createCustomTabsIntentBuilder(new Uri[0]).build());
    }

    public void performAuthorizationRequest(AuthorizationRequest request, PendingIntent completedIntent, PendingIntent canceledIntent, CustomTabsIntent customTabsIntent) {
        performAuthManagementRequest(request, completedIntent, canceledIntent, customTabsIntent);
    }

    private void performAuthManagementRequest(AuthorizationManagementRequest request, PendingIntent completedIntent, PendingIntent canceledIntent, CustomTabsIntent customTabsIntent) {
        checkNotDisposed();
        Preconditions.checkNotNull(request);
        Preconditions.checkNotNull(completedIntent);
        Preconditions.checkNotNull(customTabsIntent);
        Intent createStartIntent = AuthorizationManagementActivity.createStartIntent(this.mContext, request, prepareAuthorizationRequestIntent(request, customTabsIntent), completedIntent, canceledIntent);
        if (!isActivity(this.mContext)) {
            createStartIntent.addFlags(268435456);
        }
        this.mContext.startActivity(createStartIntent);
    }

    private boolean isActivity(Context context) {
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return true;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return false;
    }

    public void performTokenRequest(TokenRequest request, TokenResponseCallback callback) {
        performTokenRequest(request, NoClientAuthentication.INSTANCE, callback);
    }

    public void performTokenRequest(TokenRequest request, ClientAuthentication clientAuthentication, TokenResponseCallback callback) {
        checkNotDisposed();
        Logger.debug("Initiating code exchange request to %s", request.configuration.tokenEndpoint);
        new TokenRequestTask(request, clientAuthentication, this.mClientConfiguration.getConnectionBuilder(), SystemClock.INSTANCE, callback, Boolean.valueOf(this.mClientConfiguration.getSkipIssuerHttpsCheck())).execute(new Void[0]);
    }

    private void checkNotDisposed() {
        if (this.mDisposed) {
            throw new IllegalStateException("Service has been disposed and rendered inoperable");
        }
    }

    private Intent prepareAuthorizationRequestIntent(AuthorizationManagementRequest request, CustomTabsIntent customTabsIntent) {
        Intent intent;
        checkNotDisposed();
        if (this.mBrowser == null) {
            throw new ActivityNotFoundException();
        }
        Uri uri = request.toUri();
        if (this.mBrowser.useCustomTab.booleanValue()) {
            intent = customTabsIntent.intent;
        } else {
            intent = new Intent("android.intent.action.VIEW");
        }
        intent.setPackage(this.mBrowser.packageName);
        intent.setData(uri);
        Logger.debug("Using %s as browser for auth, custom tab = %s", intent.getPackage(), this.mBrowser.useCustomTab.toString());
        return intent;
    }

    private static class TokenRequestTask extends AsyncTask<Void, Void, JSONObject> {
        private TokenResponseCallback mCallback;
        private ClientAuthentication mClientAuthentication;
        private Clock mClock;
        private final ConnectionBuilder mConnectionBuilder;
        private AuthorizationException mException;
        private TokenRequest mRequest;
        private boolean mSkipIssuerHttpsCheck;

        TokenRequestTask(TokenRequest request, ClientAuthentication clientAuthentication, ConnectionBuilder connectionBuilder, Clock clock, TokenResponseCallback callback, Boolean skipIssuerHttpsCheck) {
            this.mRequest = request;
            this.mClientAuthentication = clientAuthentication;
            this.mConnectionBuilder = connectionBuilder;
            this.mClock = clock;
            this.mCallback = callback;
            this.mSkipIssuerHttpsCheck = skipIssuerHttpsCheck.booleanValue();
        }

        @Override
        public JSONObject doInBackground(Void... voids) {
            InputStream inputStream;
            InputStream errorStream;
            InputStream inputStream2 = null;
            try {
                try {
                    HttpURLConnection openConnection = this.mConnectionBuilder.openConnection(this.mRequest.configuration.tokenEndpoint);
                    openConnection.setRequestMethod("POST");
                    openConnection.setRequestProperty(Constants.Network.CONTENT_TYPE_HEADER, Constants.Network.ContentType.URL_ENCODED);
                    addJsonToAcceptHeader(openConnection);
                    openConnection.setDoOutput(true);
                    Map<String, String> requestHeaders = this.mClientAuthentication.getRequestHeaders(this.mRequest.clientId);
                    if (requestHeaders != null) {
                        for (Map.Entry<String, String> entry : requestHeaders.entrySet()) {
                            openConnection.setRequestProperty(entry.getKey(), entry.getValue());
                        }
                    }
                    Map<String, String> requestParameters = this.mRequest.getRequestParameters();
                    Map<String, String> requestParameters2 = this.mClientAuthentication.getRequestParameters(this.mRequest.clientId);
                    if (requestParameters2 != null) {
                        requestParameters.putAll(requestParameters2);
                    }
                    String formUrlEncode = UriUtil.formUrlEncode(requestParameters);
                    openConnection.setRequestProperty(Constants.Network.CONTENT_LENGTH_HEADER, String.valueOf(formUrlEncode.length()));
                    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openConnection.getOutputStream());
                    outputStreamWriter.write(formUrlEncode);
                    outputStreamWriter.flush();
                    if (openConnection.getResponseCode() >= 200 && openConnection.getResponseCode() < 300) {
                        errorStream = openConnection.getInputStream();
                    } else {
                        errorStream = openConnection.getErrorStream();
                    }
                } catch (Throwable th) {
                    th = th;
                    inputStream2 = inputStream;
                }
            } catch (IOException e) {
                e = e;
                inputStream = null;
            } catch (JSONException e2) {
                e = e2;
                inputStream = null;
            } catch (Throwable th2) {
                th = th2;
            }
            try {
                JSONObject jSONObject = new JSONObject(Utils.readInputStream(errorStream));
                Utils.closeQuietly(errorStream);
                return jSONObject;
            } catch (IOException e3) {
                inputStream = errorStream;
                e = e3;
                Logger.debugWithStack(e, "Failed to complete exchange request", new Object[0]);
                this.mException = AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.NETWORK_ERROR, e);
                Utils.closeQuietly(inputStream);
                return null;
            } catch (JSONException e4) {
                inputStream = errorStream;
                e = e4;
                Logger.debugWithStack(e, "Failed to complete exchange request", new Object[0]);
                this.mException = AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR, e);
                Utils.closeQuietly(inputStream);
                return null;
            } catch (Throwable th3) {
                th = th3;
                inputStream2 = errorStream;
                Utils.closeQuietly(inputStream2);
                throw th;
            }
        }

        @Override
        public void onPostExecute(JSONObject json) {
            AuthorizationException fromTemplate;
            AuthorizationException authorizationException = this.mException;
            if (authorizationException != null) {
                this.mCallback.onTokenRequestCompleted(null, authorizationException);
                return;
            }
            if (json.has("error")) {
                try {
                    String string = json.getString("error");
                    fromTemplate = AuthorizationException.fromOAuthTemplate(AuthorizationException.TokenRequestErrors.byString(string), string, json.optString("error_description", null), UriUtil.parseUriIfAvailable(json.optString("error_uri")));
                } catch (JSONException e) {
                    fromTemplate = AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR, e);
                }
                this.mCallback.onTokenRequestCompleted(null, fromTemplate);
                return;
            }
            try {
                TokenResponse build = new TokenResponse.Builder(this.mRequest).fromResponseJson(json).build();
                String str = build.idToken;
                if (str != null) {
                    try {
                        try {
                            IdToken.from(str).validate(this.mRequest, this.mClock, this.mSkipIssuerHttpsCheck);
                        } catch (AuthorizationException e2) {
                            this.mCallback.onTokenRequestCompleted(null, e2);
                            return;
                        }
                    } catch (IdToken.IdTokenException | JSONException e3) {
                        this.mCallback.onTokenRequestCompleted(null, AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.ID_TOKEN_PARSING_ERROR, e3));
                        return;
                    }
                }
                Logger.debug("Token exchange with %s completed", this.mRequest.configuration.tokenEndpoint);
                this.mCallback.onTokenRequestCompleted(build, null);
            } catch (JSONException e4) {
                this.mCallback.onTokenRequestCompleted(null, AuthorizationException.fromTemplate(AuthorizationException.GeneralErrors.JSON_DESERIALIZATION_ERROR, e4));
            }
        }

        private void addJsonToAcceptHeader(URLConnection conn) {
            if (TextUtils.isEmpty(conn.getRequestProperty("Accept"))) {
                conn.setRequestProperty("Accept", Constants.Network.ContentType.JSON);
            }
        }
    }
}