Skip to content

Commit

Permalink
fix: add back and fix with sonarlint
Browse files Browse the repository at this point in the history
  • Loading branch information
Extiriority committed Jul 5, 2023
1 parent bb8307c commit 1dc71a8
Show file tree
Hide file tree
Showing 19 changed files with 602 additions and 0 deletions.
87 changes: 87 additions & 0 deletions authentication-service/Controllers/AuthenticationController.cs
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;
}
}
}
29 changes: 29 additions & 0 deletions authentication-service/Controllers/TokenController.cs
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 authentication-service/Controllers/WeatherForecastController.cs
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();
}
}
}
20 changes: 20 additions & 0 deletions authentication-service/Data/DataContext.cs
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; }


}
}
9 changes: 9 additions & 0 deletions authentication-service/GlobalUsing.cs
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;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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);
});
}
}
}
Loading

0 comments on commit 1dc71a8

Please sign in to comment.