Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 952899d

Browse files
authored
Merge pull request #2 from Invoices-Manager/dev_01
Dev 01
2 parents 31e1246 + 9c96188 commit 952899d

8 files changed

Lines changed: 59 additions & 7 deletions

File tree

Controllers/v01/UserController.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Invoices_Manager_API.Controllers.v01
1+
using Invoices_Manager_API.Security;
2+
3+
namespace Invoices_Manager_API.Controllers.v01
24
{
35
[ApiController]
46
[Route("api/v01/[controller]")]
@@ -183,6 +185,13 @@ public async Task<IActionResult> Login([FromBody] LoginModel newLogin)
183185
//create the login
184186
LoginModel successfulLogin = LoginCore.LoginUser(newLogin, user, _config);
185187

188+
//add it to the user
189+
user.Logins.Add(successfulLogin);
190+
191+
//save the jwt for the response and hash the model for the db
192+
string jwt = successfulLogin.Token;
193+
successfulLogin.Token = Hasher.GetSHA512Hash(jwt);
194+
186195
//save the token
187196
user.Logins.Add(successfulLogin);
188197
await _db.SaveChangesAsync();
@@ -193,7 +202,7 @@ public async Task<IActionResult> Login([FromBody] LoginModel newLogin)
193202
//return the token
194203
return new OkObjectResult(ResponseMgr.CreateResponse(200, traceId, "The login was successful",
195204
new Dictionary<string, object>{
196-
{ "token", successfulLogin.Token },
205+
{ "token", jwt },
197206
{ "creationDate", successfulLogin.CreationDate },
198207
{ "userName", successfulLogin.Username }
199208
}

Filters/AuthFilter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Mvc.Filters;
22
using Microsoft.AspNetCore.Mvc;
33
using Invoices_Manager_API.Classes;
4+
using Invoices_Manager_API.Security;
45

56
namespace Invoices_Manager_API.Filters
67
{
@@ -25,8 +26,15 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
2526
//get the bearerToken from the header
2627
var bearerToken = potentialBearerToken.FirstOrDefault();
2728

29+
//check if (for whatever reason) the token is null
30+
if (bearerToken is null)
31+
{
32+
context.Result = new UnauthorizedObjectResult(ResponseMgr.CreateResponse(401, Guid.NewGuid(), "Your bearerToken is not valid! Get a new one!"));
33+
return;
34+
}
35+
2836
//check if this bearerToken is in the db
29-
if (!_db.Logins.Any(x => x.Token == bearerToken))
37+
if (!_db.Logins.Any(x => x.Token == Hasher.GetSHA512Hash(bearerToken)))
3038
{
3139
context.Result = new UnauthorizedObjectResult(ResponseMgr.CreateResponse(401, Guid.NewGuid(), "Your bearerToken is not valid! Get a new one!"));
3240
return;

Invoices-Manager-API.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<RootNamespace>Invoices_Manager_API</RootNamespace>
8+
<FileVersion>1.0.1.0</FileVersion>
9+
<AssemblyVersion>1.0.1.0</AssemblyVersion>
810
</PropertyGroup>
911

1012
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -34,4 +36,6 @@
3436
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.27.0" />
3537
</ItemGroup>
3638

39+
<ProjectExtensions><VisualStudio><UserProperties properties_4launchsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
40+
3741
</Project>

Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
ValidateIssuerSigningKey = true,
3737
ValidIssuer = builder.Configuration["Jwt:Issuer"],
3838
ValidAudience = builder.Configuration["Jwt:Audience"],
39-
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("asdasdasdasddsa"))
39+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:SymmetricSecurityKey"]))
4040
};
4141
});
4242

Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dotnetRunMessages": true,
1515
"launchBrowser": false,
1616
"launchUrl": "weatherforecast",
17-
"applicationUrl": "https://localhost:7170;http://localhost:5170",
17+
"applicationUrl": "https://localhost:25566;http://localhost:25565",
1818
"environmentVariables": {
1919
"ASPNETCORE_ENVIRONMENT": "Development"
2020
}

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Invoices Manager - API (written in C# DOTNET6.0)
22

33
## Important Info!
4-
The program may contain errors, if any are found, please report them!
4+
The program may contain errors, if any are found, please report them!
5+
6+
The server / api does not encrypt the data.
7+
It manages the data as it receives it, so you should encrypt the data before sending it to the api.
58

69
## Application description:
710
Are you also tired of having all your invoices (and other documents)
@@ -47,6 +50,8 @@ Press [here](https://github.com/Invoices-Manager/Invoices-Manager-API/blob/dev_0
4750
### Z = Minor version (small updates)
4851
### W = Revision version (bug fixes)
4952

53+
## v1.0.1.0
54+
- JWT are now saved hashed in the database
5055

5156
## v1.0.0.0
5257
- Set Up the whole project

Security/Hasher.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Security.Cryptography;
2+
using System.Text;
3+
4+
namespace Invoices_Manager_API.Security
5+
{
6+
public class Hasher
7+
{
8+
public static string GetSHA512Hash(string input)
9+
{
10+
using (SHA512 sha512 = SHA512.Create())
11+
{
12+
byte[] bytes = Encoding.UTF8.GetBytes(input);
13+
byte[] hashBytes = sha512.ComputeHash(bytes);
14+
15+
StringBuilder builder = new StringBuilder();
16+
for (int i = 0; i < hashBytes.Length; i++)
17+
{
18+
builder.Append(hashBytes[i].ToString("X2"));
19+
}
20+
21+
return builder.ToString();
22+
}
23+
}
24+
25+
}
26+
}

appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"AllowedHosts": "*",
33
"ConnectionStrings": {
4-
"DefaultConnection": "Server=localhost;Port=3306;Database=InvoicesMgr;Uid=root;Pwd=;"
4+
"DefaultConnection": "Server=germannightmare.com;Port=3306;Database=InvoicesMgr;Uid=IM_DebugUser;Pwd=DummyDebug123;"
55
},
66
"JwtKeys": {
77
"SymmetricSecurityKey": "TqsLHzfrk58trjoYEIvTN7aerVEHrv86YlSQyPEDQb7OJSAzpk6rOjjC4nZQmx5i9gdszR5EuaGfUS8c5ryu0tN5DfvIkTmSWCH3XCXIk0gYzxr8h7Jg60Z525tDFxQN3Mz14QX73bKKcLL2u8fkySq4RgZ6eSwfTSMab3AUBpAcOa9FdOwIRGdQkwXcWXDJSASwwdjAggbBHPP3lyVqEn8wo6iZ09Hk2IXNnCj8Wney01evwvvz19MzyaHp0u0",

0 commit comments

Comments
 (0)