public class SessionAuthenticationHandler : AuthenticationHandler<SessionAuthenticationSchemeOptions> {
private readonly ILoginClient _loginClient;
private readonly IMemoryCache _memoryCache;
public SessionAuthenticationHandler(IOptionsMonitor<SessionAuthenticationSchemeOptions> optionsMonitor,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
ILoginClient loginServerClient,
IMemoryCache memoryCache) : base(optionsMonitor,
logger,
encoder,
clock) {
_loginClient = loginServerClient;
_memoryCache = memoryCache;
}
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() {
if (Request.Query.TryGetValue("sessionId", out StringValues sessionId)) {
// I'm hiding implementation details because of Discord's message limit
string accessToken = ConvertSessionIdToAccessToken(sessionId.ToString());
return BuildTicket(accessToken);
}
// If query string is empty, we shall check for cookies
if (Request.Cookies.TryGetValue("ApplicationSession", out string? postChartAccessToken)) {
// TODO: Authenticate
return BuildTicket("");
}
// HERE! It should be authenticated even if it's from anonymous user.
return BuildTicket("");
}
private AuthenticateResult BuildTicket(string accessToken) {
var claims = new List<Claim> {
new(ClaimTypes.Name, "Session"),
new(ClaimTypes.Expiration, "3600"),
new(ClaimTypes.Anonymous, string.IsNullOrEmpty(accessToken) ? "true" : "false")
};
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new GenericPrincipal(identity, null);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
return AuthenticateResult.Success(ticket);
}
}
public class SessionAuthenticationHandler : AuthenticationHandler<SessionAuthenticationSchemeOptions> {
private readonly ILoginClient _loginClient;
private readonly IMemoryCache _memoryCache;
public SessionAuthenticationHandler(IOptionsMonitor<SessionAuthenticationSchemeOptions> optionsMonitor,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
ILoginClient loginServerClient,
IMemoryCache memoryCache) : base(optionsMonitor,
logger,
encoder,
clock) {
_loginClient = loginServerClient;
_memoryCache = memoryCache;
}
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() {
if (Request.Query.TryGetValue("sessionId", out StringValues sessionId)) {
// I'm hiding implementation details because of Discord's message limit
string accessToken = ConvertSessionIdToAccessToken(sessionId.ToString());
return BuildTicket(accessToken);
}
// If query string is empty, we shall check for cookies
if (Request.Cookies.TryGetValue("ApplicationSession", out string? postChartAccessToken)) {
// TODO: Authenticate
return BuildTicket("");
}
// HERE! It should be authenticated even if it's from anonymous user.
return BuildTicket("");
}
private AuthenticateResult BuildTicket(string accessToken) {
var claims = new List<Claim> {
new(ClaimTypes.Name, "Session"),
new(ClaimTypes.Expiration, "3600"),
new(ClaimTypes.Anonymous, string.IsNullOrEmpty(accessToken) ? "true" : "false")
};
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new GenericPrincipal(identity, null);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
return AuthenticateResult.Success(ticket);
}
}