@Route("api/v1/org/dotnet") ปรับใช้ API Key
This commit is contained in:
parent
32282b016b
commit
eede5f51c4
4 changed files with 106 additions and 3 deletions
|
|
@ -4,6 +4,7 @@ import { createDecoder, createVerifier } from "fast-jwt";
|
|||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import { handleWebServiceAuth } from "./authWebService";
|
||||
import { handleInternalAuth } from "./authInternal";
|
||||
|
||||
if (!process.env.AUTH_PUBLIC_KEY && !process.env.AUTH_REALM_URL) {
|
||||
throw new Error("Require keycloak AUTH_PUBLIC_KEY or AUTH_REALM_URL.");
|
||||
|
|
@ -39,6 +40,11 @@ export async function expressAuthentication(
|
|||
return { preferred_username: "bypassed" };
|
||||
}
|
||||
|
||||
// เพิ่มการจัดการสำหรับ Internal Authentication (.NET service)
|
||||
if (securityName === "internalAuth") {
|
||||
return await handleInternalAuth(request);
|
||||
}
|
||||
|
||||
// เพิ่มการจัดการสำหรับ Web Service Authentication
|
||||
if (securityName === "webServiceAuth") {
|
||||
return await handleWebServiceAuth(request);
|
||||
|
|
|
|||
30
src/middlewares/authInternal.ts
Normal file
30
src/middlewares/authInternal.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import * as express from "express";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
|
||||
// Internal Authentication (สำหรับ Internal Service เช่น .NET)
|
||||
// ตรวจสอบ API Key จาก Environment Variable (API_KEY)
|
||||
export async function handleInternalAuth(request: express.Request) {
|
||||
// รองรับ header หลายรูปแบบ
|
||||
const apiKey =
|
||||
request.headers["api-key"] || request.headers["apikey"];
|
||||
|
||||
if (!apiKey || typeof apiKey !== "string") {
|
||||
throw new HttpError(HttpStatus.UNAUTHORIZED, "API Key is required");
|
||||
}
|
||||
|
||||
// ตรวจสอบ API Key จาก Environment Variable (API_KEY)
|
||||
if (apiKey !== process.env.API_KEY) {
|
||||
console.log(`[InternalAuth] Invalid API key attempt: ${apiKey.substring(0, 5)}...`);
|
||||
throw new HttpError(HttpStatus.UNAUTHORIZED, "Invalid API Key");
|
||||
}
|
||||
|
||||
console.log(`[InternalAuth] Authentication successful`);
|
||||
|
||||
return {
|
||||
sub: "internal_service",
|
||||
preferred_username: "internal_service",
|
||||
name: "Internal Service",
|
||||
internalKey: true,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue