astral-api/Astral.ApiServer/Models/TokenGrantResultModel.cs

95 lines
2.7 KiB
C#

// <copyright file="TokenGrantResultModel.cs" company="alveus.dev">
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
// </copyright>
using System.Text.Json.Serialization;
using Astral.ApiServer.Models.Common;
using Astral.Core.Extensions;
using Astral.Services.Dtos;
namespace Astral.ApiServer.Models;
/// <summary>
/// OAuth Grant Request Response.
/// </summary>
public class TokenGrantResultModel : ResultModel
{
/// <summary>
/// Initializes a new instance of the <see cref="TokenGrantResultModel"/> class.
/// </summary>
public TokenGrantResultModel()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TokenGrantResultModel"/> class.
/// </summary>
/// <param name="sessionDto">Instance of <see cref="SessionDto"/> to create from.</param>
public TokenGrantResultModel(SessionDto sessionDto)
{
Success = true;
AccessToken = sessionDto.AccessToken;
CreatedAt = sessionDto.CreatedAt.Ticks;
ExpiresIn = sessionDto.AccessTokenExpires.Ticks;
RefreshToken = sessionDto.RefreshToken;
Scope = sessionDto.Scope.ToString().ToLowerInvariant();
AccountId = sessionDto.UserId.ToString();
AccountName = sessionDto.UserName;
TokenType = "Bearer";
AccountRoles = sessionDto.Role.GetAccessibleRoles().Select(role => role.ToString().ToLowerInvariant()).ToList();
}
/// <summary>
/// The granted access token.
/// </summary>
[JsonPropertyName("access_token")]
public string AccessToken { get; set; }
/// <summary>
/// When it expires (ticks).
/// </summary>
[JsonPropertyName("expires_in")]
public long ExpiresIn { get; set; }
/// <summary>
/// The granted refresh token.
/// </summary>
[JsonPropertyName("refresh_token")]
public string RefreshToken { get; set; }
/// <summary>
/// The session scope.
/// </summary>
[JsonPropertyName("scope")]
public string Scope { get; set; }
/// <summary>
/// Granted token type.
/// </summary>
[JsonPropertyName("token_type")]
public string TokenType { get; set; }
/// <summary>
/// When it was created (ticks).
/// </summary>
[JsonPropertyName("created_at")]
public long CreatedAt { get; set; }
/// <summary>
/// The user's id.
/// </summary>
[JsonPropertyName("account_id")]
public string AccountId { get; set; }
/// <summary>
/// The user's name.
/// </summary>
[JsonPropertyName("account_name")]
public string AccountName { get; set; }
/// <summary>
/// The user's roles.
/// </summary>
[JsonPropertyName("account_roles")]
public List<string> AccountRoles { get; set; }
}