Garmin: Pick fake oauth response from #3800

This commit is contained in:
José Rebelo 2024-08-04 19:20:55 +01:00
parent 8acd2838f6
commit b2c9c5436c
2 changed files with 46 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -20,10 +20,12 @@ public class HttpHandler {
private final AgpsHandler agpsHandler; private final AgpsHandler agpsHandler;
private final ContactsHandler contactsHandler; private final ContactsHandler contactsHandler;
private final FakeOauthHandler fakeOauthHandler;
public HttpHandler(GarminSupport deviceSupport) { public HttpHandler(GarminSupport deviceSupport) {
agpsHandler = new AgpsHandler(deviceSupport); agpsHandler = new AgpsHandler(deviceSupport);
contactsHandler = new ContactsHandler(deviceSupport); contactsHandler = new ContactsHandler(deviceSupport);
fakeOauthHandler = new FakeOauthHandler(deviceSupport);
} }
public GdiHttpService.HttpService handle(final GdiHttpService.HttpService httpService) { public GdiHttpService.HttpService handle(final GdiHttpService.HttpService httpService) {
@ -57,6 +59,12 @@ public class HttpHandler {
} else if (request.getPath().startsWith("/device-gateway/usercontact/")) { } else if (request.getPath().startsWith("/device-gateway/usercontact/")) {
LOG.info("Got contacts request for {}", request.getPath()); LOG.info("Got contacts request for {}", request.getPath());
response = contactsHandler.handleRequest(request); 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 { } else {
LOG.warn("Unhandled path {}", request.getPath()); LOG.warn("Unhandled path {}", request.getPath());
response = null; response = null;