jwt v1.0
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Application.Services.Jwt;
|
||||
|
||||
public class JwtService : IJwtService
|
||||
{
|
||||
private readonly string _secretKey;
|
||||
private readonly string _issuer;
|
||||
private readonly string _audience;
|
||||
private readonly double _expiryMinutes;
|
||||
|
||||
public JwtService(IConfiguration configuration)
|
||||
{
|
||||
_secretKey = configuration["Jwt:SecretKey"];
|
||||
_issuer = configuration["Jwt:Issuer"];
|
||||
_audience = configuration["Jwt:Audience"];
|
||||
_expiryMinutes = double.Parse(configuration["Jwt:ExpiryMinutes"]);
|
||||
}
|
||||
|
||||
public string GenerateJwtToken(string email)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_secretKey);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new[]
|
||||
{
|
||||
new Claim(ClaimTypes.Email, email)
|
||||
}),
|
||||
Expires = DateTime.UtcNow.AddMinutes(_expiryMinutes),
|
||||
Issuer = _issuer,
|
||||
Audience = _audience,
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
|
||||
public bool ValidateJwtToken(string token)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(token))
|
||||
return false;
|
||||
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_secretKey);
|
||||
try
|
||||
{
|
||||
tokenHandler.ValidateToken(token, new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidIssuer = _issuer,
|
||||
ValidAudience = _audience,
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
}, out SecurityToken validatedToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public string RefreshToken(string token)
|
||||
{
|
||||
var principal = ValidateTokenAndGetPrincipal(token);
|
||||
if (principal == null)
|
||||
{
|
||||
throw new SecurityTokenException("Invalid token.");
|
||||
}
|
||||
|
||||
var emailClaim = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
|
||||
if (emailClaim == null)
|
||||
{
|
||||
throw new SecurityTokenException("Token does not contain an email claim.");
|
||||
}
|
||||
|
||||
return GenerateJwtToken(emailClaim.Value);
|
||||
}
|
||||
|
||||
private ClaimsPrincipal ValidateTokenAndGetPrincipal(string token)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_secretKey);
|
||||
try
|
||||
{
|
||||
var principal = tokenHandler.ValidateToken(token, new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidIssuer = _issuer,
|
||||
ValidAudience = _audience,
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
}, out _);
|
||||
|
||||
return principal;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Log or handle validation errors if necessary
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,10 +4,10 @@ namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class ChatMongoDbService : MongoDbService, IChatMongoDbService
|
||||
{
|
||||
public ChatMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
public ChatMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: base(connectionString, databaseName, collectionName)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// Implement additional methods specific to Chat database if needed
|
||||
}
|
||||
@@ -4,10 +4,10 @@ namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class MedicalHistoryMongoDbService : MongoDbService, IMedicalHistoryMongoDbService
|
||||
{
|
||||
public MedicalHistoryMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
public MedicalHistoryMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: base(connectionString, databaseName, collectionName)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// Implement additional methods specific to Medical History database if needed
|
||||
}
|
||||
@@ -14,7 +14,7 @@ namespace Infrastructure.Services.MongoDB
|
||||
{
|
||||
_databaseName = databaseName;
|
||||
_collectionName = collectionName;
|
||||
|
||||
|
||||
Console.WriteLine($"Connection string: {connectionString}");
|
||||
Console.WriteLine($"Database Name: {databaseName}");
|
||||
Console.WriteLine($"Collection Name: {collectionName}");
|
||||
@@ -23,12 +23,12 @@ namespace Infrastructure.Services.MongoDB
|
||||
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
|
||||
_database = new MongoClient(settings);
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
var result = _database.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument("ping", 1));
|
||||
Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
@@ -71,4 +71,4 @@ namespace Infrastructure.Services.MongoDB
|
||||
await collection.DeleteOneAsync(filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user