aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Controllers
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-01-31 11:15:35 -0600
committerGitHub <noreply@github.com>2021-01-31 11:15:35 -0600
commit36ecf12404338bacf1fdd90334c252f89c897b3e (patch)
tree59772d1c8276bf363c33cd2875f395c9dba1ac5c /src/slskd/Controllers
parentcec869b9456cf6fe5597847948b2d75b9846755a (diff)
parentc9cd2a67ea65570cbd8bd72f09ff5383b1a5c2fe (diff)
downloadslskd-36ecf12404338bacf1fdd90334c252f89c897b3e.tar.gz
Merge pull request #24 from slskd/config
Add app configuration
Diffstat (limited to 'src/slskd/Controllers')
-rw-r--r--src/slskd/Controllers/ApplicationController.cs59
-rw-r--r--src/slskd/Controllers/ServerController.cs6
-rw-r--r--src/slskd/Controllers/SessionController.cs34
3 files changed, 82 insertions, 17 deletions
diff --git a/src/slskd/Controllers/ApplicationController.cs b/src/slskd/Controllers/ApplicationController.cs
new file mode 100644
index 00000000..cc9d56a1
--- /dev/null
+++ b/src/slskd/Controllers/ApplicationController.cs
@@ -0,0 +1,59 @@
+namespace slskd.Controllers
+{
+ using Microsoft.AspNetCore.Authorization;
+ using Microsoft.AspNetCore.Mvc;
+ using System.Diagnostics;
+ using System;
+ using Microsoft.Extensions.Hosting;
+ using Soulseek;
+
+ /// <summary>
+ /// Application
+ /// </summary>
+ [Route("api/v{version:apiVersion}/[controller]")]
+ [ApiVersion("0")]
+ [ApiController]
+ [Produces("application/json")]
+ [Consumes("application/json")]
+ public class ApplicationController : ControllerBase
+ {
+ private ISoulseekClient Client { get; }
+ private IHostApplicationLifetime Lifetime { get; }
+
+ public ApplicationController(ISoulseekClient client, IHostApplicationLifetime lifetime)
+ {
+ Client = client;
+ Lifetime = lifetime;
+ }
+
+ /// <summary>
+ /// Stops the application.
+ /// </summary>
+ /// <returns></returns>
+ [HttpDelete]
+ [Authorize]
+ public IActionResult Shutdown()
+ {
+ Client.Disconnect("Shut down via API");
+
+ Lifetime.StopApplication();
+ return NoContent();
+ }
+
+ /// <summary>
+ /// Restarts the application.
+ /// </summary>
+ /// <returns></returns>
+ [HttpPut]
+ [Authorize]
+ public IActionResult Restart()
+ {
+ Client.Disconnect("Restarted via API");
+
+ Process.Start(Process.GetCurrentProcess().MainModule.FileName, Environment.CommandLine);
+ Lifetime.StopApplication();
+
+ return NoContent();
+ }
+ }
+}
diff --git a/src/slskd/Controllers/ServerController.cs b/src/slskd/Controllers/ServerController.cs
index 2502a05d..ed6e84c8 100644
--- a/src/slskd/Controllers/ServerController.cs
+++ b/src/slskd/Controllers/ServerController.cs
@@ -56,12 +56,6 @@
return Ok();
}
- if (addr && port && !un && !pw)
- {
- await Client.ConnectAsync(req.Address, req.Port.Value);
- return Ok();
- }
-
if (!addr && !port && un && pw)
{
await Client.ConnectAsync(req.Username, req.Password);
diff --git a/src/slskd/Controllers/SessionController.cs b/src/slskd/Controllers/SessionController.cs
index 5c162708..235e65cf 100644
--- a/src/slskd/Controllers/SessionController.cs
+++ b/src/slskd/Controllers/SessionController.cs
@@ -8,6 +8,9 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using slskd.DTO;
+ using Microsoft.Extensions.Options;
+ using slskd;
+ using System.Text.Json;
/// <summary>
/// Session
@@ -19,8 +22,16 @@
[Consumes("application/json")]
public class SessionController : ControllerBase
{
- public SessionController()
+ private slskd.Options Options { get; set; }
+ private SymmetricSecurityKey JwtSigningKey { get; set; }
+
+ public SessionController(
+ IOptionsSnapshot<slskd.Options> optionsSnapshot,
+ SymmetricSecurityKey jwtSigningKey)
{
+ Options = optionsSnapshot.Value;
+ Console.WriteLine(JsonSerializer.Serialize(Options));
+ JwtSigningKey = jwtSigningKey;
}
/// <summary>
@@ -34,7 +45,7 @@
[ProducesResponseType(typeof(bool), 200)]
public IActionResult Enabled()
{
- return Ok(!Program.DisableAuthentication);
+ return base.Ok(!Options.Web.Authentication.Disable);
}
/// <summary>
@@ -71,7 +82,7 @@
[ProducesResponseType(400)]
[ProducesResponseType(401)]
[ProducesResponseType(typeof(string), 500)]
- public IActionResult Login([FromBody]LoginRequest login)
+ public IActionResult Login([FromBody] LoginRequest login)
{
if (login == default)
{
@@ -83,29 +94,30 @@
return BadRequest("Username and/or Password missing or invalid");
}
- if (login.Username == Startup.Username && login.Password == Startup.Password)
+ // only admin login for now
+ if (Options.Web.Authentication.Username == login.Username && Options.Web.Authentication.Password == login.Password)
{
- return Ok(new TokenResponse(GetJwtSecurityToken()));
+ return Ok(new TokenResponse(GetJwtSecurityToken(login.Username, Role.Administrator)));
}
return Unauthorized();
}
- private JwtSecurityToken GetJwtSecurityToken()
+ private JwtSecurityToken GetJwtSecurityToken(string username, Role role)
{
var issuedUtc = DateTime.UtcNow;
- var expiresUtc = DateTime.UtcNow.AddMilliseconds(Startup.SecurityTokenTTL);
+ var expiresUtc = DateTime.UtcNow.AddMilliseconds(Options.Web.Jwt.Ttl);
var claims = new List<Claim>()
{
- new Claim(ClaimTypes.Name, Startup.Username),
+ new Claim(ClaimTypes.Name, username),
new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()),
- new Claim(ClaimTypes.Role, "User"),
- new Claim("name", Startup.Username),
+ new Claim(ClaimTypes.Role, role.ToString()),
+ new Claim("name", username),
new Claim("iat", ((DateTimeOffset)issuedUtc).ToUnixTimeSeconds().ToString())
};
- var credentials = new SigningCredentials(Startup.JwtSigningKey, SecurityAlgorithms.HmacSha256);
+ var credentials = new SigningCredentials(JwtSigningKey, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: "slskd",