mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
updates for license validation
This commit is contained in:
@ -8,6 +8,7 @@ using Newtonsoft.Json.Linq;
|
||||
using Bit.Core.Utilities;
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
@ -15,10 +16,13 @@ namespace Bit.Core.Services
|
||||
{
|
||||
private dynamic _decodedToken;
|
||||
private DateTime? _nextAuthAttempt = null;
|
||||
private readonly ILogger<BaseRelayPushNotificationService> _logger;
|
||||
|
||||
public BaseRelayPushNotificationService(
|
||||
GlobalSettings globalSettings)
|
||||
GlobalSettings globalSettings,
|
||||
ILogger<BaseRelayPushNotificationService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
GlobalSettings = globalSettings;
|
||||
|
||||
PushClient = new HttpClient
|
||||
@ -65,8 +69,17 @@ namespace Bit.Core.Services
|
||||
})
|
||||
};
|
||||
|
||||
var response = await IdentityClient.SendAsync(requestMessage);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
HttpResponseMessage response = null;
|
||||
try
|
||||
{
|
||||
response = await IdentityClient.SendAsync(requestMessage);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(12339, e, "Unable to auth for push.");
|
||||
}
|
||||
|
||||
if(response == null || !response.IsSuccessStatusCode)
|
||||
{
|
||||
if(response.StatusCode == HttpStatusCode.BadRequest)
|
||||
{
|
||||
|
@ -6,19 +6,23 @@ using Microsoft.AspNetCore.Http;
|
||||
using Bit.Core.Models;
|
||||
using System.Net.Http;
|
||||
using Bit.Core.Models.Api;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class RelayPushNotificationService : BaseRelayPushNotificationService, IPushNotificationService
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly ILogger<RelayPushNotificationService> _logger;
|
||||
|
||||
public RelayPushNotificationService(
|
||||
GlobalSettings globalSettings,
|
||||
IHttpContextAccessor httpContextAccessor)
|
||||
: base(globalSettings)
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
ILogger<RelayPushNotificationService> logger)
|
||||
: base(globalSettings, logger)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task PushSyncCipherCreateAsync(Cipher cipher)
|
||||
@ -166,7 +170,15 @@ namespace Bit.Core.Services
|
||||
Method = HttpMethod.Post,
|
||||
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/send"))
|
||||
};
|
||||
await PushClient.SendAsync(message);
|
||||
|
||||
try
|
||||
{
|
||||
await PushClient.SendAsync(message);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(12334, e, "Unable to send push notification.");
|
||||
}
|
||||
}
|
||||
|
||||
private void ExcludeCurrentContext(PushSendRequestModel request)
|
||||
|
@ -5,14 +5,21 @@ using System;
|
||||
using Bit.Core.Models.Api;
|
||||
using Bit.Core.Enums;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class RelayPushRegistrationService : BaseRelayPushNotificationService, IPushRegistrationService
|
||||
{
|
||||
public RelayPushRegistrationService(GlobalSettings globalSettings)
|
||||
: base(globalSettings)
|
||||
{ }
|
||||
private readonly ILogger<RelayPushRegistrationService> _logger;
|
||||
|
||||
public RelayPushRegistrationService(
|
||||
GlobalSettings globalSettings,
|
||||
ILogger<RelayPushRegistrationService> logger)
|
||||
: base(globalSettings, logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
|
||||
string identifier, DeviceType type)
|
||||
@ -37,7 +44,15 @@ namespace Bit.Core.Services
|
||||
Method = HttpMethod.Post,
|
||||
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/register"))
|
||||
};
|
||||
await PushClient.SendAsync(message);
|
||||
|
||||
try
|
||||
{
|
||||
await PushClient.SendAsync(message);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(12335, e, "Unable to create push registration.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteRegistrationAsync(string deviceId)
|
||||
@ -53,7 +68,15 @@ namespace Bit.Core.Services
|
||||
Method = HttpMethod.Delete,
|
||||
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/", deviceId))
|
||||
};
|
||||
await PushClient.SendAsync(message);
|
||||
|
||||
try
|
||||
{
|
||||
await PushClient.SendAsync(message);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(12336, e, "Unable to delete push registration.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task AddUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
|
||||
@ -75,7 +98,15 @@ namespace Bit.Core.Services
|
||||
Method = HttpMethod.Put,
|
||||
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/add-organization"))
|
||||
};
|
||||
await PushClient.SendAsync(message);
|
||||
|
||||
try
|
||||
{
|
||||
await PushClient.SendAsync(message);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(12337, e, "Unable to add user org push registration.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
|
||||
@ -97,7 +128,15 @@ namespace Bit.Core.Services
|
||||
Method = HttpMethod.Put,
|
||||
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/delete-organization"))
|
||||
};
|
||||
await PushClient.SendAsync(message);
|
||||
|
||||
try
|
||||
{
|
||||
await PushClient.SendAsync(message);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(12338, e, "Unable to delete user org push registration.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ namespace Bit.Core.Services
|
||||
return await _userRepository.GetAccountRevisionDateAsync(userId);
|
||||
}
|
||||
|
||||
public async Task SaveUserAsync(User user)
|
||||
public async Task SaveUserAsync(User user, bool push = false)
|
||||
{
|
||||
if(user.Id == default(Guid))
|
||||
{
|
||||
@ -152,8 +152,11 @@ namespace Bit.Core.Services
|
||||
user.RevisionDate = user.AccountRevisionDate = DateTime.UtcNow;
|
||||
await _userRepository.ReplaceAsync(user);
|
||||
|
||||
// push
|
||||
await _pushService.PushSyncSettingsAsync(user.Id);
|
||||
if(push)
|
||||
{
|
||||
// push
|
||||
await _pushService.PushSyncSettingsAsync(user.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<IdentityResult> DeleteAsync(User user)
|
||||
@ -540,7 +543,7 @@ namespace Bit.Core.Services
|
||||
IPaymentService paymentService = null;
|
||||
if(_globalSettings.SelfHosted)
|
||||
{
|
||||
if(license == null || !_licenseService.VerifyLicense(license))
|
||||
if(license == null || !_licenseService.VerifyLicense(license) || !license.CanUse(user))
|
||||
{
|
||||
throw new BadRequestException("Invalid license.");
|
||||
}
|
||||
@ -605,7 +608,7 @@ namespace Bit.Core.Services
|
||||
throw new InvalidOperationException("Licenses require self hosting.");
|
||||
}
|
||||
|
||||
if(license == null || !_licenseService.VerifyLicense(license))
|
||||
if(license == null || !_licenseService.VerifyLicense(license) || !license.CanUse(user))
|
||||
{
|
||||
throw new BadRequestException("Invalid license.");
|
||||
}
|
||||
|
Reference in New Issue
Block a user