start project
This commit is contained in:
commit
68c6bc7a62
6 changed files with 155 additions and 0 deletions
78
sso.js
Normal file
78
sso.js
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
//simulate BMA SSO behavior Authenticator and reverse proxy
|
||||
//cookie with token is the final product of SSO
|
||||
|
||||
require("dotenv").config();
|
||||
const port = Number(process.env.SSO_PORT) || 80;
|
||||
const express = require("express");
|
||||
// const { createProxyMiddleware } = require("http-proxy-middleware");
|
||||
// const path = require("path");
|
||||
const jwt = require("jsonwebtoken");
|
||||
const fs = require("fs");
|
||||
const axios = require("axios");
|
||||
|
||||
const cookieName = process.env.SSO_COOKIE_NAME || "ssotoken";
|
||||
const privateKey = fs.readFileSync(`${process.cwd()}/BMA`, "utf8");
|
||||
const signOptions = {
|
||||
issuer: "BMA corp",
|
||||
subject: "sso@bangkok.go.th",
|
||||
audience: "http://sso.bangkok.go.th",
|
||||
expiresIn: "12h",
|
||||
algorithm: "RS256",
|
||||
};
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.post("/signin", async (req, res) => {
|
||||
|
||||
try {
|
||||
const login_user = req.body;
|
||||
const urlKeycloakToken = `${process.env.KC_URL}/realms/${process.env.KC_REALMS}/protocol/openid-connect/token`;
|
||||
console.log("urlKeycloakToken===>", urlKeycloakToken);
|
||||
|
||||
|
||||
const formdata = new URLSearchParams();
|
||||
formdata.append("grant_type", "password");
|
||||
formdata.append("client_id", process.env.VITE_CLIENTID_KEYCLOAK);
|
||||
formdata.append("username", login_user.username);
|
||||
formdata.append("password", login_user.password);
|
||||
|
||||
console.log("formdata===>", formdata);
|
||||
|
||||
const response = await axios.post(urlKeycloakToken, formdata, {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
|
||||
if (response.data) {
|
||||
console.log("response===>", response.data);
|
||||
|
||||
const payload = { username: login_user.username };
|
||||
let token = jwt.sign(payload, privateKey, signOptions);
|
||||
|
||||
console.log("token===>", token);
|
||||
console.log("cookieName===>", cookieName);
|
||||
|
||||
res.cookie(cookieName, token, {
|
||||
maxAge: 1000 * 60 * 60 * 24, // กำหนด timeout หน่วยเป็น millisecond
|
||||
path: "/",
|
||||
httpOnly: false,
|
||||
});
|
||||
|
||||
res.sendStatus(200);
|
||||
} else {
|
||||
res.status(401).send("Incorrect user or password");
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send("Incorrect user or password");
|
||||
}
|
||||
});
|
||||
|
||||
// app.use(express.static(path.join(__dirname, "public-sso")));
|
||||
|
||||
// app.get("/", (_req, res) => {
|
||||
// res.sendFile(`${process.cwd()}/sso.js`);
|
||||
// });
|
||||
|
||||
console.log("Start BMA SSO Simulator at port " + port);
|
||||
app.listen(port);
|
||||
Loading…
Add table
Add a link
Reference in a new issue