From 9db212da271d218ca207e5079f6217fa0d0d88fe Mon Sep 17 00:00:00 2001 From: Bailey Belvis Date: Fri, 29 Sep 2023 17:51:29 -0500 Subject: [PATCH] started stubbing things out --- src/Garmin/ApiClient.cs | 12 ++++++ src/Garmin/Auth/GarminOAuthService.cs | 61 +++++++++++++++++++++++++-- src/Garmin/Garmin.csproj | 4 ++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/src/Garmin/ApiClient.cs b/src/Garmin/ApiClient.cs index f65c3af87..7a4d8e5ff 100644 --- a/src/Garmin/ApiClient.cs +++ b/src/Garmin/ApiClient.cs @@ -14,6 +14,7 @@ namespace Garmin public interface IGarminApiClient { Task InitSigninFlowAsync(object queryParams, string userAgent, out CookieJar jar); + Task InitCookieJarAsync(object queryParams, string userAgent, out CookieJar jar) Task SendCredentialsAsync(GarminApiAuthentication auth, object queryParams, object loginData, CookieJar jar); Task SendMfaCodeAsync(string userAgent, object queryParams, object mfaData, CookieJar jar); Task SendServiceTicketAsync(string userAgent, string serviceTicket, CookieJar jar); @@ -25,6 +26,7 @@ public class ApiClient : IGarminApiClient private const string BASE_URL = "https://connect.garmin.com"; private const string SSO_URL = "https://sso.garmin.com"; private const string SIGNIN_URL = "https://sso.garmin.com/sso/signin"; + private const string SSO_EMBED_URL = "https://sso.garmin.com/sso/embed"; private static string UPLOAD_URL = $"{BASE_URL}/modern/proxy/upload-service/upload"; @@ -42,6 +44,16 @@ public Task InitSigninFlowAsync(object queryParams, string userAgent, out Cookie .GetStringAsync(); } + public Task InitCookieJarAsync(object queryParams, string userAgent, out CookieJar jar) + { + return SSO_EMBED_URL + .WithHeader("User-Agent", userAgent) + .WithHeader("origin", ORIGIN) + .SetQueryParams(queryParams) + .WithCookies(out jar) + .GetStringAsync(); + } + public async Task SendCredentialsAsync(GarminApiAuthentication auth, object queryParams, object loginData, CookieJar jar) { var result = new SendCredentialsResult(); diff --git a/src/Garmin/Auth/GarminOAuthService.cs b/src/Garmin/Auth/GarminOAuthService.cs index 133b82da6..bcd76579e 100644 --- a/src/Garmin/Auth/GarminOAuthService.cs +++ b/src/Garmin/Auth/GarminOAuthService.cs @@ -1,6 +1,12 @@ -using System; +using Common.Service; +using Common.Stateful; +using Flurl; +using Flurl.Http; +using System; using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Runtime.Intrinsics.X86; using System.Text; using System.Threading.Tasks; @@ -11,6 +17,55 @@ public interface IGarminOAuthService } -public class GarminOAuthService -{ +public class GarminOAuthService : IGarminOAuthService +{ + private readonly ISettingsService _settingsService; + private readonly IGarminApiClient _apiClient; + + public GarminOAuthService(ISettingsService settingsService, IGarminApiClient apiClient) + { + _settingsService = settingsService; + _apiClient = apiClient; + } + + private async Task GetAuthTokenAsync() + { + var auth = new GarminApiAuthentication(); + auth.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"; + + + ///////////////////////////////// + // Init Cookie Jar + //////////////////////////////// + var queryParams = new + { + id = "gauth-widget", + embedWidget = "true", + gauthHost = "https://sso.garmin.com/sso" + }; + + CookieJar jar = null; + try + { + await _apiClient.InitCookieJarAsync(queryParams, auth.UserAgent, out jar); + } + catch (FlurlHttpException e) + { + throw new GarminAuthenticationError("Failed to initialize sign in flow.", e) { Code = Code.FailedPriorToCredentialsUsed }; + } + + object loginData = new + { + embed = "true", + username = auth.Email, + password = auth.Password, + lt = "e1s1", + _eventId = "submit", + displayNameRequired = "false", + }; + + ///////////////////////////////// + // Get CSRF token + //////////////////////////////// + } } diff --git a/src/Garmin/Garmin.csproj b/src/Garmin/Garmin.csproj index feaf35d9d..b9b69dc4c 100644 --- a/src/Garmin/Garmin.csproj +++ b/src/Garmin/Garmin.csproj @@ -24,6 +24,10 @@ + + + +