2016-05-06 06:17:38 +02: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 00:54:00 +02:00
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2017-02-06 15:39:07 +01:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System.Collections.Generic;
|
2017-06-27 22:18:32 +02:00
|
|
|
|
using Bit.App.Enums;
|
2017-12-18 19:58:36 +01:00
|
|
|
|
using Bit.App.Utilities;
|
2016-05-06 06:17:38 +02:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Repositories
|
|
|
|
|
{
|
2017-02-04 07:12:25 +01:00
|
|
|
|
public class ConnectApiRepository : BaseApiRepository, IConnectApiRepository
|
2016-05-06 06:17:38 +02:00
|
|
|
|
{
|
2017-02-04 07:12:25 +01:00
|
|
|
|
public ConnectApiRepository(
|
2016-12-24 16:54:18 +01:00
|
|
|
|
IConnectivity connectivity,
|
2017-02-05 05:31:37 +01:00
|
|
|
|
IHttpService httpService,
|
|
|
|
|
ITokenService tokenService)
|
|
|
|
|
: base(connectivity, httpService, tokenService)
|
2016-07-02 00:54:00 +02:00
|
|
|
|
{ }
|
|
|
|
|
|
2017-10-04 05:56:10 +02:00
|
|
|
|
protected override string ApiRoute => "/connect";
|
2016-05-06 06:17:38 +02:00
|
|
|
|
|
|
|
|
|
public virtual async Task<ApiResult<TokenResponse>> PostTokenAsync(TokenRequest requestObj)
|
|
|
|
|
{
|
2016-07-02 00:54:00 +02:00
|
|
|
|
if(!Connectivity.IsConnected)
|
2016-05-06 06:17:38 +02:00
|
|
|
|
{
|
2016-07-02 00:54:00 +02:00
|
|
|
|
return HandledNotConnected<TokenResponse>();
|
|
|
|
|
}
|
2016-05-06 06:17:38 +02:00
|
|
|
|
|
2017-05-07 02:20:57 +02:00
|
|
|
|
using(var client = HttpService.IdentityClient)
|
2016-05-06 06:17:38 +02:00
|
|
|
|
{
|
2017-02-04 07:12:25 +01:00
|
|
|
|
var requestMessage = new HttpRequestMessage
|
2016-07-02 00:54:00 +02:00
|
|
|
|
{
|
|
|
|
|
Method = HttpMethod.Post,
|
2017-08-11 20:24:32 +02:00
|
|
|
|
RequestUri = new Uri(string.Concat(client.BaseAddress, ApiRoute, "/token")),
|
2017-02-04 07:12:25 +01:00
|
|
|
|
Content = new FormUrlEncodedContent(requestObj.ToIdentityTokenRequest())
|
2016-07-02 00:54:00 +02:00
|
|
|
|
};
|
2016-05-06 06:17:38 +02:00
|
|
|
|
|
2017-12-18 19:58:36 +01:00
|
|
|
|
requestMessage.Headers.Add("Device-Type", ((int)Helpers.OnPlatform(iOS: DeviceType.iOS,
|
|
|
|
|
Android: DeviceType.Android, Windows: DeviceType.UWP)).ToString());
|
|
|
|
|
|
2016-08-07 01:33:04 +02:00
|
|
|
|
try
|
2016-07-02 00:54:00 +02:00
|
|
|
|
{
|
2016-08-17 01:20:41 +02:00
|
|
|
|
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
|
2017-02-06 15:39:07 +01:00
|
|
|
|
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
|
|
|
|
|
2016-08-07 01:33:04 +02:00
|
|
|
|
if(!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
2017-02-06 15:39:07 +01:00
|
|
|
|
var errorResponse = JObject.Parse(responseContent);
|
2017-06-27 22:18:32 +02:00
|
|
|
|
if(errorResponse["TwoFactorProviders2"] != null)
|
2017-02-06 15:39:07 +01:00
|
|
|
|
{
|
2017-06-27 22:45:12 +02:00
|
|
|
|
TokenService.SetTwoFactorToken(requestObj.Email, null);
|
2017-06-27 22:35:29 +02:00
|
|
|
|
|
2017-02-06 15:39:07 +01:00
|
|
|
|
return ApiResult<TokenResponse>.Success(new TokenResponse
|
|
|
|
|
{
|
2017-06-27 22:18:32 +02:00
|
|
|
|
TwoFactorProviders2 =
|
|
|
|
|
errorResponse["TwoFactorProviders2"]
|
|
|
|
|
.ToObject<Dictionary<TwoFactorProviderType, Dictionary<string, object>>>()
|
2017-02-06 15:39:07 +01:00
|
|
|
|
}, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-17 01:20:41 +02:00
|
|
|
|
return await HandleErrorAsync<TokenResponse>(response).ConfigureAwait(false);
|
2016-08-07 01:33:04 +02:00
|
|
|
|
}
|
2016-07-02 00:54:00 +02:00
|
|
|
|
|
2016-08-07 01:33:04 +02:00
|
|
|
|
var responseObj = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
|
|
|
|
|
return ApiResult<TokenResponse>.Success(responseObj, response.StatusCode);
|
|
|
|
|
}
|
2017-02-06 15:55:35 +01:00
|
|
|
|
catch
|
2016-08-07 01:33:04 +02:00
|
|
|
|
{
|
|
|
|
|
return HandledWebException<TokenResponse>();
|
|
|
|
|
}
|
2016-07-02 00:54:00 +02:00
|
|
|
|
}
|
2016-05-06 06:17:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|