public void Test_GetRefreshToken(string RefreshToken) { //Arrange Uri FhirGatewayEndpoint = new Uri("https://apinams.ehealthvendortest.health.gov.au"); var RefreshTokenRequest = new RefreshTokenRequest() { Client_id = "28198d27-c475-4695-83d3-1f1f8256e01b", Client_secret = "1c73a74b-45fc-4ee4-a400-15a398e46143", RefreshToken = RefreshToken, }; FhirApi Api = new FhirApi(FhirGatewayEndpoint); //Act TokenResponse TokenResponse = Api.GetRefreshToken(RefreshTokenRequest); //Assert Assert.Equal(TokenResponse.StatusCode, System.Net.HttpStatusCode.OK); Assert.NotNull(TokenResponse.RefreshToken); Assert.NotNull(TokenResponse.AccessToken); Assert.NotNull(TokenResponse.AccessExpires); Assert.NotNull(TokenResponse.RefreshExpires); Assert.NotNull(TokenResponse.Scope); Assert.NotNull(TokenResponse.TokenType); Assert.Null(TokenResponse.ErrorResponse); }
public bool GetRefeashToken(ViewModel.UserAccountView ViewCurrentUserAccount) { //If the Access token expires in 1 hour get a new one. if (ViewCurrentUserAccount.AccessExpires < DateTime.Now.AddHours(1)) { if (!string.IsNullOrWhiteSpace(ViewCurrentUserAccount.RefreshToken)) { FhirApi FhirApi = new FhirApi(FhirGatewayEndpoint); var RefreshTokenRequest = new RefreshTokenRequest(); RefreshTokenRequest.Client_id = ApplicationStore.App_id; RefreshTokenRequest.Client_secret = ApplicationStore.App_secret; RefreshTokenRequest.RefreshToken = ViewCurrentUserAccount.RefreshToken; TokenResponse TokenResponse = FhirApi.GetRefreshToken(RefreshTokenRequest); if (TokenResponse.StatusCode == System.Net.HttpStatusCode.OK) { ViewCurrentUserAccount.AccessExpires = TokenResponse.AccessExpires; ViewCurrentUserAccount.AccessToken = TokenResponse.AccessToken; ViewCurrentUserAccount.RefreshExpires = TokenResponse.RefreshExpires; ViewCurrentUserAccount.RefreshToken = TokenResponse.RefreshToken; ViewCurrentUserAccount.Scope = TokenResponse.Scope; if (ApplicationStore.UserList.SingleOrDefault(x => x.Username == ViewCurrentUserAccount.Username) != null) { var TargetAccount = ApplicationStore.UserList.SingleOrDefault(x => x.Username == ViewCurrentUserAccount.Username); TargetAccount.AccessExpires = ViewCurrentUserAccount.AccessExpires; TargetAccount.AccessToken = ViewCurrentUserAccount.AccessToken; TargetAccount.RefreshExpires = ViewCurrentUserAccount.RefreshExpires; TargetAccount.RefreshToken = ViewCurrentUserAccount.RefreshToken; TargetAccount.Scope = ViewCurrentUserAccount.Scope; SaveApplicationStore(); } return(true); } else { if (TokenResponse.ErrorResponse != null) { this.ErrorMessage = $"Refresh Token request failed with message: Http Status: {TokenResponse.StatusCode.ToString()}, Error: {TokenResponse.ErrorResponse.Error}, ErrorMessage: {TokenResponse.ErrorResponse.Description}"; } else { this.ErrorMessage = $"Refresh Token request failed with no error message returned message, http status was: {TokenResponse.StatusCode.ToString()}"; } return(false); } } else { throw new FormatException($"No CurrectUserAccount.AuthorisationCode found for access token request."); } } return(true); }