Skip to content

Commit

Permalink
Fix the response type for the sign in APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfowl committed Jul 13, 2023
1 parent 34b8c87 commit 5a3c961
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
45 changes: 45 additions & 0 deletions TodoApi/Users/AccessTokenResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Text.Json.Serialization;

// Copied from https://github.com/dotnet/aspnetcore/blob/bad855959a99257bc6f194dd19ecd6c9aeb03acb/src/Shared/BearerToken/DTO/AccessTokenResponse.cs

namespace TodoApi;

internal sealed class AccessTokenResponse
{
/// <summary>
/// The value is always "Bearer" which indicates this response provides a "Bearer" token
/// in the form of an opaque <see cref="AccessToken"/>.
/// </summary>
/// <remarks>
/// This is serialized as "token_type": "Bearer" using System.Text.Json.
/// </remarks>
[JsonPropertyName("token_type")]
public string TokenType { get; } = "Bearer";

/// <summary>
/// The opaque bearer token to send as part of the Authorization request header.
/// </summary>
/// <remarks>
/// This is serialized as "access_token": "{AccessToken}" using System.Text.Json.
/// </remarks>
[JsonPropertyName("access_token")]
public required string AccessToken { get; init; }

/// <summary>
/// The number of seconds before the <see cref="AccessToken"/> expires.
/// </summary>
/// <remarks>
/// This is serialized as "expires_in": "{ExpiresInSeconds}" using System.Text.Json.
/// </remarks>
[JsonPropertyName("expires_in")]
public required long ExpiresInSeconds { get; init; }

/// <summary>
/// If set, this provides the ability to get a new access_token after it expires using a refresh endpoint.
/// </summary>
/// <remarks>
/// This is serialized as "refresh_token": "{RefreshToken}" using System.Text.Json.
/// </remarks>
[JsonPropertyName("refresh_token")]
public required string RefreshToken { get; init; }
}
4 changes: 2 additions & 2 deletions TodoApi/Users/UsersApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static RouteGroupBuilder MapUsers(this IEndpointRouteBuilder routes)
return TypedResults.ValidationProblem(result.Errors.ToDictionary(e => e.Code, e => new[] { e.Description }));
});

group.MapPost("/token", async Task<Results<BadRequest, SignInHttpResult>> (UserInfo userInfo, UserManager<TodoUser> userManager) =>
group.MapPost("/token", async Task<Results<BadRequest, SignInHttpResult, Ok<AccessTokenResponse>>> (UserInfo userInfo, UserManager<TodoUser> userManager) =>
{
var user = await userManager.FindByNameAsync(userInfo.Username);
Expand All @@ -41,7 +41,7 @@ public static RouteGroupBuilder MapUsers(this IEndpointRouteBuilder routes)
return TypedResults.SignIn(principal, authenticationScheme: BearerTokenDefaults.AuthenticationScheme);
});

group.MapPost("/token/{provider}", async Task<Results<SignInHttpResult, ValidationProblem>> (string provider, ExternalUserInfo userInfo, UserManager<TodoUser> userManager) =>
group.MapPost("/token/{provider}", async Task<Results<SignInHttpResult, ValidationProblem, Ok<AccessTokenResponse>>> (string provider, ExternalUserInfo userInfo, UserManager<TodoUser> userManager) =>
{
var user = await userManager.FindByLoginAsync(provider, userInfo.ProviderKey);
Expand Down

0 comments on commit 5a3c961

Please sign in to comment.