-
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.
fix: add back and fix with sonarlint
- Loading branch information
1 parent
bb8307c
commit 1dc71a8
Showing
19 changed files
with
602 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
authentication-service/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,87 @@ | ||
using authentication_service.Models; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Newtonsoft.Json; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
|
||
namespace authentication_service.Controllers | ||
{ | ||
public class AuthenticationController : Controller | ||
{ | ||
private readonly DataContext db; | ||
readonly TokenController TC = new(); | ||
public AuthenticationController(DataContext db) | ||
{ | ||
this.db = db; | ||
} | ||
|
||
[HttpPost] | ||
[Route("/api/[controller]/login")] | ||
public object login([FromBody] Person p) | ||
{ | ||
Person person = (from Person in db.Person | ||
where Person.Email == p.Email && Person.Password == p.Password | ||
select Person).FirstOrDefault(); | ||
|
||
return person == null ? Unauthorized() : TC.GenerateToken(p.Email, Convert.ToString(person.Role)); | ||
} | ||
|
||
[Route("/api/[controller]/register")] | ||
[HttpPost] | ||
public object register() | ||
{ | ||
var x = TC.GenerateToken(null, null); | ||
return x; | ||
} | ||
|
||
[HttpGet] | ||
[Route("/api/[controller]/auth")] | ||
[Authorize] | ||
public string authorize([FromHeader] string Authorization) | ||
{ | ||
string[] token = Authorization.Split(' '); | ||
var handler = new JwtSecurityTokenHandler(); | ||
var jwtSecurityToken = handler.ReadJwtToken(token[1]); | ||
string email = ""; | ||
int role = 0; | ||
foreach (Claim c in jwtSecurityToken.Claims) | ||
{ | ||
if (c.Type == "email") | ||
{ | ||
email = c.Value; | ||
} | ||
else if (ClaimTypes.Role == c.Type) | ||
{ | ||
role = Convert.ToInt32(c.Value); | ||
} | ||
} | ||
UserResponse userResponse = new UserResponse(email, role); | ||
string jSonObject = JsonConvert.SerializeObject(userResponse); | ||
return jSonObject; | ||
} | ||
|
||
[HttpGet] | ||
[Route("/api/[controller]/singleUser")] | ||
[Authorize] | ||
public Person GetUser([FromHeader] string Authorization) | ||
{ | ||
string[] token = Authorization.Split(' '); | ||
var handler = new JwtSecurityTokenHandler(); | ||
var jwtSecurityToken = handler.ReadJwtToken(token[1]); | ||
string email = ""; | ||
foreach (Claim c in jwtSecurityToken.Claims) | ||
{ | ||
if (c.Type == "email") | ||
{ | ||
email = c.Value; | ||
} | ||
} | ||
Person p = (from Person in db.Person | ||
where Person.Email == email | ||
select Person).FirstOrDefault(); | ||
return p; | ||
} | ||
} | ||
} |
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,29 @@ | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
|
||
namespace authentication_service.Controllers | ||
{ | ||
public class TokenController | ||
{ | ||
|
||
private const string SECRET_KEY = "this is my custom Secret key for authnetication"; | ||
public static readonly SymmetricSecurityKey SIGNING_KEY = new(Encoding.UTF8.GetBytes(SECRET_KEY)); | ||
|
||
public object GenerateToken(string email, string role) | ||
{ | ||
var token = new JwtSecurityToken( | ||
claims: new Claim[] | ||
{ | ||
new("email", email), | ||
new(ClaimTypes.Role, role) | ||
}, | ||
notBefore: DateTime.Now, | ||
expires: DateTime.Now.AddMinutes(60), | ||
signingCredentials: new SigningCredentials(SIGNING_KEY, SecurityAlgorithms.HmacSha256) | ||
); | ||
|
||
return new JwtSecurityTokenHandler().WriteToken(token); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
authentication-service/Controllers/WeatherForecastController.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,39 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace authentication_service.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
var rng = new Random(); | ||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
TemperatureC = rng.Next(-20, 55), | ||
Summary = Summaries[rng.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
} | ||
} | ||
} |
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 authentication_service.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace authentication_service.Data | ||
{ | ||
public class DataContext : DbContext | ||
{ | ||
public DataContext() | ||
{ | ||
} | ||
public DataContext(DbContextOptions<DataContext> options) : base(options) { } | ||
public DbSet<Person> Person { 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,9 @@ | ||
global using authentication_service.Controllers; | ||
global using authentication_service.Data; | ||
global using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
global using Microsoft.AspNetCore.Builder; | ||
global using Microsoft.EntityFrameworkCore; | ||
global using Microsoft.Extensions.DependencyInjection; | ||
global using Microsoft.Extensions.Hosting; | ||
global using Microsoft.IdentityModel.Tokens; | ||
global using System; |
49 changes: 49 additions & 0 deletions
49
authentication-service/Migrations/20211117124245_InitialMigration.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
authentication-service/Migrations/20211117124245_InitialMigration.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,32 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
namespace authentication_service.Migrations | ||
{ | ||
public partial class InitialMigration : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "Users", | ||
columns: table => new | ||
{ | ||
userId = table.Column<int>(type: "int", nullable: false) | ||
.Annotation("SqlServer:Identity", "1, 1"), | ||
name = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
email = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
password = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
role = table.Column<int>(type: "int", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_Users", x => x.userId); | ||
}); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "Users"); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
authentication-service/Migrations/20211216124828_ChangedPersonModel.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
authentication-service/Migrations/20211216124828_ChangedPersonModel.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,52 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
namespace authentication_service.Migrations | ||
{ | ||
public partial class ChangedPersonModel : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "Users"); | ||
|
||
migrationBuilder.CreateTable( | ||
name: "Person", | ||
columns: table => new | ||
{ | ||
Id = table.Column<int>(type: "int", nullable: false) | ||
.Annotation("SqlServer:Identity", "1, 1"), | ||
Name = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
Email = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
Password = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
Role = table.Column<int>(type: "int", nullable: false), | ||
FontysId = table.Column<string>(type: "nvarchar(max)", nullable: true) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_Person", x => x.Id); | ||
}); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "Person"); | ||
|
||
migrationBuilder.CreateTable( | ||
name: "Users", | ||
columns: table => new | ||
{ | ||
userId = table.Column<int>(type: "int", nullable: false) | ||
.Annotation("SqlServer:Identity", "1, 1"), | ||
email = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
name = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
password = table.Column<string>(type: "nvarchar(max)", nullable: true), | ||
role = table.Column<int>(type: "int", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_Users", x => x.userId); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.