diff --git a/src/Android/Services/HttpService.cs b/src/Android/Services/HttpService.cs index a9c132ec6..e9f08eeed 100644 --- a/src/Android/Services/HttpService.cs +++ b/src/Android/Services/HttpService.cs @@ -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()); } } diff --git a/src/App/Abstractions/Services/IHttpService.cs b/src/App/Abstractions/Services/IHttpService.cs index 85c38f6f9..731a4d89c 100644 --- a/src/App/Abstractions/Services/IHttpService.cs +++ b/src/App/Abstractions/Services/IHttpService.cs @@ -2,6 +2,7 @@ { public interface IHttpService { - ApiHttpClient Client { get; } + ApiHttpClient ApiClient { get; } + IdentityHttpClient IdentityClient { get; } } } diff --git a/src/App/Abstractions/Services/ITokenService.cs b/src/App/Abstractions/Services/ITokenService.cs index 7e6086175..c7819dc32 100644 --- a/src/App/Abstractions/Services/ITokenService.cs +++ b/src/App/Abstractions/Services/ITokenService.cs @@ -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; } diff --git a/src/App/App.csproj b/src/App/App.csproj index e907e9386..e23aa4863 100644 --- a/src/App/App.csproj +++ b/src/App/App.csproj @@ -229,6 +229,7 @@ + diff --git a/src/App/Repositories/AccountsApiRepository.cs b/src/App/Repositories/AccountsApiRepository.cs index 886db137a..414d1e17b 100644 --- a/src/App/Repositories/AccountsApiRepository.cs +++ b/src/App/Repositories/AccountsApiRepository.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() { diff --git a/src/App/Repositories/ApiRepository.cs b/src/App/Repositories/ApiRepository.cs index 627d66461..70d1663d4 100644 --- a/src/App/Repositories/ApiRepository.cs +++ b/src/App/Repositories/ApiRepository.cs @@ -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() { diff --git a/src/App/Repositories/BaseApiRepository.cs b/src/App/Repositories/BaseApiRepository.cs index f6acdbdfb..6c7b5f5a1 100644 --- a/src/App/Repositories/BaseApiRepository.cs +++ b/src/App/Repositories/BaseApiRepository.cs @@ -55,7 +55,7 @@ namespace Bit.App.Repositories var deviceInfoService = Resolver.Resolve(); var appIdService = Resolver.Resolve(); - 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 { diff --git a/src/App/Repositories/CipherApiRepository.cs b/src/App/Repositories/CipherApiRepository.cs index f3dd7ee5b..cc8f6c48c 100644 --- a/src/App/Repositories/CipherApiRepository.cs +++ b/src/App/Repositories/CipherApiRepository.cs @@ -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() { diff --git a/src/App/Repositories/ConnectApiRepository.cs b/src/App/Repositories/ConnectApiRepository.cs index cbb18f3e3..74811c1d6 100644 --- a/src/App/Repositories/ConnectApiRepository.cs +++ b/src/App/Repositories/ConnectApiRepository.cs @@ -29,7 +29,7 @@ namespace Bit.App.Repositories return HandledNotConnected(); } - using(var client = HttpService.Client) + using(var client = HttpService.IdentityClient) { var requestMessage = new HttpRequestMessage { diff --git a/src/App/Repositories/DeviceApiRepository.cs b/src/App/Repositories/DeviceApiRepository.cs index 29f5a8840..bfe268948 100644 --- a/src/App/Repositories/DeviceApiRepository.cs +++ b/src/App/Repositories/DeviceApiRepository.cs @@ -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 { diff --git a/src/App/Repositories/SettingsApiRepository.cs b/src/App/Repositories/SettingsApiRepository.cs index 94d50f3e3..e4c634d36 100644 --- a/src/App/Repositories/SettingsApiRepository.cs +++ b/src/App/Repositories/SettingsApiRepository.cs @@ -32,7 +32,7 @@ namespace Bit.App.Repositories return tokenStateResponse; } - using(var client = HttpService.Client) + using(var client = HttpService.ApiClient) { var requestMessage = new TokenHttpRequestMessage() { diff --git a/src/App/Services/TokenService.cs b/src/App/Services/TokenService.cs index f55d90962..526a9fac1 100644 --- a/src/App/Services/TokenService.cs +++ b/src/App/Services/TokenService.cs @@ -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(); + } + } + public bool TokenExpired => DateTime.UtcNow < TokenExpiration; public TimeSpan TokenTimeRemaining => TokenExpiration - DateTime.UtcNow; public bool TokenNeedsRefresh => TokenTimeRemaining.TotalMinutes < 5; diff --git a/src/App/Utilities/IdentityHttpClient.cs b/src/App/Utilities/IdentityHttpClient.cs new file mode 100644 index 000000000..0df734514 --- /dev/null +++ b/src/App/Utilities/IdentityHttpClient.cs @@ -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")); + } + } +} diff --git a/src/App/Utilities/TokenHttpRequestMessage.cs b/src/App/Utilities/TokenHttpRequestMessage.cs index 787eb73ac..340e7ca4f 100644 --- a/src/App/Utilities/TokenHttpRequestMessage.cs +++ b/src/App/Utilities/TokenHttpRequestMessage.cs @@ -12,9 +12,23 @@ namespace Bit.App { var tokenService = Resolver.Resolve(); var appIdService = Resolver.Resolve(); + if(!string.IsNullOrWhiteSpace(tokenService.Token)) { - Headers.Add("Authorization", $"Bearer2 {tokenService.Token}"); + var httpService = Resolver.Resolve(); + + 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)) { diff --git a/src/iOS.Core/Services/HttpService.cs b/src/iOS.Core/Services/HttpService.cs index 8d7f5217e..e1c2c5128 100644 --- a/src/iOS.Core/Services/HttpService.cs +++ b/src/iOS.Core/Services/HttpService.cs @@ -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(); } }