2016-05-06 07:17:38 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Models.Api;
|
|
|
|
|
using Newtonsoft.Json;
|
2016-07-02 01:54:00 +03:00
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2016-08-07 02:33:04 +03:00
|
|
|
|
using System.Net;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Repositories
|
|
|
|
|
{
|
|
|
|
|
public class AuthApiRepository : BaseApiRepository, IAuthApiRepository
|
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
public AuthApiRepository(IConnectivity connectivity)
|
|
|
|
|
: base(connectivity)
|
|
|
|
|
{ }
|
|
|
|
|
|
2016-05-06 07:17:38 +03:00
|
|
|
|
protected override string ApiRoute => "auth";
|
|
|
|
|
|
|
|
|
|
public virtual async Task<ApiResult<TokenResponse>> PostTokenAsync(TokenRequest requestObj)
|
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
if(!Connectivity.IsConnected)
|
2016-05-06 07:17:38 +03:00
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
return HandledNotConnected<TokenResponse>();
|
|
|
|
|
}
|
2016-05-06 07:17:38 +03:00
|
|
|
|
|
2016-07-02 01:54:00 +03:00
|
|
|
|
using(var client = new ApiHttpClient())
|
2016-05-06 07:17:38 +03:00
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
var requestMessage = new TokenHttpRequestMessage(requestObj)
|
|
|
|
|
{
|
|
|
|
|
Method = HttpMethod.Post,
|
|
|
|
|
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/token")),
|
|
|
|
|
};
|
2016-05-06 07:17:38 +03:00
|
|
|
|
|
2016-08-07 02:33:04 +03:00
|
|
|
|
try
|
2016-07-02 01:54:00 +03:00
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
if(!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
return await HandleErrorAsync<TokenResponse>(response).ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
}
|
2016-07-02 01:54:00 +03:00
|
|
|
|
|
2016-08-17 02:20:41 +03:00
|
|
|
|
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
|
|
|
|
|
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
catch(WebException)
|
|
|
|
|
{
|
|
|
|
|
return HandledWebException<TokenResponse>();
|
|
|
|
|
}
|
2016-07-02 01:54:00 +03:00
|
|
|
|
}
|
2016-05-06 07:17:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual async Task<ApiResult<TokenResponse>> PostTokenTwoFactorAsync(TokenTwoFactorRequest requestObj)
|
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
if(!Connectivity.IsConnected)
|
2016-05-06 07:17:38 +03:00
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
return HandledNotConnected<TokenResponse>();
|
|
|
|
|
}
|
2016-05-06 07:17:38 +03:00
|
|
|
|
|
2016-07-02 01:54:00 +03:00
|
|
|
|
using(var client = new ApiHttpClient())
|
2016-05-06 07:17:38 +03:00
|
|
|
|
{
|
2016-07-02 01:54:00 +03:00
|
|
|
|
var requestMessage = new TokenHttpRequestMessage(requestObj)
|
|
|
|
|
{
|
|
|
|
|
Method = HttpMethod.Post,
|
|
|
|
|
RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/token/two-factor")),
|
|
|
|
|
};
|
2016-05-06 07:17:38 +03:00
|
|
|
|
|
2016-08-07 02:33:04 +03:00
|
|
|
|
try
|
2016-07-02 01:54:00 +03:00
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
if(!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
return await HandleErrorAsync<TokenResponse>(response).ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
}
|
2016-07-02 01:54:00 +03:00
|
|
|
|
|
2016-08-17 02:20:41 +03:00
|
|
|
|
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
2016-08-07 02:33:04 +03:00
|
|
|
|
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
|
|
|
|
|
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
catch(WebException)
|
|
|
|
|
{
|
|
|
|
|
return HandledWebException<TokenResponse>();
|
|
|
|
|
}
|
2016-07-02 01:54:00 +03:00
|
|
|
|
}
|
2016-05-06 07:17:38 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|