-
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
6f83583
commit 3848266
Showing
11 changed files
with
247 additions
and
5 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
WebApiAuthenticationJWT/Controllers/AuthenticationController.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,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using WebApiAuthenticationJWT.Models; | ||
using WebApiAuthenticationJWT.Services.Interfaces; | ||
|
||
namespace WebApiAuthenticationJWT.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class AuthenticationController : Controller | ||
{ | ||
private readonly IAuthenticateService _authService; | ||
public AuthenticationController(IAuthenticateService authService) | ||
{ | ||
_authService = authService; | ||
} | ||
|
||
[AllowAnonymous] | ||
[HttpPost, Route("request")] | ||
public IActionResult RequestToken([FromBody] TokenRequest request) | ||
{ | ||
|
||
if (!ModelState.IsValid) | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
|
||
string token; | ||
if (_authService.IsAuthenticated(request, out token)) | ||
{ | ||
return Ok(token); | ||
} | ||
|
||
return BadRequest("Invalid Request"); | ||
} | ||
} | ||
} |
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,28 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace WebApiAuthenticationJWT.Models | ||
{ | ||
[JsonObject("tokenManagement")] | ||
public class TokenManagement | ||
{ | ||
[JsonProperty("secret")] | ||
public string Secret { get; set; } | ||
|
||
[JsonProperty("issuer")] | ||
public string Issuer { get; set; } | ||
|
||
[JsonProperty("audience")] | ||
public string Audience { get; set; } | ||
|
||
[JsonProperty("accessExpiration")] | ||
public int AccessExpiration { get; set; } | ||
|
||
[JsonProperty("refreshExpiration")] | ||
public int RefreshExpiration { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace WebApiAuthenticationJWT.Models | ||
{ | ||
public class TokenRequest | ||
{ | ||
[Required] | ||
[JsonProperty("username")] | ||
public string Username { get; set; } | ||
|
||
|
||
[Required] | ||
[JsonProperty("password")] | ||
public string Password { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace WebApiAuthenticationJWT.Models | ||
{ | ||
public class User | ||
{ | ||
public int Id { get; set; } | ||
public string UserName { get; set; } | ||
public string Email { get; set; } | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
public string PasswordHash { get; set; } | ||
public string Role { get; set; } | ||
public DateTime CreatedAt { get; set; } | ||
public DateTime UpdatedAt { get; set; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
WebApiAuthenticationJWT/Services/Interfaces/IAuthenticateService.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,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using WebApiAuthenticationJWT.Models; | ||
|
||
namespace WebApiAuthenticationJWT.Services.Interfaces | ||
{ | ||
public interface IAuthenticateService | ||
{ | ||
bool IsAuthenticated(TokenRequest request, out string token); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
WebApiAuthenticationJWT/Services/Interfaces/IUserManagementService.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,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace WebApiAuthenticationJWT.Services.Interfaces | ||
{ | ||
public interface IUserManagementService | ||
{ | ||
bool IsValidUser(string username, string password); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
WebApiAuthenticationJWT/Services/TokenAuthenticationService.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,51 @@ | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.IdentityModel.Tokens; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using WebApiAuthenticationJWT.Models; | ||
using WebApiAuthenticationJWT.Services.Interfaces; | ||
|
||
namespace WebApiAuthenticationJWT.Services | ||
{ | ||
public class TokenAuthenticationService: IAuthenticateService | ||
{ | ||
private readonly IUserManagementService _userManagementService; | ||
private readonly TokenManagement _tokenManagement; | ||
|
||
public TokenAuthenticationService(IUserManagementService service, IOptions<TokenManagement> tokenManagement) | ||
{ | ||
_userManagementService = service; | ||
_tokenManagement = tokenManagement.Value; | ||
} | ||
|
||
public bool IsAuthenticated(TokenRequest request, out string token) | ||
{ | ||
|
||
token = string.Empty; | ||
if (!_userManagementService.IsValidUser(request.Username, request.Password)) return false; | ||
|
||
var claim = new[] | ||
{ | ||
new Claim(ClaimTypes.Name, request.Username) | ||
}; | ||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenManagement.Secret)); | ||
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); | ||
|
||
var jwtToken = new JwtSecurityToken( | ||
_tokenManagement.Issuer, | ||
_tokenManagement.Audience, | ||
claim, | ||
expires: DateTime.Now.AddMinutes(_tokenManagement.AccessExpiration), | ||
signingCredentials: credentials | ||
); | ||
token = new JwtSecurityTokenHandler().WriteToken(jwtToken); | ||
return true; | ||
|
||
} | ||
} | ||
} |
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 System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using WebApiAuthenticationJWT.Services.Interfaces; | ||
|
||
namespace WebApiAuthenticationJWT.Services | ||
{ | ||
public class UserManagementService : IUserManagementService | ||
{ | ||
//Todo: Dependency injection of IUserManagementRepository | ||
public bool IsValidUser(string userName, string password) | ||
{ | ||
return true; | ||
} | ||
} | ||
} |
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