mirror of
https://github.com/bitwarden/android.git
synced 2024-12-18 15:21:53 +03:00
identity server client for auth
This commit is contained in:
parent
7117f00480
commit
d1cf6c68f3
15 changed files with 84 additions and 23 deletions
|
@ -7,6 +7,7 @@ namespace Bit.Android.Services
|
|||
{
|
||||
public class HttpService : IHttpService
|
||||
{
|
||||
public ApiHttpClient Client => new ApiHttpClient(new AndroidClientHandler());
|
||||
public ApiHttpClient ApiClient => new ApiHttpClient(new AndroidClientHandler());
|
||||
public IdentityHttpClient IdentityClient => new IdentityHttpClient(new AndroidClientHandler());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
{
|
||||
public interface IHttpService
|
||||
{
|
||||
ApiHttpClient Client { get; }
|
||||
ApiHttpClient ApiClient { get; }
|
||||
IdentityHttpClient IdentityClient { get; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ namespace Bit.App.Abstractions
|
|||
[Obsolete("Old auth scheme")]
|
||||
string AuthBearer { get; set; }
|
||||
DateTime TokenExpiration { get; }
|
||||
string TokenIssuer { get; }
|
||||
bool TokenExpired { get; }
|
||||
TimeSpan TokenTimeRemaining { get; }
|
||||
bool TokenNeedsRefresh { get; }
|
||||
|
|
|
@ -229,6 +229,7 @@
|
|||
<Compile Include="Pages\Vault\VaultEditLoginPage.cs" />
|
||||
<Compile Include="Pages\Vault\VaultListLoginsPage.cs" />
|
||||
<Compile Include="Services\PasswordGenerationService.cs" />
|
||||
<Compile Include="Utilities\IdentityHttpClient.cs" />
|
||||
<Compile Include="Utilities\Extentions.cs" />
|
||||
<Compile Include="Utilities\ExtendedObservableCollection.cs" />
|
||||
<Compile Include="Utilities\ApiHttpClient.cs" />
|
||||
|
|
|
@ -28,7 +28,7 @@ namespace Bit.App.Repositories
|
|||
return HandledNotConnected();
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage(requestObj)
|
||||
{
|
||||
|
@ -60,7 +60,7 @@ namespace Bit.App.Repositories
|
|||
return HandledNotConnected();
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage(requestObj)
|
||||
{
|
||||
|
@ -98,7 +98,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage()
|
||||
{
|
||||
|
@ -147,7 +147,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage()
|
||||
{
|
||||
|
@ -187,7 +187,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage()
|
||||
{
|
||||
|
|
|
@ -35,7 +35,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage()
|
||||
{
|
||||
|
@ -75,7 +75,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage()
|
||||
{
|
||||
|
@ -115,7 +115,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage(requestObj)
|
||||
{
|
||||
|
@ -155,7 +155,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage(requestObj)
|
||||
{
|
||||
|
@ -195,7 +195,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage()
|
||||
{
|
||||
|
|
|
@ -55,7 +55,7 @@ namespace Bit.App.Repositories
|
|||
var deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
|
||||
var appIdService = Resolver.Resolve<IAppIdService>();
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.IdentityClient)
|
||||
{
|
||||
var requestMessage = new HttpRequestMessage
|
||||
{
|
||||
|
@ -97,7 +97,7 @@ namespace Bit.App.Repositories
|
|||
}
|
||||
else if(TokenService.TokenNeedsRefresh && !string.IsNullOrWhiteSpace(TokenService.RefreshToken))
|
||||
{
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.IdentityClient)
|
||||
{
|
||||
var requestMessage = new HttpRequestMessage
|
||||
{
|
||||
|
|
|
@ -32,7 +32,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage()
|
||||
{
|
||||
|
@ -72,7 +72,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage()
|
||||
{
|
||||
|
|
|
@ -29,7 +29,7 @@ namespace Bit.App.Repositories
|
|||
return HandledNotConnected<TokenResponse>();
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.IdentityClient)
|
||||
{
|
||||
var requestMessage = new HttpRequestMessage
|
||||
{
|
||||
|
|
|
@ -33,7 +33,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage(request)
|
||||
{
|
||||
|
@ -65,7 +65,7 @@ namespace Bit.App.Repositories
|
|||
return HandledNotConnected();
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage
|
||||
{
|
||||
|
|
|
@ -32,7 +32,7 @@ namespace Bit.App.Repositories
|
|||
return tokenStateResponse;
|
||||
}
|
||||
|
||||
using(var client = HttpService.Client)
|
||||
using(var client = HttpService.ApiClient)
|
||||
{
|
||||
var requestMessage = new TokenHttpRequestMessage()
|
||||
{
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using Bit.App.Abstractions;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Bit.App.Services
|
||||
|
@ -77,6 +76,20 @@ namespace Bit.App.Services
|
|||
}
|
||||
}
|
||||
|
||||
public string TokenIssuer
|
||||
{
|
||||
get
|
||||
{
|
||||
var decoded = DecodeToken();
|
||||
if(decoded?["iss"] == null)
|
||||
{
|
||||
throw new InvalidOperationException("No issuer in token.");
|
||||
}
|
||||
|
||||
return decoded?["iss"].Value<string>();
|
||||
}
|
||||
}
|
||||
|
||||
public bool TokenExpired => DateTime.UtcNow < TokenExpiration;
|
||||
public TimeSpan TokenTimeRemaining => TokenExpiration - DateTime.UtcNow;
|
||||
public bool TokenNeedsRefresh => TokenTimeRemaining.TotalMinutes < 5;
|
||||
|
|
29
src/App/Utilities/IdentityHttpClient.cs
Normal file
29
src/App/Utilities/IdentityHttpClient.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
using System.Net.Http;
|
||||
using System;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace Bit.App
|
||||
{
|
||||
public class IdentityHttpClient : HttpClient
|
||||
{
|
||||
public IdentityHttpClient()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
public IdentityHttpClient(HttpMessageHandler handler)
|
||||
: base(handler)
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
//BaseAddress = new Uri("http://169.254.80.80:33656"); // Desktop from VS Android Emulator
|
||||
//BaseAddress = new Uri("http://192.168.1.8:33656"); // Desktop
|
||||
//BaseAddress = new Uri("https://identity-api.bitwarden.com"); // Preview
|
||||
BaseAddress = new Uri("https://api.bitwarden.com"); // Production
|
||||
DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -12,9 +12,23 @@ namespace Bit.App
|
|||
{
|
||||
var tokenService = Resolver.Resolve<ITokenService>();
|
||||
var appIdService = Resolver.Resolve<IAppIdService>();
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(tokenService.Token))
|
||||
{
|
||||
Headers.Add("Authorization", $"Bearer2 {tokenService.Token}");
|
||||
var httpService = Resolver.Resolve<IHttpService>();
|
||||
|
||||
var bearerString = "Bearer";
|
||||
var tokenIssuer = tokenService.TokenIssuer;
|
||||
if(tokenIssuer == httpService.ApiClient.BaseAddress.OriginalString)
|
||||
{
|
||||
bearerString = string.Concat(bearerString, "2");
|
||||
}
|
||||
else if(tokenIssuer == httpService.IdentityClient.BaseAddress.OriginalString)
|
||||
{
|
||||
bearerString = string.Concat(bearerString, "3");
|
||||
}
|
||||
|
||||
Headers.Add("Authorization", $"{bearerString} {tokenService.Token}");
|
||||
}
|
||||
if(!string.IsNullOrWhiteSpace(appIdService.AppId))
|
||||
{
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Bit.iOS.Core.Services
|
|||
{
|
||||
public class HttpService : IHttpService
|
||||
{
|
||||
public ApiHttpClient Client => new ApiHttpClient();
|
||||
public ApiHttpClient ApiClient => new ApiHttpClient();
|
||||
public IdentityHttpClient IdentityClient => new IdentityHttpClient();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue