DeviceHub: /device WS with bearer auth + hello/hello_ack + ping/pong
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
using backend.Data;
|
||||
using backend.Devices;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace backend.DeviceHub;
|
||||
|
||||
public class DeviceAuth(AppDbContext db, DeviceTokenService tokens)
|
||||
{
|
||||
public async Task<Device?> AuthenticateAsync(string? authHeader, CancellationToken ct)
|
||||
{
|
||||
if (string.IsNullOrEmpty(authHeader)) return null;
|
||||
const string prefix = "Bearer ";
|
||||
if (!authHeader.StartsWith(prefix, StringComparison.Ordinal)) return null;
|
||||
var token = authHeader[prefix.Length..].Trim();
|
||||
if (string.IsNullOrEmpty(token)) return null;
|
||||
var hash = tokens.Hash(token);
|
||||
var device = await db.Devices.Include(d => d.Config)
|
||||
.FirstOrDefaultAsync(d => d.TokenHash == hash && !d.IsRevoked, ct);
|
||||
return device;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user