Skip to content

Commit

Permalink
feat: allow for X-API-TOKEN authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Oct 23, 2023
1 parent c79c548 commit 56cd6bd
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 91 deletions.
32 changes: 16 additions & 16 deletions App/Modules/Cyan/API/V1/Controllers/PluginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ public async Task<ActionResult<IEnumerable<PluginPrincipalResp>>> Search([FromQu
}


[HttpGet("{id:guid}")]
public async Task<ActionResult<PluginResp>> Get(Guid id)
[HttpGet("id/{userId}/{pluginId:guid}")]
public async Task<ActionResult<PluginResp>> Get(string userId, Guid pluginId)
{
var plugin = await this._service.Get(id)
var plugin = await this._service.Get(userId, pluginId)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(plugin,
new EntityNotFound("Plugin not found", typeof(PluginPrincipal), id.ToString()));
new EntityNotFound("Plugin not found", typeof(PluginPrincipal), pluginId.ToString()));
}

[HttpGet("{username}/{name}")]
[HttpGet("slug/{username}/{name}")]
public async Task<ActionResult<PluginResp>> Get(string username, string name)
{
var plugin = await this._service.Get(username, name)
Expand All @@ -82,7 +82,7 @@ public async Task<ActionResult<PluginResp>> Get(string username, string name)
new EntityNotFound("Plugin not found", typeof(PluginPrincipal), $"{username}/{name}"));
}

