private async Task <CsrfResponse> AuthenticateStep2(HomeyApiConfig config, string token) { string authUrl = $"https://accounts.athom.com/oauth2/authorise?client_id={config.ClientId}&redirect_uri={Encode(RedirectUrl)}&response_type=code&user_token={token}"; CsrfResponse csrfResponse = new CsrfResponse(); using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(authUrl); EnsureStatusCodeOk("Step 2", response); string responseBody = await response.Content.ReadAsStringAsync(); csrfResponse.Csrf = GetCsrf(responseBody, new string[] { "name=\"_csrf\" value=\"" }, "\">"); IEnumerable <string> cockieContent = response.Headers.GetValues("set-cookie"); foreach (string entry in cockieContent) { foreach (string cookie in entry.Split(';')) { string[] dc = cookie.Split('='); if (dc[0] == "_csrf") { csrfResponse.Cookie = dc[1]; } } } return(csrfResponse); } }
public async Task <string> Login(HomeyApiConfig config, string userName, string password) { AuthResponse responseStep1 = await AuthenticateStep1(userName, password); CsrfResponse csrfResponse = await AuthenticateStep2(config, responseStep1.Token); string code = await AuthenticateStep3(config, responseStep1.Token, csrfResponse); TokenInfo tokenInfo = await AuthenticateStep4(config, code); // Get JWT Token string accessToken = await AuthenticateStep5(config, tokenInfo); // get bearer Token return(await AuthenticateStep6(config, accessToken)); }
private async Task <string> AuthenticateStep3(HomeyApiConfig config, string token, CsrfResponse csrf) { string authorizeUrl = $"https://accounts.athom.com/authorise?client_id={config.ClientId}&redirect_uri={Encode(RedirectUrl)}&response_type=code&user_token={token}"; string contentString = $"resource=resource.homey.{config.CloudId}&_csrf={csrf.Csrf}&allow=Allow"; string cookie = $"_csrf={csrf.Cookie}"; HttpContent content = new StringContent(contentString, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded"); client.DefaultRequestHeaders.Add("Cookie", cookie); HttpResponseMessage response = await client.PostAsync(authorizeUrl, content); EnsureStatusCodeOk("Step 3", response, HttpStatusCode.Found); return(response.Headers.GetValues("Location").First().Split('=')[1]); } }