diff --git a/src/App/Abstractions/Repositories/IDeviceApiRepository.cs b/src/App/Abstractions/Repositories/IDeviceApiRepository.cs index 8f0874d5a..aaffe6a7f 100644 --- a/src/App/Abstractions/Repositories/IDeviceApiRepository.cs +++ b/src/App/Abstractions/Repositories/IDeviceApiRepository.cs @@ -9,5 +9,6 @@ namespace Bit.App.Abstractions public interface IDeviceApiRepository : IApiRepository { Task> PutTokenAsync(string identifier, DeviceTokenRequest request); + Task> PutClearTokenAsync(string identifier); } } \ No newline at end of file diff --git a/src/App/Repositories/DeviceApiRepository.cs b/src/App/Repositories/DeviceApiRepository.cs index 0d4811547..a9c1731b6 100644 --- a/src/App/Repositories/DeviceApiRepository.cs +++ b/src/App/Repositories/DeviceApiRepository.cs @@ -42,5 +42,32 @@ namespace Bit.App.Repositories return ApiResult.Success(responseObj, response.StatusCode); } } + + public virtual async Task> PutClearTokenAsync(string identifier) + { + if(!Connectivity.IsConnected) + { + return HandledNotConnected(); + } + + using(var client = new ApiHttpClient()) + { + var requestMessage = new TokenHttpRequestMessage + { + Method = HttpMethod.Put, + RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/identifier/", identifier, "/clear-token")), + }; + + var response = await client.SendAsync(requestMessage); + if(!response.IsSuccessStatusCode) + { + return await HandleErrorAsync(response); + } + + var responseContent = await response.Content.ReadAsStringAsync(); + var responseObj = JsonConvert.DeserializeObject(responseContent); + return ApiResult.Success(responseObj, response.StatusCode); + } + } } } diff --git a/src/App/Services/PushNotificationListener.cs b/src/App/Services/PushNotificationListener.cs index cec8a984b..2fb967ba8 100644 --- a/src/App/Services/PushNotificationListener.cs +++ b/src/App/Services/PushNotificationListener.cs @@ -95,10 +95,11 @@ namespace Bit.App.Services } } - public void OnUnregistered(DeviceType deviceType) + public async void OnUnregistered(DeviceType deviceType) { Debug.WriteLine("Push Notification - Device Unnregistered"); _settings.Remove(Constants.PushLastRegistrationDate); + await _deviceApiRepository.PutClearTokenAsync(_appIdService.AppId); } public void OnError(string message, DeviceType deviceType) diff --git a/src/App/Utilities/TokenHttpRequestMessage.cs b/src/App/Utilities/TokenHttpRequestMessage.cs index c322df397..f85d4c6b5 100644 --- a/src/App/Utilities/TokenHttpRequestMessage.cs +++ b/src/App/Utilities/TokenHttpRequestMessage.cs @@ -12,7 +12,10 @@ namespace Bit.App { var authService = Resolver.Resolve(); var appIdService = Resolver.Resolve(); - Headers.Add("Authorization", $"Bearer {authService.Token}"); + if(authService.IsAuthenticated) + { + Headers.Add("Authorization", $"Bearer {authService.Token}"); + } if(!string.IsNullOrWhiteSpace(appIdService.AppId)) { Headers.Add("DeviceIdentifier", appIdService.AppId);