[Authorize, HttpPost("{userId}")]
[Authorize, HttpPost("id/{userId}")]
public async Task<ActionResult<PluginPrincipalResp>> Create(string userId, [FromBody] CreatePluginReq req)
{
var sub = this.Sub();
Expand All @@ -101,7 +101,7 @@ public async Task<ActionResult<PluginPrincipalResp>> Create(string userId, [From
}


[Authorize, HttpPut("{userId}/{pluginId}")]
[Authorize, HttpPut("id/{userId}/{pluginId}")]
public async Task<ActionResult<PluginPrincipalResp>> Update(string userId, Guid pluginId,
[FromBody] UpdatePluginReq req)
{
Expand All @@ -120,7 +120,7 @@ public async Task<ActionResult<PluginPrincipalResp>> Update(string userId, Guid
return this.ReturnNullableResult(plugin, new EntityNotFound("Plugin not found", typeof(PluginPrincipal), pluginId.ToString()));
}

[Authorize, HttpPost("{username}/{pluginName}/like/{likerId}/{like}")]
[Authorize, HttpPost("slug/{username}/{pluginName}/like/{likerId}/{like}")]
public async Task<ActionResult<Unit>> Like(string username, string pluginName, string likerId, bool like)
{
var sub = this.Sub();
Expand All @@ -137,7 +137,7 @@ public async Task<ActionResult<Unit>> Like(string username, string pluginName, s
new EntityNotFound("Plugin not found", typeof(PluginPrincipal), $"{username}/{pluginName}"));
}

[Authorize(Policy = AuthPolicies.OnlyAdmin), HttpDelete("{userId}/{pluginId:guid}")]
[Authorize(Policy = AuthPolicies.OnlyAdmin), HttpDelete("id/{userId}/{pluginId:guid}")]
public async Task<ActionResult<Unit>> Delete(string userId, Guid pluginId)
{
var plugin = await this._service.Delete(userId, pluginId)
Expand All @@ -146,7 +146,7 @@ public async Task<ActionResult<Unit>> Delete(string userId, Guid pluginId)
new EntityNotFound("Plugin not found", typeof(PluginPrincipal), $"{userId}/{pluginId}"));
}

[HttpGet("{username}/{pluginName}/versions")]
[HttpGet("slug/{username}/{pluginName}/versions")]
public async Task<ActionResult<IEnumerable<PluginVersionPrincipalResp>>> SearchVersion(string username,
string pluginName, [FromQuery] SearchPluginVersionQuery query)
{
Expand All @@ -158,7 +158,7 @@ public async Task<ActionResult<IEnumerable<PluginVersionPrincipalResp>>> SearchV
return this.ReturnResult(plugins);
}

[HttpGet("{userId}/{pluginId:guid}/versions")]
[HttpGet("id/{userId}/{pluginId:guid}/versions")]
public async Task<ActionResult<IEnumerable<PluginVersionPrincipalResp>>> SearchVersion(string userId, Guid pluginId,
[FromQuery] SearchPluginVersionQuery query)
{
Expand All @@ -170,7 +170,7 @@ public async Task<ActionResult<IEnumerable<PluginVersionPrincipalResp>>> SearchV
return this.ReturnResult(plugins);
}

[HttpGet("{username}/{pluginName}/versions/{ver}")]
[HttpGet("slug/{username}/{pluginName}/versions/{ver}")]
public async Task<ActionResult<PluginVersionPrincipalResp>> GetVersion(string username, string pluginName, ulong ver,
bool bumpDownload)
{
Expand All @@ -180,7 +180,7 @@ public async Task<ActionResult<PluginVersionPrincipalResp>> GetVersion(string us
new EntityNotFound("Plugin not found", typeof(PluginVersionPrincipal), $"{username}/{pluginName}:{ver}"));
}

[HttpGet("{userId}/{pluginId:guid}/versions/{ver}")]
[HttpGet("id/{userId}/{pluginId:guid}/versions/{ver}")]
public async Task<ActionResult<PluginVersionPrincipalResp>> GetVersion(string userId, Guid pluginId, ulong ver)
{
var plugin = await this._service.GetVersion(userId, pluginId, ver)
Expand All @@ -189,7 +189,7 @@ public async Task<ActionResult<PluginVersionPrincipalResp>> GetVersion(string us
new EntityNotFound("Plugin not found", typeof(PluginVersionPrincipal), $"{userId}/{pluginId}:{ver}"));
}

[Authorize, HttpPost("{username}/{pluginName}/versions")]
[Authorize, HttpPost("slug/{username}/{pluginName}/versions")]
public async Task<ActionResult<PluginVersionPrincipalResp>> CreateVersion(string username, string pluginName,
[FromBody] CreatePluginVersionReq req)
{
Expand All @@ -214,7 +214,7 @@ public async Task<ActionResult<PluginVersionPrincipalResp>> CreateVersion(string
new EntityNotFound("Plugin not found", typeof(PluginPrincipal), $"{username}/{pluginName}"));
}

[Authorize, HttpPost("{userId}/{pluginId:guid}/versions")]
[Authorize, HttpPost("id/{userId}/{pluginId:guid}/versions")]
public async Task<ActionResult<PluginVersionPrincipalResp>> CreateVersion(string userId, Guid pluginId,
[FromBody] CreatePluginVersionReq req)
{
Expand All @@ -235,7 +235,7 @@ public async Task<ActionResult<PluginVersionPrincipalResp>> CreateVersion(string
new EntityNotFound("Plugin not found", typeof(PluginPrincipal), $"{userId}/{pluginId}"));
}

[Authorize, HttpPut("{userId}/{pluginId:guid}/versions/{ver}")]
[Authorize, HttpPut("id/{userId}/{pluginId:guid}/versions/{ver}")]
public async Task<ActionResult<PluginVersionPrincipalResp>> UpdateVersion(string userId, Guid pluginId, ulong ver,
[FromBody] UpdatePluginVersionReq req)
{
Expand Down
32 changes: 16 additions & 16 deletions App/Modules/Cyan/API/V1/Controllers/ProcessorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ public async Task<ActionResult<IEnumerable<ProcessorPrincipalResp>>> Search([Fro
return this.ReturnResult(processors);
}

[HttpGet("{id:guid}")]
public async Task<ActionResult<ProcessorResp>> Get(Guid id)
[HttpGet("id/{userId}/{processorId:guid}")]
public async Task<ActionResult<ProcessorResp>> Get(string userId, Guid processorId)
{
var processor = await this._service.Get(id)
var processor = await this._service.Get(userId, processorId)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(processor,
new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), id.ToString()));
new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), processorId.ToString()));
}

[HttpGet("{username}/{name}")]
[HttpGet("slug/{username}/{name}")]
public async Task<ActionResult<ProcessorResp>> Get(string username, string name)
{
var processor = await this._service.Get(username, name)
Expand All @@ -81,7 +81,7 @@ public async Task<ActionResult<ProcessorResp>> Get(string username, string name)
new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), $"{username}/{name}"));
}

