/// <summary> /// SSO Token helper /// </summary> /// <param name="clientId"></param> /// <param name="secretKey"></param> /// <param name="grantType"></param> /// <param name="code">The authorization_code or the refresh_token</param> /// <returns></returns> public async Task <SsoToken> GetToken(GrantType grantType, string code) { var body = $"grant_type={grantType.ToEsiValue()}"; if (grantType == GrantType.AuthorizationCode) { body += $"&code={code}"; } else if (grantType == GrantType.RefreshToken) { body += $"&refresh_token={Uri.EscapeDataString(code)}"; } HttpContent postBody = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _clientKey); HttpResponseMessage responseBase = null; if (_config.AuthVersion == AuthVersion.v1) { responseBase = await _client.PostAsync($"{_ssoUrl}/oauth/token", postBody); } else if (_config.AuthVersion == AuthVersion.v2) { responseBase = await _client.PostAsync($"{_ssoUrl}/v2/oauth/token", postBody); } var response = await responseBase.Content.ReadAsStringAsync(); var token = JsonConvert.DeserializeObject <SsoToken>(response); return(token); }
/// <summary> /// Get SSO Token for the v2 Auth flow /// </summary> public async Task <SsoToken> GetTokenV2(GrantType grantType, string code, string codeVerifier = "", List <string> scopes = null) { var body = $"grant_type={grantType.ToEsiValue()}"; body += $"&client_id={_config.ClientId}"; if (grantType == GrantType.AuthorizationCode) { body += $"&code={code}"; var codeVerifierBytes = Encoding.ASCII.GetBytes(codeVerifier); var base64CodeVerifierBytes = Convert.ToBase64String(codeVerifierBytes).TrimEnd('=').Replace('+', '-').Replace('/', '_'); body += $"&code_verifier={base64CodeVerifierBytes}"; } else if (grantType == GrantType.RefreshToken) { body += $"&refresh_token={code}"; if (scopes != null) { body += $"&scope={string.Join(" ", scopes)}"; } } HttpContent postBody = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); _client.DefaultRequestHeaders.Host = "login.eveonline.com"; var response = await _client.PostAsync("https://login.eveonline.com/v2/oauth/token", postBody).Result.Content.ReadAsStringAsync(); var token = JsonConvert.DeserializeObject <SsoToken>(response); return(token); }
/// <summary> /// SSO Token helper /// </summary> /// <param name="grantType"></param> /// <param name="code">The authorization_code or the refresh_token</param> /// <param name="codeChallenge">Provide the same value that was provided for codeChallenge in CreateAuthenticationUrl(). All hashing/encryption will be done automatically. Just provide the code.</param> /// <returns></returns> public async Task <SsoToken> GetToken(GrantType grantType, string code, string codeChallenge = null) { var body = $"grant_type={grantType.ToEsiValue()}"; if (grantType == GrantType.AuthorizationCode) { body += $"&code={code}"; if (codeChallenge != null) { var bytes = Encoding.ASCII.GetBytes(codeChallenge); var base64 = Convert.ToBase64String(bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_'); body += $"&code_verifier={base64}&client_id={_config.ClientId}"; } } else if (grantType == GrantType.RefreshToken) { body += $"&refresh_token={Uri.EscapeDataString(code)}"; if (codeChallenge != null) { body += $"&client_id={_config.ClientId}"; } } HttpContent postBody = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); if (codeChallenge == null) { _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _clientKey); _client.DefaultRequestHeaders.Host = _ssoUrl; } var response = await _client.PostAsync($"https://{_ssoUrl}/v2/oauth/token", postBody); var content = await response.Content.ReadAsStringAsync(); if (response.StatusCode != HttpStatusCode.OK) { string message = "unknown"; if (!string.IsNullOrEmpty(content)) { message = JsonConvert.DeserializeAnonymousType(content, new { error_description = string.Empty }).error_description; } throw new ArgumentException(message); } var token = JsonConvert.DeserializeObject <SsoToken>(content); return(token); }
/// <summary> /// SSO Token helper /// </summary> /// <param name="clientId"></param> /// <param name="secretKey"></param> /// <param name="grantType"></param> /// <param name="code">The authorization_code or the refresh_token</param> /// <returns></returns> public async Task <SsoToken> GetToken(GrantType grantType, string code) { var body = $"grant_type={grantType.ToEsiValue()}"; if (grantType == GrantType.AuthorizationCode) { body += $"&code={code}"; } else if (grantType == GrantType.RefreshToken) { body += $"&refresh_token={code}"; } HttpContent postBody = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", clientKey); var response = await _client.PostAsync("https://login.eveonline.com/oauth/token", postBody).Result.Content.ReadAsStringAsync(); var token = JsonConvert.DeserializeObject <SsoToken>(response); return(token); }