2019-07-09 18:39:29 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
namespace PluralKit.API
|
|
|
|
{
|
|
|
|
public class TokenAuthService: IMiddleware
|
|
|
|
{
|
|
|
|
public PKSystem CurrentSystem { get; set; }
|
|
|
|
|
2019-10-26 17:45:30 +00:00
|
|
|
private IDataStore _data;
|
2019-07-09 18:39:29 +00:00
|
|
|
|
2019-10-26 17:45:30 +00:00
|
|
|
public TokenAuthService(IDataStore data)
|
2019-07-09 18:39:29 +00:00
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
_data = data;
|
2019-07-09 18:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
|
|
|
{
|
|
|
|
var token = context.Request.Headers["Authorization"].FirstOrDefault();
|
|
|
|
if (token != null)
|
|
|
|
{
|
2019-10-26 17:45:30 +00:00
|
|
|
CurrentSystem = await _data.GetSystemByToken(token);
|
2019-07-09 18:39:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await next.Invoke(context);
|
|
|
|
CurrentSystem = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|