Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[524] migrate garmin authentication to oauth #531

Merged
merged 10 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading