1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-28 17:57:37 +01:00

stubbing out api setup for u2f

This commit is contained in:
Kyle Spearrin 2017-06-21 21:46:52 -04:00
parent 03f3118182
commit fd5e2c9466
16 changed files with 170 additions and 64 deletions

View File

@ -112,6 +112,25 @@ namespace Bit.Api.Controllers
return response;
}
[HttpPost("get-u2f")]
public async Task<TwoFactorU2fResponseModel> GetU2f([FromBody]TwoFactorRequestModel model)
{
var user = await CheckPasswordAsync(model.MasterPasswordHash);
var response = new TwoFactorU2fResponseModel(user);
return response;
}
[HttpPut("u2f")]
[HttpPost("u2f")]
public async Task<TwoFactorU2fResponseModel> PutU2f([FromBody]TwoFactorU2fRequestModel model)
{
var user = await CheckPasswordAsync(model.MasterPasswordHash);
model.ToUser(user);
await _userService.UpdateTwoFactorProviderAsync(user, TwoFactorProviderType.U2f);
var response = new TwoFactorU2fResponseModel(user);
return response;
}
public async Task ValidateYubiKeyAsync(User user, string name, string value)
{
if(string.IsNullOrWhiteSpace(value) || value.Length == 12)

View File

@ -6,6 +6,6 @@
Email = 1,
Duo = 2,
YubiKey = 3,
U2F = 4
U2f = 4
}
}

View File

@ -14,7 +14,7 @@ namespace Bit.Core.Identity
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Authenticator);
var canGenerate = user.TwoFactorProviderIsEnabled(TwoFactorProviderType.Authenticator)
&& !string.IsNullOrWhiteSpace(provider.MetaData["Key"]);
&& !string.IsNullOrWhiteSpace((string)provider.MetaData["Key"]);
return Task.FromResult(canGenerate);
}
@ -27,7 +27,7 @@ namespace Bit.Core.Identity
public Task<bool> ValidateAsync(string purpose, string token, UserManager<User> manager, User user)
{
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Authenticator);
var otp = new Totp(Base32Encoding.ToBytes(provider.MetaData["Key"]));
var otp = new Totp(Base32Encoding.ToBytes((string)provider.MetaData["Key"]));
long timeStepMatched;
var valid = otp.VerifyTotp(token, out timeStepMatched, new VerificationWindow(1, 1));

View File

@ -14,7 +14,7 @@ namespace Bit.Core.Identity
{
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Duo);
var canGenerate = user.TwoFactorProviderIsEnabled(TwoFactorProviderType.Duo)
&& !string.IsNullOrWhiteSpace(provider?.MetaData["UserId"]);
&& !string.IsNullOrWhiteSpace((string)provider?.MetaData["UserId"]);
return Task.FromResult(canGenerate);
}
@ -23,13 +23,14 @@ namespace Bit.Core.Identity
public async Task<string> GenerateAsync(string purpose, UserManager<User> manager, User user)
{
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Duo);
var duoClient = new DuoApi(provider.MetaData["IKey"], provider.MetaData["SKey"], provider.MetaData["Host"]);
var duoClient = new DuoApi((string)provider.MetaData["IKey"], (string)provider.MetaData["SKey"],
(string)provider.MetaData["Host"]);
var parts = purpose.Split(':');
var parameters = new Dictionary<string, string>
{
["async"] = "1",
["user_id"] = provider.MetaData["UserId"],
["user_id"] = (string)provider.MetaData["UserId"],
["factor"] = parts[0]
};
@ -61,7 +62,8 @@ namespace Bit.Core.Identity
public async Task<bool> ValidateAsync(string purpose, string token, UserManager<User> manager, User user)
{
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Duo);
var duoClient = new DuoApi(provider.MetaData["IKey"], provider.MetaData["SKey"], provider.MetaData["Host"]);
var duoClient = new DuoApi((string)provider.MetaData["IKey"], (string)provider.MetaData["SKey"],
(string)provider.MetaData["Host"]);
var parameters = new Dictionary<string, string>
{

View File

@ -32,7 +32,7 @@ namespace Bit.Core.Identity
return Task.FromResult<string>(null);
}
var signatureRequest = DuoWeb.SignRequest(provider.MetaData["IKey"], provider.MetaData["SKey"],
var signatureRequest = DuoWeb.SignRequest((string)provider.MetaData["IKey"], (string)provider.MetaData["SKey"],
_globalSettings.Duo.AKey, user.Id.ToString());
return Task.FromResult(signatureRequest);
}
@ -45,7 +45,7 @@ namespace Bit.Core.Identity
return Task.FromResult(false);
}
var response = DuoWeb.VerifyResponse(provider.MetaData["IKey"], provider.MetaData["SKey"],
var response = DuoWeb.VerifyResponse((string)provider.MetaData["IKey"], (string)provider.MetaData["SKey"],
_globalSettings.Duo.AKey, token);
Guid userId;

View File

@ -20,7 +20,7 @@ namespace Bit.Core.Identity
{
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.YubiKey);
var canGenerate = user.TwoFactorProviderIsEnabled(TwoFactorProviderType.YubiKey)
&& (provider?.MetaData.Values.Any(v => !string.IsNullOrWhiteSpace(v)) ?? false);
&& (provider?.MetaData.Values.Any(v => !string.IsNullOrWhiteSpace((string)v)) ?? false);
return Task.FromResult(canGenerate);
}

