Skip to content

Commit

Permalink
[524] migrate garmin authentication to oauth (#531)
Browse files Browse the repository at this point in the history
* [524] migrate garmin authentication to oauth

* started stubbing things out

* stubbing out more of the flow

* good progress

* rough draft working :)

* happy path is working for Console app

* mfa not working :(

* fix compile error

* polish

* update release notes
  • Loading branch information
philosowaffle authored Oct 1, 2023
1 parent daec177 commit 1203744
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 355 deletions.
4 changes: 3 additions & 1 deletion src/Common/Service/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public void SetGarminAuthentication(GarminApiAuthentication authentication)
lock (_lock)
{
var key = $"{GarminApiAuthKey}:{authentication.Email}";
_cache.Set(key, authentication, new MemoryCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(45) });
var expiration = authentication.OAuth2Token?.Expires_In - (60 * 60) ?? 0; // expire an hour early
var finalExpiration = expiration <= 0 ? 45 * 60 : expiration; // default to 45min
_cache.Set(key, authentication, new MemoryCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(finalExpiration) });
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/Common/Stateful/GarminApiAuthentication.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Flurl.Http;


namespace Common.Stateful;

public class GarminApiAuthentication : IApiAuthentication
Expand All @@ -8,18 +8,26 @@ public class GarminApiAuthentication : IApiAuthentication
public string Password { get; set; }
public AuthStage AuthStage { get; set; }
public CookieJar CookieJar { get; set; }
public string UserAgent { get; set; } = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/50.0";
public string UserAgent { get; set; } = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148";
public string MFACsrfToken { get; set; }
public OAuth1Token OAuth1Token { get; set; }
public OAuth2Token OAuth2Token { get; set; }

public bool IsValid(Settings settings)
{
return Email == settings.Garmin.Email
&& Password == settings.Garmin.Password
&& CookieJar is object
&& AuthStage == AuthStage.Completed;
&& AuthStage == AuthStage.Completed
&& !string.IsNullOrWhiteSpace(OAuth2Token?.Access_Token);
}
}

public class OAuth1Token
{
public string Token { get; set; }
public string TokenSecret { get; set; }
}

public enum AuthStage : byte
{
None = 0,
Expand Down
12 changes: 12 additions & 0 deletions src/Common/Stateful/OAuth2Token.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Common.Stateful;

public record OAuth2Token
{
public string Scope { get; set; }
public string Jti { get; set; }
public string Access_Token { get; set; }
public string Token_Type { get; set; }
public string Refresh_Token { get; set; }
public int Expires_In { get; set; }
public int Refresh_Token_Expires_In { get; set; }
}
Loading

0 comments on commit 1203744

Please sign in to comment.