1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-02 13:53:23 +01:00
bitwarden-server/src/Core/Identity/ReadOnlyEnvIdentityUserStore.cs
2018-03-28 10:45:30 -04:00

55 lines
1.7 KiB
C#

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
namespace Bit.Core.Identity
{
public class ReadOnlyEnvIdentityUserStore : ReadOnlyIdentityUserStore
{
private readonly IConfiguration _configuration;
public ReadOnlyEnvIdentityUserStore(IConfiguration configuration)
{
_configuration = configuration;
}
public override Task<IdentityUser> FindByEmailAsync(string normalizedEmail,
CancellationToken cancellationToken = default(CancellationToken))
{
var usersCsv = _configuration["adminSettings:admins"];
if(!CoreHelpers.SettingHasValue(usersCsv))
{
return Task.FromResult<IdentityUser>(null);
}
var users = usersCsv.ToLowerInvariant().Split(',');
var user = users.Where(a => a.Trim() == normalizedEmail).FirstOrDefault();
if(user == null || !user.Contains("@"))
{
return Task.FromResult<IdentityUser>(null);
}
user = user.Trim();
return Task.FromResult(new IdentityUser
{
Id = user,
Email = user,
NormalizedEmail = user,
EmailConfirmed = true,
UserName = user,
NormalizedUserName = user,
SecurityStamp = user
});
}
public override Task<IdentityUser> FindByIdAsync(string userId,
CancellationToken cancellationToken = default(CancellationToken))
{
return FindByEmailAsync(userId, cancellationToken);
}
}
}