From b2c9c5436c7fd7f1aa817c02636d1dcfc7c05325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Sun, 4 Aug 2024 19:20:55 +0100 Subject: [PATCH] Garmin: Pick fake oauth response from #3800 --- .../devices/garmin/http/FakeOauthHandler.java | 38 +++++++++++++++++++ .../devices/garmin/http/HttpHandler.java | 8 ++++ 2 files changed, 46 insertions(+) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/http/FakeOauthHandler.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/http/FakeOauthHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/http/FakeOauthHandler.java new file mode 100644 index 000000000..d9eff41ab --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/http/FakeOauthHandler.java @@ -0,0 +1,38 @@ +package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.http; + +import androidx.annotation.NonNull; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.charset.StandardCharsets; + +import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst; +import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.GarminSupport; + +public class FakeOauthHandler { + private static final Logger LOG = LoggerFactory.getLogger(FakeOauthHandler.class); + private final GarminSupport deviceSupport; + + public FakeOauthHandler(GarminSupport deviceSupport) { + this.deviceSupport = deviceSupport; + } + + private GarminHttpResponse createGarminHttpResponse(String logMessage, String body) { + LOG.info(logMessage); + final GarminHttpResponse response = new GarminHttpResponse(); + response.setStatus(200); + response.setBody(body.getBytes(StandardCharsets.UTF_8)); + return response; + } + + public GarminHttpResponse handleInitialOAuthRequest(GarminHttpRequest request) { + final String fakeResponse = "{ \"accessToken\": \"t1\", \"tokenType\": \"Bearer\", \"refreshToken\": \"r1\", \"expiresIn\": 7776000, \"scope\": \"GCS_EPHEMERIS_SONY_READ GCS_CIQ_APPSTORE_MOBILE_READ GCS_EMERGENCY_ASSISTANCE_CREATE GCS_GEOLOCATION_ELEVATION_READ GCS_IMAGE_READ GCS_LIVETRACK_FIT_CREATE GCS_LIVETRACK_FIT_READ GCS_LIVETRACK_FIT_UPDATE OMT_GOLF_SUBSCRIPTION_READ\", \"refreshTokenExpiresIn\": \"31536000\", \"customerId\": \"c1\" }"; + return createGarminHttpResponse("Sending fake initial oauth response", fakeResponse); + } + + public GarminHttpResponse handleOAuthRequest(GarminHttpRequest request) { + final String fakeOauth = "{\"access_token\":\"t\",\"token_type\":\"Bearer\",\"expires_in\":7776000,\"scope\":\"GCS_EPHEMERIS_SONY_READ GCS_CIQ_APPSTORE_MOBILE_READ GCS_EMERGENCY_ASSISTANCE_CREATE GCS_GEOLOCATION_ELEVATION_READ GCS_IMAGE_READ GCS_LIVETRACK_FIT_CREATE GCS_LIVETRACK_FIT_READ GCS_LIVETRACK_FIT_UPDATE OMT_GOLF_SUBSCRIPTION_READ\",\"refresh_token\":\"r\",\"refresh_token_expires_in\":\"31536000\",\"customerId\":\"c\"}"; + return createGarminHttpResponse("Sending fake oauth", fakeOauth); + } +} \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/http/HttpHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/http/HttpHandler.java index 04a808136..7acde1fec 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/http/HttpHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/http/HttpHandler.java @@ -20,10 +20,12 @@ public class HttpHandler { private final AgpsHandler agpsHandler; private final ContactsHandler contactsHandler; + private final FakeOauthHandler fakeOauthHandler; public HttpHandler(GarminSupport deviceSupport) { agpsHandler = new AgpsHandler(deviceSupport); contactsHandler = new ContactsHandler(deviceSupport); + fakeOauthHandler = new FakeOauthHandler(deviceSupport); } public GdiHttpService.HttpService handle(final GdiHttpService.HttpService httpService) { @@ -57,6 +59,12 @@ public class HttpHandler { } else if (request.getPath().startsWith("/device-gateway/usercontact/")) { LOG.info("Got contacts request for {}", request.getPath()); response = contactsHandler.handleRequest(request); + } else if (request.getPath().equalsIgnoreCase("/api/oauth/token")){ + LOG.info("Got oauth request for {}", request.getPath()); + response = fakeOauthHandler.handleOAuthRequest(request); + } else if (request.getPath().equalsIgnoreCase("/oauthTokenExchangeService/connectToIT")){ + LOG.info("Got initial oauth request for {}", request.getPath()); + response = fakeOauthHandler.handleInitialOAuthRequest(request); } else { LOG.warn("Unhandled path {}", request.getPath()); response = null;