Skip to content

Commit 5578930

Browse files
committed
Auth working on the VM, must be on the usask network for it to work
1 parent a724484 commit 5578930

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

server/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from "express";
22
import sqlite3 from "sqlite3";
33
import cors from "cors";
44
import bodyParser from "body-parser";
5+
import dotenv from "dotenv";
56
import { open, Database } from "sqlite";
67

78
// Import routers
@@ -13,7 +14,7 @@ import adminRouter from "./routes/admin-router";
1314

1415
const app = express();
1516
const PORT = process.env.PORT || 3000; // Use environment variable if available, otherwise default to 3000
16-
17+
dotenv.config();
1718
/* In the future this will be used to ensure that only requests from certain domains are accepted
1819
const corsOptions = {
1920
origin: (origin: string | undefined, callback: (err: Error | null, allowed: boolean) => void) => {

server/src/routes/admin-router.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ export default function adminRouter(dbController: DatabaseController): Router {
5555
router.get("/auth/cas-validate", async (req: Request, res: Response) => {
5656
const { ticket, service } = req.query;
5757

58-
console.log("hit");
59-
console.log(ticket);
60-
console.log(service);
6158
if (!ticket || !service) {
6259
res.status(400).json({ error: "Missing ticket or service" });
6360
}
@@ -74,25 +71,27 @@ export default function adminRouter(dbController: DatabaseController): Router {
7471

7572

7673
const casData = casResponse.data; // assumed user CAS info, need to test to see
77-
const nsid = casData.user; // Potentally the nsid of the user. Again need to test
78-
79-
console.log(casData);
80-
console.log(nsid);
81-
if (!nsid) {
82-
res.status(401).json({ error: "Invalid CAS Ticket" });
83-
}
84-
85-
if (!allowedNSIDs.includes(nsid)) {
86-
res.status(403).json({ error: "Access denied" });
87-
}
88-
89-
const token = jwt.sign(
90-
{ username: casData.user, roles: casData.roles },
91-
process.env.JWT_SECRET!,
92-
{ expiresIn: "3h" },
93-
);
94-
95-
res.json({ token });
74+
if (casData.includes('<cas:authenticationSuccess>')) {
75+
const nsid = casData.match(/<cas:user>(.*?)<\/cas:user>/)[1];
76+
console.log(`User logged in with nsid: ${nsid}`);
77+
if (!nsid) {
78+
res.status(401).json({ error: "Invalid CAS Ticket" });
79+
}
80+
81+
if (!allowedNSIDs.includes(nsid)) {
82+
console.log(`User attempted to login with nsid: ${nsid}`);
83+
res.status(403).json({ error: "Access denied" });
84+
}
85+
const token = jwt.sign(
86+
{ username: casData.user, roles: casData.roles },
87+
process.env.JWT_SECRET!,
88+
{ expiresIn: "3h" },
89+
);
90+
res.json({token});
91+
} else {
92+
res.status(401).json({ error: 'CAS authentication failed' });
93+
94+
}
9695
} catch (error) {
9796
res.status(500).json({ error: "CAS validation failed" });
9897
}

web/src/auth/CASCallback.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const CASCallback: React.FC = () => {
88
const urlParams = new URLSearchParams(window.location.search);
99
const ticket = urlParams.get('ticket');
1010

11-
console.log(ticket);
12-
1311
if (ticket) {
1412
// Call backend to validate ticket
1513
fetch(`${BACKEND_URL}/api/adminRequest/auth/cas-validate?ticket=${ticket}&service=${encodeURIComponent(window.location.origin + '/cas-callback')}`)

0 commit comments

Comments
 (0)