From 0586c201c573eb80dd4dbff608db0ba23e83b11d Mon Sep 17 00:00:00 2001 From: Charles Showalter Date: Thu, 19 May 2022 15:47:12 -0700 Subject: [PATCH] emr push --- API/Controllers/AccountController.cs | 41 +++++++++++++++++-- API/Controllers/BuggyController.cs | 8 ++++ .../ApplicationServicesExtensions.cs | 2 + API/Extensions/IdentityServiceExtensions.cs | 17 +++++++- API/Startup.cs | 3 +- API/appsettings.Development.json | 6 ++- Infrastructure/Services/TokenService.cs | 4 +- 7 files changed, 72 insertions(+), 9 deletions(-) diff --git a/API/Controllers/AccountController.cs b/API/Controllers/AccountController.cs index fe2e7dd..5febf8d 100644 --- a/API/Controllers/AccountController.cs +++ b/API/Controllers/AccountController.cs @@ -1,6 +1,9 @@ +using System.Security.Claims; using API.Dtos; using API.Errors; using Core.Entities.Identity; +using Core.Interfaces; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; @@ -10,12 +13,43 @@ namespace API.Controllers { private readonly UserManager _userManager; private readonly SignInManager _signInManager; - public AccountController(UserManager userManager, SignInManager signInManager) + private readonly ITokenService _tokenService; + public AccountController(UserManager userManager, SignInManager signInManager, ITokenService tokenService) { + _tokenService = tokenService; _signInManager = signInManager; _userManager = userManager; } + [Authorize] + [HttpGet] + public async Task> GetCurrentUser() + { + var email = User.FindFirstValue(ClaimTypes.Email); + var user = await _userManager.FindByEmailAsync(email); + return new UserDto + { + Email = user.Email, + Token = _tokenService.CreateToken(user), + DisplayName = user.DisplayName + }; + } + + [HttpGet("emailexists")] + public async Task> CheckEmailExistsAsync([FromQuery] string email){ + return await _userManager.FindByEmailAsync(email) != null; + } + + [Authorize] + [HttpGet("address")] + public async Task> GetUserAddress() + { + var email = User.FindFirstValue(ClaimTypes.Email); + var user = await _userManager.FindByEmailAsync(email); + return user.Address; + + } + [HttpPost("login")] public async Task> Login(LoginDto loginDto) { @@ -26,7 +60,7 @@ namespace API.Controllers return new UserDto { Email = user.Email, - Token = "This will be a token", + Token = _tokenService.CreateToken(user), DisplayName = user.DisplayName }; } @@ -46,9 +80,10 @@ namespace API.Controllers return new UserDto { DisplayName = user.DisplayName, - Token = "This will be a token", + Token = _tokenService.CreateToken(user), Email = user.Email }; } + } } \ No newline at end of file diff --git a/API/Controllers/BuggyController.cs b/API/Controllers/BuggyController.cs index 6504349..c5503dc 100644 --- a/API/Controllers/BuggyController.cs +++ b/API/Controllers/BuggyController.cs @@ -1,5 +1,6 @@ using API.Errors; using Infrastructure.Data; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace API.Controllers @@ -12,6 +13,13 @@ namespace API.Controllers _context = context; } + [HttpGet("testauth")] + [Authorize] + public ActionResult GetSecretText() + { + return "secret stuff"; + } + [HttpGet("notfound")] public ActionResult GetNotFoundRequest() { diff --git a/API/Extensions/ApplicationServicesExtensions.cs b/API/Extensions/ApplicationServicesExtensions.cs index 48be291..28b9d88 100644 --- a/API/Extensions/ApplicationServicesExtensions.cs +++ b/API/Extensions/ApplicationServicesExtensions.cs @@ -1,6 +1,7 @@ using API.Errors; using Core.Interfaces; using Infrastructure.Data; +using Infrastructure.Services; using Microsoft.AspNetCore.Mvc; namespace API.Extensions @@ -9,6 +10,7 @@ namespace API.Extensions { public static IServiceCollection AddApplicationServices(this IServiceCollection services) { + services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>))); diff --git a/API/Extensions/IdentityServiceExtensions.cs b/API/Extensions/IdentityServiceExtensions.cs index c43a1a8..8d0ef2f 100644 --- a/API/Extensions/IdentityServiceExtensions.cs +++ b/API/Extensions/IdentityServiceExtensions.cs @@ -1,19 +1,32 @@ +using System.Text; using Core.Entities.Identity; using Infrastructure.Identity; +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; +using Microsoft.IdentityModel.Tokens; namespace API.Extensions { public static class IdentityServiceExtensions { - public static IServiceCollection AddIdentityServices(this IServiceCollection services) + public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration config) { var builder = services.AddIdentityCore(); builder = new IdentityBuilder(builder.UserType, builder.Services); builder.AddEntityFrameworkStores(); builder.AddSignInManager>(); - services.AddAuthentication(); + services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options =>{ + options.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Token:Key"])), + ValidIssuer = config["Token:Issuer"], + ValidateIssuer = true, + ValidateAudience = false + }; + }); return services; } diff --git a/API/Startup.cs b/API/Startup.cs index f926793..f1780f7 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -23,7 +23,7 @@ namespace API services.AddControllers(); services.AddApplicationServices(); - services.AddIdentityServices(); + services.AddIdentityServices(_config); services.AddSwaggerDocumentation(); services.AddDbContext(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection"))); services.AddDbContext(x => x.UseSqlite(_config.GetConnectionString("IdentityConnection"))); @@ -51,6 +51,7 @@ namespace API app.UseRouting(); app.UseStaticFiles(); app.UseCors("CorsPolicy"); + app.UseAuthentication(); app.UseAuthorization(); app.UseSwaggerDocumentation(); app.UseEndpoints(endpoints => diff --git a/API/appsettings.Development.json b/API/appsettings.Development.json index 972b855..3b22f0e 100644 --- a/API/appsettings.Development.json +++ b/API/appsettings.Development.json @@ -2,7 +2,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Microsoft.AspNetCore": "Information" } }, "ConnectionStrings": { @@ -10,5 +10,9 @@ "IdentityConnection": "Data source=indentity.db", "Redis": "localhost" }, + "Token": { + "Key": "super secret key", + "Issuer": "https://localhost:5001" + }, "ApiUrl": "https://localhost:5001/" } diff --git a/Infrastructure/Services/TokenService.cs b/Infrastructure/Services/TokenService.cs index 2c1a2c1..b703dbd 100644 --- a/Infrastructure/Services/TokenService.cs +++ b/Infrastructure/Services/TokenService.cs @@ -22,8 +22,8 @@ namespace Infrastructure.Services { var claims = new List { - new Claim(ClaimTypes.Email, user.Email), - new Claim(ClaimTypes.GivenName, user.DisplayName) + new Claim(JwtRegisteredClaimNames.Email, user.Email), + new Claim(JwtRegisteredClaimNames.GivenName, user.DisplayName) }; var creds = new SigningCredentials(_key, SecurityAlgorithms.HmacSha512Signature);