add api key
This commit is contained in:
parent
94ea64247f
commit
f111132184
13 changed files with 291 additions and 4666 deletions
|
|
@ -20,6 +20,8 @@ import { In } from "typeorm";
|
|||
import { RequestWithUser } from "../middlewares/user";
|
||||
import { ApiName } from "../entities/ApiName";
|
||||
import { ApiHistory } from "../entities/ApiHistory";
|
||||
|
||||
const jwt = require("jsonwebtoken");
|
||||
@Route("api/v1/org/apiKey")
|
||||
@Tags("ApiKey")
|
||||
@Security("bearerAuth")
|
||||
|
|
@ -32,6 +34,32 @@ export class ApiKeyController extends Controller {
|
|||
private apiNameRepository = AppDataSource.getRepository(ApiName);
|
||||
private apiHistoryRepository = AppDataSource.getRepository(ApiHistory);
|
||||
|
||||
/**
|
||||
* API ตรวจสอบและถอดรหัส JWT token
|
||||
*
|
||||
* @summary ตรวจสอบ JWT API Key
|
||||
*/
|
||||
@Post("verify")
|
||||
async verifyApiKey(@Body() requestBody: { token: string }) {
|
||||
try {
|
||||
const jwtSecret = process.env.JWT_SECRET || "your-default-secret-key";
|
||||
console.log("JWT_SECRET from env:", process.env.JWT_SECRET ? "exists" : "not found");
|
||||
console.log("Using secret:", jwtSecret);
|
||||
|
||||
const decoded = jwt.verify(requestBody.token, jwtSecret);
|
||||
return new HttpSuccess({
|
||||
valid: true,
|
||||
data: decoded,
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error("JWT Verification Error:", error.message);
|
||||
return new HttpSuccess({
|
||||
valid: false,
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API สร้าง Api Key
|
||||
*
|
||||
|
|
@ -52,8 +80,33 @@ export class ApiKeyController extends Controller {
|
|||
const apiName = await this.apiNameRepository.find({
|
||||
where: { id: In(requestBody.apiId) },
|
||||
});
|
||||
|
||||
const apiKey = Object.assign(new ApiKey(), requestBody);
|
||||
apiKey.keyApi = require("crypto").randomBytes(64).toString("base64");
|
||||
|
||||
// Create JWT token with embedded data
|
||||
const tokenPayload = {
|
||||
keyId: apiKey.id || require("crypto").randomUUID(),
|
||||
name: apiKey.name,
|
||||
accessType: apiKey.accessType,
|
||||
dnaRootId: apiKey.dnaRootId,
|
||||
dnaChild1Id: apiKey.dnaChild1Id,
|
||||
dnaChild2Id: apiKey.dnaChild2Id,
|
||||
dnaChild3Id: apiKey.dnaChild3Id,
|
||||
dnaChild4Id: apiKey.dnaChild4Id,
|
||||
apiIds: requestBody.apiId,
|
||||
createdBy: request.user.sub,
|
||||
createdAt: new Date().toISOString(),
|
||||
iat: Math.floor(Date.now() / 1000),
|
||||
};
|
||||
|
||||
// Sign JWT with secret (you should use environment variable for the secret)
|
||||
const jwtSecret = process.env.JWT_SECRET || "your-default-secret-key";
|
||||
|
||||
const jwtToken = jwt.sign(tokenPayload, jwtSecret, {
|
||||
expiresIn: "365d", // 1 year expiration
|
||||
});
|
||||
|
||||
apiKey.keyApi = jwtToken;
|
||||
apiKey.apiNames = apiName;
|
||||
apiKey.createdUserId = request.user.sub;
|
||||
apiKey.createdFullName = request.user.name;
|
||||
|
|
@ -104,6 +157,12 @@ export class ApiKeyController extends Controller {
|
|||
createdUserId: _data.createdUserId,
|
||||
createdFullName: _data.createdFullName,
|
||||
name: _data.name,
|
||||
accessType: _data.accessType,
|
||||
dnaRootId: _data.dnaRootId,
|
||||
dnaChild1Id: _data.dnaChild1Id,
|
||||
dnaChild2Id: _data.dnaChild2Id,
|
||||
dnaChild3Id: _data.dnaChild3Id,
|
||||
dnaChild4Id: _data.dnaChild4Id,
|
||||
apiNames: _data.apiNames.map((x) => ({
|
||||
id: x.id,
|
||||
name: x.name,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue