shellcoreDB made... n shi
This commit is contained in:
1 parent
244bd918d6
commit
bcc9894062
19 files changed
+493
-22
No files matched your search
@@ -0,0 +1,46 @@
|
||||
using Npgsql;
|
||||
using Dapper;
|
||||
|
||||
namespace ShellcoreDB;
|
||||
|
||||
public sealed class SteamAuth(Database database, Sessions sessions, SteamVerifier verifier)
|
||||
{
|
||||
public async Task<(LoginData? login, string? error)> Authenticate(string ticket, string? username, CancellationToken cancellationToken)
|
||||
{
|
||||
var steamId = await verifier.Verify(ticket, cancellationToken);
|
||||
if (steamId is null) return (null, "invalidSteamTicket");
|
||||
await using var connection = await database.Open(cancellationToken);
|
||||
await using var transaction = await connection.BeginTransactionAsync(cancellationToken);
|
||||
var linked = await connection.QuerySingleOrDefaultAsync<SteamAccount>(new CommandDefinition(
|
||||
"SELECT a.id, a.username, a.status FROM externalAccounts e JOIN accounts a ON a.id = e.accountId WHERE e.provider = 'steam' AND e.providerUserId = @steamId",
|
||||
new { steamId }, transaction, cancellationToken: cancellationToken));
|
||||
if (linked is not null)
|
||||
{
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
return linked.status == "active" ? (await sessions.Issue(linked.id, linked.username, cancellationToken), null) : (null, "accountUnavailable");
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(username)) return (null, "usernameRequired");
|
||||
try
|
||||
{
|
||||
var accountId = await connection.QuerySingleAsync<Guid>(new CommandDefinition(
|
||||
"INSERT INTO accounts (username) VALUES (@username) RETURNING id",
|
||||
new { username }, transaction, cancellationToken: cancellationToken));
|
||||
await connection.ExecuteAsync(new CommandDefinition(
|
||||
"INSERT INTO externalAccounts (accountId, provider, providerUserId) VALUES (@accountId, 'steam', @steamId)",
|
||||
new { accountId, steamId }, transaction, cancellationToken: cancellationToken));
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
return (await sessions.Issue(accountId, username, cancellationToken), null);
|
||||
}
|
||||
catch (PostgresException exception) when (exception.SqlState == PostgresErrorCodes.UniqueViolation)
|
||||
{
|
||||
return (null, "usernameTaken");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SteamAccount
|
||||
{
|
||||
public Guid id { get; set; }
|
||||
public string username { get; set; } = "";
|
||||
public string status { get; set; } = "";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user