diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-11-18 11:18:44 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-18 11:18:44 -0600 |
| commit | 43c3a052796fbd490677de931c2b6925efa0fa5e (patch) | |
| tree | 79d4b6a810d1fe591baafc720d5dd0c0cfb14b4c /src/slskd/Search/API/Controllers/SearchesController.cs | |
| parent | f959a86d865eee5a8a1f353afdc34560067ba544 (diff) | |
| parent | 7b129b69211909a3af9bbeddf0acc1dc3d84e20c (diff) | |
| download | slskd-43c3a052796fbd490677de931c2b6925efa0fa5e.tar.gz | |
Merge pull request #757 from slskd/agent-apis
Forbid server API calls in Agent mode
Diffstat (limited to 'src/slskd/Search/API/Controllers/SearchesController.cs')
| -rw-r--r-- | src/slskd/Search/API/Controllers/SearchesController.cs | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index aadf01b2..ae7f42db 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -15,12 +15,15 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // </copyright> +using Microsoft.Extensions.Options; + namespace slskd.Search.API { using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using slskd.Relay; using SearchQuery = Soulseek.SearchQuery; using SearchScope = Soulseek.SearchScope; @@ -38,12 +41,16 @@ namespace slskd.Search.API /// Initializes a new instance of the <see cref="SearchesController"/> class. /// </summary> /// <param name="searchService"></param> - public SearchesController(ISearchService searchService) + /// <param name="optionsSnapshot"></param> + public SearchesController(ISearchService searchService, IOptionsSnapshot<Options> optionsSnapshot) { Searches = searchService; + OptionsSnapshot = optionsSnapshot; } private ISearchService Searches { get; } + private IOptionsSnapshot<Options> OptionsSnapshot { get; } + private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum<OperationMode>() == OperationMode.Agent; /// <summary> /// Performs a search for the specified <paramref name="request"/>. @@ -57,6 +64,11 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task<IActionResult> Post([FromBody] SearchRequest request) { + if (IsAgent) + { + return Forbid(); + } + if (string.IsNullOrWhiteSpace(request.SearchText)) { return BadRequest("SearchText may not be null or empty"); @@ -94,6 +106,11 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task<IActionResult> GetById([FromRoute] Guid id, [FromQuery] bool includeResponses = false) { + if (IsAgent) + { + return Forbid(); + } + var search = await Searches.FindAsync(search => search.Id == id, includeResponses); if (search == default) @@ -115,6 +132,11 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task<IActionResult> GetResponsesById([FromRoute] Guid id) { + if (IsAgent) + { + return Forbid(); + } + var search = await Searches.FindAsync(search => search.Id == id, includeResponses: true); if (search == default) @@ -133,6 +155,11 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task<IActionResult> GetAll() { + if (IsAgent) + { + return Forbid(); + } + var searches = await Searches.ListAsync(); return Ok(searches); } @@ -150,6 +177,11 @@ namespace slskd.Search.API [ProducesResponseType(304)] public async Task<IActionResult> Cancel([FromRoute] Guid id) { + if (IsAgent) + { + return Forbid(); + } + var search = await Searches.FindAsync(search => search.Id == id); if (search == default) @@ -178,6 +210,11 @@ namespace slskd.Search.API [ProducesResponseType(404)] public async Task<IActionResult> Delete([FromRoute] Guid id) { + if (IsAgent) + { + return Forbid(); + } + var search = await Searches.FindAsync(search => search.Id == id, includeResponses: true); if (search == default) |
