导航菜单

页面标题

页面副标题

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

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

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


package com.eveningoutpost.dexdrip.tidepool;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Base64;
import android.util.Log;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.utilitymodels.Pref;
import com.eveningoutpost.dexdrip.utils.CipherUtils;
import com.eveningoutpost.dexdrip.xdrip;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import net.openid.appauth.AppAuthConfiguration;
import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException;
import net.openid.appauth.AuthorizationRequest;
import net.openid.appauth.AuthorizationService;
import net.openid.appauth.AuthorizationServiceConfiguration;
import net.openid.appauth.browser.BrowserAllowList;
import net.openid.appauth.browser.BrowserMatcher;
import net.openid.appauth.browser.VersionedBrowserMatcher;

@SuppressLint({"StaticFieldLeak"})
public class AuthFlowOut {
    private static final String INTEGRATION_BASE_URL = "https://auth.integration.tidepool.org/realms/integration";
    private static final String MY_CLIENT_ID = "xdrip";
    private static final Uri MY_REDIRECT_URI = Uri.parse("xdrip://callback/tidepool");
    private static final String PREF_TIDEPOOL_SERVICE_CONFIGURATON = "tidepool-service-configuration";
    private static final String PREF_TIDEPOOL_STATE_STORE = "tidepool-last-response";
    private static final String PRODUCTION_BASE_URL = "https://auth.tidepool.org/realms/tidepool";
    private static final String TAG = "TidePoolAuth";
    private static volatile AuthorizationService authService;
    private static volatile AuthState authState;

    public static AuthState getAuthState() {
        if (authState == null) {
            try {
                authState = new AuthState(AuthorizationServiceConfiguration.fromJson(Pref.getStringDefaultBlank(PREF_TIDEPOOL_SERVICE_CONFIGURATON)));
                authState = AuthState.jsonDeserialize(Pref.getStringDefaultBlank(PREF_TIDEPOOL_STATE_STORE));
                Log.d(TAG, "Auth state loaded with: " + authState.getAccessToken());
            } catch (Exception e) {
                Log.d(TAG, "Error during getAuthState - could just be empty cache data: " + e);
            }
        }
        return authState;
    }

    public static void saveAuthState() {
        AuthState authState2 = authState;
        if (authState2 != null) {
            Pref.setString(PREF_TIDEPOOL_STATE_STORE, authState2.jsonSerializeString());
        }
    }

    public static void eraseAuthState() {
        Pref.setString(PREF_TIDEPOOL_STATE_STORE, "");
    }

    public static AuthorizationService getAuthService() {
        if (authService == null) {
            authService = new AuthorizationService(xdrip.getAppContext(), new AppAuthConfiguration.Builder().setBrowserMatcher(new BrowserAllowList(new BrowserMatcher[]{VersionedBrowserMatcher.CHROME_CUSTOM_TAB, VersionedBrowserMatcher.FIREFOX_CUSTOM_TAB, VersionedBrowserMatcher.SAMSUNG_CUSTOM_TAB})).build());
        }
        return authService;
    }

    private static synchronized void resetAll() {
        synchronized (AuthFlowOut.class) {
            authState = null;
            authService = null;
            getAuthService();
            getAuthState();
        }
    }

    public static synchronized void clearAllSavedData() {
        synchronized (AuthFlowOut.class) {
            Pref.setString(PREF_TIDEPOOL_SERVICE_CONFIGURATON, "");
            eraseAuthState();
            resetAll();
        }
    }

    public static void doTidePoolInitialLogin() {
        doTidePoolInitialLogin(false);
    }

    public static void doTidePoolInitialLogin(final boolean z) {
        final Context appContext = xdrip.getAppContext();
        JoH.static_toast_long("Connecting to Tidepool");
        AuthorizationServiceConfiguration.fetchFromIssuer(Uri.parse(Pref.getBooleanDefaultFalse("tidepool_dev_servers") ? INTEGRATION_BASE_URL : PRODUCTION_BASE_URL), new AuthorizationServiceConfiguration.RetrieveConfigurationCallback() {
            public final void onFetchConfigurationCompleted(AuthorizationServiceConfiguration authorizationServiceConfiguration, AuthorizationException authorizationException) {
                AuthFlowOut.lambda$doTidePoolInitialLogin$0(z, appContext, authorizationServiceConfiguration, authorizationException);
            }
        });
    }

    public static void lambda$doTidePoolInitialLogin$0(boolean z, Context context, AuthorizationServiceConfiguration authorizationServiceConfiguration, AuthorizationException authorizationException) {
        if (authorizationException != null || authorizationServiceConfiguration == null) {
            Log.e(TAG, "failed to fetch configuration");
            return;
        }
        Pref.setString(PREF_TIDEPOOL_SERVICE_CONFIGURATON, authorizationServiceConfiguration.toJsonString());
        resetAll();
        String encodeToString = Base64.encodeToString(CipherUtils.getRandomKey(64), 11);
        try {
            String encodeToString2 = Base64.encodeToString(MessageDigest.getInstance("SHA-256").digest(encodeToString.getBytes(StandardCharsets.UTF_8)), 11);
            AuthorizationRequest.Builder builder = new AuthorizationRequest.Builder(authorizationServiceConfiguration, "xdrip", "code", MY_REDIRECT_URI);
            builder.setScopes(new String[]{"openid", "offline_access"}).setLoginHint(Pref.getString("tidepool_username", "")).setCodeVerifier(encodeToString, encodeToString2, "S256");
            if (z) {
                builder.setPrompt("login");
            }
            AuthorizationRequest build = builder.build();
            Log.d(TAG, "Firing off request");
            getAuthService().performAuthorizationRequest(build, PendingIntent.getActivity(context, 0, new Intent(context, (Class<?>) AuthFlowIn.class), 0), PendingIntent.getActivity(context, 0, new Intent(context, (Class<?>) AuthFlowIn.class), 0));
        } catch (NoSuchAlgorithmException e) {
            Log.wtf(TAG, "Failed to get message digest: " + e);
        }
    }
}