Added authorization

Endpoint to create contacts with the queueing system is now functional
This commit is contained in:
kdeng00
2023-10-01 12:10:13 -04:00
parent 78614e5629
commit b9d6318d0e
4 changed files with 100 additions and 9 deletions
@@ -1,7 +1,9 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace TextSender_API.Controllers;
[Authorize]
[ApiController]
[Route("api/v1/contact/queue")]
public class ContactQueueCreationController : ControllerBase
+83 -3
View File
@@ -1,6 +1,13 @@
using System.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using TextSender_API.Models;
@@ -28,6 +35,53 @@ public class UsersController : ControllerBase
#endregion
#region Methods
private string AllScopes()
{
var allScopes = new List<String>()
{
"create:contact"
};
var scopes = string.Empty;
for (var i = 0; i < allScopes.Count; i++)
{
if (i == allScopes.Count - 1)
{
scopes += allScopes[i];
}
else
{
scopes += allScopes[i] + " ";
}
}
return scopes;
}
private List<Claim> Payload()
{
var expLimit = 30;
var currentDate = DateTime.Now;
var expiredDate = currentDate.AddMinutes(expLimit);
var issued = Math.Floor((currentDate - DateTime.UnixEpoch).TotalSeconds);
var expires = Math.Floor((expiredDate - DateTime.UnixEpoch).TotalSeconds);
var issuer = this._config["JWT:Issuer"];
var audience = this._config["JWT:Audience"];
var subject = this._config["JWT:Subject"];
var claim = new List<System.Security.Claims.Claim>()
{
new System.Security.Claims.Claim("scope", AllScopes(), "string"),
new System.Security.Claims.Claim(JwtRegisteredClaimNames.Exp, expiredDate.ToString(), "string"),
new System.Security.Claims.Claim(JwtRegisteredClaimNames.Aud, audience!),
new System.Security.Claims.Claim(JwtRegisteredClaimNames.Iss, issuer!),
new Claim(JwtRegisteredClaimNames.Sub, subject!)
};
return claim;
}
[HttpPost("login")]
public IActionResult Login([FromBody] User userRequest)
{
@@ -43,11 +97,37 @@ public class UsersController : ControllerBase
if (result.Item1 && pwdVerify.VerifyPassword(result.Item2, userRequest.Password!))
{
var user = result.Item2;
var token = new Token();
// TODO: Generate access token
token.AccessToken = string.Empty;
token.Issued = DateTime.Now;
token.UserId = result.Item2.Id;
token.UserId = user.Id;
token.TokenType = "JWT";
var payload = Payload();
payload.Add(new System.Security.Claims.Claim("user_id", user.Id!, ClaimValueTypes.String));
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(this._config["JWT:Secret"]!));
var signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var tok = new JwtSecurityToken(
this._config["JWT:Issuer"],
this._config["JWT:Audience"],
payload,
expires: DateTime.UtcNow.AddMinutes(30),
signingCredentials: signIn);
token.AccessToken = new JwtSecurityTokenHandler().WriteToken(tok);
var expClaim = payload.FirstOrDefault(cl =>
{
return cl.Type.Equals("exp");
});
if (expClaim != null)
{
var expiredDate = DateTime.Parse(expClaim!.Value);
var exp = Math.Floor((expiredDate - DateTime.UnixEpoch).TotalSeconds);
}
response.Data.Add(token);
Ok(response);