internal static async Task <EveToken> GetWebToken(IScope scope, string code, string clientID, string clientSecret) { EveCredentials credentials = await RetriveWebCredentials(code, clientID, clientSecret); JwtToken jwtToken = await ValidateCredentials(credentials); return(new EveToken(credentials, jwtToken, scope)); }
/// <summary> /// Wait for auth response after user har logged in. /// </summary> /// <param name="scope"></param> /// <param name="callback"></param> /// <param name="state"></param> /// <param name="codeVerifier"></param> /// <param name="clientID"></param> /// <returns></returns> internal static async Task <EveToken> ValidateResponse(IScope scope, string callback, string state, string codeVerifier, string clientID) { var response = await GetAuthResponse(callback); if (state != response.state) { throw new Exception("Response state not matching sent state."); } EveCredentials credential = await RetriveClientCredentials(response.code, codeVerifier, clientID); JwtToken token = await ValidateCredentials(credential); return(new EveToken(credential, token, scope)); }
/// <summary> /// Load EveToken from json created by ToJson method. /// </summary> /// <param name="json"></param> /// <returns></returns> internal static async Task <EveToken> FromJson(string json, HttpClient httpClient = default) { if (httpClient != default && Client == null) { Client = httpClient; } List <string> content = JsonConvert.DeserializeObject <List <string> >(json); Scope scope = content[0]; string refreshToken = content[1]; string clientID = content[2]; EveCredentials credentials = await RefreshToken(scope, refreshToken, clientID); JwtToken token = await ValidateCredentials(credentials); return(new EveToken(credentials, token, scope)); }
/// <summary> /// Validate that the token received has not been tampered with and get additional character information. /// </summary> /// <param name="credential"></param> /// <returns></returns> static async Task <JwtToken> ValidateCredentials(EveCredentials credential) { JwtToken token; using (HttpResponseMessage response = await Client.GetAsync("https://login.eveonline.com/oauth/jwks")) { string json = await response.Content.ReadAsStringAsync(); Dictionary <string, JToken> keys = JsonConvert.DeserializeObject <Dictionary <string, JToken> >(json); var headers = JWT.Headers(credential.AccessToken); var jwk = keys["keys"]; RSACryptoServiceProvider key = new RSACryptoServiceProvider(); key.ImportParameters(new RSAParameters { Modulus = Base64Url.Decode(jwk[1]["n"].ToString()), Exponent = Base64Url.Decode(jwk[1]["e"].ToString()) }); token = JWT.Decode <JwtToken>(credential.AccessToken, key); if (token.Issuer != "login.eveonline.com") { throw new Exception("Invalid JWT Token"); } int unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; if (token.Expiery < unixTimestamp) { throw new Exception("Invalid JWT Token"); } } return(token); }