-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: normalize username / host capitalization
Resolves #74
- Loading branch information
1 parent
4c530e9
commit aa5e393
Showing
5 changed files
with
44 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 27 additions & 9 deletions
36
ModShark/Services/ServiceAccountService.cs → ModShark/Services/UserService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,55 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using SharkeyDB; | ||
using SharkeyDB.Entities; | ||
|
||
namespace ModShark.Services; | ||
|
||
public interface IServiceAccountService | ||
public interface IUserService | ||
{ | ||
/// <summary> | ||
/// Finds a User by username and hostname. | ||
/// If host is null, then searches local users. | ||
/// </summary> | ||
/// <remarks> | ||
/// The returned IQueryable is non-tracking. | ||
/// </remarks> | ||
IQueryable<User> QueryByUserHost(string username, string? host); | ||
Task<string?> GetServiceAccountId(CancellationToken stoppingToken); | ||
|
||
Task<string?> GetServiceAccountToken(CancellationToken stoppingToken); | ||
} | ||
|
||
public class ServiceAccountService(SharkeyConfig config, SharkeyContext db) : IServiceAccountService | ||
public class UserService(SharkeyConfig config, SharkeyContext db) : IUserService | ||
{ | ||
private string? CachedId { get; set; } | ||
private string? CachedToken { get; set; } | ||
|
||
|
||
public IQueryable<User> QueryByUserHost(string username, string? host) | ||
{ | ||
var usernameLower = username.ToLower(); | ||
var hostLower = host?.ToLower(); | ||
|
||
return | ||
db.Users | ||
.AsNoTracking() | ||
.Where(u => u.UsernameLower == usernameLower && u.Host == hostLower); | ||
} | ||
|
||
public async Task<string?> GetServiceAccountId(CancellationToken stoppingToken) | ||
=> CachedId ??= await LookupReporterId(stoppingToken); | ||
|
||
private async Task<string?> LookupReporterId(CancellationToken stoppingToken) | ||
=> await db.Users | ||
.AsNoTracking() | ||
.Where(u => u.Host == null && u.Username == config.ServiceAccount) | ||
=> await | ||
QueryByUserHost(config.ServiceAccount, null) | ||
.Select(u => u.Id) | ||
.FirstOrDefaultAsync(stoppingToken); | ||
|
||
public async Task<string?> GetServiceAccountToken(CancellationToken stoppingToken) | ||
=> CachedToken ??= await LookupReporterToken(stoppingToken); | ||
|
||
private async Task<string?> LookupReporterToken(CancellationToken stoppingToken) | ||
=> await db.Users | ||
.AsNoTracking() | ||
.Where(u => u.Host == null && u.Username == config.ServiceAccount) | ||
=> await | ||
QueryByUserHost(config.ServiceAccount, null) | ||
.Select(u => u.Token) | ||
.FirstOrDefaultAsync(stoppingToken); | ||
} |