Jackett/src/Jackett.Server/Services/SecurityService.cs

64 lines
1.9 KiB
C#
Raw Normal View History

using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using Jackett.Common.Models.Config;
using Jackett.Common.Services.Interfaces;
2018-05-01 11:41:34 +00:00
namespace Jackett.Server.Services
{
2021-11-16 13:06:07 +00:00
internal class SecurityService : ISecurityService
{
private const string COOKIENAME = "JACKETT";
private readonly ServerConfig _serverConfig;
2021-11-16 13:06:07 +00:00
public SecurityService(ServerConfig sc) => _serverConfig = sc;
public string HashPassword(string input)
{
if (input == null)
return null;
// Append key as salt
input += _serverConfig.APIKey;
var UE = new UnicodeEncoding();
byte[] hashValue;
var message = UE.GetBytes(input);
2021-11-17 05:06:27 +00:00
#pragma warning disable SYSLIB0021
var hashString = new SHA512Managed();
2021-11-17 05:06:27 +00:00
#pragma warning restore SYSLIB0021
hashValue = hashString.ComputeHash(message);
2021-11-17 05:06:27 +00:00
var hex = "";
foreach (var x in hashValue)
{
hex += string.Format("{0:x2}", x);
}
return hex;
}
public void Login(HttpResponseMessage response) => response.Headers.Add("Set-Cookie", COOKIENAME + "=" + _serverConfig.AdminPassword + "; path=/");
public void Logout(HttpResponseMessage response) => response.Headers.Add("Set-Cookie", COOKIENAME + "=; path=/");
public bool CheckAuthorised(HttpRequestMessage request)
{
if (string.IsNullOrEmpty(_serverConfig.AdminPassword))
return true;
try
{
2018-05-01 11:41:34 +00:00
var cookie = request.Headers.GetValues(COOKIENAME).FirstOrDefault();
if (cookie != null)
{
2018-05-01 11:41:34 +00:00
return cookie == _serverConfig.AdminPassword;
}
}
catch { }
return false;
}
}
}