Skip to content

Commit add317b

Browse files
committed
Auth: Use Zwift OpenID Connect endpoint and switch to bearer tokens.
1 parent c2d2370 commit add317b

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

ZwiftPowerService.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ public class ZwiftPowerService
1616
private readonly HttpClient _httpClient;
1717
private readonly JsonSerializerOptions _options;
1818
private readonly IConfiguration _config;
19-
19+
20+
private string _accessToken;
21+
//private string _refreshToken;
22+
//private int _refreshExpiresIn;
23+
2024
public HttpClient Client { get => _httpClient; }
2125

2226
public ZwiftPowerService(HttpClient httpClient, IConfiguration configuration)
@@ -111,13 +115,27 @@ public async Task Login()
111115
{
112116
{ "username", _config["Zwiftpower:Username"] },
113117
{ "password", _config["Zwiftpower:Password"] },
114-
{ "redirect", "./index.php" },
115-
{ "login", "Login" }
118+
{ "client_id", "Zwift Game Client" },
119+
{ "grant_type", "password" }
116120
});
117121

118-
using var indexPage = await _httpClient.PostAsync("/ucp.php?mode=login", content);
122+
var httpClient = new HttpClient();
123+
124+
using var response = await httpClient.PostAsync("https://secure.zwift.com/auth/realms/zwift/protocol/openid-connect/token", content);
125+
126+
var loginResponse = await response.Content.ReadFromJsonAsync<LoginResponse>();
127+
128+
_accessToken = loginResponse.access_token;
129+
130+
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", this._accessToken);
131+
132+
await _httpClient.GetAsync("/ucp.php?mode=login&login=external&oauth_service=oauthzpsso");
119133
}
120134

135+
public string BearerToken() { return _accessToken; }
136+
137+
public record LoginResponse(string access_token, string refresh_token, int expires_in, int refresh_expires_in, string session_state);
138+
121139
internal class Data<T>
122140
{
123141
#pragma warning disable 0649 // used by JSON deserialization

0 commit comments

Comments
 (0)