1
0
mirror of https://github.com/bitwarden/mobile.git synced 2025-01-22 21:11:27 +01:00
bitwarden-mobile/src/App/Repositories/ConnectApiRepository.cs
2017-12-18 13:58:36 -05:00

79 lines
3.0 KiB
C#

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models.Api;
using Newtonsoft.Json;
using Plugin.Connectivity.Abstractions;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Bit.App.Enums;
using Bit.App.Utilities;
namespace Bit.App.Repositories
{
public class ConnectApiRepository : BaseApiRepository, IConnectApiRepository
{
public ConnectApiRepository(
IConnectivity connectivity,
IHttpService httpService,
ITokenService tokenService)
: base(connectivity, httpService, tokenService)
{ }
protected override string ApiRoute => "/connect";
public virtual async Task<ApiResult<TokenResponse>> PostTokenAsync(TokenRequest requestObj)
{
if(!Connectivity.IsConnected)
{
return HandledNotConnected<TokenResponse>();
}
using(var client = HttpService.IdentityClient)
{
var requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(string.Concat(client.BaseAddress, ApiRoute, "/token")),
Content = new FormUrlEncodedContent(requestObj.ToIdentityTokenRequest())
};
requestMessage.Headers.Add("Device-Type", ((int)Helpers.OnPlatform(iOS: DeviceType.iOS,
Android: DeviceType.Android, Windows: DeviceType.UWP)).ToString());
try
{
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
var errorResponse = JObject.Parse(responseContent);
if(errorResponse["TwoFactorProviders2"] != null)
{
TokenService.SetTwoFactorToken(requestObj.Email, null);
return ApiResult<TokenResponse>.Success(new TokenResponse
{
TwoFactorProviders2 =
errorResponse["TwoFactorProviders2"]
.ToObject<Dictionary<TwoFactorProviderType, Dictionary<string, object>>>()
}, response.StatusCode);
}
return await HandleErrorAsync<TokenResponse>(response).ConfigureAwait(false);
}
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
}
catch
{
return HandledWebException<TokenResponse>();
}
}
}
}
}