Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
TeddyRoncin committed May 15, 2024
1 parent 033fc6a commit aa57173
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 47 deletions.
1 change: 1 addition & 0 deletions web/.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ SESAME_LENGTH=4
SENTRY_DSN=
TIME_BEFORE_CHEST_DEATH=3600
TOTAL_JOYCONS=4
API_KEY=test
9 changes: 8 additions & 1 deletion web/src/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import {generateCode, setLastTimeChestWasAlive} from "./utils";

const apiRouter = Router();

apiRouter.use((request: Request, response: Response, next) => {
if (!request.headers['Authorisation'] || request.headers['Authorisation'] !== `Bearer ${process.env.API_KEY}`) {
return response.status(403).send("Invalid API Key");
}
return next();
});

apiRouter.post("/sesame", async (request: Request, response: Response) => {
const sesame: string | undefined = request.body.code;
if (!sesame) return response.status(400).send("Missing code");
Expand Down Expand Up @@ -50,7 +57,7 @@ apiRouter.post("/sesame", async (request: Request, response: Response) => {
return response.status(200).send("Sésame ouvre toi");
});

apiRouter.get("/imstillalive", async (request: Request, response: Response) => {
apiRouter.get("/ping", async (request: Request, response: Response) => {
setLastTimeChestWasAlive(Date.now());
return response.status(200).send("Good news ! (Me too)");
});
Expand Down
92 changes: 46 additions & 46 deletions web/src/webRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,58 +91,58 @@ webRouter.get("/code", async (request: Request, response: Response) => {
});

webRouter.get("/login", async (request: Request, response: Response) => {
if (request.query["ticket"]) {
const res = await fetch(
`https://cas.utt.fr/cas/serviceValidate?service=${encodeURI(
process.env.CAS_SERVICE
)}&ticket=${request.query["ticket"]}`
);
const resData: {
["cas:serviceResponse"]:
| {
["cas:authenticationSuccess"]: {
["cas:attributes"]: {
"cas:uid": string;
"cas:mail": string;
"cas:sn": string;
"cas:givenName": string;
};
};
}
| { "cas:authenticationFailure": unknown };
} = new XMLParser().parse(await res.text());
if ("cas:authenticationFailure" in resData["cas:serviceResponse"]) {
return response.redirect("/login");
if (!request.query["ticket"]) {
return response.sendFile(path.join(__dirname, "../www/login.html"));
}
const res = await fetch(
`https://cas.utt.fr/cas/serviceValidate?service=${encodeURI(
process.env.CAS_SERVICE
)}&ticket=${request.query["ticket"]}`
);
const resData: {
["cas:serviceResponse"]:
| {
["cas:authenticationSuccess"]: {
["cas:attributes"]: {
"cas:uid": string;
"cas:mail": string;
"cas:sn": string;
"cas:givenName": string;
};
};
}
const userData = {
login:
resData["cas:serviceResponse"]["cas:authenticationSuccess"][
"cas:attributes"
]["cas:uid"],
mail: resData["cas:serviceResponse"]["cas:authenticationSuccess"][
| { "cas:authenticationFailure": unknown };
} = new XMLParser().parse(await res.text());
if ("cas:authenticationFailure" in resData["cas:serviceResponse"]) {
return response.redirect('/login');
}
const userData = {
login:
resData["cas:serviceResponse"]["cas:authenticationSuccess"][
"cas:attributes"
]["cas:uid"],
mail: resData["cas:serviceResponse"]["cas:authenticationSuccess"][
"cas:attributes"
]["cas:mail"],
lastName:
resData["cas:serviceResponse"]["cas:authenticationSuccess"][
"cas:attributes"
lastName:
resData["cas:serviceResponse"]["cas:authenticationSuccess"][
"cas:attributes"
]["cas:sn"],
firstName:
resData["cas:serviceResponse"]["cas:authenticationSuccess"][
"cas:attributes"
firstName:
resData["cas:serviceResponse"]["cas:authenticationSuccess"][
"cas:attributes"
]["cas:givenName"],
};
let user = await prisma.user.findUnique({
where: { login: userData.login },
});
if (!user) {
await prisma.user.create({ data: userData });
}
const token = jwt.sign({ login: userData.login }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRES_IN,
});
return response.cookie("token", token).redirect("/");
};
let user = await prisma.user.findUnique({
where: { login: userData.login },
});
if (!user) {
await prisma.user.create({ data: userData });
}
return response.sendFile(path.join(__dirname, "../www/login.html"));
const token = jwt.sign({ login: userData.login }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRES_IN,
});
return response.cookie("token", token).redirect("/");
});

webRouter.get("/login/cas", async (request: Request, response: Response) => {
Expand Down

0 comments on commit aa57173

Please sign in to comment.