Added catalog generation, need to bug fix

This commit is contained in:
Kevin FEDYNA
2025-01-15 10:15:02 +01:00
parent ccad788e19
commit eea49969b5
17 changed files with 248 additions and 99 deletions
+12
View File
@@ -0,0 +1,12 @@
type FooterProps = Record<string | number | symbol, never>;
export default function Footer(_props: FooterProps) {
return (
<footer>
<p>
&copy; 2025 PolyMPR -{" "}
<a href="/about" f-client-nav={false}>About</a>
</p>
</footer>
);
}
+17
View File
@@ -0,0 +1,17 @@
type HeaderProps = {
link: string;
};
export default function Header(props: HeaderProps) {
return (
<header>
<h1>
<a href="/" f-client-nav={false}>PolyMPR</a>
</h1>
<nav>
<a href="/apps" f-client-nav={false}>Catalog</a>
<a href={`/log${props.link}`} f-client-nav={false}>Log {props.link}</a>
</nav>
</header>
);
}
+27
View File
@@ -0,0 +1,27 @@
export interface AppProperties {
name: string;
icon: string;
}
type AppNavigatorProps = Record<string | number | symbol, never>;
export default async function AppNavigator(_props: AppNavigatorProps) {
const apps: Record<string, AppProperties> = {};
for await (const appDir of Deno.readDir("../(apps)")) {
try {
const properties: AppProperties = await import(`../(apps)/${appDir.name}/(_props)/props.ts`);
apps[appDir.name] = properties;
}
catch (error) {
console.error(`Couldn't import app "${appDir.name}": ${error}`);
}
}
return (
<>
<p>{JSON.stringify(apps)}</p>
</>
);
}
+8
View File
@@ -0,0 +1,8 @@
import { AppProperties } from "../../../(_islands)/AppNavigator.tsx";
const properties: AppProperties = {
name: "PolyNotes",
icon: "school"
};
export default properties;
+12
View File
@@ -0,0 +1,12 @@
type ModulesProps = Record<string | number | symbol, never>;
export default function Modules(_props: ModulesProps) {
return (
<>
<h2>All PolyMPR modules</h2>
<nav>
</nav>
</>
);
}
View File
+18 -16
View File
@@ -1,8 +1,14 @@
import { FreshContext } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
import { State } from "./_middleware.ts";
import Header from "./(_components)/Header.tsx";
import Footer from "./(_components)/Footer.tsx";
export default async function App(request: Request, context: FreshContext) {
// deno-lint-ignore require-await
export default async function App(
_request: Request,
context: FreshContext<State>,
) {
const link = context.state.isAuthenticated ? "out" : "in";
return (
@@ -11,23 +17,19 @@ export default async function App(request: Request, context: FreshContext) {
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PolyMPR</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,100..800;1,100..800&display=swap" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0&icon_names=school" />
<link rel="stylesheet" href="/styles/main.css" />
</head>
<body f-client-nav>
<header>
<h1>PolyMPR</h1>
<nav>
<a href="/modules" f-partial="/partials/modules">Modules</a>
<a href={`/log${link}`} f-client-nav={false}>Log {link}</a>
</nav>
</header>
<Partial name="body">
<context.Component />
</Partial>
<footer>
<p>&copy; 2025 PolyMPR - <a href="/about" f-partial="/partials/about">About</a></p>
</footer>
<Header link={link} />
<section>
<Partial name="body">
<context.Component />
</Partial>
</section>
<Footer />
</body>
</html>
);
}
}
+28 -15
View File
@@ -1,39 +1,52 @@
import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { FreshContext } from "$fresh/server.ts";
import { getCookies } from "$std/http/cookie.ts";
import { isJwtValid } from "@popov/jwt";
const PUBLIC_ROUTES = [
"/",
"/login",
"/logout",
"/about",
"/partials/about",
"/contact",
];
const PUBLIC_ROUTES = ["/", "/login", "/logout", "/about", "/contact"];
interface State {
export interface State {
isAuthenticated: boolean;
}
function isRoutePublic(route: string) {
return PUBLIC_ROUTES.includes(route) || route.match(/\..+$/);
}
export const handler = [
async function checkAuthentication(request: Request, context: MiddlewareHandlerContext<State>) {
async function checkAuthentication(
request: Request,
context: FreshContext<State>,
) {
const cookies = getCookies(request.headers);
context.state.isAuthenticated = await isJwtValid(cookies["sessionToken"] ?? "", "NEED TO CHANGE THIS KEY FURTHER IN DEV");
context.state.isAuthenticated = await isJwtValid(
cookies["sessionToken"] ?? "",
"NEED TO CHANGE THIS KEY FURTHER IN DEV",
);
return context.next();
return await context.next();
},
async function ensureAuthentication(request: Request, context: MiddlewareHandlerContext<State>) {
async function ensureAuthentication(
request: Request,
context: FreshContext<State>,
) {
const url = new URL(request.url);
if (!isRoutePublic(url.pathname) && !context.state.isAuthenticated) {
return new Response(null, {
status: 302,
headers: {
Location: "/login"
}
Location: "/login",
},
});
}
return context.next();
}
];
return await context.next();
},
];
+15
View File
@@ -0,0 +1,15 @@
import { FreshContext } from "$fresh/server.ts";
// deno-lint-ignore require-await
export default async function About(_request: Request, _context: FreshContext) {
return (
<>
<h2>About PolyMPR</h2>
<p>
PolyMPR is born from the will to enhance Polytech INFO department's HR
infrastructure.
</p>
<h3>Terms of Use</h3>
</>
);
}
+13
View File
@@ -0,0 +1,13 @@
import { FreshContext } from "$fresh/server.ts";
import AppNavigator from "./(_islands)/AppNavigator.tsx";
// deno-lint-ignore require-await
export default async function About(_request: Request, _context: FreshContext) {
return (
<>
{
//<AppNavigator />
}
</>
);
}
+6 -4
View File
@@ -1,8 +1,10 @@
import { useSignal } from "@preact/signals";
import Counter from "../islands/Counter.tsx";
import { FreshContext } from "$fresh/server.ts";
export default function Home() {
// deno-lint-ignore require-await
export default async function Home(_request: Request, _context: FreshContext) {
return (
<h1>PolyMPR</h1>
<>
<h2>Welcome to PolyMPR!</h2>
</>
);
}
+40 -28
View File
@@ -1,34 +1,45 @@
import { Handlers } from "$fresh/server.ts";
import { State } from "./_middleware.ts";
import { parse, type XmlNode } from "@melvdouc/xml-parser";
import {
parse,
type RegularTagNode,
type TextNode,
} from "@melvdouc/xml-parser";
import { createJwt } from "@popov/jwt";
import { setCookie } from "$std/http/cookie.ts";
const SERVICE = "https://localhost/login";
const CAS = "https://ident.univ-amu.fr/cas";
interface CasTagNode extends RegularTagNode {
children: [TextNode];
}
function getTag(tag: XmlNode): [string, string] {
interface CasGroupNode extends RegularTagNode {
children: CasTagNode[];
}
interface CasResponse extends RegularTagNode {
children: [TextNode, CasGroupNode];
}
function getTag(tag: CasTagNode): [string, string] {
return [
tag.tagName.replace("cas:", ""),
tag.children[0].value
tag.children[0].value,
];
}
async function createUserJWT(casResponse: XmlNode): Promise<string> {
function createUserJWT(casResponse: CasResponse): Promise<string> {
const nodes = casResponse.children[1].children.map(getTag);
const fullUserInfos = {};
const fullUserInfos: Record<string, string | string[]> = {};
nodes.forEach(([key, value]) => {
if (fullUserInfos[key] && Array.isArray(fullUserInfos[key])) {
fullUserInfos[key].push(value);
}
else if (fullUserInfos[key]) {
} else if (fullUserInfos[key]) {
fullUserInfos[key] = [fullUserInfos[key], value];
}
else {
} else {
fullUserInfos[key] = value;
}
});
@@ -41,30 +52,32 @@ async function createUserJWT(casResponse: XmlNode): Promise<string> {
iat: now,
exp: now + oneHour,
aud: "PolyMPR",
user: fullUserInfos
user: fullUserInfos,
};
const key = "NEED TO CHANGE THIS KEY FURTHER IN DEV";
return createJwt(payload, key);
}
// deno-lint-ignore no-explicit-any
export const handler: Handlers<any, State> = {
async GET(request, context) {
const url = new URL(request.url);
const ticket = url.searchParams.get("ticket");
if (ticket) {
const response = await fetch(`${CAS}/serviceValidate?service=${SERVICE}&ticket=${ticket}`);
const body = parse(await response.text(), "application/xml");
const casResponse = body[0].children[0];
const response = await fetch(
`${CAS}/serviceValidate?service=${SERVICE}&ticket=${ticket}`,
);
const body = parse(await response.text()) as [RegularTagNode];
const casResponse = body[0].children[0] as CasResponse;
if (casResponse.tagName != "cas:authenticationSuccess") {
return new Response(null, {
status: 302,
headers: {
Location: `${CAS}/login?service=${SERVICE}`
}
Location: `${CAS}/login?service=${SERVICE}`,
},
});
}
@@ -72,13 +85,13 @@ export const handler: Handlers<any, State> = {
setCookie(headers, {
name: "sessionToken",
value: await createUserJWT(casResponse)
value: await createUserJWT(casResponse),
});
headers.set("Location", "/apps");
return new Response(null, {
status: 302,
headers
headers,
});
}
@@ -86,17 +99,16 @@ export const handler: Handlers<any, State> = {
return new Response(null, {
status: 302,
headers: {
Location: "/apps"
}
Location: "/apps",
},
});
}
else {
} else {
return new Response(null, {
status: 302,
headers: {
Location: `${CAS}/login?service=${SERVICE}`
}
Location: `${CAS}/login?service=${SERVICE}`,
},
});
}
}
};
},
};
+8 -10
View File
@@ -2,13 +2,12 @@ import { Handlers } from "$fresh/server.ts";
import { State } from "./_middleware.ts";
import { deleteCookie } from "$std/http/cookie.ts";
const SERVICE = "https://localhost/";
const CAS = "https://ident.univ-amu.fr/cas";
// deno-lint-ignore no-explicit-any
export const handler: Handlers<any, State> = {
async GET(request, context) {
GET(_request, context) {
if (context.state.isAuthenticated) {
const headers = new Headers();
@@ -17,16 +16,15 @@ export const handler: Handlers<any, State> = {
return new Response(null, {
status: 302,
headers
headers,
});
}
else {
} else {
return new Response(null, {
status: 302,
headers: {
Location: "/"
}
Location: "/",
},
});
}
}
};
},
};
-15
View File
@@ -1,15 +0,0 @@
import { RouteConfig } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts";
export const config: RouteConfig = {
skipAppWrapper: true,
skipInheritedLayouts: true,
};
export default async function About(request, context) {
return (
<Partial name="body">
<p>C'est nous wsh</p>
</Partial>
);
};
View File