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