From cd6874b3dd54c6b3e501439d308d591e4b2aada5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 5 Oct 2022 18:30:31 -0500 Subject: add auth policies --- src/slskd/Common/Authentication/AuthPolicy.cs | 28 ++++++++++++++++ .../Core/API/Controllers/ApplicationController.cs | 14 ++++---- src/slskd/Core/API/Controllers/LogsController.cs | 2 +- .../Core/API/Controllers/MetricsController.cs | 2 +- .../Core/API/Controllers/OptionsController.cs | 14 ++++---- src/slskd/Core/API/Controllers/ServerController.cs | 6 ++-- .../Core/API/Controllers/SessionController.cs | 2 +- src/slskd/Core/API/Hubs/ApplicationHub.cs | 2 +- src/slskd/Core/API/Hubs/LogsHub.cs | 2 +- .../API/Controllers/ConversationsController.cs | 12 +++---- .../API/Controllers/PublicChatController.cs | 4 +-- .../Messaging/API/Controllers/RoomsController.cs | 20 +++++------ src/slskd/Program.cs | 39 +++++++++++++++++----- .../Search/API/Controllers/SearchesController.cs | 12 +++---- src/slskd/Search/API/Hubs/SearchHub.cs | 2 +- .../Shares/API/Controllers/SharesController.cs | 12 +++---- .../API/Controllers/TransfersController.cs | 20 +++++------ src/slskd/Users/API/Controllers/UsersController.cs | 10 +++--- 18 files changed, 127 insertions(+), 76 deletions(-) create mode 100644 src/slskd/Common/Authentication/AuthPolicy.cs (limited to 'src') diff --git a/src/slskd/Common/Authentication/AuthPolicy.cs b/src/slskd/Common/Authentication/AuthPolicy.cs new file mode 100644 index 00000000..e355b2e6 --- /dev/null +++ b/src/slskd/Common/Authentication/AuthPolicy.cs @@ -0,0 +1,28 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + /// + /// Authentication policies. + /// + public static class AuthPolicy + { + public const string JwtOnly = "Jwt"; + public const string Any = "Any"; + } +} diff --git a/src/slskd/Core/API/Controllers/ApplicationController.cs b/src/slskd/Core/API/Controllers/ApplicationController.cs index 7fb914e9..c09b5513 100644 --- a/src/slskd/Core/API/Controllers/ApplicationController.cs +++ b/src/slskd/Core/API/Controllers/ApplicationController.cs @@ -58,7 +58,7 @@ namespace slskd.Core.API /// /// [HttpGet] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public IActionResult State() { return Ok(ApplicationStateMonitor.CurrentValue); @@ -69,7 +69,7 @@ namespace slskd.Core.API /// /// [HttpDelete] - [Authorize] + [Authorize(Policy = AuthPolicy.JwtOnly)] public IActionResult Shutdown() { Program.MasterCancellationTokenSource.Cancel(); @@ -89,7 +89,7 @@ namespace slskd.Core.API /// /// [HttpPut] - [Authorize] + [Authorize(Policy = AuthPolicy.JwtOnly)] public IActionResult Restart() { Program.MasterCancellationTokenSource.Cancel(); @@ -104,7 +104,7 @@ namespace slskd.Core.API /// /// [HttpGet("version")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public IActionResult GetVersion() { return Ok(Program.SemanticVersion); @@ -115,7 +115,7 @@ namespace slskd.Core.API /// /// [HttpGet("version/latest")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public async Task CheckVersion([FromQuery] bool forceCheck = false) { if (forceCheck) @@ -131,7 +131,7 @@ namespace slskd.Core.API /// /// [HttpPost("gc")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public IActionResult CollectGarbage() { Application.CollectGarbage(); @@ -140,7 +140,7 @@ namespace slskd.Core.API } [HttpGet("dump")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public async Task DumpMemory() { using var dumper = new Dumper(); diff --git a/src/slskd/Core/API/Controllers/LogsController.cs b/src/slskd/Core/API/Controllers/LogsController.cs index 57366867..2beaf279 100644 --- a/src/slskd/Core/API/Controllers/LogsController.cs +++ b/src/slskd/Core/API/Controllers/LogsController.cs @@ -35,7 +35,7 @@ namespace slskd.Core.API /// /// [HttpGet] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public IActionResult Logs() { return Ok(Program.LogBuffer); diff --git a/src/slskd/Core/API/Controllers/MetricsController.cs b/src/slskd/Core/API/Controllers/MetricsController.cs index da2707c1..886f1f05 100644 --- a/src/slskd/Core/API/Controllers/MetricsController.cs +++ b/src/slskd/Core/API/Controllers/MetricsController.cs @@ -36,7 +36,7 @@ namespace slskd.Core.API /// /// [HttpGet] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public async Task Get() { var response = await Metrics.BuildAsync(); diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index b0856ed3..f621b37a 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -56,7 +56,7 @@ namespace slskd.Core.API /// /// [HttpGet] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Options), 200)] public IActionResult Current() { @@ -69,7 +69,7 @@ namespace slskd.Core.API /// [HttpGet] [Route("startup")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Options), 200)] public IActionResult Startup() { @@ -82,7 +82,7 @@ namespace slskd.Core.API /// [HttpGet] [Route("debug")] - [Authorize] + [Authorize(Policy = AuthPolicy.JwtOnly)] [ProducesResponseType(typeof(string), 200)] public IActionResult Debug() { @@ -95,7 +95,7 @@ namespace slskd.Core.API } [HttpGet] - [Authorize] + [Authorize(Policy = AuthPolicy.JwtOnly)] [Route("yaml/location")] public IActionResult GetYamlFileLocation() { @@ -103,7 +103,7 @@ namespace slskd.Core.API } [HttpGet] - [Authorize] + [Authorize(Policy = AuthPolicy.JwtOnly)] [Route("yaml")] public IActionResult GetYamlFile() { @@ -117,7 +117,7 @@ namespace slskd.Core.API } [HttpPost] - [Authorize] + [Authorize(Policy = AuthPolicy.JwtOnly)] [Route("yaml")] public IActionResult UpdateYamlFile([FromBody] string yaml) { @@ -144,7 +144,7 @@ namespace slskd.Core.API } [HttpPost] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [Route("yaml/validate")] public IActionResult ValidateYamlFile([FromBody] string yaml) { diff --git a/src/slskd/Core/API/Controllers/ServerController.cs b/src/slskd/Core/API/Controllers/ServerController.cs index 172333ec..a725c978 100644 --- a/src/slskd/Core/API/Controllers/ServerController.cs +++ b/src/slskd/Core/API/Controllers/ServerController.cs @@ -54,7 +54,7 @@ namespace slskd.Core.API /// [HttpPut] [Route("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] [ProducesResponseType(403)] public async Task Connect() @@ -74,7 +74,7 @@ namespace slskd.Core.API /// [HttpDelete] [Route("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(204)] [ProducesResponseType(403)] public IActionResult Disconnect([FromBody] string message) @@ -94,7 +94,7 @@ namespace slskd.Core.API /// [HttpGet] [Route("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(ServerState), 200)] [ProducesResponseType(403)] public IActionResult Get() diff --git a/src/slskd/Core/API/Controllers/SessionController.cs b/src/slskd/Core/API/Controllers/SessionController.cs index 5f20aa9f..65a43e72 100644 --- a/src/slskd/Core/API/Controllers/SessionController.cs +++ b/src/slskd/Core/API/Controllers/SessionController.cs @@ -61,7 +61,7 @@ namespace slskd.Core.API /// The authentication is is invalid. [HttpGet] [Route("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] [ProducesResponseType(401)] public IActionResult Check() diff --git a/src/slskd/Core/API/Hubs/ApplicationHub.cs b/src/slskd/Core/API/Hubs/ApplicationHub.cs index 9861d0f9..063dfd2b 100644 --- a/src/slskd/Core/API/Hubs/ApplicationHub.cs +++ b/src/slskd/Core/API/Hubs/ApplicationHub.cs @@ -60,7 +60,7 @@ namespace slskd.Core.API /// /// The application SignalR hub. /// - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public class ApplicationHub : Hub { public ApplicationHub( diff --git a/src/slskd/Core/API/Hubs/LogsHub.cs b/src/slskd/Core/API/Hubs/LogsHub.cs index 7c14cc17..2bd30192 100644 --- a/src/slskd/Core/API/Hubs/LogsHub.cs +++ b/src/slskd/Core/API/Hubs/LogsHub.cs @@ -47,7 +47,7 @@ namespace slskd.Core.API /// /// The logs SignalR hub. /// - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public class LogsHub : Hub { public override async Task OnConnectedAsync() diff --git a/src/slskd/Messaging/API/Controllers/ConversationsController.cs b/src/slskd/Messaging/API/Controllers/ConversationsController.cs index 492340f0..17c55a10 100644 --- a/src/slskd/Messaging/API/Controllers/ConversationsController.cs +++ b/src/slskd/Messaging/API/Controllers/ConversationsController.cs @@ -66,7 +66,7 @@ namespace slskd.Messaging.API /// A conversation with the specified username, or a message matching the specified id could not be found. /// [HttpPut("{username}/{id}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] [ProducesResponseType(404)] public async Task Acknowledge([FromRoute]string username, [FromRoute]int id) @@ -90,7 +90,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// A conversation with the specified username could not be found. [HttpPut("{username}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] [ProducesResponseType(404)] public async Task AcknowledgeAll([FromRoute]string username) @@ -124,7 +124,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// A conversation with the specified username could not be found. [HttpDelete("{username}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(404)] [ProducesResponseType(204)] public IActionResult Delete([FromRoute]string username) @@ -145,7 +145,7 @@ namespace slskd.Messaging.API /// /// The request completed successfully. [HttpGet("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Dictionary>), 200)] public IActionResult GetAll() { @@ -166,7 +166,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// A matching search was not found. [HttpGet("{username}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(404)] public IActionResult GetByUsername([FromRoute]string username) @@ -192,7 +192,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified message is null or empty. [HttpPost("{username}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(201)] [ProducesResponseType(400)] public async Task Send([FromRoute]string username, [FromBody]string message) diff --git a/src/slskd/Messaging/API/Controllers/PublicChatController.cs b/src/slskd/Messaging/API/Controllers/PublicChatController.cs index c30e08a8..83f7ede7 100644 --- a/src/slskd/Messaging/API/Controllers/PublicChatController.cs +++ b/src/slskd/Messaging/API/Controllers/PublicChatController.cs @@ -45,7 +45,7 @@ namespace slskd.Messaging.API /// /// [HttpPost] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public async Task Start() { await Client.StartPublicChatAsync(); @@ -57,7 +57,7 @@ namespace slskd.Messaging.API /// /// [HttpDelete] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public async Task Stop() { await Client.StopPublicChatAsync(); diff --git a/src/slskd/Messaging/API/Controllers/RoomsController.cs b/src/slskd/Messaging/API/Controllers/RoomsController.cs index 8a3e5f3f..66993630 100644 --- a/src/slskd/Messaging/API/Controllers/RoomsController.cs +++ b/src/slskd/Messaging/API/Controllers/RoomsController.cs @@ -59,7 +59,7 @@ namespace slskd.Messaging.API /// /// The request completed successfully. [HttpGet("joined")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Dictionary>), 200)] public IActionResult GetAll() { @@ -74,7 +74,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified roomName could not be found. [HttpGet("joined/{roomName}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Room), 200)] [ProducesResponseType(404)] public IActionResult GetByRoomName([FromRoute]string roomName) @@ -96,7 +96,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified roomName could not be found. [HttpPost("joined/{roomName}/messages")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(201)] [ProducesResponseType(404)] public async Task SendMessage([FromRoute]string roomName, [FromBody]string message) @@ -119,7 +119,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified roomName could not be found. [HttpPost("joined/{roomName}/ticker")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(201)] [ProducesResponseType(404)] public async Task SetTicker([FromRoute] string roomName, [FromBody] string message) @@ -142,7 +142,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified roomName could not be found. [HttpPost("joined/{roomName}/members")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(201)] [ProducesResponseType(404)] public async Task AddRoomMember([FromRoute]string roomName, [FromBody]string username) @@ -164,7 +164,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified roomName could not be found. [HttpGet("joined/{roomName}/users")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IList), 200)] [ProducesResponseType(404)] public IActionResult GetUsersByRoomName([FromRoute]string roomName) @@ -188,7 +188,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified roomName could not be found. [HttpGet("joined/{roomName}/messages")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IList), 200)] [ProducesResponseType(404)] public IActionResult GetMessagesByRoomName([FromRoute]string roomName) @@ -209,7 +209,7 @@ namespace slskd.Messaging.API /// /// [HttpGet("available")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(List), 200)] public async Task GetRooms() { @@ -237,7 +237,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The room has already been joined. [HttpPost("joined")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Room), 201)] [ProducesResponseType(304)] public async Task JoinRoom([FromBody]string roomName) @@ -273,7 +273,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The room has not been joined. [HttpDelete("joined/{roomName}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(204)] [ProducesResponseType(404)] public async Task LeaveRoom([FromRoute]string roomName) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index c9417ed8..43d802b1 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -579,6 +579,22 @@ namespace slskd if (!OptionsAtStartup.Web.Authentication.Disabled) { + services.AddAuthorization(options => + { + options.AddPolicy(AuthPolicy.JwtOnly, policy => + { + policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); + policy.RequireAuthenticatedUser(); + }); + + options.AddPolicy(AuthPolicy.Any, policy => + { + policy.AuthenticationSchemes.Add(ApiKeyAuthentication.AuthenticationScheme); + policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); + policy.RequireAuthenticatedUser(); + }); + }); + services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { @@ -610,20 +626,27 @@ namespace slskd return Task.CompletedTask; }, }; + }) + .AddScheme(ApiKeyAuthentication.AuthenticationScheme, options => + { + options.EnableSignalRSupport = true; + options.SignalRRoutePrefix = "/hub"; + options.Role = Role.Administrator; }); - - //services.AddAuthentication(ApiKeyAuthentication.AuthenticationScheme) - // .AddScheme(ApiKeyAuthentication.AuthenticationScheme, options => - // { - // options.EnableSignalRSupport = true; - // options.SignalRRoutePrefix = "/hub"; - // options.Role = Role.Administrator; - // }); } else { Log.Warning("Authentication of web requests is DISABLED"); + services.AddAuthorization(options => + { + options.AddPolicy(AuthPolicy.Any, policy => + { + policy.AuthenticationSchemes.Add(PassthroughAuthentication.AuthenticationScheme); + policy.RequireAuthenticatedUser(); + }); + }); + services.AddAuthentication(PassthroughAuthentication.AuthenticationScheme) .AddScheme(PassthroughAuthentication.AuthenticationScheme, options => { diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index acae18e0..aadf01b2 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -54,7 +54,7 @@ namespace slskd.Search.API /// The specified was malformed. /// The search terminated abnormally. [HttpPost("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public async Task Post([FromBody] SearchRequest request) { if (string.IsNullOrWhiteSpace(request.SearchText)) @@ -91,7 +91,7 @@ namespace slskd.Search.API /// The request completed successfully. /// A matching search was not found. [HttpGet("{id}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public async Task GetById([FromRoute] Guid id, [FromQuery] bool includeResponses = false) { var search = await Searches.FindAsync(search => search.Id == id, includeResponses); @@ -112,7 +112,7 @@ namespace slskd.Search.API /// The request completed successfully. /// A matching search was not found. [HttpGet("{id}/responses")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public async Task GetResponsesById([FromRoute] Guid id) { var search = await Searches.FindAsync(search => search.Id == id, includeResponses: true); @@ -130,7 +130,7 @@ namespace slskd.Search.API /// /// [HttpGet("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public async Task GetAll() { var searches = await Searches.ListAsync(); @@ -145,7 +145,7 @@ namespace slskd.Search.API /// The search was not in progress. /// [HttpPut("{id}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] [ProducesResponseType(304)] public async Task Cancel([FromRoute] Guid id) @@ -173,7 +173,7 @@ namespace slskd.Search.API /// A search with the specified id could not be found. /// [HttpDelete("{id}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(204)] [ProducesResponseType(404)] public async Task Delete([FromRoute] Guid id) diff --git a/src/slskd/Search/API/Hubs/SearchHub.cs b/src/slskd/Search/API/Hubs/SearchHub.cs index aaf9a287..b1fbf725 100644 --- a/src/slskd/Search/API/Hubs/SearchHub.cs +++ b/src/slskd/Search/API/Hubs/SearchHub.cs @@ -73,7 +73,7 @@ namespace slskd.Search.API /// /// The search SignalR hub. /// - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] public class SearchHub : Hub { public SearchHub( diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index 5f87f900..d07e8eed 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -32,7 +32,6 @@ namespace slskd.Shares.API [Produces("application/json")] [Consumes("application/json")] [Route("api/v{version:apiVersion}/[controller]")] - [Authorize] public class SharesController : ControllerBase { /// @@ -53,7 +52,7 @@ namespace slskd.Shares.API /// The request completed successfully. /// [HttpGet("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] public async Task List() { @@ -87,7 +86,7 @@ namespace slskd.Shares.API /// The requested share could not be found. /// [HttpGet("{id}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(SummarizedShare), 200)] [ProducesResponseType(404)] public async Task Get(string id) @@ -122,7 +121,7 @@ namespace slskd.Shares.API /// /// The request completed successfully. [HttpGet("contents")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] public async Task BrowseAll() { @@ -137,6 +136,7 @@ namespace slskd.Shares.API /// The request completed successfully. /// The requested share could not be found. [HttpGet("{id}/contents")] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(404)] public async Task BrowseShare(string id) @@ -161,7 +161,7 @@ namespace slskd.Shares.API /// A share scan is already in progress. [HttpPut] [Route("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(204)] [ProducesResponseType(409)] public IActionResult RescanSharesAsync() @@ -186,7 +186,7 @@ namespace slskd.Shares.API /// A share scan was not in progress. [HttpDelete] [Route("")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(204)] [ProducesResponseType(404)] public IActionResult CancelShareScan() diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index bdab6582..45896c2e 100644 --- a/src/slskd/Transfers/API/Controllers/TransfersController.cs +++ b/src/slskd/Transfers/API/Controllers/TransfersController.cs @@ -57,7 +57,7 @@ namespace slskd.Transfers.API /// The download was cancelled successfully. /// The specified download was not found. [HttpDelete("downloads/{username}/{id}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(204)] [ProducesResponseType(404)] public async Task CancelDownloadAsync([FromRoute, Required] string username, [FromRoute, Required]string id, [FromQuery]bool remove = false) @@ -94,7 +94,7 @@ namespace slskd.Transfers.API /// The upload was cancelled successfully. /// The specified upload was not found. [HttpDelete("uploads/{username}/{id}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(204)] [ProducesResponseType(404)] public async Task CancelUpload([FromRoute, Required] string username, [FromRoute, Required]string id, [FromQuery]bool remove = false) @@ -131,7 +131,7 @@ namespace slskd.Transfers.API /// The download was rejected. /// An unexpected error was encountered. [HttpPost("downloads/{username}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(201)] [ProducesResponseType(typeof(string), 403)] [ProducesResponseType(typeof(string), 500)] @@ -154,7 +154,7 @@ namespace slskd.Transfers.API /// /// The request completed successfully. [HttpGet("downloads")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] public async Task GetDownloadsAsync([FromQuery]bool includeRemoved = false) { @@ -181,7 +181,7 @@ namespace slskd.Transfers.API /// /// The request completed successfully. [HttpGet("downloads/{username}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] public async Task GetDownloadsAsync([FromRoute, Required] string username) { @@ -207,7 +207,7 @@ namespace slskd.Transfers.API } [HttpGet("downloads/{username}/{id}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(API.Transfer), 200)] [ProducesResponseType(404)] public async Task GetDownload([FromRoute, Required] string username, [FromRoute, Required] string id) @@ -237,7 +237,7 @@ namespace slskd.Transfers.API /// The request completed successfully. /// The specified download was not found. [HttpGet("downloads/{username}/{id}/position")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(API.Transfer), 200)] [ProducesResponseType(404)] public async Task GetPlaceInQueueAsync([FromRoute, Required] string username, [FromRoute, Required] string id) @@ -268,7 +268,7 @@ namespace slskd.Transfers.API /// /// The request completed successfully. [HttpGet("uploads")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] public async Task GetUploads([FromQuery] bool includeRemoved = false) { @@ -295,7 +295,7 @@ namespace slskd.Transfers.API /// /// The request completed successfully. [HttpGet("uploads/{username}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] public async Task GetUploads([FromRoute, Required] string username) { @@ -328,7 +328,7 @@ namespace slskd.Transfers.API /// /// The request completed successfully. [HttpGet("uploads/{username}/{id}")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] public async Task GetUploads([FromRoute, Required] string username, [FromRoute, Required] string id) { diff --git a/src/slskd/Users/API/Controllers/UsersController.cs b/src/slskd/Users/API/Controllers/UsersController.cs index 2439785e..0f06a677 100644 --- a/src/slskd/Users/API/Controllers/UsersController.cs +++ b/src/slskd/Users/API/Controllers/UsersController.cs @@ -59,7 +59,7 @@ namespace slskd.Users.API /// /// The request completed successfully. [HttpGet("{username}/endpoint")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IPEndPoint), 200)] [ProducesResponseType(404)] public async Task Endpoint([FromRoute, Required] string username) @@ -81,7 +81,7 @@ namespace slskd.Users.API /// The username of the user. /// [HttpGet("{username}/browse")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(404)] public async Task Browse([FromRoute, Required] string username) @@ -110,7 +110,7 @@ namespace slskd.Users.API /// The username of the user. /// [HttpGet("{username}/browse/status")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(decimal), 200)] [ProducesResponseType(404)] public IActionResult BrowseStatus([FromRoute, Required] string username) @@ -129,7 +129,7 @@ namespace slskd.Users.API /// The username of the user. /// [HttpGet("{username}/info")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Info), 200)] [ProducesResponseType(404)] public async Task Info([FromRoute, Required] string username) @@ -151,7 +151,7 @@ namespace slskd.Users.API /// The username of the user. /// [HttpGet("{username}/status")] - [Authorize] + [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Status), 200)] [ProducesResponseType(404)] public async Task Status([FromRoute, Required] string username) -- cgit v1.2.3 From 486d0a9768525c6f5d94ebc5b3c02cc9f7376b17 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 6 Oct 2022 08:12:49 -0500 Subject: add policies for passthrough --- src/slskd/Program.cs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 43d802b1..f4d25630 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -645,6 +645,12 @@ namespace slskd policy.AuthenticationSchemes.Add(PassthroughAuthentication.AuthenticationScheme); policy.RequireAuthenticatedUser(); }); + + options.AddPolicy(AuthPolicy.JwtOnly, policy => + { + policy.AuthenticationSchemes.Add(PassthroughAuthentication.AuthenticationScheme); + policy.RequireAuthenticatedUser(); + }); }); services.AddAuthentication(PassthroughAuthentication.AuthenticationScheme) -- cgit v1.2.3