-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28012cb
commit 7b07cf0
Showing
23 changed files
with
456 additions
and
47 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,70 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using apekade.Dto; | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace apekade.Controllers; | ||
|
||
[Route("api/[controller]")] | ||
[ApiController] | ||
// [Authorize] | ||
public class TestController : ControllerBase | ||
{ | ||
|
||
[HttpGet] | ||
public IActionResult GetServerStatus() | ||
{ | ||
var response = new ApiRes<object>{ | ||
Status = true, | ||
Code= 200, | ||
Data = new { Message = "Server Online" } | ||
}; | ||
return Ok(response); | ||
|
||
} | ||
// Only SuperAdmin can access this endpoint | ||
[Authorize(Roles = "SUPER_ADMIN")] | ||
[HttpGet("superadmin")] | ||
public IActionResult SuperAdminOnly() | ||
{ | ||
return Ok("Only SuperAdmin can access this."); | ||
} | ||
|
||
// Only Seller can access this endpoint | ||
[Authorize(Roles = "SELLER")] | ||
[HttpGet("seller")] | ||
public IActionResult SellerOnly() | ||
{ | ||
return Ok("Only Sellers can access this."); | ||
} | ||
|
||
// Only Buyer can access this endpoint | ||
[Authorize(Roles = "BUYER")] | ||
[HttpGet("buyer")] | ||
public IActionResult BuyerOnly() | ||
{ | ||
return Ok("Only Buyers can access this."); | ||
} | ||
|
||
// Both Seller and Buyer can access this endpoint | ||
[Authorize(Roles = "SELLER,BUYER")] | ||
[HttpGet("seller-buyer")] | ||
public IActionResult SellerAndBuyerAccess() | ||
{ | ||
return Ok("Both Sellers and Buyers can access this."); | ||
} | ||
|
||
// Any authenticated user can access this endpoint | ||
[Authorize] | ||
[HttpGet("common")] | ||
public IActionResult CommonAccess() | ||
{ | ||
return Ok("Any authenticated user can access this."); | ||
} | ||
|
||
// Any one can access this endpoint | ||
[HttpGet("open")] | ||
public IActionResult OpenAccess() | ||
{ | ||
return Ok("Any one can access this."); | ||
} | ||
} |
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,23 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using apekade.Enums; | ||
using apekade.Services; | ||
using apekade.Dto.UserDto; | ||
|
||
namespace apekade.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class UserController : ControllerBase{ | ||
private readonly IUserService _userService; | ||
|
||
public UserController(IUserService userService){ | ||
_userService = userService; | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> CreateUser([FromBody] UserReqtDto userReqtDto){ | ||
|
||
var response = await _userService.CreateNewUser(userReqtDto); | ||
return Ok(response); | ||
} | ||
} |
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 apekade.Enums; | ||
|
||
public enum Role | ||
{ | ||
ADMIN, | ||
SELLER, | ||
BUYER | ||
} |
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,40 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using Microsoft.IdentityModel.Tokens; | ||
using apekade.Models; | ||
|
||
namespace apekade.Helpers; | ||
|
||
public class GenerateJwtToken{ | ||
private readonly IConfiguration _configuration; | ||
|
||
public GenerateJwtToken(IConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
public string GenerateJwt(User user) | ||
{ | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
|
||
var appSettingToken = _configuration.GetSection("AppSettings:Token").Value; | ||
if (appSettingToken is null) | ||
throw new Exception("AppSettings Token is null!"); | ||
|
||
var key = Encoding.UTF8.GetBytes(appSettingToken); | ||
|
||
var tokenDescriptor = new SecurityTokenDescriptor | ||
{ | ||
Subject = new ClaimsIdentity(new[] | ||
{ | ||
new Claim(ClaimTypes.NameIdentifier, user.Id), | ||
new Claim(ClaimTypes.Role, user.Role.ToString()) | ||
}), | ||
Expires = DateTime.UtcNow.AddHours(1), | ||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) | ||
}; | ||
|
||
var token = tokenHandler.CreateToken(tokenDescriptor); | ||
return tokenHandler.WriteToken(token); | ||
} | ||
} |
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,19 @@ | ||
using System.Security.Cryptography; | ||
|
||
namespace apekade.Helpers; | ||
|
||
public class HashPassword{ | ||
public static void CreatePasswordHash(string password, out string passwordHash, out string passwordSalt) | ||
{ | ||
using var hmac = new HMACSHA512(); | ||
passwordSalt = Convert.ToBase64String(hmac.Key); | ||
passwordHash = Convert.ToBase64String(hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password))); | ||
} | ||
|
||
public static bool VerifyPasswordHash(string password, string storedHash, string storedSalt) | ||
{ | ||
using var hmac = new HMACSHA512(Convert.FromBase64String(storedSalt)); | ||
var computedHash = Convert.ToBase64String(hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password))); | ||
return computedHash == storedHash; | ||
} | ||
} |
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,18 @@ | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
using apekade.Enums; | ||
|
||
namespace apekade.Models; | ||
|
||
public class User{ | ||
[BsonId] | ||
[BsonRepresentation(BsonType.ObjectId)] | ||
public required string Id { get; set; } | ||
public required string FirstName { get; set; } | ||
public string? LastName { get; set; } | ||
public required string Email { get; set; } | ||
public required string PasswordHash { get; set; } | ||
public required string PasswordSalt { get; set; } | ||
[BsonRepresentation((BsonType.String))] | ||
public required Role Role { get; set; } | ||
} |
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,19 @@ | ||
using MongoDB.Driver; | ||
using apekade.Models; | ||
|
||
namespace apekade.Repositories; | ||
|
||
public class UserRepository | ||
{ | ||
private readonly IMongoCollection<User> _usersCollection; | ||
|
||
public UserRepository(IMongoDatabase database) | ||
{ | ||
_usersCollection = database.GetCollection<User>("Users"); | ||
} | ||
|
||
public async Task save(User user) | ||
{ | ||
await _usersCollection.InsertOneAsync(user); | ||
} | ||
} |
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,9 @@ | ||
using apekade.Dto; | ||
using apekade.Dto.UserDto; | ||
|
||
namespace apekade.Services; | ||
|
||
public interface IUserService | ||
{ | ||
Task<ApiRes<UserTokenResDto>> CreateNewUser(UserReqtDto userReqtDto); | ||
} |
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,57 @@ | ||
using AutoMapper; | ||
using apekade.Models; | ||
using apekade.Dto; | ||
using apekade.Repositories; | ||
using apekade.Helpers; | ||
using apekade.Services.Impl; | ||
using apekade.Dto.UserDto; | ||
|
||
namespace apekade.Services.Impl; | ||
|
||
public class UserService : IUserService | ||
{ | ||
private readonly IMapper _mapper; | ||
|
||
private readonly UserRepository _userRepository; | ||
|
||
private readonly GenerateJwtToken _generateJwtToken; | ||
|
||
public UserService(IMapper mapper, UserRepository userRepository, GenerateJwtToken generateJwtToken) | ||
{ | ||
_mapper = mapper; | ||
_userRepository = userRepository; | ||
_generateJwtToken = generateJwtToken; | ||
} | ||
|
||
public async Task<ApiRes<UserTokenResDto>> CreateNewUser(UserReqtDto userRequestDto) | ||
{ | ||
var response = new ApiRes<UserTokenResDto>(); | ||
|
||
try | ||
{ | ||
var newUser = _mapper.Map<User>(userRequestDto); | ||
HashPassword.CreatePasswordHash(userRequestDto.Password, out var passwordHash, out var passwordSalt); | ||
newUser.PasswordHash = passwordHash; | ||
newUser.PasswordSalt = passwordSalt; | ||
|
||
await _userRepository.save(newUser); | ||
|
||
var token = _generateJwtToken.GenerateJwt(newUser); | ||
|
||
var userResponse = _mapper.Map<UserResDto>(newUser); | ||
|
||
response.Status = true; | ||
response.Code = 201; | ||
response.Data = new UserTokenResDto { User = userResponse, Token = token }; | ||
response.Message = "User created successfully!"; | ||
} | ||
catch (Exception ex) | ||
{ | ||
response.Status = false; | ||
response.Code = 500; | ||
response.Message = ex.Message; | ||
} | ||
|
||
return response; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,33 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using MongoDB.Driver; | ||
using apekade.Data; | ||
|
||
namespace apekade.Configuration; | ||
|
||
public static class DbContextConfiguration | ||
{ | ||
public static void ConfigureDbContextServices(IServiceCollection services, IConfiguration configuration) | ||
{ | ||
// Register the DbSettings configuration | ||
services.Configure<DbSettings>(configuration.GetSection(nameof(DbSettings))); | ||
// Register the DbSettings configuration | ||
services.Configure<DbSettings>(configuration.GetSection(nameof(DbSettings))); | ||
|
||
// Register the MongoDB DbContext | ||
// services.AddSingleton<DbContext>(); | ||
|
||
services.AddSingleton(provider =>{ | ||
var dbSettings = provider.GetService<IOptions<DbSettings>>() ?? throw new InvalidOperationException("MongoDbSettings is not configured properly"); | ||
// Register the MongoDB DbContext | ||
// service is created once for all. | ||
services.AddSingleton(provider => | ||
{ | ||
var dbSettings = provider.GetRequiredService<IOptions<DbSettings>>() ?? throw new InvalidOperationException("MongoDbSettings is not configured properly"); | ||
// Initialize DbContext with the DbSettings | ||
return new DbContext(dbSettings); | ||
return new DbContext(dbSettings); | ||
}); | ||
|
||
// service is created once per HTTP request in web applications. | ||
services.AddScoped(sp => | ||
{ | ||
var settings = sp.GetRequiredService<IOptions<DbSettings>>().Value; | ||
var client = sp.GetRequiredService<IMongoClient>(); | ||
return client.GetDatabase(settings.DatabaseName); | ||
}); | ||
} | ||
} |
Oops, something went wrong.