mirror of
https://github.com/bitwarden/server.git
synced 2025-04-06 21:48:12 -05:00
implemented grant repository and identity server PersistedGrantStore
This commit is contained in:
parent
9749d1e3a8
commit
2abb1aaae5
@ -16,7 +16,7 @@ using Bit.Core.Domains;
|
|||||||
using Bit.Core.Identity;
|
using Bit.Core.Identity;
|
||||||
using Bit.Core.Repositories;
|
using Bit.Core.Repositories;
|
||||||
using Bit.Core.Services;
|
using Bit.Core.Services;
|
||||||
using Repos = Bit.Core.Repositories.SqlServer;
|
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Loggr.Extensions.Logging;
|
using Loggr.Extensions.Logging;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -28,6 +28,7 @@ using Bit.Api.Middleware;
|
|||||||
using IdentityServer4.Validation;
|
using IdentityServer4.Validation;
|
||||||
using IdentityServer4.Services;
|
using IdentityServer4.Services;
|
||||||
using IdentityModel.AspNetCore.OAuth2Introspection;
|
using IdentityModel.AspNetCore.OAuth2Introspection;
|
||||||
|
using IdentityServer4.Stores;
|
||||||
|
|
||||||
namespace Bit.Api
|
namespace Bit.Api
|
||||||
{
|
{
|
||||||
@ -70,9 +71,10 @@ namespace Bit.Api
|
|||||||
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
|
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
|
||||||
|
|
||||||
// Repositories
|
// Repositories
|
||||||
services.AddSingleton<IUserRepository, Repos.UserRepository>();
|
services.AddSingleton<IUserRepository, SqlServerRepos.UserRepository>();
|
||||||
services.AddSingleton<ICipherRepository, Repos.CipherRepository>();
|
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
|
||||||
services.AddSingleton<IDeviceRepository, Repos.DeviceRepository>();
|
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
|
||||||
|
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
|
||||||
|
|
||||||
// Context
|
// Context
|
||||||
services.AddScoped<CurrentContext>();
|
services.AddScoped<CurrentContext>();
|
||||||
@ -92,6 +94,7 @@ namespace Bit.Api
|
|||||||
.AddInMemoryClients(Clients.GetClients());
|
.AddInMemoryClients(Clients.GetClients());
|
||||||
services.AddSingleton<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
|
services.AddSingleton<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
|
||||||
services.AddSingleton<IProfileService, ProfileService>();
|
services.AddSingleton<IProfileService, ProfileService>();
|
||||||
|
services.AddSingleton<IPersistedGrantStore, PersistedGrantStore>();
|
||||||
|
|
||||||
// Identity
|
// Identity
|
||||||
services.AddTransient<ILookupNormalizer, LowerInvariantLookupNormalizer>();
|
services.AddTransient<ILookupNormalizer, LowerInvariantLookupNormalizer>();
|
||||||
|
15
src/Core/Domains/Grant.cs
Normal file
15
src/Core/Domains/Grant.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Bit.Core.Domains
|
||||||
|
{
|
||||||
|
public class Grant
|
||||||
|
{
|
||||||
|
public string Key { get; set; }
|
||||||
|
public string Type { get; set; }
|
||||||
|
public string SubjectId { get; set; }
|
||||||
|
public string ClientId { get; set; }
|
||||||
|
public DateTime CreationDate { get; set; }
|
||||||
|
public DateTime? ExpirationDate { get; set; }
|
||||||
|
public string Data { get; set; }
|
||||||
|
}
|
||||||
|
}
|
90
src/Core/Identity/PersistedGrantStore.cs
Normal file
90
src/Core/Identity/PersistedGrantStore.cs
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core.Domains;
|
||||||
|
using Bit.Core.Repositories;
|
||||||
|
using IdentityServer4.Models;
|
||||||
|
using IdentityServer4.Stores;
|
||||||
|
|
||||||
|
namespace Bit.Core.Identity
|
||||||
|
{
|
||||||
|
public class PersistedGrantStore : IPersistedGrantStore
|
||||||
|
{
|
||||||
|
private readonly IGrantRepository _grantRepository;
|
||||||
|
|
||||||
|
public PersistedGrantStore(
|
||||||
|
IGrantRepository grantRepository)
|
||||||
|
{
|
||||||
|
_grantRepository = grantRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)
|
||||||
|
{
|
||||||
|
var grants = await _grantRepository.GetManyAsync(subjectId);
|
||||||
|
var pGrants = grants.Select(g => ToPersistedGrant(g));
|
||||||
|
return pGrants;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PersistedGrant> GetAsync(string key)
|
||||||
|
{
|
||||||
|
var grant = await _grantRepository.GetByKeyAsync(key);
|
||||||
|
if(grant == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var pGrant = ToPersistedGrant(grant);
|
||||||
|
return pGrant;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RemoveAllAsync(string subjectId, string clientId)
|
||||||
|
{
|
||||||
|
await _grantRepository.DeleteAsync(subjectId, clientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RemoveAllAsync(string subjectId, string clientId, string type)
|
||||||
|
{
|
||||||
|
await _grantRepository.DeleteAsync(subjectId, clientId, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RemoveAsync(string key)
|
||||||
|
{
|
||||||
|
await _grantRepository.DeleteAsync(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task StoreAsync(PersistedGrant pGrant)
|
||||||
|
{
|
||||||
|
var grant = ToGrant(pGrant);
|
||||||
|
await _grantRepository.SaveAsync(grant);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Grant ToGrant(PersistedGrant pGrant)
|
||||||
|
{
|
||||||
|
return new Grant
|
||||||
|
{
|
||||||
|
Key = pGrant.Key,
|
||||||
|
Type = pGrant.Type,
|
||||||
|
SubjectId = pGrant.SubjectId,
|
||||||
|
ClientId = pGrant.ClientId,
|
||||||
|
CreationDate = pGrant.CreationTime,
|
||||||
|
ExpirationDate = pGrant.Expiration,
|
||||||
|
Data = pGrant.Data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private PersistedGrant ToPersistedGrant(Grant grant)
|
||||||
|
{
|
||||||
|
return new PersistedGrant
|
||||||
|
{
|
||||||
|
Key = grant.Key,
|
||||||
|
Type = grant.Type,
|
||||||
|
SubjectId = grant.SubjectId,
|
||||||
|
ClientId = grant.ClientId,
|
||||||
|
CreationTime = grant.CreationDate,
|
||||||
|
Expiration = grant.ExpirationDate,
|
||||||
|
Data = grant.Data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
src/Core/Repositories/IGrantRepository.cs
Normal file
17
src/Core/Repositories/IGrantRepository.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core.Domains;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Bit.Core.Repositories
|
||||||
|
{
|
||||||
|
public interface IGrantRepository
|
||||||
|
{
|
||||||
|
Task<Grant> GetByKeyAsync(string key);
|
||||||
|
Task<ICollection<Grant>> GetManyAsync(string subjectId);
|
||||||
|
Task SaveAsync(Grant obj);
|
||||||
|
Task DeleteAsync(string key);
|
||||||
|
Task DeleteAsync(string subjectId, string clientId);
|
||||||
|
Task DeleteAsync(string subjectId, string clientId, string type);
|
||||||
|
}
|
||||||
|
}
|
92
src/Core/Repositories/SqlServer/GrantRepository.cs
Normal file
92
src/Core/Repositories/SqlServer/GrantRepository.cs
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core.Domains;
|
||||||
|
using Dapper;
|
||||||
|
|
||||||
|
namespace Bit.Core.Repositories.SqlServer
|
||||||
|
{
|
||||||
|
public class GrantRepository : BaseRepository, IGrantRepository
|
||||||
|
{
|
||||||
|
public GrantRepository(GlobalSettings globalSettings)
|
||||||
|
: this(globalSettings.SqlServer.ConnectionString)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public GrantRepository(string connectionString)
|
||||||
|
: base(connectionString)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public async Task<Grant> GetByKeyAsync(string key)
|
||||||
|
{
|
||||||
|
using(var connection = new SqlConnection(ConnectionString))
|
||||||
|
{
|
||||||
|
var results = await connection.QueryAsync<Grant>(
|
||||||
|
"[dbo].[Grant_ReadByKey]",
|
||||||
|
new { Key = key },
|
||||||
|
commandType: CommandType.StoredProcedure);
|
||||||
|
|
||||||
|
return results.SingleOrDefault();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ICollection<Grant>> GetManyAsync(string subjectId)
|
||||||
|
{
|
||||||
|
using(var connection = new SqlConnection(ConnectionString))
|
||||||
|
{
|
||||||
|
var results = await connection.QueryAsync<Grant>(
|
||||||
|
"[dbo].[Grant_ReadBySubjectId]",
|
||||||
|
new { SubjectId = subjectId },
|
||||||
|
commandType: CommandType.StoredProcedure);
|
||||||
|
|
||||||
|
return results.ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SaveAsync(Grant obj)
|
||||||
|
{
|
||||||
|
using(var connection = new SqlConnection(ConnectionString))
|
||||||
|
{
|
||||||
|
var results = await connection.ExecuteAsync(
|
||||||
|
"[dbo].[Grant_Save]",
|
||||||
|
obj,
|
||||||
|
commandType: CommandType.StoredProcedure);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAsync(string key)
|
||||||
|
{
|
||||||
|
using(var connection = new SqlConnection(ConnectionString))
|
||||||
|
{
|
||||||
|
await connection.ExecuteAsync(
|
||||||
|
"[dbo].[Grant_DeleteByKey]",
|
||||||
|
new { Key = key },
|
||||||
|
commandType: CommandType.StoredProcedure);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAsync(string subjectId, string clientId)
|
||||||
|
{
|
||||||
|
using(var connection = new SqlConnection(ConnectionString))
|
||||||
|
{
|
||||||
|
await connection.ExecuteAsync(
|
||||||
|
"[dbo].[Grant_DeleteBySubjectIdClientId]",
|
||||||
|
new { SubjectId = subjectId, ClientId = clientId },
|
||||||
|
commandType: CommandType.StoredProcedure);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAsync(string subjectId, string clientId, string type)
|
||||||
|
{
|
||||||
|
using(var connection = new SqlConnection(ConnectionString))
|
||||||
|
{
|
||||||
|
await connection.ExecuteAsync(
|
||||||
|
"[dbo].[Grant_DeleteBySubjectIdClientIdType]",
|
||||||
|
new { SubjectId = subjectId, ClientId = clientId, Type = type },
|
||||||
|
commandType: CommandType.StoredProcedure);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user