aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Service/Application/API
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Service/Application/API')
-rw-r--r--src/slskd/Service/Application/API/Controllers/ApplicationController.cs76
-rw-r--r--src/slskd/Service/Application/API/Controllers/SessionController.cs143
-rw-r--r--src/slskd/Service/Application/API/DTO/LoginRequest.cs25
-rw-r--r--src/slskd/Service/Application/API/DTO/TokenResponse.cs67
4 files changed, 0 insertions, 311 deletions
diff --git a/src/slskd/Service/Application/API/Controllers/ApplicationController.cs b/src/slskd/Service/Application/API/Controllers/ApplicationController.cs
deleted file mode 100644
index 7fa59ff0..00000000
--- a/src/slskd/Service/Application/API/Controllers/ApplicationController.cs
+++ /dev/null
@@ -1,76 +0,0 @@
-// <copyright file="ApplicationController.cs" company="slskd Team">
-// 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/.
-// </copyright>
-
-namespace slskd.Application.API
-{
- using System;
- using System.Diagnostics;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- 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
- {
- public ApplicationController(ISoulseekClient client, IHostApplicationLifetime lifetime)
- {
- Client = client;
- Lifetime = lifetime;
- }
-
- private ISoulseekClient Client { get; }
- private IHostApplicationLifetime Lifetime { get; }
-
- /// <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/Service/Application/API/Controllers/SessionController.cs b/src/slskd/Service/Application/API/Controllers/SessionController.cs
deleted file mode 100644
index a8e8fac5..00000000
--- a/src/slskd/Service/Application/API/Controllers/SessionController.cs
+++ /dev/null
@@ -1,143 +0,0 @@
-// <copyright file="SessionController.cs" company="slskd Team">
-// 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/.
-// </copyright>
-
-namespace slskd.Application.API
-{
- using System;
- using System.Collections.Generic;
- using System.IdentityModel.Tokens.Jwt;
- using System.Security.Claims;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.IdentityModel.Tokens;
- using slskd.Authentication;
-
- /// <summary>
- /// Session.
- /// </summary>
- [Route("api/v{version:apiVersion}/[controller]")]
- [ApiVersion("0")]
- [ApiController]
- [Produces("application/json")]
- [Consumes("application/json")]
- public class SessionController : ControllerBase
- {
- public SessionController(
- Microsoft.Extensions.Options.IOptionsSnapshot<Options> optionsSnapshot,
- SymmetricSecurityKey jwtSigningKey)
- {
- Options = optionsSnapshot.Value;
- JwtSigningKey = jwtSigningKey;
- }
-
- private SymmetricSecurityKey JwtSigningKey { get; set; }
- private Options Options { get; set; }
-
- /// <summary>
- /// Checks whether the provided authentication is valid.
- /// </summary>
- /// <remarks>This is a no-op provided so that the application can test for an expired token on load.</remarks>
- /// <returns></returns>
- /// <response code="200">The authentication is valid.</response>
- /// <response code="403">The authentication is is invalid.</response>
- [HttpGet]
- [Route("")]
- [Authorize]
- [ProducesResponseType(200)]
- [ProducesResponseType(401)]
- public IActionResult Check()
- {
- return Ok();
- }
-
- /// <summary>
- /// Checks whether security is enabled.
- /// </summary>
- /// <returns></returns>
- /// <response code="200">True if security is enabled, false otherwise.</response>
- [HttpGet]
- [Route("enabled")]
- [AllowAnonymous]
- [ProducesResponseType(typeof(bool), 200)]
- public IActionResult Enabled()
- {
- return Ok(!Options.Web.Authentication.Disable);
- }
-
- /// <summary>
- /// Logs in.
- /// </summary>
- /// <param name="login"></param>
- /// <returns></returns>
- /// <response code="200">Login was successful.</response>
- /// <response code="400">Bad request.</response>
- /// <response code="401">Login failed.</response>
- [HttpPost]
- [Route("")]
- [AllowAnonymous]
- [ProducesResponseType(typeof(TokenResponse), 200)]
- [ProducesResponseType(400)]
- [ProducesResponseType(401)]
- [ProducesResponseType(typeof(string), 500)]
- public IActionResult Login([FromBody] LoginRequest login)
- {
- if (login == default)
- {
- return BadRequest();
- }
-
- if (string.IsNullOrWhiteSpace(login.Username) || string.IsNullOrWhiteSpace(login.Password))
- {
- return BadRequest("Username and/or Password missing or invalid");
- }
-
- // only admin login for now
- if (Options.Web.Authentication.Username == login.Username && Options.Web.Authentication.Password == login.Password)
- {
- return Ok(new TokenResponse(GetJwtSecurityToken(login.Username, Role.Administrator)));
- }
-
- return Unauthorized();
- }
-
- private JwtSecurityToken GetJwtSecurityToken(string username, Role role)
- {
- var issuedUtc = DateTime.UtcNow;
- var expiresUtc = DateTime.UtcNow.AddMilliseconds(Options.Web.Authentication.Jwt.Ttl);
-
- var claims = new List<Claim>()
- {
- new Claim(ClaimTypes.Name, username),
- new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()),
- new Claim(ClaimTypes.Role, role.ToString()),
- new Claim("name", username),
- new Claim("iat", ((DateTimeOffset)issuedUtc).ToUnixTimeSeconds().ToString()),
- };
-
- var credentials = new SigningCredentials(JwtSigningKey, SecurityAlgorithms.HmacSha256);
-
- var token = new JwtSecurityToken(
- issuer: Program.AppName,
- claims: claims,
- notBefore: issuedUtc,
- expires: expiresUtc,
- signingCredentials: credentials);
-
- return token;
- }
- }
-} \ No newline at end of file
diff --git a/src/slskd/Service/Application/API/DTO/LoginRequest.cs b/src/slskd/Service/Application/API/DTO/LoginRequest.cs
deleted file mode 100644
index 2f1fb072..00000000
--- a/src/slskd/Service/Application/API/DTO/LoginRequest.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-// <copyright file="LoginRequest.cs" company="slskd Team">
-// 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/.
-// </copyright>
-
-namespace slskd.Application.API
-{
- public class LoginRequest
- {
- public string Username { get; set; }
- public string Password { get; set; }
- }
-}
diff --git a/src/slskd/Service/Application/API/DTO/TokenResponse.cs b/src/slskd/Service/Application/API/DTO/TokenResponse.cs
deleted file mode 100644
index 2668d566..00000000
--- a/src/slskd/Service/Application/API/DTO/TokenResponse.cs
+++ /dev/null
@@ -1,67 +0,0 @@
-// <copyright file="TokenResponse.cs" company="slskd Team">
-// 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/.
-// </copyright>
-
-namespace slskd.Application.API
-{
- using System;
- using System.IdentityModel.Tokens.Jwt;
- using System.Linq;
- using System.Security.Claims;
- using System.Text.Json.Serialization;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
-
- public class TokenResponse
- {
- public TokenResponse(JwtSecurityToken jwtSecurityToken)
- {
- JwtSecurityToken = jwtSecurityToken;
- }
-
- /// <summary>
- /// Gets the time at which the Access Token expires.
- /// </summary>
- public long Expires => ((DateTimeOffset)JwtSecurityToken.ValidTo).ToUnixTimeSeconds();
-
- /// <summary>
- /// Gets the time at which the Access Token was issued.
- /// </summary>
- public long Issued => ((DateTimeOffset)JwtSecurityToken.ValidFrom).ToUnixTimeSeconds();
-
- /// <summary>
- /// Gets the value of the Name claim from the Access Token.
- /// </summary>
- public string Name => JwtSecurityToken.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
-
- /// <summary>
- /// Gets the value of the Not Before claim from the Access Token.
- /// </summary>
- public long NotBefore => long.Parse(JwtSecurityToken.Claims.SingleOrDefault(c => c.Type == "nbf").Value);
-
- /// <summary>
- /// Gets the Access Token string.
- /// </summary>
- public string Token => new JwtSecurityTokenHandler().WriteToken(JwtSecurityToken);
-
- /// <summary>
- /// Gets the Token type.
- /// </summary>
- public string TokenType => JwtBearerDefaults.AuthenticationScheme;
-
- [JsonIgnore]
- private JwtSecurityToken JwtSecurityToken { get; }
- }
-} \ No newline at end of file