[Authorize, HttpPost("{userId}")]
[Authorize, HttpPost("id/{userId}")]
public async Task<ActionResult<ProcessorPrincipalResp>> Create(string userId, [FromBody] CreateProcessorReq req)
{
var sub = this.Sub();
Expand All @@ -100,7 +100,7 @@ public async Task<ActionResult<ProcessorPrincipalResp>> Create(string userId, [F
}


[Authorize, HttpPut("{userId}/{processorId}")]
[Authorize, HttpPut("id/{userId}/{processorId}")]
public async Task<ActionResult<ProcessorPrincipalResp>> Update(string userId, Guid processorId,
[FromBody] UpdateProcessorReq req)
{
Expand All @@ -119,7 +119,7 @@ public async Task<ActionResult<ProcessorPrincipalResp>> Update(string userId, Gu
return this.ReturnNullableResult(processor, new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), processorId.ToString()));
}

[Authorize, HttpPost("{username}/{processorName}/like/{likerId}/{like}")]
[Authorize, HttpPost("slug/{username}/{processorName}/like/{likerId}/{like}")]
public async Task<ActionResult<Unit>> Like(string username, string processorName, string likerId, bool like)
{
var sub = this.Sub();
Expand All @@ -136,7 +136,7 @@ public async Task<ActionResult<Unit>> Like(string username, string processorName
new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), $"{username}/{processorName}"));
}

[Authorize(Policy = AuthPolicies.OnlyAdmin), HttpDelete("{userId}/{processorId:guid}")]
[Authorize(Policy = AuthPolicies.OnlyAdmin), HttpDelete("id/{userId}/{processorId:guid}")]
public async Task<ActionResult<Unit>> Delete(string userId, Guid processorId)
{
var processor = await this._service.Delete(userId, processorId)
Expand All @@ -145,7 +145,7 @@ public async Task<ActionResult<Unit>> Delete(string userId, Guid processorId)
new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), $"{userId}/{processorId}"));
}

[HttpGet("{username}/{processorName}/versions")]
[HttpGet("slug/{username}/{processorName}/versions")]
public async Task<ActionResult<IEnumerable<ProcessorVersionPrincipalResp>>> SearchVersion(string username,
string processorName, [FromQuery] SearchProcessorVersionQuery query)
{
Expand All @@ -157,7 +157,7 @@ public async Task<ActionResult<IEnumerable<ProcessorVersionPrincipalResp>>> Sear
return this.ReturnResult(processors);
}

[HttpGet("{userId}/{processorId:guid}/versions")]
[HttpGet("id/{userId}/{processorId:guid}/versions")]
public async Task<ActionResult<IEnumerable<ProcessorVersionPrincipalResp>>> SearchVersion(string userId, Guid processorId,
[FromQuery] SearchProcessorVersionQuery query)
{
Expand All @@ -169,7 +169,7 @@ public async Task<ActionResult<IEnumerable<ProcessorVersionPrincipalResp>>> Sear
return this.ReturnResult(processors);
}

[HttpGet("{username}/{processorName}/versions/{ver}")]
[HttpGet("slug/{username}/{processorName}/versions/{ver}")]
public async Task<ActionResult<ProcessorVersionPrincipalResp>> GetVersion(string username, string processorName, ulong ver,
bool bumpDownload)
{
Expand All @@ -179,7 +179,7 @@ public async Task<ActionResult<ProcessorVersionPrincipalResp>> GetVersion(string
new EntityNotFound("Processor not found", typeof(ProcessorVersionPrincipal), $"{username}/{processorName}:{ver}"));
}

[HttpGet("{userId}/{processorId:guid}/versions/{ver}")]
[HttpGet("id/{userId}/{processorId:guid}/versions/{ver}")]
public async Task<ActionResult<ProcessorVersionPrincipalResp>> GetVersion(string userId, Guid processorId, ulong ver)
{
var processor = await this._service.GetVersion(userId, processorId, ver)
Expand All @@ -188,7 +188,7 @@ public async Task<ActionResult<ProcessorVersionPrincipalResp>> GetVersion(string
new EntityNotFound("Processor not found", typeof(ProcessorVersionPrincipal), $"{userId}/{processorId}:{ver}"));
}

[Authorize, HttpPost("{username}/{processorName}/versions")]
[Authorize, HttpPost("slug/{username}/{processorName}/versions")]
public async Task<ActionResult<ProcessorVersionPrincipalResp>> CreateVersion(string username, string processorName,
[FromBody] CreateProcessorVersionReq req)
{
Expand All @@ -213,7 +213,7 @@ public async Task<ActionResult<ProcessorVersionPrincipalResp>> CreateVersion(str
new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), $"{username}/{processorName}"));
}

[Authorize, HttpPost("{userId}/{processorId:guid}/versions")]
[Authorize, HttpPost("id/{userId}/{processorId:guid}/versions")]
public async Task<ActionResult<ProcessorVersionPrincipalResp>> CreateVersion(string userId, Guid processorId,
[FromBody] CreateProcessorVersionReq req)
{
Expand All @@ -234,7 +234,7 @@ public async Task<ActionResult<ProcessorVersionPrincipalResp>> CreateVersion(str
new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), $"{userId}/{processorId}"));
}

