mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-05 05:17:54 +00:00
[WIP] feat: scoped api keys
This commit is contained in:
parent
e7ee593a85
commit
06cb160f95
45 changed files with 1264 additions and 154 deletions
55
PluralKit.Core/Database/Repository/ModelRepository.ApiKey.cs
Normal file
55
PluralKit.Core/Database/Repository/ModelRepository.ApiKey.cs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
using Dapper;
|
||||
|
||||
using SqlKata;
|
||||
|
||||
namespace PluralKit.Core;
|
||||
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public async Task<PKApiKey?> GetApiKey(Guid id)
|
||||
{
|
||||
var query = new Query("api_keys")
|
||||
.Select("id", "system", "scopes", "app", "name", "created")
|
||||
.SelectRaw("[kind]::text")
|
||||
.Where("id", id);
|
||||
|
||||
return await _db.QueryFirst<PKApiKey?>(query);
|
||||
}
|
||||
|
||||
public async Task<PKApiKey?> GetApiKeyByName(SystemId system, string name)
|
||||
{
|
||||
var query = new Query("api_keys")
|
||||
.Select("id", "system", "scopes", "app", "name", "created")
|
||||
.SelectRaw("[kind]::text")
|
||||
.Where("system", system)
|
||||
.WhereRaw("lower(name) = lower(?)", name.ToLower());
|
||||
|
||||
return await _db.QueryFirst<PKApiKey?>(query);
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<PKApiKey> GetSystemApiKeys(SystemId system)
|
||||
{
|
||||
var query = new Query("api_keys")
|
||||
.Select("id", "system", "scopes", "app", "name", "created")
|
||||
.SelectRaw("[kind]::text")
|
||||
.Where("system", system)
|
||||
.WhereRaw("[kind]::text not in ( 'dashboard' )")
|
||||
.OrderByDesc("created");
|
||||
|
||||
return _db.QueryStream<PKApiKey>(query);
|
||||
}
|
||||
|
||||
public async Task UpdateApiKey(Guid id, ApiKeyPatch patch)
|
||||
{
|
||||
_logger.Information("Updated API key {keyId}: {@ApiKeyPatch}", id, patch);
|
||||
var query = patch.Apply(new Query("api_keys").Where("id", id));
|
||||
await _db.ExecuteQuery(query, "returning *");
|
||||
}
|
||||
|
||||
public async Task DeleteApiKey(Guid id)
|
||||
{
|
||||
var query = new Query("api_keys").AsDelete().Where("id", id);
|
||||
await _db.ExecuteQuery(query);
|
||||
_logger.Information("Deleted ApiKey {keyId}", id);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue