1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00

public and private keys added to db and user domain. added account APIs got getting and putting keys.

This commit is contained in:
Kyle Spearrin 2017-02-11 23:00:55 -05:00
parent 7589f9c933
commit 024ee08907
6 changed files with 75 additions and 4 deletions

View File

@ -263,6 +263,22 @@ namespace Bit.Api.Controllers
return response;
}
[HttpPut("keys")]
[HttpPost("keys")]
public async Task<KeysResponseModel> PutKeys([FromBody]KeysRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
await _userService.SaveUserAsync(model.ToUser(user));
return new KeysResponseModel(user);
}
[HttpGet("keys")]
public async Task<KeysResponseModel> GetKeys()
{
var user = await _userService.GetUserByPrincipalAsync(User);
return new KeysResponseModel(user);
}
[HttpPost("delete")]
public async Task PostDelete([FromBody]DeleteAccountRequestModel model)
{

View File

@ -118,8 +118,13 @@ namespace Bit.Api.IdentityServer
claims.Add(new Claim("device", device.Identifier));
}
context.Result = new GrantValidationResult(user.Id.ToString(), "Application", identityProvider: "bitwarden",
claims: claims.Count > 0 ? claims : null);
context.Result = new GrantValidationResult(user.Id.ToString(), "Application",
identityProvider: "bitwarden",
claims: claims.Count > 0 ? claims : null,
customResponse: new Dictionary<string, object>
{
{ "PrivateKey", user.PrivateKey }
});
}
private void BuildTwoFactorResult(User user, ResourceOwnerPasswordValidationContext context)
@ -139,8 +144,8 @@ namespace Bit.Api.IdentityServer
private void BuildErrorResult(bool twoFactorRequest, ResourceOwnerPasswordValidationContext context)
{
context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, customResponse:
new Dictionary<string, object>
context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant,
customResponse: new Dictionary<string, object>
{{
"ErrorModel", new ErrorResponseModel(twoFactorRequest ?
"Code is not correct. Try again." : "Username or password is incorrect. Try again.")

View File

@ -0,0 +1,23 @@
using Bit.Core.Domains;
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class KeysRequestModel
{
public string PublicKey { get; set; }
[Required]
public string PrivateKey { get; set; }
public User ToUser(User existingUser)
{
if(!string.IsNullOrWhiteSpace(PublicKey))
{
existingUser.PublicKey = PublicKey;
}
existingUser.PrivateKey = PrivateKey;
return existingUser;
}
}
}

View File

@ -0,0 +1,23 @@
using System;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class KeysResponseModel : ResponseModel
{
public KeysResponseModel(User user)
: base("keys")
{
if(user == null)
{
throw new ArgumentNullException(nameof(user));
}
PublicKey = user.PublicKey;
PrivateKey = user.PrivateKey;
}
public string PublicKey { get; set; }
public string PrivateKey { get; set; }
}
}

View File

@ -21,6 +21,8 @@ namespace Bit.Core.Domains
public string EquivalentDomains { get; set; }
public string ExcludedGlobalEquivalentDomains { get; set; }
public DateTime AccountRevisionDate { get; internal set; } = DateTime.UtcNow;
public string PublicKey { get; set; }
public string PrivateKey { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;

View File

@ -14,6 +14,8 @@
[EquivalentDomains] NVARCHAR (MAX) NULL,
[ExcludedGlobalEquivalentDomains] NVARCHAR (MAX) NULL,
[AccountRevisionDate] DATETIME2 (7) NOT NULL,
[PublicKey] NVARCHAR (MAX) NULL,
[PrivateKey] NVARCHAR (MAX) NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ([Id] ASC)