[Authorize, HttpPut("{userId}/{processorId:guid}/versions/{ver}")]
[Authorize, HttpPut("id/{userId}/{processorId:guid}/versions/{ver}")]
public async Task<ActionResult<ProcessorVersionPrincipalResp>> UpdateVersion(string userId, Guid processorId, ulong ver,
[FromBody] UpdateProcessorVersionReq req)
{
Expand Down
32 changes: 16 additions & 16 deletions App/Modules/Cyan/API/V1/Controllers/TemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ public async Task<ActionResult<IEnumerable<TemplatePrincipalResp>>> Search([From
return this.ReturnResult(templates);
}

[HttpGet("{id:guid}")]
public async Task<ActionResult<TemplateResp>> Get(Guid id)
[HttpGet("id/{userId}/{templateId:guid}")]
public async Task<ActionResult<TemplateResp>> Get(string userId, Guid templateId)
{
var template = await this._service.Get(id)
var template = await this._service.Get(userId, templateId)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(template,
new EntityNotFound("Template not found", typeof(TemplatePrincipal), id.ToString()));
new EntityNotFound("Template not found", typeof(TemplatePrincipal), templateId.ToString()));
}

[HttpGet("{username}/{name}")]
[HttpGet("slug/{username}/{name}")]
public async Task<ActionResult<TemplateResp>> Get(string username, string name)
{
var template = await this._service.Get(username, name)
Expand All @@ -81,7 +81,7 @@ public async Task<ActionResult<TemplateResp>> Get(string username, string name)
new EntityNotFound("Template not found", typeof(TemplatePrincipal), $"{username}/{name}"));
}

[Authorize, HttpPost("{userId}")]
[Authorize, HttpPost("id/{userId}")]
public async Task<ActionResult<TemplatePrincipalResp>> Create(string userId, [FromBody] CreateTemplateReq req)
{
var sub = this.Sub();
Expand All @@ -100,7 +100,7 @@ public async Task<ActionResult<TemplatePrincipalResp>> Create(string userId, [Fr
}


[Authorize, HttpPut("{userId}/{templateId:guid}")]
[Authorize, HttpPut("id/{userId}/{templateId:guid}")]
public async Task<ActionResult<TemplatePrincipalResp>> Update(string userId, Guid templateId,
[FromBody] UpdateTemplateReq req)
{
Expand All @@ -120,7 +120,7 @@ public async Task<ActionResult<TemplatePrincipalResp>> Update(string userId, Gui
new EntityNotFound("Template not found", typeof(TemplatePrincipal), templateId.ToString()));
}

[Authorize, HttpPost("{username}/{templateName}/like/{likerId}/{like:bool}")]
[Authorize, HttpPost("slug/{username}/{templateName}/like/{likerId}/{like:bool}")]
public async Task<ActionResult<Unit>> Like(string username, string templateName, string likerId, bool like)
{
var sub = this.Sub();
Expand All @@ -137,7 +137,7 @@ public async Task<ActionResult<Unit>> Like(string username, string templateName,
new EntityNotFound("Template not found", typeof(TemplatePrincipal), $"{username}/{templateName}"));
}

[Authorize(Policy = AuthPolicies.OnlyAdmin), HttpDelete("{userId}/{templateId:guid}")]
[Authorize(Policy = AuthPolicies.OnlyAdmin), HttpDelete("id/{userId}/{templateId:guid}")]
public async Task<ActionResult<Unit>> Delete(string userId, Guid templateId)
{
var template = await this._service.Delete(userId, templateId)
Expand All @@ -146,7 +146,7 @@ public async Task<ActionResult<Unit>> Delete(string userId, Guid templateId)
new EntityNotFound("Template not found", typeof(TemplatePrincipal), $"{userId}/{templateId}"));
}

[HttpGet("{username}/{templateName}/versions")]
[HttpGet("slug/{username}/{templateName}/versions")]
public async Task<ActionResult<IEnumerable<TemplateVersionPrincipalResp>>> SearchVersion(string username,
string templateName, [FromQuery] SearchTemplateVersionQuery query)
{
Expand All @@ -158,7 +158,7 @@ public async Task<ActionResult<IEnumerable<TemplateVersionPrincipalResp>>> Searc
return this.ReturnResult(templates);
}

[HttpGet("{userId}/{templateId:guid}/versions")]
[HttpGet("id/{userId}/{templateId:guid}/versions")]
public async Task<ActionResult<IEnumerable<TemplateVersionPrincipalResp>>> SearchVersion(string userId,
Guid templateId,
[FromQuery] SearchTemplateVersionQuery query)
Expand All @@ -171,7 +171,7 @@ public async Task<ActionResult<IEnumerable<TemplateVersionPrincipalResp>>> Searc
return this.ReturnResult(templates);
}

