using System.Net.Http.Headers;
namespace IOL.BookmarkThing.Server.Api.V1;
///
[Authorize(AuthenticationSchemes = AuthSchemes)]
[ApiController]
public class BaseV1Route : ControllerBase
{
private const string AuthSchemes = CookieAuthenticationDefaults.AuthenticationScheme + "," + Constants.BASIC_AUTH_SCHEME;
///
/// User data for the currently logged on user.
///
protected LoggedInV1User LoggedInUser => new() {
Username = User.Identity?.Name,
Id = User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value.ToGuid() ?? default
};
protected bool IsApiCall() {
if (!Request.Headers.ContainsKey("Authorization")) return false;
try {
var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
if (authHeader.Parameter == null) return false;
} catch (Exception e) {
Console.WriteLine(e);
return false;
}
return true;
}
protected bool HasApiPermission(string permission_key) {
var permission_claim = User.Claims.SingleOrDefault(c => c.Type == permission_key);
return permission_claim is {
Value: "True"
};
}
}