View File

@ -166,7 +166,7 @@ namespace Bit.Core.IdentityServer
case TwoFactorProviderType.Authenticator:
case TwoFactorProviderType.Duo:
case TwoFactorProviderType.YubiKey:
case TwoFactorProviderType.U2F:
case TwoFactorProviderType.U2f:
return await _userManager.VerifyTwoFactorTokenAsync(user, type.ToString(), token);
case TwoFactorProviderType.Email:
return await _userService.VerifyTwoFactorEmailAsync(user, token);
@ -181,7 +181,7 @@ namespace Bit.Core.IdentityServer
switch(type)
{
case TwoFactorProviderType.Duo:
case TwoFactorProviderType.U2F:
case TwoFactorProviderType.U2f:
var token = await _userManager.GenerateTwoFactorTokenAsync(user, type.ToString());
if(type == TwoFactorProviderType.Duo)
{
@ -191,7 +191,7 @@ namespace Bit.Core.IdentityServer
["Signature"] = token
};
}
else if(type == TwoFactorProviderType.U2F)
else if(type == TwoFactorProviderType.U2f)
{
// TODO: U2F challenge
return new Dictionary<string, object> { };

View File

@ -30,7 +30,7 @@ namespace Bit.Core.Models.Api
providers.Add(TwoFactorProviderType.Authenticator, new TwoFactorProvider
{
MetaData = new Dictionary<string, string> { ["Key"] = Key },
MetaData = new Dictionary<string, object> { ["Key"] = Key },
Enabled = true
});
extistingUser.SetTwoFactorProviders(providers);
@ -64,7 +64,7 @@ namespace Bit.Core.Models.Api
providers.Add(TwoFactorProviderType.Duo, new TwoFactorProvider
{
MetaData = new Dictionary<string, string>
MetaData = new Dictionary<string, object>
{
["SKey"] = SecretKey,
["IKey"] = IntegrationKey,
@ -107,7 +107,7 @@ namespace Bit.Core.Models.Api
providers.Add(TwoFactorProviderType.YubiKey, new TwoFactorProvider
{
MetaData = new Dictionary<string, string>
MetaData = new Dictionary<string, object>
{
["Key1"] = FormatKey(Key1),
["Key2"] = FormatKey(Key2),
@ -187,7 +187,40 @@ namespace Bit.Core.Models.Api
providers.Add(TwoFactorProviderType.Email, new TwoFactorProvider
{
MetaData = new Dictionary<string, string> { ["Email"] = Email },
MetaData = new Dictionary<string, object> { ["Email"] = Email },
Enabled = true
});
extistingUser.SetTwoFactorProviders(providers);
return extistingUser;
}
}
public class TwoFactorU2fRequestModel : TwoFactorRequestModel
{
[Required]
public string DeviceResponse { get; set; }
public User ToUser(User extistingUser)
{
var providers = extistingUser.GetTwoFactorProviders();
if(providers == null)
{
providers = new Dictionary<TwoFactorProviderType, TwoFactorProvider>();
}
else if(providers.ContainsKey(TwoFactorProviderType.U2f))
{
providers.Remove(TwoFactorProviderType.U2f);
}
providers.Add(TwoFactorProviderType.U2f, new TwoFactorProvider
{
MetaData = new Dictionary<string, object>
{
["Key1"] = new TwoFactorProvider.U2fMetaData
{
// TODO
}
},
Enabled = true
});
extistingUser.SetTwoFactorProviders(providers);

View File

@ -18,7 +18,7 @@ namespace Bit.Core.Models.Api
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Authenticator);
if(provider?.MetaData?.ContainsKey("Key") ?? false)
{
Key = provider.MetaData["Key"];
Key = (string)provider.MetaData["Key"];
Enabled = provider.Enabled;
}
else

View File

@ -21,15 +21,15 @@ namespace Bit.Core.Models.Api
if(provider.MetaData.ContainsKey("Host"))
{
Host = provider.MetaData["Host"];
Host = (string)provider.MetaData["Host"];
}
if(provider.MetaData.ContainsKey("SKey"))
{
SecretKey = provider.MetaData["SKey"];
SecretKey = (string)provider.MetaData["SKey"];
}
if(provider.MetaData.ContainsKey("IKey"))
{
IntegrationKey = provider.MetaData["IKey"];
IntegrationKey = (string)provider.MetaData["IKey"];
}
}
else

View File

@ -17,7 +17,7 @@ namespace Bit.Core.Models.Api
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
if(provider?.MetaData?.ContainsKey("Email") ?? false)
{
Email = provider.MetaData["Email"];
Email = (string)provider.MetaData["Email"];
Enabled = provider.Enabled;
}
else

View File

@ -0,0 +1,43 @@
using System;
using Bit.Core.Enums;
using Bit.Core.Models.Table;
namespace Bit.Core.Models.Api
{
public class TwoFactorU2fResponseModel : ResponseModel
{
public TwoFactorU2fResponseModel(User user)
: base("twoFactorU2f")
{
if(user == null)
{
throw new ArgumentNullException(nameof(user));
}
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.U2f);
if(provider?.MetaData != null && provider.MetaData.Count > 0)
{
Challenge = new ChallengeModel
{
// TODO
};
Enabled = provider.Enabled;
}
else
{
Enabled = false;
}
}
public ChallengeModel Challenge { get; set; }
public bool Enabled { get; set; }
public class ChallengeModel
{
public string UserId { get; set; }
public string AppId { get; set; }
public string Challenge { get; set; }
public string Version { get; set; }
}
}
}

View File

@ -21,23 +21,23 @@ namespace Bit.Core.Models.Api
if(provider.MetaData.ContainsKey("Key1"))
{
Key1 = provider.MetaData["Key1"];
Key1 = (string)provider.MetaData["Key1"];
}
if(provider.MetaData.ContainsKey("Key2"))
{
Key2 = provider.MetaData["Key2"];
Key2 = (string)provider.MetaData["Key2"];
}
if(provider.MetaData.ContainsKey("Key3"))
{
Key3 = provider.MetaData["Key3"];
Key3 = (string)provider.MetaData["Key3"];
}
if(provider.MetaData.ContainsKey("Key4"))
{
Key4 = provider.MetaData["Key4"];
Key4 = (string)provider.MetaData["Key4"];
}
if(provider.MetaData.ContainsKey("Key5"))
{
Key5 = provider.MetaData["Key5"];
Key5 = (string)provider.MetaData["Key5"];
}
}
else

View File

@ -5,6 +5,15 @@ namespace Bit.Core.Models
public class TwoFactorProvider
{
public bool Enabled { get; set; }
public Dictionary<string, string> MetaData { get; set; } = new Dictionary<string, string>();
public Dictionary<string, object> MetaData { get; set; } = new Dictionary<string, object>();
public class U2fMetaData
{
public string KeyHandle { get; set; }
public string PublicKey { get; set; }
public string Certificate { get; set; }
public int Counter { get; set; }
public bool Compromised { get; set; }
}
}
}

View File

@ -192,7 +192,7 @@ namespace Bit.Core.Services
var token = await base.GenerateUserTokenAsync(user, TokenOptions.DefaultEmailProvider,
"2faEmail:" + provider.MetaData["Email"]);
await _mailService.SendChangeEmailEmailAsync(provider.MetaData["Email"], token);
await _mailService.SendChangeEmailEmailAsync((string)provider.MetaData["Email"], token);
}
public async Task<bool> VerifyTwoFactorEmailAsync(User user, string token)

View File

@ -68,29 +68,32 @@
</ItemGroup>
<ItemGroup>
<Build Include="dbo\Tables\Grant.sql" />
<Build Include="dbo\Tables\Organization.sql" />
<Build Include="dbo\Tables\User.sql" />
<Build Include="dbo\Tables\U2f.sql" />
<Build Include="dbo\Tables\Device.sql" />
<Build Include="dbo\Tables\Folder.sql" />
<Build Include="dbo\Tables\Cipher.sql" />
<Build Include="dbo\Tables\User.sql" />
<Build Include="dbo\Tables\Collection.sql" />
<Build Include="dbo\Tables\Group.sql" />
<Build Include="dbo\Tables\CollectionCipher.sql" />
<Build Include="dbo\Tables\GroupUser.sql" />
<Build Include="dbo\Tables\CollectionGroup.sql" />
<Build Include="dbo\Tables\CollectionUser.sql" />
<Build Include="dbo\Tables\Group.sql" />
<Build Include="dbo\Tables\GroupUser.sql" />
<Build Include="dbo\Tables\OrganizationUser.sql" />
<Build Include="dbo\Tables\Organization.sql" />
<Build Include="dbo\Views\GrantView.sql" />
<Build Include="dbo\Views\UserView.sql" />
<Build Include="dbo\Views\U2fView.sql" />
<Build Include="dbo\Views\CipherView.sql" />
<Build Include="dbo\Views\DeviceView.sql" />
<Build Include="dbo\Views\CollectionView.sql" />
<Build Include="dbo\Views\GroupView.sql" />
<Build Include="dbo\Views\CollectionView.sql" />
<Build Include="dbo\Views\FolderView.sql" />
<Build Include="dbo\Views\OrganizationUserOrganizationDetailsView.sql" />
<Build Include="dbo\Views\OrganizationUserUserDetailsView.sql" />
<Build Include="dbo\Views\OrganizationUserView.sql" />
<Build Include="dbo\Views\OrganizationView.sql" />
<Build Include="dbo\Functions\UserCipherDetails.sql" />
<Build Include="dbo\Functions\CipherDetails.sql" />
<Build Include="dbo\Stored Procedures\Organization_ReadById.sql" />
<Build Include="dbo\Stored Procedures\Organization_ReadByUserId.sql" />
@ -118,15 +121,23 @@
<Build Include="dbo\Stored Procedures\User_BumpAccountRevisionDate.sql" />
<Build Include="dbo\Stored Procedures\User_BumpAccountRevisionDateByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\User_BumpAccountRevisionDateByOrganizationUserId.sql" />
<Build Include="dbo\Stored Procedures\Cipher_Delete.sql" />
<Build Include="dbo\Stored Procedures\User_ReadPublicKeyById.sql" />
<Build Include="dbo\Stored Procedures\Cipher_Move.sql" />
<Build Include="dbo\Stored Procedures\Cipher_UpdatePartial.sql" />
<Build Include="dbo\Stored Procedures\Device_ClearPushTokenById.sql" />
<Build Include="dbo\Stored Procedures\Cipher_UpdateWithCollections.sql" />
<Build Include="dbo\Stored Procedures\Device_ReadByIdentifier.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_Create.sql" />
<Build Include="dbo\Stored Procedures\User_UpdateKeys.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_Update.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_Create.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_Delete.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_UpdateCollections.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_UpdateCollectionsAdmin.sql" />
<Build Include="dbo\Stored Procedures\U2f_Create.sql" />
<Build Include="dbo\Stored Procedures\U2f_DeleteByUserId.sql" />
<Build Include="dbo\Stored Procedures\U2f_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Folder_Create.sql" />
<Build Include="dbo\Stored Procedures\Folder_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Folder_Update.sql" />
@ -143,62 +154,51 @@
<Build Include="dbo\Stored Procedures\Device_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Device_Update.sql" />
<Build Include="dbo\Stored Procedures\User_Create.sql" />
<Build Include="dbo\Stored Procedures\User_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\User_ReadByEmail.sql" />
<Build Include="dbo\Stored Procedures\User_ReadById.sql" />
<Build Include="dbo\Stored Procedures\User_Update.sql" />
<Build Include="dbo\Stored Procedures\User_UpdateKeys.sql" />
<Build Include="dbo\Stored Procedures\Device_ClearPushTokenById.sql" />
<Build Include="dbo\Stored Procedures\Collection_CreateWithGroups.sql" />
<Build Include="dbo\Stored Procedures\User_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadWithGroupsById.sql" />
<Build Include="dbo\Stored Procedures\User_ReadByEmail.sql" />
<Build Include="dbo\Stored Procedures\Collection_UpdateWithGroups.sql" />
<Build Include="dbo\Stored Procedures\User_ReadById.sql" />
<Build Include="dbo\Stored Procedures\CollectionUser_Delete.sql" />
<Build Include="dbo\Stored Procedures\User_Update.sql" />
<Build Include="dbo\Stored Procedures\CollectionUserDetails_ReadByCollectionId.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Group_Create.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByIdUserId.sql" />
<Build Include="dbo\Stored Procedures\Group_CreateWithCollections.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByTypeUserId.sql" />
<Build Include="dbo\Stored Procedures\Group_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Group_ReadById.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByUserIdHasCollection.sql" />
<Build Include="dbo\Stored Procedures\Group_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Group_ReadWithCollectionsById.sql" />
<Build Include="dbo\Stored Procedures\Collection_Create.sql" />
<Build Include="dbo\Stored Procedures\Group_Update.sql" />
<Build Include="dbo\Stored Procedures\Collection_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Group_UpdateWithCollections.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadById.sql" />
<Build Include="dbo\Stored Procedures\GroupUser_Delete.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\GroupUser_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\GroupUser_ReadGroupIdsByOrganizationUserId.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\GroupUser_UpdateGroups.sql" />
<Build Include="dbo\Stored Procedures\Collection_Update.sql" />
<Build Include="dbo\Stored Procedures\GroupUser_UpdateUsers.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\GroupUserDetails_ReadByGroupId.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_CreateWithCollections.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_ReadByUserIdCipherId.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadWithCollectionsById.sql" />
<Build Include="dbo\Stored Procedures\OrganizationUser_UpdateWithCollections.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByIdUserId.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByTypeUserId.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByUserIdHasCollection.sql" />
<Build Include="dbo\Stored Procedures\Collection_Create.sql" />
<Build Include="dbo\Stored Procedures\Collection_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadById.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Collection_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Collection_Update.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_ReadByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\CollectionCipher_ReadByUserIdCipherId.sql" />
<Build Include="dbo\Stored Procedures\Folder_ReadById.sql" />
<Build Include="dbo\Stored Procedures\Folder_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Organization_Create.sql" />
<Build Include="dbo\User Defined Types\GuidIdArray.sql" />
<Build Include="dbo\User Defined Types\SelectionReadOnlyArray.sql" />
<Build Include="dbo\Stored Procedures\Device_ReadByIdentifier.sql" />
<Build Include="dbo\Stored Procedures\Cipher_Delete.sql" />
<Build Include="dbo\Functions\UserCipherDetails.sql" />
<Build Include="dbo\Stored Procedures\Cipher_Move.sql" />
<Build Include="dbo\Tables\U2f.sql" />
<Build Include="dbo\Stored Procedures\U2f_Create.sql" />
<Build Include="dbo\Stored Procedures\U2f_DeleteByUserId.sql" />
<Build Include="dbo\Stored Procedures\U2f_ReadByUserId.sql" />
<Build Include="dbo\Views\U2fView.sql" />
</ItemGroup>
</Project>