All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m12s
Build & Deploy Discipline Service / build (push) Successful in 1m27s
Build & Deploy Insignia Service / build (push) Successful in 1m25s
Build & Deploy Placement Service / build (push) Successful in 1m17s
Build & Deploy Retirement Service / build (push) Successful in 1m23s
814 lines
No EOL
35 KiB
C#
814 lines
No EOL
35 KiB
C#
using BMA.EHR.Domain.Common;
|
|
using BMA.EHR.Domain.Shared;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Nest;
|
|
using Newtonsoft.Json;
|
|
using System.Diagnostics;
|
|
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Claims;
|
|
using System.Text.Encodings.Web;
|
|
using System.Text.Json;
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
|
|
|
namespace BMA.EHR.Domain.Middlewares
|
|
{
|
|
public class CombinedErrorHandlerAndLoggingMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
private readonly IConfiguration _configuration;
|
|
private static ElasticClient? _elasticClient;
|
|
private static readonly object _lock = new object();
|
|
private static readonly Dictionary<string, (GetProfileByKeycloakIdLocal Profile, DateTime ExpiryTime)> _profileCache = new();
|
|
private static readonly TimeSpan _cacheExpiry = TimeSpan.FromMinutes(10);
|
|
|
|
private string Uri = "";
|
|
private string IndexFormat = "";
|
|
private string SystemName = "";
|
|
|
|
public CombinedErrorHandlerAndLoggingMiddleware(RequestDelegate next, IConfiguration configuration)
|
|
{
|
|
_next = next;
|
|
_configuration = configuration;
|
|
|
|
Uri = _configuration["ElasticConfiguration:Uri"] ?? "http://192.168.1.40:9200";
|
|
IndexFormat = _configuration["ElasticConfiguration:IndexFormat"] ?? "bma-ehr-log-index";
|
|
SystemName = _configuration["ElasticConfiguration:SystemName"] ?? "Unknown";
|
|
|
|
// สร้าง ElasticClient แค่ครั้งเดียว
|
|
if (_elasticClient == null)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (_elasticClient == null)
|
|
{
|
|
var settings = new ConnectionSettings(new Uri(Uri))
|
|
.DefaultIndex(IndexFormat)
|
|
.DisableDirectStreaming() // เพิ่มประสิทธิภาพ
|
|
.RequestTimeout(TimeSpan.FromSeconds(5)); // กำหนด timeout
|
|
_elasticClient = new ElasticClient(settings);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
{
|
|
var startTime = DateTime.UtcNow;
|
|
var stopwatch = Stopwatch.StartNew();
|
|
string? requestBodyJson = null;
|
|
Exception? caughtException = null;
|
|
|
|
// อ่าน Request Body
|
|
string requestBody = await ReadRequestBodyAsync(context);
|
|
if (!string.IsNullOrEmpty(requestBody))
|
|
{
|
|
requestBodyJson = await FormatRequestBody(context, requestBody);
|
|
}
|
|
|
|
var originalBodyStream = context.Response.Body;
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
{
|
|
// เปลี่ยน stream ของ Response เพื่อให้สามารถอ่านได้
|
|
context.Response.Body = memoryStream;
|
|
|
|
string keycloakId = Guid.Empty.ToString("D");
|
|
var token = context.Request.Headers["Authorization"];
|
|
GetProfileByKeycloakIdLocal? pf = null;
|
|
var tokenUserInfo = await ExtractTokenUserInfoAsync(token);
|
|
|
|
// ดึง keycloakId จาก JWT token
|
|
keycloakId = tokenUserInfo.KeycloakId;
|
|
|
|
// ดึง profile จาก cache หรือ API
|
|
if (Guid.TryParse(keycloakId, out var parsedId) && parsedId != Guid.Empty)
|
|
{
|
|
pf = await GetProfileWithCacheAsync(parsedId, token);
|
|
}
|
|
|
|
try
|
|
{
|
|
await _next(context);
|
|
Console.WriteLine($"Request completed with status: {context.Response.StatusCode}");
|
|
|
|
// หลังจาก Authentication middleware ทำงานแล้ว ลองดึง claims อีกครั้ง
|
|
if (context.User?.Identity?.IsAuthenticated == true)
|
|
{
|
|
var authenticatedKeycloakId = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value
|
|
?? context.User.FindFirst("sub")?.Value;
|
|
if (!string.IsNullOrEmpty(authenticatedKeycloakId) && authenticatedKeycloakId != keycloakId)
|
|
{
|
|
keycloakId = authenticatedKeycloakId;
|
|
Console.WriteLine($"Updated keycloakId from authenticated user: {keycloakId}");
|
|
|
|
// อัพเดต profile ด้วย keycloakId ที่ถูกต้อง
|
|
// try
|
|
// {
|
|
// if (Guid.TryParse(keycloakId, out var parsedId))
|
|
// {
|
|
// //pf = await GetProfileByKeycloakIdAsync(parsedId, token);
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Console.WriteLine($"Error updating profile after authentication: {ex.Message}");
|
|
// }
|
|
}
|
|
}
|
|
|
|
// จัดการ response format หลังจาก request ผ่าน pipeline แล้ว
|
|
await FormatResponse(context, memoryStream);
|
|
}
|
|
catch (ObjectDisposedException)
|
|
{
|
|
Console.WriteLine("ObjectDisposedException caught in main Invoke");
|
|
caughtException = new ObjectDisposedException("Response");
|
|
return;
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
Console.WriteLine("OperationCanceledException caught in main Invoke");
|
|
caughtException = new OperationCanceledException("Operation was cancelled");
|
|
return;
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
Console.WriteLine($"Exception caught in main Invoke: {error.Message}");
|
|
caughtException = error;
|
|
// จัดการ exception และ format เป็น response
|
|
await FormatExceptionResponse(context, error, memoryStream);
|
|
}
|
|
finally
|
|
{
|
|
stopwatch.Stop();
|
|
|
|
// อ่านข้อมูล response ก่อนที่ stream จะถูก dispose
|
|
string? responseBodyForLogging = null;
|
|
if (memoryStream.Length > 0)
|
|
{
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
|
using var reader = new StreamReader(memoryStream, leaveOpen: true);
|
|
responseBodyForLogging = await reader.ReadToEndAsync();
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
|
}
|
|
|
|
// เก็บข้อมูลที่จำเป็นจาก HttpContext ก่อนที่มันจะถูก dispose
|
|
var logData = new
|
|
{
|
|
RemoteIpAddress = context.Connection.RemoteIpAddress?.ToString(),
|
|
HostValue = context.Request.Host.Value,
|
|
Method = context.Request.Method,
|
|
Path = context.Request.Path.ToString(),
|
|
QueryString = context.Request.QueryString.ToString(),
|
|
StatusCode = context.Response.StatusCode,
|
|
ContentType = context.Response.ContentType ?? ""
|
|
};
|
|
|
|
// เขียนข้อมูลกลับไปยัง original Response body ก่อน
|
|
if (memoryStream.Length > 0)
|
|
{
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
|
await memoryStream.CopyToAsync(originalBodyStream);
|
|
}
|
|
|
|
// ทำ logging แบบ await
|
|
Console.WriteLine("[DEBUG] Starting logging...");
|
|
try
|
|
{
|
|
await LogRequestAsync(_elasticClient!, startTime, stopwatch, pf, keycloakId, requestBodyJson, responseBodyForLogging, caughtException, logData);
|
|
Console.WriteLine("[DEBUG] Logging completed successfully");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[ERROR] Logging error: {ex.Message}");
|
|
Console.WriteLine($"[ERROR] Stack trace: {ex.StackTrace}");
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("=== CombinedErrorHandlerAndLoggingMiddleware End ===");
|
|
}
|
|
|
|
private async Task<string> FormatRequestBody(HttpContext context, string requestBody)
|
|
{
|
|
try
|
|
{
|
|
if (context.Request.HasFormContentType)
|
|
{
|
|
var form = await context.Request.ReadFormAsync();
|
|
var formData = new Dictionary<string, object>();
|
|
|
|
foreach (var field in form)
|
|
{
|
|
formData[field.Key] = field.Value.ToString();
|
|
}
|
|
|
|
if (form.Files.Count > 0)
|
|
{
|
|
var fileDataList = new List<object>();
|
|
foreach (var file in form.Files)
|
|
{
|
|
fileDataList.Add(new
|
|
{
|
|
FileName = file.FileName,
|
|
ContentType = file.ContentType,
|
|
Size = file.Length
|
|
});
|
|
}
|
|
formData["Files"] = fileDataList;
|
|
}
|
|
|
|
var jsonOptions = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
|
WriteIndented = true,
|
|
Converters = { new DateTimeFixConverter() }
|
|
};
|
|
return JsonSerializer.Serialize(formData, jsonOptions);
|
|
}
|
|
else
|
|
{
|
|
var jsonOptions = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
|
WriteIndented = true,
|
|
Converters = { new DateTimeFixConverter() }
|
|
};
|
|
return JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(requestBody), jsonOptions);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error formatting request body: {ex.Message}");
|
|
return requestBody;
|
|
}
|
|
}
|
|
|
|
private static async Task FormatResponse(HttpContext context, MemoryStream memoryStream)
|
|
{
|
|
try
|
|
{
|
|
if (context?.Response == null)
|
|
return;
|
|
|
|
var response = context.Response;
|
|
var statusCode = response.StatusCode;
|
|
|
|
|
|
string? message = null;
|
|
string? responseBodyJson = null;
|
|
if (memoryStream.Length > 0)
|
|
{
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
|
var responseBody = new StreamReader(memoryStream).ReadToEnd();
|
|
|
|
var jsonOptions = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
|
WriteIndented = true,
|
|
Converters = { new DateTimeFixConverter() }
|
|
};
|
|
responseBodyJson =
|
|
JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(responseBody), jsonOptions);
|
|
|
|
var json = JsonSerializer.Deserialize<JsonElement>(responseBody);
|
|
if (json.ValueKind == JsonValueKind.Array)
|
|
{
|
|
message = "success";
|
|
}
|
|
else
|
|
{
|
|
if (json.TryGetProperty("message", out var messageElement))
|
|
{
|
|
message = messageElement.GetString();
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine($"FormatResponse: StatusCode={statusCode}, HasStarted={response.HasStarted}");
|
|
|
|
// จัดการ response format แม้กับ status code จาก Authentication middleware
|
|
if (!response.HasStarted && ShouldFormatResponse(statusCode))
|
|
{
|
|
Console.WriteLine($"Formatting response for status: {statusCode}");
|
|
|
|
var responseModel = CreateResponseModel(statusCode, message);
|
|
|
|
// Clear memory stream และเขียน response ใหม่
|
|
memoryStream.SetLength(0);
|
|
memoryStream.Position = 0;
|
|
|
|
// ไม่เปลี่ยน status code ที่ Authentication middleware ตั้งไว้
|
|
response.ContentType = "application/json; charset=utf-8";
|
|
|
|
var jsonOptions = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
WriteIndented = false
|
|
};
|
|
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
|
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
|
|
|
// กำหนด Content-Length ให้ตรงกับขนาดจริง
|
|
response.ContentLength = bytes.Length;
|
|
|
|
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
|
|
|
Console.WriteLine($"Response formatted successfully: {jsonResponse}");
|
|
}
|
|
// หากเป็น 401/403 แต่ยังไม่มี response body ให้สร้างใหม่
|
|
else if (!response.HasStarted && (statusCode == 401 || statusCode == 403) && memoryStream.Length == 0)
|
|
{
|
|
Console.WriteLine($"Creating response body for {statusCode} status");
|
|
|
|
var responseModel = CreateResponseModel(statusCode, message);
|
|
response.ContentType = "application/json; charset=utf-8";
|
|
|
|
var jsonOptions = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
WriteIndented = false
|
|
};
|
|
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
|
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
|
|
|
// กำหนด Content-Length ให้ตรงกับขนาดจริง
|
|
response.ContentLength = bytes.Length;
|
|
|
|
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
|
|
|
Console.WriteLine($"Response body created: {jsonResponse}");
|
|
}
|
|
}
|
|
catch (ObjectDisposedException)
|
|
{
|
|
Console.WriteLine("ObjectDisposedException in FormatResponse");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error in FormatResponse: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private static async Task FormatExceptionResponse(HttpContext context, Exception error, MemoryStream memoryStream)
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine($"FormatExceptionResponse: Error={error.Message}");
|
|
|
|
if (context?.Response == null)
|
|
return;
|
|
|
|
var response = context.Response;
|
|
Console.WriteLine($"Response HasStarted: {response.HasStarted}");
|
|
|
|
if (!response.HasStarted)
|
|
{
|
|
// Clear memory stream และเขียน error response
|
|
memoryStream.SetLength(0);
|
|
memoryStream.Position = 0;
|
|
|
|
response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
|
response.ContentType = "application/json; charset=utf-8";
|
|
|
|
var responseModel = new ResponseObject
|
|
{
|
|
Status = response.StatusCode,
|
|
Message = GetErrorMessage(error),
|
|
Result = null
|
|
};
|
|
|
|
var jsonOptions = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
WriteIndented = false
|
|
};
|
|
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
|
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
|
|
|
// กำหนด Content-Length ให้ตรงกับขนาดจริง
|
|
response.ContentLength = bytes.Length;
|
|
|
|
|
|
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
|
|
|
Console.WriteLine($"Exception response formatted: {jsonResponse}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Cannot format exception response - response already started");
|
|
}
|
|
}
|
|
catch (ObjectDisposedException)
|
|
{
|
|
Console.WriteLine("ObjectDisposedException in FormatExceptionResponse");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error in FormatExceptionResponse: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private async Task LogRequestAsync(ElasticClient client, DateTime startTime, Stopwatch stopwatch,
|
|
GetProfileByKeycloakIdLocal? pf, string keycloakId, string? requestBodyJson, string? responseBodyForLogging, Exception? caughtException, dynamic contextData)
|
|
{
|
|
Console.WriteLine("[DEBUG] LogRequestAsync called");
|
|
try
|
|
{
|
|
var processTime = stopwatch.ElapsedMilliseconds;
|
|
var endTime = DateTime.UtcNow;
|
|
|
|
var statusCode = caughtException != null ? (int)HttpStatusCode.InternalServerError : (int)contextData.StatusCode;
|
|
|
|
var logType = caughtException != null ? "error" : statusCode switch
|
|
{
|
|
>= 500 => "error",
|
|
>= 400 => "warning",
|
|
_ => "info"
|
|
};
|
|
|
|
string? message = null;
|
|
string? responseBodyJson = null;
|
|
|
|
// ใช้ response body ที่ส่งมาจากการอ่านก่อนหน้า
|
|
if (!string.IsNullOrEmpty(responseBodyForLogging))
|
|
{
|
|
var contentType = (string)contextData.ContentType;
|
|
var isFileResponse = !contentType.StartsWith("application/json") && !contentType.StartsWith("text/html") && (
|
|
contentType.StartsWith("application/") ||
|
|
contentType.StartsWith("image/") ||
|
|
contentType.StartsWith("audio/")
|
|
);
|
|
|
|
if (isFileResponse)
|
|
{
|
|
responseBodyJson = "";
|
|
message = "success";
|
|
}
|
|
else
|
|
{
|
|
// ใช้ response body ที่มีอยู่แล้วโดยไม่ serialize ซ้ำ
|
|
responseBodyJson = responseBodyForLogging;
|
|
|
|
try
|
|
{
|
|
var json = JsonSerializer.Deserialize<JsonElement>(responseBodyForLogging);
|
|
if (json.ValueKind == JsonValueKind.Array)
|
|
{
|
|
message = "success";
|
|
}
|
|
else if (json.TryGetProperty("message", out var messageElement))
|
|
{
|
|
message = messageElement.GetString();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
message = caughtException?.Message ?? "Unknown error";
|
|
}
|
|
}
|
|
}
|
|
|
|
if (caughtException != null)
|
|
{
|
|
message = caughtException.Message;
|
|
}
|
|
|
|
var logData = new
|
|
{
|
|
logType = logType,
|
|
ip = (string)contextData.RemoteIpAddress,
|
|
//rootId = pf?.RootId,
|
|
rootId = pf?.RootDnaId,
|
|
systemName = SystemName,
|
|
startTimeStamp = startTime.ToString("o"),
|
|
endTimeStamp = endTime.ToString("o"),
|
|
processTime = processTime,
|
|
host = (string)contextData.HostValue,
|
|
method = (string)contextData.Method,
|
|
endpoint = (string)contextData.Path + (string)contextData.QueryString,
|
|
responseCode = statusCode == 304 ? "200" : statusCode.ToString(),
|
|
responseDescription = message,
|
|
input = requestBodyJson,
|
|
output = responseBodyJson,
|
|
userId = keycloakId,
|
|
userName = $"{pf?.Prefix ?? ""}{pf?.FirstName ?? ""} {pf?.LastName ?? ""}",
|
|
user = pf?.CitizenId ?? "",
|
|
exception = caughtException?.ToString()
|
|
};
|
|
|
|
Console.WriteLine($"[DEBUG] Sending log to Elasticsearch: {logType} - {(string)contextData.Method} {(string)contextData.Path}");
|
|
var response = await client.IndexDocumentAsync(logData);
|
|
Console.WriteLine($"[DEBUG] Elasticsearch response: IsValid={response.IsValid}, Index={response.Index}");
|
|
|
|
if (!response.IsValid)
|
|
{
|
|
Console.WriteLine($"[ERROR] Elasticsearch error: {response.OriginalException?.Message ?? response.ServerError?.ToString()}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[ERROR] Error logging request: {ex.Message}");
|
|
Console.WriteLine($"[ERROR] Stack trace: {ex.StackTrace}");
|
|
}
|
|
}
|
|
|
|
private static bool ShouldFormatResponse(int statusCode)
|
|
{
|
|
return statusCode == (int)HttpStatusCode.Unauthorized ||
|
|
statusCode == (int)HttpStatusCode.Forbidden ||
|
|
statusCode == (int)HttpStatusCode.BadRequest ||
|
|
statusCode == (int)HttpStatusCode.NotFound ||
|
|
statusCode == (int)HttpStatusCode.Conflict ||
|
|
statusCode == (int)HttpStatusCode.UnprocessableEntity ||
|
|
statusCode == (int)HttpStatusCode.InternalServerError;
|
|
}
|
|
|
|
private static ResponseObject CreateResponseModel(int statusCode, string? error)
|
|
{
|
|
var message = statusCode switch
|
|
{
|
|
(int)HttpStatusCode.Unauthorized => GlobalMessages.NotAuthorized,
|
|
(int)HttpStatusCode.Forbidden => GlobalMessages.ForbiddenAccess,
|
|
(int)HttpStatusCode.BadRequest => "Bad Request",
|
|
(int)HttpStatusCode.NotFound => "Resource Not Found",
|
|
(int)HttpStatusCode.Conflict => "Conflict",
|
|
(int)HttpStatusCode.UnprocessableEntity => "Validation Error",
|
|
(int)HttpStatusCode.InternalServerError => GlobalMessages.ExceptionOccured,
|
|
_ => "Error"
|
|
};
|
|
|
|
return new ResponseObject
|
|
{
|
|
Status = statusCode,
|
|
Message = error ?? message
|
|
};
|
|
}
|
|
|
|
private static string GetErrorMessage(Exception error)
|
|
{
|
|
var msg = error.Message;
|
|
var inner = error.InnerException;
|
|
while (inner != null)
|
|
{
|
|
msg += $" {inner.Message}\r\n";
|
|
inner = inner.InnerException;
|
|
}
|
|
return msg;
|
|
}
|
|
|
|
private async Task<string> ExtractKeycloakIdFromToken(string? authorizationHeader)
|
|
{
|
|
var tokenInfo = await ExtractTokenUserInfoAsync(authorizationHeader);
|
|
return tokenInfo.KeycloakId;
|
|
}
|
|
|
|
private async Task<TokenUserInfo> ExtractTokenUserInfoAsync(string? authorizationHeader)
|
|
{
|
|
var defaultResult = new TokenUserInfo { KeycloakId = Guid.Empty.ToString("D") };
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
|
|
{
|
|
return defaultResult;
|
|
}
|
|
|
|
var token = authorizationHeader.Replace("Bearer ", "");
|
|
|
|
// แยก JWT token เพื่อดึง payload (แบบง่าย โดยไม่ verify signature)
|
|
var parts = token.Split('.');
|
|
if (parts.Length != 3)
|
|
{
|
|
return defaultResult;
|
|
}
|
|
|
|
// Decode Base64Url payload (JWT uses Base64Url encoding, not standard Base64)
|
|
var payload = parts[1];
|
|
|
|
// แปลง Base64Url เป็น Base64 ก่อน
|
|
payload = payload.Replace('-', '+').Replace('_', '/');
|
|
|
|
// เพิ่ม padding ถ้าจำเป็น
|
|
var padLength = 4 - (payload.Length % 4);
|
|
if (padLength < 4)
|
|
{
|
|
payload += new string('=', padLength);
|
|
}
|
|
|
|
var payloadBytes = Convert.FromBase64String(payload);
|
|
var payloadJson = System.Text.Encoding.UTF8.GetString(payloadBytes);
|
|
|
|
Console.WriteLine($"JWT Payload: {payloadJson}");
|
|
|
|
// Parse JSON และดึง claims ต่างๆ
|
|
var jsonDoc = JsonDocument.Parse(payloadJson);
|
|
var result = new TokenUserInfo();
|
|
|
|
// ดึง keycloak ID
|
|
if (jsonDoc.RootElement.TryGetProperty("sub", out var subElement))
|
|
{
|
|
result.KeycloakId = subElement.GetString() ?? Guid.Empty.ToString("D");
|
|
}
|
|
else if (jsonDoc.RootElement.TryGetProperty("nameid", out var nameidElement))
|
|
{
|
|
result.KeycloakId = nameidElement.GetString() ?? Guid.Empty.ToString("D");
|
|
}
|
|
else if (jsonDoc.RootElement.TryGetProperty("user_id", out var userIdElement))
|
|
{
|
|
result.KeycloakId = userIdElement.GetString() ?? Guid.Empty.ToString("D");
|
|
}
|
|
else
|
|
{
|
|
result.KeycloakId = Guid.Empty.ToString("D");
|
|
}
|
|
|
|
// ดึง preferred_username
|
|
if (jsonDoc.RootElement.TryGetProperty("preferred_username", out var preferredUsernameElement))
|
|
{
|
|
result.PreferredUsername = preferredUsernameElement.GetString();
|
|
Console.WriteLine($"Extracted preferred_username: {result.PreferredUsername}");
|
|
}
|
|
|
|
// ดึง given_name
|
|
if (jsonDoc.RootElement.TryGetProperty("given_name", out var givenNameElement))
|
|
{
|
|
result.GivenName = givenNameElement.GetString();
|
|
Console.WriteLine($"Extracted given_name: {result.GivenName}");
|
|
}
|
|
|
|
// ดึง family_name
|
|
if (jsonDoc.RootElement.TryGetProperty("family_name", out var familyNameElement))
|
|
{
|
|
result.FamilyName = familyNameElement.GetString();
|
|
Console.WriteLine($"Extracted family_name: {result.FamilyName}");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error extracting token user info: {ex.Message}");
|
|
return defaultResult;
|
|
}
|
|
}
|
|
|
|
protected async Task<string> GetExternalAPIAsync(string apiPath, string accessToken, string apiKey)
|
|
{
|
|
try
|
|
{
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
|
|
client.DefaultRequestHeaders.Add("api-key", apiKey);
|
|
var _res = await client.GetAsync(apiPath);
|
|
if (_res.IsSuccessStatusCode)
|
|
{
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
return _result;
|
|
}
|
|
return string.Empty;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<GetProfileByKeycloakIdLocal?> GetProfileByKeycloakIdAsync(Guid keycloakId, string? accessToken)
|
|
{
|
|
try
|
|
{
|
|
//var apiPath = $"{_configuration["API"]}/org/dotnet/by-keycloak/{keycloakId}";
|
|
var apiPath = $"{_configuration["API"]}/org/dotnet/user-logs/{keycloakId}";
|
|
var apiKey = _configuration["API_KEY"];
|
|
|
|
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
|
if (apiResult != null)
|
|
{
|
|
var raw = JsonConvert.DeserializeObject<GetProfileByKeycloakIdResultLocal>(apiResult);
|
|
if (raw != null)
|
|
return raw.Result;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private async Task<GetProfileByKeycloakIdLocal?> GetProfileWithCacheAsync(Guid keycloakId, string? accessToken)
|
|
{
|
|
var cacheKey = keycloakId.ToString();
|
|
|
|
// ตรวจสอบ cache
|
|
lock (_profileCache)
|
|
{
|
|
if (_profileCache.TryGetValue(cacheKey, out var cached))
|
|
{
|
|
if (cached.ExpiryTime > DateTime.UtcNow)
|
|
{
|
|
return cached.Profile;
|
|
}
|
|
// ลบ cache ที่หมดอายุ
|
|
_profileCache.Remove(cacheKey);
|
|
}
|
|
}
|
|
|
|
// ดึงข้อมูลจาก API
|
|
try
|
|
{
|
|
var profile = await GetProfileByKeycloakIdAsync(keycloakId, accessToken);
|
|
if (profile != null)
|
|
{
|
|
// เก็บใน cache
|
|
lock (_profileCache)
|
|
{
|
|
_profileCache[cacheKey] = (profile, DateTime.UtcNow.Add(_cacheExpiry));
|
|
|
|
// ลบ cache เก่าที่เกิน 1000 รายการ
|
|
if (_profileCache.Count > 1000)
|
|
{
|
|
var expiredKeys = _profileCache
|
|
.Where(x => x.Value.ExpiryTime < DateTime.UtcNow)
|
|
.Select(x => x.Key)
|
|
.ToList();
|
|
foreach (var key in expiredKeys)
|
|
{
|
|
_profileCache.Remove(key);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return profile;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private async Task<string> ReadRequestBodyAsync(HttpContext context)
|
|
{
|
|
context.Request.EnableBuffering();
|
|
using var reader = new StreamReader(context.Request.Body, leaveOpen: true);
|
|
var body = await reader.ReadToEndAsync();
|
|
context.Request.Body.Position = 0;
|
|
return body;
|
|
}
|
|
}
|
|
|
|
// Model classes
|
|
public class TokenUserInfo
|
|
{
|
|
public string KeycloakId { get; set; } = string.Empty;
|
|
public string? PreferredUsername { get; set; }
|
|
public string? GivenName { get; set; }
|
|
public string? FamilyName { get; set; }
|
|
}
|
|
|
|
public class GetProfileByKeycloakIdLocal
|
|
{
|
|
public Guid Id { get; set; }
|
|
public string? Prefix { get; set; }
|
|
public string? FirstName { get; set; }
|
|
public string? LastName { get; set; }
|
|
public string? CitizenId { get; set; }
|
|
public string? Root { get; set; }
|
|
public string? Child1 { get; set; }
|
|
public string? Child2 { get; set; }
|
|
public string? Child3 { get; set; }
|
|
public string? Child4 { get; set; }
|
|
public Guid? RootId { get; set; }
|
|
public Guid? Child1Id { get; set; }
|
|
public Guid? Child2Id { get; set; }
|
|
public Guid? Child3Id { get; set; }
|
|
public Guid? Child4Id { get; set; }
|
|
public Guid? RootDnaId { get; set; }
|
|
public Guid? Child1DnaId { get; set; }
|
|
public Guid? Child2DnaId { get; set; }
|
|
public Guid? Child3DnaId { get; set; }
|
|
public Guid? Child4DnaId { get; set; }
|
|
public double? Amount { get; set; }
|
|
public double? PositionSalaryAmount { get; set; }
|
|
public string? Commander { get; set; }
|
|
public Guid? CommanderId { get; set; }
|
|
public Guid? CommanderKeycloak { get; set; }
|
|
}
|
|
|
|
public class GetProfileByKeycloakIdResultLocal
|
|
{
|
|
public string Message { get; set; } = string.Empty;
|
|
public int Status { get; set; } = -1;
|
|
public GetProfileByKeycloakIdLocal? Result { get; set; }
|
|
}
|
|
|
|
} |