导航菜单

页面标题

页面副标题

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

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

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


package com.eveningoutpost.dexdrip.tidepool;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.eveningoutpost.dexdrip.models.JoH;
import com.eveningoutpost.dexdrip.models.UserError;
import com.eveningoutpost.dexdrip.utilitymodels.Inevitable;
import com.eveningoutpost.dexdrip.utilitymodels.Pref;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference;
import net.openid.appauth.AppAuthConfiguration;
import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException;
import net.openid.appauth.AuthorizationResponse;
import net.openid.appauth.AuthorizationService;
import net.openid.appauth.AuthorizationServiceConfiguration;
import net.openid.appauth.AuthorizationServiceDiscovery;
import net.openid.appauth.TokenResponse;
import okio.Okio;
import org.json.JSONException;
import org.json.JSONObject;

public class AuthFlowIn extends AppCompatActivity {
    private static final String PREF_TIDEPOOL_SUB_NAME = "tidepool_subname";
    private static final String PREF_TIDEPOOL_USER_NAME = "tidepool_username";
    private static final String TAG = "TidePoolAuth";
    final AtomicReference<JSONObject> userInfo = new AtomicReference<>();

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        UserError.Log.d(TAG, "Got response");
        Inevitable.task("tidepool-process-auth", 10L, new Runnable() {
            @Override
            public final void run() {
                AuthFlowIn.this.lambda$onCreate$0();
            }
        });
        finish();
    }

    public void lambda$onCreate$0() {
        processIntent(getIntent());
    }

    private void processIntent(Intent intent) {
        AuthorizationResponse fromIntent = AuthorizationResponse.fromIntent(intent);
        AuthorizationException fromIntent2 = AuthorizationException.fromIntent(intent);
        final AuthState authState = AuthFlowOut.getAuthState();
        if (authState == null) {
            UserError.Log.wtf(TAG, "Could not get auth state");
            return;
        }
        authState.update(fromIntent, fromIntent2);
        if (fromIntent2 != null) {
            UserError.Log.d(TAG, "Got authorization error - resetting state: " + fromIntent2);
            AuthFlowOut.eraseAuthState();
        }
        if (fromIntent != null) {
            AuthFlowOut.saveAuthState();
            final AuthorizationService authService = AuthFlowOut.getAuthService();
            authService.performTokenRequest(fromIntent.createTokenExchangeRequest(), new AuthorizationService.TokenResponseCallback() {
                public final void onTokenRequestCompleted(TokenResponse tokenResponse, AuthorizationException authorizationException) {
                    AuthFlowIn.this.lambda$processIntent$3(authState, authService, tokenResponse, authorizationException);
                }
            });
        } else {
            UserError.Log.e(TAG, "Got response failure " + fromIntent2.toString());
        }
    }

    public void lambda$processIntent$3(final AuthState authState, final AuthorizationService authorizationService, final TokenResponse tokenResponse, AuthorizationException authorizationException) {
        authState.update(tokenResponse, authorizationException);
        if (authorizationException != null) {
            UserError.Log.d(TAG, "Token request exception: " + authorizationException);
            AuthFlowOut.eraseAuthState();
        }
        if (tokenResponse != null) {
            UserError.Log.d(TAG, "Got first token");
            AuthFlowOut.saveAuthState();
            AuthorizationServiceConfiguration authorizationServiceConfiguration = authState.getAuthorizationServiceConfiguration();
            if (authorizationServiceConfiguration == null) {
                UserError.Log.wtf(TAG, "Got null for authorization service configuration");
                return;
            }
            AuthorizationServiceDiscovery authorizationServiceDiscovery = authorizationServiceConfiguration.discoveryDoc;
            if (authorizationServiceDiscovery == null) {
                UserError.Log.wtf(TAG, "Got null for discoveryDoc");
                return;
            }
            final Uri userinfoEndpoint = authorizationServiceDiscovery.getUserinfoEndpoint();
            if (userinfoEndpoint == null) {
                UserError.Log.wtf(TAG, "Got null for userInfoEndpoint");
                return;
            } else {
                Inevitable.task("tidepool-get-userinfo", 100L, new Runnable() {
                    @Override
                    public final void run() {
                        AuthFlowIn.this.lambda$processIntent$2(userinfoEndpoint, tokenResponse, authState, authorizationService);
                    }
                });
                return;
            }
        }
        UserError.Log.e(TAG, "First token err: " + authorizationException);
    }

    public void lambda$processIntent$2(Uri uri, final TokenResponse tokenResponse, AuthState authState, AuthorizationService authorizationService) {
        try {
            HttpURLConnection openConnection = AppAuthConfiguration.DEFAULT.getConnectionBuilder().openConnection(uri);
            openConnection.setRequestProperty("Authorization", "Bearer " + tokenResponse.accessToken);
            openConnection.setInstanceFollowRedirects(false);
            String readString = Okio.buffer(Okio.source(openConnection.getInputStream())).readString(StandardCharsets.UTF_8);
            UserError.Log.d(TAG, "UserInfo: " + readString);
            this.userInfo.set(new JSONObject(readString));
            authState.performActionWithFreshTokens(authorizationService, new AuthState.AuthStateAction() {
                public final void execute(String str, String str2, AuthorizationException authorizationException) {
                    AuthFlowIn.this.lambda$processIntent$1(tokenResponse, str, str2, authorizationException);
                }
            });
        } catch (IOException e) {
            UserError.Log.e(TAG, "Network error when querying userinfo endpoint", e);
        } catch (JSONException unused) {
            UserError.Log.e(TAG, "Failed to parse userinfo response");
        }
    }

    public void lambda$processIntent$1(TokenResponse tokenResponse, String str, String str2, AuthorizationException authorizationException) {
        if (authorizationException != null) {
            UserError.Log.e(TAG, "Got fresh token exception: " + authorizationException);
            return;
        }
        Session session = new Session(tokenResponse.tokenType, TidepoolUploader.getSESSION_TOKEN_HEADER());
        session.authReply = new MAuthReply(str2);
        session.token = str;
        try {
            String string = this.userInfo.get().getString("email");
            if (!JoH.emptyString(string)) {
                UserError.Log.d(TAG, "Setting username to: " + string);
                Pref.setString(PREF_TIDEPOOL_USER_NAME, string);
            } else {
                UserError.Log.wtf(TAG, "Could not get userinfo email");
            }
            session.authReply.userid = this.userInfo.get().getString("sub");
            if (!JoH.emptyString(session.authReply.userid)) {
                Pref.setString(PREF_TIDEPOOL_SUB_NAME, session.authReply.userid);
                TidepoolUploader.startSession(session, true);
            } else {
                UserError.Log.wtf(TAG, "Could not get 'sub' field - cannot proceed");
            }
        } catch (JSONException e) {
            UserError.Log.wtf(TAG, "Getting Access Token 1 Exception: " + e);
        }
    }

    public static void handleTokenLoginAndStartSession() {
        final AuthState authState = AuthFlowOut.getAuthState();
        if (authState != null) {
            authState.performActionWithFreshTokens(AuthFlowOut.getAuthService(), new AuthState.AuthStateAction() {
                public final void execute(String str, String str2, AuthorizationException authorizationException) {
                    AuthFlowIn.lambda$handleTokenLoginAndStartSession$4(authState, str, str2, authorizationException);
                }
            });
        } else {
            UserError.Log.e(TAG, "Failing to get state - trying initial login");
            retryInitialLogin();
        }
    }

    public static void lambda$handleTokenLoginAndStartSession$4(AuthState authState, String str, String str2, AuthorizationException authorizationException) {
        if (authorizationException != null) {
            UserError.Log.e(TAG, "Got exception token: " + authorizationException);
        }
        if (str != null) {
            TokenResponse lastTokenResponse = authState.getLastTokenResponse();
            if (lastTokenResponse != null) {
                Session session = new Session(lastTokenResponse.tokenType, TidepoolUploader.getSESSION_TOKEN_HEADER());
                MAuthReply mAuthReply = new MAuthReply(str2);
                session.authReply = mAuthReply;
                session.token = str;
                mAuthReply.userid = Pref.getStringDefaultBlank(PREF_TIDEPOOL_SUB_NAME);
                TidepoolUploader.startSession(session, false);
                AuthFlowOut.saveAuthState();
                return;
            }
            UserError.Log.e(TAG, "Failing to get response / token type - trying initial login again");
            retryInitialLogin();
            return;
        }
        UserError.Log.e(TAG, "Failing to use access token - trying initial login again");
        retryInitialLogin();
    }

    private static void retryInitialLogin() {
        if (JoH.pratelimit("tidepool-retry-login", 600)) {
            AuthFlowOut.doTidePoolInitialLogin();
        }
    }
}