cronjob admin token

This commit is contained in:
AdisakKanthawilang 2024-12-18 10:59:57 +07:00
parent 4077900079
commit 42c9c8a8f5
4 changed files with 36 additions and 28 deletions

View file

@ -61,6 +61,8 @@ import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory
import permission from "../interfaces/permission";
import { CommandSign } from "../entities/CommandSign";
import { RoleKeycloak } from "../entities/RoleKeycloak";
import axios from "axios";
import querystring from "querystring";
@Route("api/v1/org/command")
@Tags("Command")
@ -1244,29 +1246,23 @@ export class CommandController extends Controller {
},
});
const data = {
let body = {
client_id: "gettoken",
client_secret: process.env.AUTH_ACCOUNT_SECRET,
grant_type: "password",
requested_token_type: "urn:ietf:params:oauth:token-type:refresh_token",
username: process.env.USERNAME_,
password: process.env.PASSWORD_,
grant_type: "client_credentials",
};
let _data: any = null;
await Promise.all([
await new CallAPI()
.PostDataKeycloak(`/realms/${process.env.KC_REALM}/protocol/openid-connect/token`, data)
.then(async (x) => {
_data = x;
})
.catch(async (x) => {
throw new HttpError(HttpStatus.UNAUTHORIZED, "ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง");
}),
]);
if (_data == null) {
return new HttpError(HttpStatus.UNAUTHORIZED, "ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง");
}
const postData = querystring.stringify(body);
// get admin token
const response = await axios.post(`${process.env.KC_URL}/realms/${process.env.KC_REALM}/protocol/openid-connect/token`, postData, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
api_key: process.env.API_KEY,
},
});
const adminToken = response.data.access_token;
command.forEach(async (x) => {
const path = commandTypePath(x.commandType.code);
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
@ -1280,8 +1276,8 @@ export class CommandController extends Controller {
// lastUpdateFullName: _data.user.name,
lastUpdatedAt: new Date(),
},
user: _data.user,
token: _data.access_token,
// user: _data.user,
token: adminToken,
};
sendToQueue(msg);
});

View file

@ -62,7 +62,7 @@ function createConsumer( //----> consumer
console.log("[AMQ] Process Consumer success");
return channel.ack(msg);
}
console.log("[AMQ] Process Consumer fail");
console.log("[AMQ] Process Consumer failed");
return await new Promise((resolve) => setTimeout(() => resolve(channel.nack(msg)), 3000));
},
{ noAck: false },
@ -82,9 +82,10 @@ async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
const path = commandTypePath(command.commandType.code);
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
return await new CallAPI()
.PostData(
{
headers: { authorization: token }, //time bomb
headers: { authorization: token },
},
path + "/excecute",
{