-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #527 from lanedirt/525-prevent-email-address-colli…
…sion-from-occuring Prevent email address collision from occurring during identity generation
- Loading branch information
Showing
12 changed files
with
795 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="IdentityController.cs" company="lanedirt"> | ||
// Copyright (c) lanedirt. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace AliasVault.Api.Controllers; | ||
|
||
using AliasServerDb; | ||
using AliasVault.Api.Controllers.Abstracts; | ||
using AliasVault.Api.Helpers; | ||
using Asp.Versioning; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
/// <summary> | ||
/// Controller for generating identities taking into account existing information on the AliasVault server. | ||
/// </summary> | ||
/// <param name="userManager">UserManager instance.</param> | ||
/// <param name="dbContextFactory">DbContextFactory instance.</param> | ||
[ApiVersion("1")] | ||
public class IdentityController(UserManager<AliasVaultUser> userManager, IAliasServerDbContextFactory dbContextFactory) : AuthenticatedRequestController(userManager) | ||
{ | ||
/// <summary> | ||
/// Verify that provided email address is not already taken by another user. | ||
/// </summary> | ||
/// <param name="email">The full email address to check.</param> | ||
/// <returns>True if the email address is already taken, false otherwise.</returns> | ||
[HttpPost("CheckEmail/{email}")] | ||
public async Task<IActionResult> CheckEmail(string email) | ||
{ | ||
var user = await GetCurrentUserAsync(); | ||
if (user == null) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
bool isTaken = await EmailClaimExistsAsync(email); | ||
return Ok(new { isTaken }); | ||
} | ||
|
||
/// <summary> | ||
/// Verify that provided email address is not already taken by another user. | ||
/// </summary> | ||
/// <param name="email">The email address to check.</param> | ||
/// <returns>True if the email address is already taken, false otherwise.</returns> | ||
private async Task<bool> EmailClaimExistsAsync(string email) | ||
{ | ||
await using var context = await dbContextFactory.CreateDbContextAsync(); | ||
|
||
var sanitizedEmail = EmailHelper.SanitizeEmail(email); | ||
var claimExists = await context.UserEmailClaims.FirstOrDefaultAsync(c => c.Address == sanitizedEmail); | ||
|
||
return claimExists != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="EmailHelper.cs" company="lanedirt"> | ||
// Copyright (c) lanedirt. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace AliasVault.Api.Helpers; | ||
|
||
/// <summary> | ||
/// EmailHelper class which contains helper methods for email. | ||
/// </summary> | ||
public static class EmailHelper | ||
{ | ||
/// <summary> | ||
/// Sanitize email address by trimming and converting to lowercase. | ||
/// </summary> | ||
/// <param name="email">Email address to sanitize.</param> | ||
/// <returns>Sanitized email address.</returns> | ||
public static string SanitizeEmail(string email) | ||
{ | ||
return email.Trim().ToLower(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.