-
-
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.
feat(api): added cors, jwt, user and issue management
- Loading branch information
1 parent
405b203
commit 7bb31c8
Showing
12 changed files
with
278 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Newtonsoft.Json.Linq; | ||
using TrackBEE.API.Data; | ||
|
||
namespace TrackBEE.API.Controllers | ||
{ | ||
[ApiController] | ||
[Route("issues")] | ||
public class IssuesController : ControllerBase | ||
{ | ||
[HttpGet] | ||
[AllowAnonymous] | ||
public async Task<IActionResult> GetIssues() | ||
{ | ||
try | ||
{ | ||
var result = await NpgSQLDatabaseInterface.ExecuteStored("st_get_issues", null, null); | ||
var issuesJson = JObject.Parse(result); | ||
return StatusCode(200, issuesJson); | ||
} | ||
catch(Exception ex) | ||
{ | ||
return StatusCode(500, ex.Message); | ||
} | ||
} | ||
} | ||
} | ||
|
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,65 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Newtonsoft.Json.Linq; | ||
using TrackBEE.API.Data; | ||
using TrackBEE.API.Models.Users; | ||
using TrackBEE.API.Managers; | ||
|
||
using Swashbuckle.AspNetCore.Annotations; | ||
|
||
namespace TrackBEE.API.Controllers | ||
{ | ||
[ApiController] | ||
[Route("api")] | ||
[Produces("application/json")] | ||
public class AuthController : ControllerBase | ||
{ | ||
[AllowAnonymous] | ||
[HttpPost("login")] | ||
public async Task<IActionResult> Login([FromBody] UserModelRequest login) | ||
{ | ||
IActionResult response = Unauthorized(); | ||
var user = await UsersManager.AuthenticateUser(login); | ||
if (user != null) | ||
{ | ||
var tokenString = await UsersManager.GenerateJSONWebToken(user, 120); | ||
response = Ok(new { user = user.Data, access_token = tokenString }); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
[AllowAnonymous] | ||
[HttpPost("register")] | ||
[SwaggerResponse(200)] | ||
public async Task<IActionResult> RegisterUser() { | ||
try | ||
{ | ||
var result = await NpgSQLDatabaseInterface.ExecuteStored("st_users_register", new { }, null); | ||
return StatusCode(204); | ||
}catch(Exception e) | ||
{ | ||
return StatusCode(500, e.Message); | ||
} | ||
} | ||
|
||
[Authorize] | ||
[HttpGet("loginWithToken")] | ||
public async Task<IActionResult> LoginWithToken() | ||
{ | ||
try | ||
{ | ||
var userUid = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; | ||
var result = await NpgSQLDatabaseInterface.ExecuteStored("st_users_get_details", new { user_uid = userUid }, null); | ||
|
||
return StatusCode(200, JObject.Parse(result)); | ||
} | ||
catch (Exception e) | ||
{ | ||
return StatusCode(500, e.Message); | ||
} | ||
} | ||
} | ||
} | ||
|
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,67 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using TrackBEE.API.Data; | ||
using TrackBEE.API.Models.Users; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace TrackBEE.API.Managers | ||
{ | ||
public static class UsersManager | ||
{ | ||
public static async Task<string> GenerateJSONWebToken(UserModel userInfo, int validMinutes) | ||
{ | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
var claims = await CreateClaimsIdentitiesAsync(userInfo); | ||
|
||
var token = tokenHandler.CreateJwtSecurityToken( | ||
issuer: Startup.Configuration!["Jwt:Issuer"], | ||
audience: Startup.Configuration!["Jwt:Audience"], | ||
subject: claims, | ||
notBefore: DateTime.UtcNow, | ||
expires: DateTime.UtcNow.AddMinutes(validMinutes), | ||
signingCredentials: | ||
new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Startup.Configuration["Jwt:Key"])), SecurityAlgorithms.HmacSha256Signature) | ||
); | ||
|
||
return tokenHandler.WriteToken(token); | ||
} | ||
|
||
private static Task<ClaimsIdentity> CreateClaimsIdentitiesAsync(UserModel user) | ||
{ | ||
ClaimsIdentity claimsIdentity = new(); | ||
claimsIdentity.AddClaim(new Claim(ClaimTypes.Email, user.Email)); | ||
claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Uid)); | ||
return Task.FromResult(claimsIdentity); | ||
} | ||
|
||
public static async Task<UserModel> AuthenticateUser(UserModelRequest login) | ||
{ | ||
UserModel user = null; | ||
try | ||
{ | ||
var dbResult = await NpgSQLDatabaseInterface.ExecuteStored("st_users_login", new { user_mail = login.Email, user_password = login.Password }, null); | ||
var result = JObject.Parse(dbResult); | ||
if (result.ContainsKey("data")) | ||
{ | ||
var userData = result.SelectToken("data"); | ||
|
||
user = new UserModel() | ||
{ | ||
Uid = userData!.SelectToken("user_uid")!.ToString(), | ||
Email = userData!.SelectToken("user_email")!.ToString(), | ||
Data = userData | ||
}; | ||
return user; | ||
} | ||
} | ||
catch | ||
{ | ||
return null; | ||
} | ||
return 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,11 @@ | ||
using System; | ||
namespace TrackBEE.API.Models.Users | ||
{ | ||
public class UserModel | ||
{ | ||
public string Uid { get; set; } | ||
public string Email { get; set; } | ||
public object Data { 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,10 @@ | ||
using System; | ||
namespace TrackBEE.API.Models.Users | ||
{ | ||
public class UserModelRequest | ||
{ | ||
public string Email { get; set; } | ||
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
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,50 @@ | ||
<!DOCTYPE html> | ||
<html | ||
lang="en" | ||
xmlns="http://www.w3.org/1999/xhtml" | ||
xmlns:o="urn:schemas-microsoft-com:office:office"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<meta name="x-apple-disable-message-reformatting"> | ||
<title></title> | ||
<!--[if mso]> | ||
<noscript> | ||
<xml> | ||
<o:OfficeDocumentSettings> | ||
<o:PixelsPerInch>96</o:PixelsPerInch> | ||
</o:OfficeDocumentSettings> | ||
</xml> | ||
</noscript> | ||
<![endif]--> | ||
<style> | ||
table, | ||
td, | ||
div, | ||
h1, | ||
p { | ||
font-family: Arial, sans-serif; | ||
} | ||
table, | ||
td { | ||
border: 2px solid #cccccc !important; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<table role="presentation" style="width: 100%;border: 0;border-collapse: collapse;border-spacing: 0;"> | ||
<table role="presentation" style="width:602px;border: 0;border-collapse: collapse border-spacing: 0;"> | ||
<tr> | ||
<td align="center" style="padding: 0;"> | ||
<img src="images/thank-you.png"> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td align="center" style="text-align: center;padding: 0;"> | ||
Hey! Welcome to Track BEE! | ||
</td> | ||
</tr> | ||
</table> | ||
</table> | ||
</body> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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