mirror of
https://github.com/bitwarden/server
synced 2025-12-25 12:43:14 +00:00
* Get limited life attachment download URL This change limits url download to a 1min lifetime. This requires moving to a new container to allow for non-public blob access. Clients will have to call GetAttachmentData api function to receive the download URL. For backwards compatibility, attachment URLs are still present, but will not work for attachments stored in non-public access blobs. * Make GlobalSettings interface for testing * Test LocalAttachmentStorageService equivalence * Remove comment * Add missing globalSettings using * Simplify default attachment container * Default to attachments containe for existing methods A new upload method will be made for uploading to attachments-v2. For compatibility for clients which don't use these new methods, we need to still use the old container. The new container will be used only for new uploads * Remove Default MetaData fixture. * Keep attachments container blob-level security for all instances * Close unclosed FileStream * Favor default value for noop services
90 lines
3.0 KiB
C#
90 lines
3.0 KiB
C#
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Bit.Core.Models.Table;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Utilities.Duo;
|
|
using Bit.Core.Models;
|
|
using System;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Settings;
|
|
|
|
namespace Bit.Core.Identity
|
|
{
|
|
public class DuoWebTokenProvider : IUserTwoFactorTokenProvider<User>
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly GlobalSettings _globalSettings;
|
|
|
|
public DuoWebTokenProvider(
|
|
IServiceProvider serviceProvider,
|
|
GlobalSettings globalSettings)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
_globalSettings = globalSettings;
|
|
}
|
|
|
|
public async Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<User> manager, User user)
|
|
{
|
|
var userService = _serviceProvider.GetRequiredService<IUserService>();
|
|
if (!(await userService.CanAccessPremium(user)))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Duo);
|
|
if (!HasProperMetaData(provider))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return await userService.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType.Duo, user);
|
|
}
|
|
|
|
public async Task<string> GenerateAsync(string purpose, UserManager<User> manager, User user)
|
|
{
|
|
var userService = _serviceProvider.GetRequiredService<IUserService>();
|
|
if (!(await userService.CanAccessPremium(user)))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Duo);
|
|
if (!HasProperMetaData(provider))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var signatureRequest = DuoWeb.SignRequest((string)provider.MetaData["IKey"],
|
|
(string)provider.MetaData["SKey"], _globalSettings.Duo.AKey, user.Email);
|
|
return signatureRequest;
|
|
}
|
|
|
|
public async Task<bool> ValidateAsync(string purpose, string token, UserManager<User> manager, User user)
|
|
{
|
|
var userService = _serviceProvider.GetRequiredService<IUserService>();
|
|
if (!(await userService.CanAccessPremium(user)))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Duo);
|
|
if (!HasProperMetaData(provider))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var response = DuoWeb.VerifyResponse((string)provider.MetaData["IKey"], (string)provider.MetaData["SKey"],
|
|
_globalSettings.Duo.AKey, token);
|
|
|
|
return response == user.Email;
|
|
}
|
|
|
|
private bool HasProperMetaData(TwoFactorProvider provider)
|
|
{
|
|
return provider?.MetaData != null && provider.MetaData.ContainsKey("IKey") &&
|
|
provider.MetaData.ContainsKey("SKey") && provider.MetaData.ContainsKey("Host");
|
|
}
|
|
}
|
|
}
|