Skip to content

Commit

Permalink
started stubbing things out
Browse files Browse the repository at this point in the history
  • Loading branch information
philosowaffle committed Sep 29, 2023
1 parent 67d20b9 commit 9db212d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/Garmin/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 17 in src/Garmin/ApiClient.cs

View workflow job for this annotation

GitHub Actions / build-and-test (6.0, ubuntu-latest)

; expected

Check failure on line 17 in src/Garmin/ApiClient.cs

View workflow job for this annotation

GitHub Actions / build-and-test (6.0, ubuntu-latest)

; expected
Task<SendCredentialsResult> SendCredentialsAsync(GarminApiAuthentication auth, object queryParams, object loginData, CookieJar jar);
Task<string> SendMfaCodeAsync(string userAgent, object queryParams, object mfaData, CookieJar jar);
Task<IFlurlResponse> SendServiceTicketAsync(string userAgent, string serviceTicket, CookieJar jar);
Expand All @@ -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";

Expand All @@ -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<SendCredentialsResult> SendCredentialsAsync(GarminApiAuthentication auth, object queryParams, object loginData, CookieJar jar)
{
var result = new SendCredentialsResult();
Expand Down
61 changes: 58 additions & 3 deletions src/Garmin/Auth/GarminOAuthService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
////////////////////////////////
}
}
4 changes: 4 additions & 0 deletions src/Garmin/Garmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<None Remove="Fit\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="IdentityModel" Version="6.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>
Expand Down

0 comments on commit 9db212d

Please sign in to comment.