[HttpGet("{username}/{templateName}/versions/{ver}")]
[HttpGet("slug/{username}/{templateName}/versions/{ver}")]
public async Task<ActionResult<TemplateVersionResp>> GetVersion(string username, string templateName,
ulong ver,
bool bumpDownload)
Expand All @@ -182,7 +182,7 @@ public async Task<ActionResult<TemplateVersionResp>> GetVersion(string username,
new EntityNotFound("Template not found", typeof(TemplateVersionResp), $"{username}/{templateName}:{ver}"));
}

[HttpGet("{userId}/{templateId:guid}/versions/{ver}")]
[HttpGet("id/{userId}/{templateId:guid}/versions/{ver}")]
public async Task<ActionResult<TemplateVersionResp>> GetVersion(string userId, Guid templateId, ulong ver)
{
var template = await this._service.GetVersion(userId, templateId, ver)
Expand All @@ -191,7 +191,7 @@ public async Task<ActionResult<TemplateVersionResp>> GetVersion(string userId, G
new EntityNotFound("Template not found", typeof(TemplateVersionPrincipal), $"{userId}/{templateId}:{ver}"));
}

[Authorize, HttpPost("{username}/{templateName}/versions")]
[Authorize, HttpPost("slug/{username}/{templateName}/versions")]
public async Task<ActionResult<TemplateVersionPrincipalResp>> CreateVersion(string username, string templateName,
[FromBody] CreateTemplateVersionReq req)
{
Expand All @@ -218,7 +218,7 @@ public async Task<ActionResult<TemplateVersionPrincipalResp>> CreateVersion(stri
new EntityNotFound("Template not found", typeof(TemplatePrincipal), $"{username}/{templateName}"));
}

[Authorize, HttpPost("{userId}/{templateId:guid}/versions")]
[Authorize, HttpPost("id/{userId}/{templateId:guid}/versions")]
public async Task<ActionResult<TemplateVersionPrincipalResp>> CreateVersion(string userId, Guid templateId,
[FromBody] CreateTemplateVersionReq req)
{
Expand All @@ -243,7 +243,7 @@ public async Task<ActionResult<TemplateVersionPrincipalResp>> CreateVersion(stri
new EntityNotFound("Template not found", typeof(TemplatePrincipal), $"{userId}/{templateId}"));
}

[Authorize, HttpPut("{userId}/{templateId:guid}/versions/{ver}")]
[Authorize, HttpPut("id/{userId}/{templateId:guid}/versions/{ver}")]
public async Task<ActionResult<TemplateVersionPrincipalResp>> UpdateVersion(string userId, Guid templateId, ulong ver,
[FromBody] UpdateTemplateVersionReq req)
{
Expand Down
6 changes: 3 additions & 3 deletions App/Modules/Cyan/Data/Repositories/PluginRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public async Task<Result<IEnumerable<PluginPrincipal>>> Search(PluginSearch sear
}
}

public async Task<Result<Plugin?>> Get(Guid id)
public async Task<Result<Plugin?>> Get(string userId, Guid id)
{
try
{
this._logger.LogInformation("Getting plugin with '{ID}'", id);
var plugin = await this._db.Plugins
.Where(x => x.Id == id)
.Include(x => x.Likes)
.Include(x => x.User)
.Where(x => x.Id == id && x.UserId == userId)
.Include(x => x.Likes)
.Include(x => x.Versions)
.ThenInclude(x => x.Templates)
.FirstOrDefaultAsync();
Expand Down
4 changes: 2 additions & 2 deletions App/Modules/Cyan/Data/Repositories/ProcessorRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public async Task<Result<IEnumerable<ProcessorPrincipal>>> Search(ProcessorSearc
}
}

public async Task<Result<Processor?>> Get(Guid id)
public async Task<Result<Processor?>> Get(string userId, Guid id)
{
try
{
this._logger.LogInformation("Getting processor with '{ID}'", id);
var processor = await this._db.Processors
.Where(x => x.Id == id)
.Where(x => x.Id == id && x.UserId == userId)
.Include(x => x.Likes)
.Include(x => x.User)
.Include(x => x.Versions)
Expand Down
Loading

0 comments on commit 56cd6bd

Please sign in to comment.