mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[SG-419] Fix problems with push notifications on self-host (#2338)
* Added "internal" to non-user-based request types to avoid failing validation. * Added handling of unsuccessful response so that JSON parsing eror doesn't occur. * Added logging for token errors. (cherry picked from commit dad143b3e42247bc6b397b60803e25d243bd83a5) * Fixed bug in next auth attempt handling. * Fixed linting. * Added deserialization options to handle case insensitivity. * Added a new method for SendAsync that does not expect a result from the client. * hasJsonResult param to make Send more reusable * some cleanup * fix lint problems * Added launch config for Notifications. * Added Notifications to Full Server config. Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com>
This commit is contained in:
@ -72,12 +72,13 @@ public class SelfHostedSyncSponsorshipsCommand : BaseIdentityClientService, ISel
|
||||
|
||||
foreach (var orgSponsorshipsBatch in organizationSponsorshipsDict.Values.Chunk(1000))
|
||||
{
|
||||
var response = await SendAsync<OrganizationSponsorshipSyncRequestModel, OrganizationSponsorshipSyncResponseModel>(HttpMethod.Post, "organization/sponsorship/sync", new OrganizationSponsorshipSyncRequestModel
|
||||
{
|
||||
BillingSyncKey = billingSyncConfig.BillingSyncKey,
|
||||
SponsoringOrganizationCloudId = cloudOrganizationId,
|
||||
SponsorshipsBatch = orgSponsorshipsBatch.Select(s => new OrganizationSponsorshipRequestModel(s))
|
||||
});
|
||||
var response = await SendAsync<OrganizationSponsorshipSyncRequestModel, OrganizationSponsorshipSyncResponseModel>(
|
||||
HttpMethod.Post, "organization/sponsorship/sync", new OrganizationSponsorshipSyncRequestModel
|
||||
{
|
||||
BillingSyncKey = billingSyncConfig.BillingSyncKey,
|
||||
SponsoringOrganizationCloudId = cloudOrganizationId,
|
||||
SponsorshipsBatch = orgSponsorshipsBatch.Select(s => new OrganizationSponsorshipRequestModel(s))
|
||||
}, true);
|
||||
|
||||
if (response == null)
|
||||
{
|
||||
|
@ -47,19 +47,21 @@ public abstract class BaseIdentityClientService : IDisposable
|
||||
protected string AccessToken { get; private set; }
|
||||
|
||||
protected Task SendAsync(HttpMethod method, string path) =>
|
||||
SendAsync<object, object>(method, path, null);
|
||||
SendAsync<object>(method, path, null);
|
||||
|
||||
protected Task SendAsync<TRequest>(HttpMethod method, string path, TRequest body) =>
|
||||
SendAsync<TRequest, object>(method, path, body);
|
||||
protected Task SendAsync<TRequest>(HttpMethod method, string path, TRequest requestModel) =>
|
||||
SendAsync<TRequest, object>(method, path, requestModel, false);
|
||||
|
||||
protected async Task<TResult> SendAsync<TRequest, TResult>(HttpMethod method, string path, TRequest requestModel)
|
||||
protected async Task<TResult> SendAsync<TRequest, TResult>(HttpMethod method, string path,
|
||||
TRequest requestModel, bool hasJsonResult)
|
||||
{
|
||||
var fullRequestPath = string.Concat(Client.BaseAddress, path);
|
||||
|
||||
var tokenStateResponse = await HandleTokenStateAsync();
|
||||
if (!tokenStateResponse)
|
||||
{
|
||||
_logger.LogError("Unable to send {method} request to {requestUri} because an access token was unable to be obtained", method.Method, fullRequestPath);
|
||||
_logger.LogError("Unable to send {method} request to {requestUri} because an access token was unable to be obtained",
|
||||
method.Method, fullRequestPath);
|
||||
return default;
|
||||
}
|
||||
|
||||
@ -71,7 +73,19 @@ public abstract class BaseIdentityClientService : IDisposable
|
||||
try
|
||||
{
|
||||
var response = await Client.SendAsync(message);
|
||||
return await response.Content.ReadFromJsonAsync<TResult>();
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
if (hasJsonResult)
|
||||
{
|
||||
return await response.Content.ReadFromJsonAsync<TResult>();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError("Request to {url} is unsuccessful with status of {code}-{reason}",
|
||||
message.RequestUri.ToString(), response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
return default;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@ -82,8 +96,9 @@ public abstract class BaseIdentityClientService : IDisposable
|
||||
|
||||
protected async Task<bool> HandleTokenStateAsync()
|
||||
{
|
||||
if (_nextAuthAttempt.HasValue && DateTime.UtcNow > _nextAuthAttempt.Value)
|
||||
if (_nextAuthAttempt.HasValue && DateTime.UtcNow < _nextAuthAttempt.Value)
|
||||
{
|
||||
_logger.LogInformation("Not requesting a token at {now} because the next request time is {nextAttempt}", DateTime.UtcNow, _nextAuthAttempt.Value);
|
||||
return false;
|
||||
}
|
||||
_nextAuthAttempt = null;
|
||||
@ -118,12 +133,13 @@ public abstract class BaseIdentityClientService : IDisposable
|
||||
|
||||
if (response == null)
|
||||
{
|
||||
_logger.LogError("Empty token response from {identity} for client {clientId} with status {code}-{reason}", IdentityClient.BaseAddress, _identityClientId, response.StatusCode, response.ReasonPhrase);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogInformation("Unsuccessful token response from {identity} for client {clientId} with status code {StatusCode}", IdentityClient.BaseAddress, _identityClientId, response.StatusCode);
|
||||
_logger.LogError("Unsuccessful token response from {identity} for client {clientId} with status {code}-{reason}", IdentityClient.BaseAddress, _identityClientId, response.StatusCode, response.ReasonPhrase);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.BadRequest)
|
||||
{
|
||||
@ -139,7 +155,8 @@ public abstract class BaseIdentityClientService : IDisposable
|
||||
return false;
|
||||
}
|
||||
|
||||
using var jsonDocument = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
|
||||
var content = await response.Content.ReadAsStreamAsync();
|
||||
using var jsonDocument = await JsonDocument.ParseAsync(content);
|
||||
|
||||
AccessToken = jsonDocument.RootElement.GetProperty("access_token").GetString();
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user