1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 16:12:49 -05:00

Support for CipherKey and Share APIs

This commit is contained in:
Kyle Spearrin
2017-02-27 22:58:01 -05:00
parent 48cf44f5b2
commit 8c7f1dd343
27 changed files with 144 additions and 53 deletions

View File

@ -8,7 +8,6 @@ using Microsoft.AspNetCore.Authorization;
using Bit.Api.Models;
using Bit.Core.Exceptions;
using Bit.Core.Domains;
using Microsoft.AspNetCore.Identity;
using Bit.Core.Services;
namespace Bit.Api.Controllers
@ -34,29 +33,29 @@ namespace Bit.Api.Controllers
}
[HttpGet("{id}")]
public async Task<LoginResponseModel> Get(string id, string[] expand = null)
public async Task<LoginShareResponseModel> Get(string id, string[] expand = null)
{
var userId = _userService.GetProperUserId(User).Value;
var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
var login = await _cipherRepository.GetShareByIdAsync(new Guid(id), userId);
if(login == null || login.Type != Core.Enums.CipherType.Login)
{
throw new NotFoundException();
}
var response = new LoginResponseModel(login, userId);
var response = new LoginShareResponseModel(login, userId);
await ExpandAsync(login, response, expand, null, userId);
return response;
}
[HttpGet("")]
public async Task<ListResponseModel<LoginResponseModel>> Get(string[] expand = null)
public async Task<ListResponseModel<LoginShareResponseModel>> Get(string[] expand = null)
{
var userId = _userService.GetProperUserId(User).Value;
ICollection<Cipher> logins = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Login,
var logins = await _cipherRepository.GetManyShareByTypeAndUserIdAsync(Core.Enums.CipherType.Login,
userId);
var responses = logins.Select(s => new LoginResponseModel(s, userId)).ToList();
var responses = logins.Select(s => new LoginShareResponseModel(s, userId)).ToList();
await ExpandManyAsync(logins, responses, expand, null, userId);
return new ListResponseModel<LoginResponseModel>(responses);
return new ListResponseModel<LoginShareResponseModel>(responses);
}
[HttpPost("")]
@ -120,8 +119,8 @@ namespace Bit.Api.Controllers
}
}
private async Task ExpandManyAsync(IEnumerable<Cipher> logins, ICollection<LoginResponseModel> responses,
string[] expand, IEnumerable<Cipher> folders, Guid userId)
private async Task ExpandManyAsync<TResponseModel>(IEnumerable<Cipher> logins, ICollection<TResponseModel> responses,
string[] expand, IEnumerable<Cipher> folders, Guid userId) where TResponseModel : LoginResponseModel
{
if(expand == null || expand.Count() == 0)
{

View File

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Authorization;
using Bit.Api.Models;
using Bit.Core.Exceptions;
using Bit.Core.Services;
using System.Collections.Generic;
namespace Bit.Api.Controllers
{
@ -41,6 +42,19 @@ namespace Bit.Api.Controllers
return new ShareResponseModel(share);
}
//[HttpGet("cipher/{cipherId}")]
//public async Task<IEnumerable<ShareResponseModel>> GetCipher(string cipherId)
//{
// var userId = _userService.GetProperUserId(User).Value;
// var share = await _shareRepository.GetByIdAsync(new Guid(cipherId), userId);
// if(share == null)
// {
// throw new NotFoundException();
// }
// return new ShareResponseModel(share);
//}
[HttpPost("")]
public async Task<ShareResponseModel> Post([FromBody]ShareRequestModel model)
{

View File

@ -1,8 +1,6 @@
using System;
using Bit.Core.Domains;
using Bit.Core.Models.Data;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Bit.Api.Models
{
@ -21,6 +19,7 @@ namespace Bit.Api.Models
Type = cipher.Type;
Favorite = cipher.Favorite;
RevisionDate = cipher.RevisionDate;
Key = cipher.Key;
switch(cipher.Type)
{
@ -39,6 +38,7 @@ namespace Bit.Api.Models
public string FolderId { get; set; }
public Core.Enums.CipherType Type { get; set; }
public bool Favorite { get; set; }
public string Key { get; set; }
public dynamic Data { get; set; }
public DateTime RevisionDate { get; set; }
}
@ -48,14 +48,11 @@ namespace Bit.Api.Models
public CipherShareResponseModel(CipherShare cipherShare, Guid userId)
: base(cipherShare, userId, "cipherShare")
{
Key = cipherShare.Key;
Permissions = cipherShare.Permissions == null ? null :
JsonConvert.DeserializeObject<IEnumerable<Core.Enums.SharePermissionType>>(cipherShare.Permissions);
ReadOnly = cipherShare.ReadOnly;
Status = cipherShare.Status;
}
public string Key { get; set; }
public IEnumerable<Core.Enums.SharePermissionType> Permissions { get; set; }
public bool ReadOnly { get; set; }
public Core.Enums.ShareStatusType? Status { get; set; }
}
}

View File

@ -1,8 +1,5 @@
using System;
using Bit.Core.Domains;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Bit.Api.Models
{
@ -30,7 +27,6 @@ namespace Bit.Api.Models
public string Id { get; set; }
public string Name { get; set; }
public string Key { get; set; }
public DateTime RevisionDate { get; set; }
}
}

View File

@ -1,14 +1,12 @@
using System;
using Bit.Core.Domains;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using Bit.Core.Models.Data;
namespace Bit.Api.Models
{
public class LoginResponseModel : ResponseModel
{
public LoginResponseModel(Cipher cipher, Guid userId)
public LoginResponseModel(Cipher cipher, Guid userId, string obj = "login")
: base("login")
{
if(cipher == null)
@ -32,6 +30,7 @@ namespace Bit.Api.Models
Password = data.Password;
Notes = data.Notes;
RevisionDate = cipher.RevisionDate;
Key = cipher.Key;
}
public string Id { get; set; }
@ -42,10 +41,23 @@ namespace Bit.Api.Models
public string Username { get; set; }
public string Password { get; set; }
public string Notes { get; set; }
public string Key { get; set; }
public DateTime RevisionDate { get; set; }
public string Key { get; set; }
// Expandables
public FolderResponseModel Folder { get; set; }
}
public class LoginShareResponseModel : LoginResponseModel
{
public LoginShareResponseModel(CipherShare cipherShare, Guid userId)
: base(cipherShare, userId, "loginShare")
{
ReadOnly = cipherShare.ReadOnly;
Status = cipherShare.Status;
}
public bool ReadOnly { get; set; }
public Core.Enums.ShareStatusType? Status { get; set; }
}
}

View File

@ -1,7 +1,5 @@
using System;
using Bit.Core.Domains;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Bit.Api.Models
{
@ -20,8 +18,7 @@ namespace Bit.Api.Models
SharerUserId = share.SharerUserId.ToString();
CipherId = share.CipherId.ToString();
Key = Key;
Permissions = share.Permissions == null ? null :
JsonConvert.DeserializeObject<IEnumerable<Core.Enums.SharePermissionType>>(share.Permissions);
ReadOnly = share.ReadOnly;
Status = share.Status;
}
@ -30,7 +27,7 @@ namespace Bit.Api.Models
public string SharerUserId { get; set; }
public string CipherId { get; set; }
public string Key { get; set; }
public IEnumerable<Core.Enums.SharePermissionType> Permissions { get; set; }
public bool ReadOnly { get; set; }
public Core.Enums.ShareStatusType? Status { get; set; }
}
}

View File

@ -79,6 +79,7 @@ namespace Bit.Api
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
services.AddSingleton<IShareRepository, SqlServerRepos.ShareRepository>();
// Context
services.AddScoped<CurrentContext>();