-
Notifications
You must be signed in to change notification settings - Fork 1
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 #151 from soat-fiap/cognito_gateway
Cognito gateway
- Loading branch information
Showing
27 changed files
with
531 additions
and
24 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,8 @@ | ||
namespace FIAP.TechChallenge.ByteMeBurger.Api.Auth; | ||
|
||
public static class BmbRoles | ||
{ | ||
public const string Admin = "admin"; | ||
public const string Kitchen = "kitchen"; | ||
public const string Customer = "customer"; | ||
} |
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
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
17 changes: 17 additions & 0 deletions
17
src/FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway/CognitoClientFactory.cs
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,17 @@ | ||
using Amazon; | ||
using Amazon.CognitoIdentityProvider; | ||
using Amazon.Runtime; | ||
using FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway.Factory; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway; | ||
|
||
public class CognitoClientFactory(IOptions<CognitoSettings> settings) : ICognitoClientFactory | ||
{ | ||
public IAmazonCognitoIdentityProvider CreateClient() | ||
{ | ||
return new AmazonCognitoIdentityProviderClient( | ||
new BasicAWSCredentials(settings.Value.ClientId, settings.Value.ClientSecret), | ||
RegionEndpoint.GetBySystemName(settings.Value.Region)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway/CognitoSettings.cs
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,38 @@ | ||
namespace FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway; | ||
|
||
/// <summary> | ||
/// Cognito User Pool settings | ||
/// </summary> | ||
public class CognitoSettings | ||
{ | ||
/// <summary> | ||
/// User Pool Id | ||
/// </summary> | ||
public string UserPoolId { get; set; } = string.Empty; | ||
|
||
|
||
/// <summary> | ||
/// Client Id | ||
/// </summary> | ||
public string UserPoolClientId { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Enabled | ||
/// </summary> | ||
public bool Enabled { get; set; } = false; | ||
|
||
/// <summary> | ||
/// AWS Region | ||
/// </summary> | ||
public string Region { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// AWS Secret Id | ||
/// </summary> | ||
public string ClientSecret { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// AWS Client Id | ||
/// </summary> | ||
public string ClientId { get; set; } = string.Empty; | ||
} |
122 changes: 122 additions & 0 deletions
122
src/FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway/CognitoUserManager.cs
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,122 @@ | ||
using System.Security.Cryptography; | ||
using FIAP.TechChallenge.ByteMeBurger.Domain.Entities; | ||
using FIAP.TechChallenge.ByteMeBurger.Domain.Interfaces; | ||
using Amazon.CognitoIdentityProvider; | ||
using Amazon.CognitoIdentityProvider.Model; | ||
using FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway.Factory; | ||
using FIAP.TechChallenge.ByteMeBurger.Domain.Base; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway; | ||
|
||
public class CognitoUserManager : ICustomerRepository | ||
{ | ||
private readonly IAmazonCognitoIdentityProvider _cognitoClient; | ||
private readonly string _userPoolId; | ||
private readonly string _clientId; | ||
|
||
public CognitoUserManager(ICognitoClientFactory cognitoClientFactory, IOptions<CognitoSettings> settings) | ||
{ | ||
_cognitoClient = cognitoClientFactory.CreateClient(); | ||
_userPoolId = settings.Value.UserPoolId; | ||
_clientId = settings.Value.UserPoolClientId; | ||
} | ||
|
||
public async Task<Customer?> FindByCpfAsync(string cpf) | ||
{ | ||
try | ||
{ | ||
var response = await _cognitoClient.AdminGetUserAsync(new AdminGetUserRequest | ||
{ | ||
UserPoolId = _userPoolId, | ||
Username = cpf | ||
}); | ||
|
||
var email = response.UserAttributes.First(attr => attr.Name == "email").Value; | ||
var name = response.UserAttributes.First(attr => attr.Name == "name").Value; | ||
var sub = response.UserAttributes.First(attr => attr.Name == "sub").Value; | ||
var customer = new Customer(Guid.Parse(sub), cpf, name, email); | ||
|
||
return customer; | ||
} | ||
catch (UserNotFoundException) | ||
{ | ||
return null; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error fetching user: {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
|
||
public async Task<Customer> CreateAsync(Customer customer) | ||
{ | ||
try | ||
{ | ||
var signUpResponse = await _cognitoClient.AdminCreateUserAsync(new AdminCreateUserRequest() | ||
{ | ||
Username = customer.Cpf, | ||
UserPoolId = _userPoolId, | ||
UserAttributes = | ||
{ | ||
new AttributeType { Name = "email", Value = customer.Email }, | ||
new AttributeType { Name = "name", Value = customer.Name } | ||
} | ||
}); | ||
|
||
customer.Id = Guid.Parse(signUpResponse.User.Attributes.First(a=>a.Name is "sub").Value); | ||
return customer; | ||
} | ||
catch (UsernameExistsException ex) | ||
{ | ||
Console.WriteLine($"Error registering user: {ex.Message}"); | ||
throw new DomainException("There's already a customer using the provided CPF value."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error registering user: {ex.Message}"); | ||
throw; | ||
} | ||
} | ||
|
||
private static string GenerateRandomPassword(int length) | ||
{ | ||
using var rng = RandomNumberGenerator.Create(); | ||
var characterSets = new[] | ||
{ | ||
"abcdefghijklmnopqrstuvwxyz", | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", | ||
"1234567890", | ||
"!@#$%^&*()" | ||
}; | ||
var allChars = string.Concat(characterSets); | ||
var passwordChars = new char[length]; | ||
// Ensure the password contains at least one character from each character set | ||
for (int i = 0; i < characterSets.Length && i < length; i++) | ||
{ | ||
passwordChars[i] = GetRandomChar(characterSets[i], rng); | ||
} | ||
// Fill the rest of the password with random characters | ||
for (int i = characterSets.Length; i < length; i++) | ||
{ | ||
passwordChars[i] = GetRandomChar(allChars, rng); | ||
} | ||
// Shuffle the password to ensure randomness | ||
passwordChars = passwordChars.OrderBy(_ => GetRandomInt(rng, int.MaxValue)).ToArray(); | ||
return new string(passwordChars); | ||
} | ||
private static char GetRandomChar(string chars, RandomNumberGenerator rng) | ||
{ | ||
var bytes = new byte[4]; | ||
rng.GetBytes(bytes); | ||
var index = BitConverter.ToUInt32(bytes, 0) % chars.Length; | ||
return chars[(int)index]; | ||
} | ||
private static int GetRandomInt(RandomNumberGenerator rng, int max) | ||
{ | ||
var bytes = new byte[4]; | ||
rng.GetBytes(bytes); | ||
return (int)(BitConverter.ToUInt32(bytes, 0) % max); | ||
} | ||
} |
Oops, something went wrong.