public async Task SendAsync_ShouldRaise_SessionExpired_WhenRefreshFailed() { // Arrange _tokenClient.RefreshTokenAsync(Arg.Any <CancellationToken>()) .Returns(ApiResponseResult <RefreshTokenResponse> .Fail(HttpStatusCode.BadRequest, "Refresh failed")); var handler = new UnauthorizedResponseHandler(_tokenClient, _tokenStorage, _userStorage) { InnerHandler = _innerHandler }; var client = new HttpClient(handler) { BaseAddress = _baseAddress }; _innerHandler.Expect(HttpMethod.Get, "https://api.protonvpn.ch/logicals") .Respond(HttpStatusCode.Unauthorized); using (var monitoredSubject = handler.Monitor()) { // Act var request = new HttpRequestMessage(HttpMethod.Get, "/logicals"); await client.SendAsync(request); // Assert monitoredSubject.Should().Raise(nameof(UnauthorizedResponseHandler.SessionExpired)); _innerHandler.VerifyNoOutstandingExpectation(); } }
private async Task <ApiResponseResult <CertificateResponseData> > MockOfRequestAuthCertificateAsync(CertificateRequestData arg) { if (arg.ClientPublicKey.IsNullOrEmpty() || _usedClientPublicKeys.Contains(arg.ClientPublicKey)) { return(ApiResponseResult <CertificateResponseData> .Fail(CreateClientPublicKeyConflictCertificateResponseData(), HttpStatusCode.BadRequest, string.Empty)); } _usedClientPublicKeys.Add(arg.ClientPublicKey); _certificateResponseData ??= CreateCertificateResponseData(); return(ApiResponseResult <CertificateResponseData> .Ok(_certificateResponseData)); }
public async Task <ApiResponseResult <VpnInfoResponse> > RefreshVpnInfo() { ApiResponseResult <VpnInfoResponse> vpnInfo = await _apiClient.GetVpnInfoResponse(); if (vpnInfo.Success) { if (!vpnInfo.Value.Vpn.Status.Equals(UserStatusVpnAccess)) { return(ApiResponseResult <VpnInfoResponse> .Fail(vpnInfo.StatusCode, "User has no vpn access.")); } _userStorage.StoreVpnInfo(vpnInfo.Value); } return(vpnInfo); }
public async Task <ApiResponseResult <BaseResponse> > GetValidateResult() { if (PlanInfoExists()) { return(ApiResponseResult <BaseResponse> .Ok(new BaseResponse())); } var vpnInfoResult = await _userAuth.RefreshVpnInfo(); if (vpnInfoResult.Success) { _userStorage.StoreVpnInfo(vpnInfoResult.Value); return(ApiResponseResult <BaseResponse> .Ok(vpnInfoResult.Value)); } return(ApiResponseResult <BaseResponse> .Fail(vpnInfoResult.StatusCode, vpnInfoResult.Error)); }
public async Task <ApiResponseResult <AuthResponse> > AuthAsync(string username, string password) { ApiResponseResult <AuthInfo> authInfo = await _apiClient.GetAuthInfoResponse(new AuthInfoRequestData { Username = username }); if (!authInfo.Success) { return(ApiResponseResult <AuthResponse> .Fail(authInfo.StatusCode, authInfo.Error)); } SrpPInvoke.GoProofs proofs = SrpPInvoke.GenerateProofs(4, username, password, authInfo.Value.Salt, authInfo.Value.Modulus, authInfo.Value.ServerEphemeral); AuthRequestData authData = new AuthRequestData { ClientEphemeral = Convert.ToBase64String(proofs.ClientEphemeral), ClientProof = Convert.ToBase64String(proofs.ClientProof), SrpSession = authInfo.Value.SrpSession, TwoFactorCode = "", Username = username }; ApiResponseResult <AuthResponse> response = await _apiClient.GetAuthResponse(authData); if (response.Success) { if (!Convert.ToBase64String(proofs.ExpectedServerProof).Equals(response.Value.ServerProof)) { return(ApiResponseResult <AuthResponse> .Fail(0, "Invalid server proof.")); } _userStorage.SaveUsername(username); _tokenStorage.Uid = response.Value.Uid; _tokenStorage.AccessToken = response.Value.AccessToken; _tokenStorage.RefreshToken = response.Value.RefreshToken; } return(response); }
public async Task <ApiResponseResult <AuthResponse> > LoginUserAsync(string username, string password) { _logger?.Info("Trying to login user"); UserLoggingIn?.Invoke(this, EventArgs.Empty); ApiResponseResult <AuthResponse> authResult = await AuthAsync(username, password); if (authResult.Failure) { return(authResult); } ApiResponseResult <VpnInfoResponse> vpnInfo = await RefreshVpnInfo(); if (vpnInfo.Success) { _userStorage.StoreVpnInfo(vpnInfo.Value); InvokeUserLoggedIn(false); return(authResult); } return(ApiResponseResult <AuthResponse> .Fail(vpnInfo.StatusCode, vpnInfo.Error)); }