From a8503735d12f7aa92e2bdaea04e1a5efa834d05a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 7 Jun 2025 22:06:38 +0800 Subject: [PATCH] :sparkles: Account devices --- .../Account/AccountCurrentController.cs | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/DysonNetwork.Sphere/Account/AccountCurrentController.cs b/DysonNetwork.Sphere/Account/AccountCurrentController.cs index f663492..04c8092 100644 --- a/DysonNetwork.Sphere/Account/AccountCurrentController.cs +++ b/DysonNetwork.Sphere/Account/AccountCurrentController.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using NodaTime; +using Org.BouncyCastle.Utilities; namespace DysonNetwork.Sphere.Account; @@ -421,6 +422,43 @@ public class AccountCurrentController( } } + public class AuthorizedDevice + { + public string? Label { get; set; } + public string UserAgent { get; set; } = null!; + public string DeviceId { get; set; } = null!; + public List Sessions { get; set; } = new(); + } + + [HttpGet("devices")] + [Authorize] + public async Task>> GetDevices() + { + if (HttpContext.Items["CurrentUser"] is not Account currentUser) + { + return Unauthorized(); + } + + // Group sessions by the related DeviceId, then create an AuthorizedDevice for each group. + var deviceGroups = await db.AuthSessions + .Where(s => s.Account.Id == currentUser.Id) + // Include the challenge if you need it to access DeviceId + .Include(s => s.Challenge) + .GroupBy(s => s.Challenge.DeviceId!) + .Select(g => new AuthorizedDevice + { + DeviceId = g.Key!, + UserAgent = g.First(x => x.Challenge.UserAgent != null).Challenge.UserAgent!, + Label = g.Where(x => string.IsNullOrWhiteSpace(x.Label)).Select(x => x.Label).FirstOrDefault(), + Sessions = g + .OrderByDescending(x => x.LastGrantedAt) + .ToList() + }) + .ToListAsync(); + + return Ok(deviceGroups); + } + [HttpGet("sessions")] [Authorize] public async Task>> GetSessions( @@ -434,8 +472,7 @@ public class AccountCurrentController( var query = db.AuthSessions .Include(session => session.Account) .Include(session => session.Challenge) - .Where(session => session.Account.Id == currentUser.Id) - .OrderByDescending(session => session.CreatedAt); + .Where(session => session.Account.Id == currentUser.Id); var total = await query.CountAsync(); Response.Headers.Append("X-Total", total.ToString());