✨ Auth via authorized device
This commit is contained in:
@@ -275,6 +275,14 @@ public class AuthController(
|
||||
public string Token { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class NewSessionRequest
|
||||
{
|
||||
[Required] [MaxLength(512)] public string DeviceId { get; set; } = null!;
|
||||
[MaxLength(1024)] public string? DeviceName { get; set; }
|
||||
[Required] public DysonNetwork.Shared.Models.ClientPlatform Platform { get; set; }
|
||||
public Instant? ExpiredAt { get; set; }
|
||||
}
|
||||
|
||||
[HttpPost("token")]
|
||||
public async Task<ActionResult<TokenExchangeResponse>> ExchangeToken([FromBody] TokenExchangeRequest request)
|
||||
{
|
||||
@@ -325,4 +333,35 @@ public class AuthController(
|
||||
});
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("login/session")]
|
||||
[Microsoft.AspNetCore.Authorization.Authorize] // Use full namespace to avoid ambiguity with DysonNetwork.Pass.Permission.Authorize
|
||||
public async Task<ActionResult<TokenExchangeResponse>> LoginFromSession([FromBody] NewSessionRequest request)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not SnAccount currentUser ||
|
||||
HttpContext.Items["CurrentSession"] is not Shared.Models.SnAuthSession currentSession) return Unauthorized();
|
||||
|
||||
var newSession = await auth.CreateSessionFromParentAsync(
|
||||
currentSession,
|
||||
request.DeviceId,
|
||||
request.DeviceName,
|
||||
request.Platform,
|
||||
request.ExpiredAt
|
||||
);
|
||||
|
||||
var tk = auth.CreateToken(newSession);
|
||||
|
||||
// Set cookie using HttpContext, similar to CreateSessionAndIssueToken
|
||||
var cookieDomain = _cookieDomain;
|
||||
HttpContext.Response.Cookies.Append(AuthConstants.CookieTokenName, tk, new CookieOptions
|
||||
{
|
||||
HttpOnly = true,
|
||||
Secure = true,
|
||||
SameSite = SameSiteMode.Lax,
|
||||
Domain = cookieDomain,
|
||||
Expires = request.ExpiredAt?.ToDateTimeOffset() ?? DateTime.UtcNow.AddYears(20)
|
||||
});
|
||||
|
||||
return Ok(new TokenExchangeResponse { Token = tk });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -664,4 +664,40 @@ public class AuthService(
|
||||
|
||||
return Convert.FromBase64String(padded);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new session derived from an existing parent session.
|
||||
/// </summary>
|
||||
/// <param name="parentSession">The existing session from which the new session is derived.</param>
|
||||
/// <param name="deviceId">The ID of the device for the new session.</param>
|
||||
/// <param name="deviceName">The name of the device for the new session.</param>
|
||||
/// <param name="platform">The platform of the device for the new session.</param>
|
||||
/// <param name="expiredAt">Optional: The expiration time for the new session.</param>
|
||||
/// <returns>The newly created SnAuthSession.</returns>
|
||||
public async Task<SnAuthSession> CreateSessionFromParentAsync(
|
||||
SnAuthSession parentSession,
|
||||
string deviceId,
|
||||
string? deviceName,
|
||||
ClientPlatform platform,
|
||||
Instant? expiredAt = null
|
||||
)
|
||||
{
|
||||
var device = await GetOrCreateDeviceAsync(parentSession.AccountId, deviceId, deviceName, platform);
|
||||
|
||||
var now = SystemClock.Instance.GetCurrentInstant();
|
||||
var session = new SnAuthSession
|
||||
{
|
||||
AccountId = parentSession.AccountId,
|
||||
CreatedAt = now,
|
||||
LastGrantedAt = now,
|
||||
ExpiredAt = expiredAt,
|
||||
ParentSessionId = parentSession.Id,
|
||||
ClientId = device.Id,
|
||||
};
|
||||
|
||||
db.AuthSessions.Add(session);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return session;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user