Skip to content

Commit cd79104

Browse files
committed
Added some auth, getting 'The application you attempted to authenticate to is not authorized to use CAS.' upon attempted login. Need to try on VM
1 parent 2849460 commit cd79104

File tree

7 files changed

+98
-6
lines changed

7 files changed

+98
-6
lines changed

server/src/types/express.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as express from "express";
1+
import { Request } from "express";
22

33
declare global {
44
namespace Express {
5-
interface Request {
6-
user?: any; // Adjust the type as needed, e.g., `Record<string, any>` or a custom User type
5+
export interface Request {
6+
user?: any; // Adjust the type as needed
77
}
88
}
99
}

server/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,6 @@
109109
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
110110
"skipLibCheck": true /* Skip type checking all .d.ts files. */
111111
},
112-
"include": ["src/**/*.ts", "tests/**/*.ts"]
112+
"include": ["src/**/*.ts", "tests/**/*.ts"],
113+
"files": ["src/types/express.d.ts"]
113114
}

web/src/App.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { useState } from "react";
22
import { Route, Routes, BrowserRouter } from "react-router-dom";
33
import "./App.css";
4+
import CASCallback from "./auth/CASCallback";
5+
import ProtectedRoute from "./auth/ProtectedRoute";
6+
import UploadPage from "./pages/UploadPage";
7+
import ModifyPage from "./pages/ModifyPage";
48

59
function App() {
610
//const [count, setCount] = useState(0);
@@ -9,13 +13,25 @@ function App() {
913

1014
<BrowserRouter>
1115
<Routes>
16+
{/* Unprotected routes */}
17+
<Route path="/cas-callback" element={<CASCallback />} />
18+
19+
{/* Protected Routes */}
1220
<Route
1321
path="/upload"
14-
//element={}
22+
element={
23+
<ProtectedRoute>
24+
<UploadPage />
25+
</ProtectedRoute>
26+
}
1527
></Route>
1628
<Route
1729
path="/modify"
18-
//element={}
30+
element={
31+
<ProtectedRoute>
32+
<ModifyPage />
33+
</ProtectedRoute>
34+
}
1935
></Route>
2036
</Routes>
2137

web/src/auth/CASCallback.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React, { useEffect } from 'react';
2+
3+
const BACKEND_URL = 'https://localhost:3000'; // Replace with your backend URL
4+
5+
const CASCallback: React.FC = () => {
6+
useEffect(() => {
7+
// Extract CAS ticket from URL
8+
const urlParams = new URLSearchParams(window.location.search);
9+
const ticket = urlParams.get('ticket');
10+
11+
if (ticket) {
12+
// Call backend to validate ticket
13+
fetch(`${BACKEND_URL}/admin-router/auth/cas-validate?ticket=${ticket}&service=${encodeURIComponent(window.location.origin + '/cas-callback')}`)
14+
.then((response) => response.json())
15+
.then((data) => {
16+
if (data.token) {
17+
// Store JWT in localStorage or cookies
18+
localStorage.setItem('jwt', data.token);
19+
// Redirect to a protected page or home
20+
window.location.href = '/';
21+
} else {
22+
console.error('CAS validation failed:', data.error);
23+
}
24+
})
25+
.catch((error) => {
26+
console.error('Error during CAS validation:', error);
27+
});
28+
} else {
29+
console.error('No ticket found in URL');
30+
}
31+
}, []);
32+
33+
return (
34+
<div>
35+
Authenticating with CAS...
36+
</div>)
37+
};
38+
39+
export default CASCallback;

web/src/auth/ProtectedRoute.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function redirectToCAS() {
2+
const CAS_SERVER = 'https://cas.usask.ca/cas';
3+
const FRONTEND_URL = 'https://localhost:5173/';
4+
const serviceURL = `${FRONTEND_URL}/cas-callback`;
5+
6+
window.location.href = `${CAS_SERVER}/login?service=${encodeURIComponent(serviceURL)}`;
7+
}
8+
9+
10+
export default function ProtectedRoute({ children }: { children: JSX.Element}) {
11+
const token = localStorage.getItem('jwt');
12+
13+
if (!token) {
14+
redirectToCAS(); // Redirect to CAS login if no JWT
15+
return null; // Optional: Display a loading spinner while redirecting
16+
}
17+
18+
return children;
19+
};
20+
21+
22+

web/src/pages/ModifyPage.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "../App.css"
2+
3+
export default function ModifyPage() {
4+
return (
5+
<div>Modify Page</div>
6+
)
7+
}

web/src/pages/UploadPage.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "../App.css"
2+
3+
export default function UploadPage() {
4+
return (
5+
<div>Upload Page</div>
6+
)
7+
}

0 commit comments

Comments
 (0)