Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pm): set correct AuthorName with API key #232

Merged
merged 6 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,27 @@ public async Task<ActionResult> MarkMyPrivateMessageAsReadAsync(
/// with the same recipient uid, it will push a toast message to those devices.
/// The toast message content is defined by the `ToastTitle` and `ToastMessage` properties.
/// </remarks>
/// <param name="Request"></param>
/// <param name="request"></param>
/// <returns>The `Id` of the message that has been delivered.</returns>
/// <response code="400">Unable to parse `Request`</response>
[Authorize(AuthenticationSchemes = $"{ApiKeyAuthenticationDefaults.AuthenticationScheme},{OAuth2IntrospectionDefaults.AuthenticationScheme}", Roles = "Admin,PrivateMessageSender")]
[HttpPost("PrivateMessages")]
[HttpPost("PrivateMessages/:byRegistrationId")]
[ProducesResponseType(typeof(Guid), 200)]
public async Task<ActionResult> SendPrivateMessageAsync(
[FromBody] SendPrivateMessageByRegSysRequest Request,
[FromBody] SendPrivateMessageByRegSysRequest request,
CancellationToken cancellationToken = default)
{
if (Request == null) return BadRequest();
if (request == null) return BadRequest();

if (User.IsInRole("Attendee"))
// Only admins may set the AuthorName via the API
if (!User.IsInRole("Admin") || string.IsNullOrWhiteSpace(request.AuthorName))
{
Request.AuthorName = User.GetName();
request.AuthorName = User.GetName();
}

return Json(await _privateMessageService.SendPrivateMessageAsync(
Request,
request,
User.GetSubject(),
cancellationToken
));
Expand All @@ -118,25 +119,26 @@ public async Task<ActionResult> SendPrivateMessageAsync(
/// with the same recipient uid, it will push a toast message to those devices.
/// The toast message content is defined by the `ToastTitle` and `ToastMessage` properties.
/// </remarks>
/// <param name="Request"></param>
/// <param name="request"></param>
/// <returns>The `Id` of the message that has been delivered.</returns>
/// <response code="400">Unable to parse `Request`</response>
[Authorize(AuthenticationSchemes = $"{ApiKeyAuthenticationDefaults.AuthenticationScheme},{OAuth2IntrospectionDefaults.AuthenticationScheme}", Roles = "Admin,PrivateMessageSender")]
[HttpPost("PrivateMessages/:byIdentityId")]
[ProducesResponseType(typeof(Guid), 200)]
public async Task<ActionResult> SendPrivateMessageIdentityAsync(
[FromBody] SendPrivateMessageByIdentityRequest Request,
[FromBody] SendPrivateMessageByIdentityRequest request,
CancellationToken cancellationToken = default)
{
if (Request == null) return BadRequest();
if (request == null) return BadRequest();

if (User.IsInRole("Attendee"))
// Only admins may set the AuthorName via the API
if (!User.IsInRole("Admin") || string.IsNullOrWhiteSpace(request.AuthorName))
{
Request.AuthorName = User.GetName();
request.AuthorName = User.GetName();
}

return Json(await _privateMessageService.SendPrivateMessageAsync(
Request,
request,
User.GetSubject(),
cancellationToken
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ protected override Task<AuthenticateResult> HandleAuthenticateAsync()

if (Options.ApiKeys.FirstOrDefault(apiKey => apiKey.Key == requestApiKey && DateTime.Now.CompareTo(apiKey.ValidUntil) <= 0) is { } apiKeyOptions)
{
Logger.LogInformation($"Configured API key for {apiKeyOptions.PrincipalName} with roles {string.Join(',', apiKeyOptions.Roles)} valid until {apiKeyOptions.ValidUntil}.");
Logger.LogInformation($"Matched API key for {apiKeyOptions.PrincipalName} with roles {string.Join(',', apiKeyOptions.Roles)} valid until {apiKeyOptions.ValidUntil}.");

var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, apiKeyOptions.PrincipalName),
new Claim("sub", apiKeyOptions.PrincipalName)
new Claim("name", apiKeyOptions.PrincipalName),
new Claim("sub", $"{ApiKeyAuthenticationDefaults.AuthenticationScheme}:{apiKeyOptions.PrincipalName}"),
new Claim(ClaimTypes.Role, ApiKeyAuthenticationDefaults.AuthenticationScheme)
};

foreach (var role in apiKeyOptions.Roles)
Expand Down