This commit is contained in:
Warunee Tamkoo 2025-02-13 13:38:04 +07:00
parent cecb85d066
commit 296e23c588

108
sso.js
View file

@ -117,69 +117,69 @@ app.post("/api/v1/sso/kcauth", async (req, res) => {
} }
// const oldssotoken = cookies['oldssotoken']; // const oldssotoken = cookies['oldssotoken'];
const olduid = cookies['olduid']; // const olduid = cookies['olduid'];
if (username !== olduid) { // if (username !== olduid) {
// create body for admin token // create body for admin token
let body = { let body = {
client_id: clientId, client_id: clientId,
client_secret: clientSecret, client_secret: clientSecret,
grant_type: "client_credentials", grant_type: "client_credentials",
}; };
const postData = querystring.stringify(body); const postData = querystring.stringify(body);
// get admin token // get admin token
const response = await axios.post(urlKeycloakToken, postData, { const response = await axios.post(urlKeycloakToken, postData, {
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": "application/x-www-form-urlencoded",
}, },
}); });
// console.log("==== admin token ===="); // console.log("==== admin token ====");
const adminToken = response.data.access_token; const adminToken = response.data.access_token;
// console.log(adminToken); // console.log(adminToken);
// create body for user token // create body for user token
let body2 = { let body2 = {
client_id: clientId, client_id: clientId,
client_secret: clientSecret, client_secret: clientSecret,
grant_type: "urn:ietf:params:oauth:grant-type:token-exchange", grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
subject_token: adminToken, subject_token: adminToken,
requested_token_type: "urn:ietf:params:oauth:token-type:refresh_token", requested_token_type: "urn:ietf:params:oauth:token-type:refresh_token",
audience: clientId, audience: clientId,
requested_subject: username, requested_subject: username,
}; };
const postData2 = querystring.stringify(body2); const postData2 = querystring.stringify(body2);
// console.log("==== postData2 ===="); // console.log("==== postData2 ====");
// console.log(body2); // console.log(body2);
// get admin token // get admin token
const tokenResponse = await axios.post(urlKeycloakToken, postData2, { const tokenResponse = await axios.post(urlKeycloakToken, postData2, {
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": "application/x-www-form-urlencoded",
}, },
}); });
// console.log("==== user token ===="); // console.log("==== user token ====");
// console.log(tokenResponse.data); // console.log(tokenResponse.data);
// await postLog('เข้าสู่ระบบ', tokenResponse.data.access_token); // await postLog('เข้าสู่ระบบ', tokenResponse.data.access_token);
// res.cookie('oldssotoken', tokenSSO, { // res.cookie('oldssotoken', tokenSSO, {
// maxAge: 1000 * 60 * 60 * 24, // กำหนด timeout หน่วยเป็น millisecond // maxAge: 1000 * 60 * 60 * 24, // กำหนด timeout หน่วยเป็น millisecond
// path: "/", // path: "/",
// httpOnly: true, // httpOnly: true,
// }); // });
res.cookie('olduid', username, { // res.cookie('olduid', username, {
maxAge: 1000 * 60 * 60 * 24, // กำหนด timeout หน่วยเป็น millisecond // maxAge: 1000 * 60 * 60 * 24, // กำหนด timeout หน่วยเป็น millisecond
path: "/", // path: "/",
httpOnly: true, // httpOnly: true,
}); // });
res.status(200).send(tokenResponse.data); res.status(200).send(tokenResponse.data);
} else { // } else {
res.status(200).send({ isLogin: true }); // res.status(200).send({ isLogin: true });
} // }
} catch (error) { } catch (error) {
// console.log("error===>", error); // console.log("error===>", error);