public CharacterRolesModel GetCharacterRoles() { if (CheckScope("esi-characters.read_corporation_roles.v1") == false) { return(null); } if (characterRolesResponse != null) { if (EveHelper.GetCacheTimer(characterRolesResponse) > DateTime.UtcNow) { return(characterRoles); } } RestClient Client = new RestClient("https://esi.tech.ccp.is"); RestRequest request = new RestRequest(CharacterRolesModel.EndpointVersioned, Method.GET); request.AddUrlSegment("character_id", this.CharacterId); characterRolesResponse = Client.Execute <CharacterRolesModel>(request); characterRoles = characterRolesResponse.Data; OnTokenUpdated?.Invoke(); return(characterRoles); }
/// <summary> /// Request a new bearer token and store it on the local Account. /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns>Returns success state</returns> public static async Task <bool> RequestToken(string username, string password) { var authenticator = new OAuth2PasswordCredentialsAuthenticator(TokenEndpoint); authenticator.SetCredentials(username, password); try { var account = await authenticator.SignInAsync(); if (account == null) { return(false); } account.Properties.Add("password", password); account.Properties.Add("date", DateTime.Now.ToString()); await AccountStore.SaveAsync(account, App.Settings.ServiceId); AuthAccount = account; OnTokenUpdated?.Invoke(null, (string)account.Properties["access_token"]); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Update access token if it is expired /// </summary> /// <param name="cancellationToken">Cancellation token</param> /// <returns>Access token</returns> protected async Task UpdateTokenAsync(CancellationToken cancellationToken = default(CancellationToken)) { if (_token.ExpireDateTime > DateTime.UtcNow.AddMinutes(-1)) { return; } var nToken = await _oAuth2Provider.GenerateAccessTokenAsync(_token.RefreshToken, cancellationToken).ConfigureAwait(false); _token = new Token(nToken.AccessToken, nToken.TokenType, nToken.ExpiresIn, _token.RefreshToken, nToken.ExpireDateTime); OnTokenUpdated?.Invoke(this, _token); }
/// <summary> /// Fully refreshes the token's AccessToken and VerificationInfo fields. /// </summary> /// <returns></returns> public async Task Refresh() { if (this.RefreshToken == null) { return; } DateTime now = DateTime.Now; DateTime exp = DateTime.Parse(Expiry); if (now < exp) { return; // Token is not expired, no need to refresh it. } tokenAccessInfo = await AuthClient.RequestAccessToken(tokenAccessInfo.RefreshToken); tokenVerification = await AuthClient.RequestTokenVerificationOld(tokenAccessInfo.AccessToken); OnTokenUpdated?.Invoke(); }
public CharacterPublicInformation GetCharacterInformation() { // If exists AND cache has not expired, return existing entry. if (characterInformationResponse != null) { if (EveHelper.GetCacheTimer(characterInformationResponse) > DateTime.UtcNow) { return(characterInformation); } } RestClient Client = new RestClient("https://esi.tech.ccp.is"); RestRequest request = new RestRequest(CharacterPublicInformation.EndpointVersioned, Method.GET); request.AddUrlSegment("character_id", this.CharacterId); characterInformationResponse = Client.Execute <CharacterPublicInformation>(request); characterInformation = characterInformationResponse.Data; OnTokenUpdated?.Invoke(); return(characterInformation); }