Fixed partial handling and added cookies
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
create table students (
|
||||||
|
userId text primary key,
|
||||||
|
firstName text,
|
||||||
|
lastName text,
|
||||||
|
mail text,
|
||||||
|
promo integer,
|
||||||
|
foreign key(promo) references promo(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
create table promo (
|
||||||
|
id integer,
|
||||||
|
endyear integer,
|
||||||
|
current integer
|
||||||
|
);
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { type RegularTagNode, type TextNode } from "@melvdouc/xml-parser";
|
||||||
|
|
||||||
export interface AppProperties {
|
export interface AppProperties {
|
||||||
name: string;
|
name: string;
|
||||||
icon: string;
|
icon: string;
|
||||||
@@ -6,4 +8,43 @@ export interface AppProperties {
|
|||||||
hint: string;
|
hint: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CasTagNode extends RegularTagNode {
|
||||||
|
children: [TextNode];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CasGroupNode extends RegularTagNode {
|
||||||
|
children: CasTagNode[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CasResponse extends RegularTagNode {
|
||||||
|
children: [TextNode, CasGroupNode];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CasContent {
|
||||||
|
amuCampus: string;
|
||||||
|
amuComposante: string;
|
||||||
|
amuDateValidation: string;
|
||||||
|
coGroup: string;
|
||||||
|
eduPersonPrimaryAffiliation: string;
|
||||||
|
eduPersonPrincipalName: string;
|
||||||
|
mail: string;
|
||||||
|
displayName: string;
|
||||||
|
givenName: string;
|
||||||
|
memberOf: string[];
|
||||||
|
sn: string;
|
||||||
|
supannCivilite: string;
|
||||||
|
supannEntiteAffectation: string;
|
||||||
|
supannEtuAnneeInscription: string;
|
||||||
|
supannEtuEtape: string;
|
||||||
|
uid: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LoginJWT {
|
||||||
|
iss: "PolyMPR";
|
||||||
|
iat: number;
|
||||||
|
exp: number;
|
||||||
|
aud: "PolyMPR";
|
||||||
|
user: CasContent;
|
||||||
|
}
|
||||||
|
|
||||||
export type EmptyObject = Record<string | number | symbol, never>;
|
export type EmptyObject = Record<string | number | symbol, never>;
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { EmptyObject } from "$root/defaults/interfaces.ts";
|
import { FreshContext } from "$fresh/server.ts";
|
||||||
|
import { State } from "$root/routes/_middleware.ts";
|
||||||
|
|
||||||
export default function makeIndex<
|
export default function makeIndex(basePath: string) {
|
||||||
IndexProps = EmptyObject,
|
return async function Index(
|
||||||
>(basePath: string) {
|
request: Request,
|
||||||
return async function Index(props: IndexProps) {
|
context: FreshContext<State>,
|
||||||
|
) {
|
||||||
const index = (await import(`${basePath}/partials/index.tsx`)).Index;
|
const index = (await import(`${basePath}/partials/index.tsx`)).Index;
|
||||||
return index(props);
|
return index(request, context);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { JSX } from "preact";
|
import { JSX } from "preact";
|
||||||
import { Partial } from "$fresh/runtime.ts";
|
import { Partial } from "$fresh/runtime.ts";
|
||||||
import { RouteConfig } from "$fresh/server.ts";
|
import { FreshContext, RouteConfig } from "$fresh/server.ts";
|
||||||
|
import { State } from "$root/routes/_middleware.ts";
|
||||||
|
|
||||||
export function getPartialsConfig(): RouteConfig {
|
export function getPartialsConfig(): RouteConfig {
|
||||||
return {
|
return {
|
||||||
@@ -9,14 +10,19 @@ export function getPartialsConfig(): RouteConfig {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
export function makePartials(
|
||||||
export function makePartials<Props extends any>(
|
page: (
|
||||||
page: (props: Props) => JSX.Element,
|
request: Request,
|
||||||
|
context: FreshContext<State>,
|
||||||
|
) => Promise<JSX.Element>,
|
||||||
) {
|
) {
|
||||||
return function WrappedElements(props: Props): JSX.Element {
|
return async function WrappedElements(
|
||||||
|
request: Request,
|
||||||
|
context: FreshContext<State>,
|
||||||
|
): Promise<JSX.Element> {
|
||||||
return (
|
return (
|
||||||
<Partial name="body">
|
<Partial name="body">
|
||||||
{page(props)}
|
{await page(request, context)}
|
||||||
</Partial>
|
</Partial>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ import * as $_apps_notes_partials_admin_courses from "./routes/(apps)/notes/part
|
|||||||
import * as $_apps_notes_partials_admin_students from "./routes/(apps)/notes/partials/(admin)/students.tsx";
|
import * as $_apps_notes_partials_admin_students from "./routes/(apps)/notes/partials/(admin)/students.tsx";
|
||||||
import * as $_apps_notes_partials_index from "./routes/(apps)/notes/partials/index.tsx";
|
import * as $_apps_notes_partials_index from "./routes/(apps)/notes/partials/index.tsx";
|
||||||
import * as $_apps_notes_partials_notes from "./routes/(apps)/notes/partials/notes.tsx";
|
import * as $_apps_notes_partials_notes from "./routes/(apps)/notes/partials/notes.tsx";
|
||||||
|
import * as $_apps_students_api_example from "./routes/(apps)/students/api/example.ts";
|
||||||
|
import * as $_apps_students_index from "./routes/(apps)/students/index.tsx";
|
||||||
|
import * as $_apps_students_partials_index from "./routes/(apps)/students/partials/index.tsx";
|
||||||
import * as $_404 from "./routes/_404.tsx";
|
import * as $_404 from "./routes/_404.tsx";
|
||||||
import * as $_app from "./routes/_app.tsx";
|
import * as $_app from "./routes/_app.tsx";
|
||||||
import * as $_middleware from "./routes/_middleware.ts";
|
import * as $_middleware from "./routes/_middleware.ts";
|
||||||
@@ -47,6 +50,10 @@ const manifest = {
|
|||||||
$_apps_notes_partials_admin_students,
|
$_apps_notes_partials_admin_students,
|
||||||
"./routes/(apps)/notes/partials/index.tsx": $_apps_notes_partials_index,
|
"./routes/(apps)/notes/partials/index.tsx": $_apps_notes_partials_index,
|
||||||
"./routes/(apps)/notes/partials/notes.tsx": $_apps_notes_partials_notes,
|
"./routes/(apps)/notes/partials/notes.tsx": $_apps_notes_partials_notes,
|
||||||
|
"./routes/(apps)/students/api/example.ts": $_apps_students_api_example,
|
||||||
|
"./routes/(apps)/students/index.tsx": $_apps_students_index,
|
||||||
|
"./routes/(apps)/students/partials/index.tsx":
|
||||||
|
$_apps_students_partials_index,
|
||||||
"./routes/_404.tsx": $_404,
|
"./routes/_404.tsx": $_404,
|
||||||
"./routes/_app.tsx": $_app,
|
"./routes/_app.tsx": $_app,
|
||||||
"./routes/_middleware.ts": $_middleware,
|
"./routes/_middleware.ts": $_middleware,
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { AppProperties } from "$root/defaults/interfaces.ts";
|
||||||
|
|
||||||
|
const properties: AppProperties = {
|
||||||
|
name: "Students",
|
||||||
|
icon: "badge",
|
||||||
|
pages: {
|
||||||
|
index: "Homepage",
|
||||||
|
upload: "Upload students",
|
||||||
|
},
|
||||||
|
adminOnly: ["upload"],
|
||||||
|
hint: "See student information",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default properties;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { Handlers } from "$fresh/server.ts";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
async POST(request, context) {
|
||||||
|
if (request.headers.get("content-type") != "application/json") {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 400,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseBody = {
|
||||||
|
requestBody: await request.json(),
|
||||||
|
context,
|
||||||
|
};
|
||||||
|
|
||||||
|
return new Response(JSON.stringify(responseBody), {
|
||||||
|
headers: {
|
||||||
|
"content-type": "application/json",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
import makeIndex from "$root/defaults/makeIndex.ts";
|
||||||
|
export default makeIndex(import.meta.dirname!);
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import {
|
||||||
|
getPartialsConfig,
|
||||||
|
makePartials,
|
||||||
|
} from "$root/defaults/makePartials.tsx";
|
||||||
|
import { FreshContext } from "$fresh/server.ts";
|
||||||
|
import { State } from "$root/routes/_middleware.ts";
|
||||||
|
|
||||||
|
// deno-lint-ignore require-await
|
||||||
|
export async function Index(_request: Request, context: FreshContext<State>) {
|
||||||
|
return <h2>Welcome to {context.state.session?.displayName}.</h2>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const config = getPartialsConfig();
|
||||||
|
export default makePartials(Index);
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { FreshContext } from "$fresh/server.ts";
|
import { FreshContext } from "$fresh/server.ts";
|
||||||
import { getCookies } from "$std/http/cookie.ts";
|
import { getCookies } from "$std/http/cookie.ts";
|
||||||
import { getJwtPayload, isJwtValid } from "@popov/jwt";
|
import { getJwtPayload, isJwtValid } from "@popov/jwt";
|
||||||
import { LoginJWT } from "$root/routes/login.tsx";
|
import { CasContent, LoginJWT } from "$root/defaults/interfaces.ts";
|
||||||
|
|
||||||
const PUBLIC_ROUTES = [
|
const PUBLIC_ROUTES = [
|
||||||
"/",
|
"/",
|
||||||
@@ -16,6 +16,7 @@ const jwtKeyCache: Record<string, string> = {};
|
|||||||
|
|
||||||
export interface State {
|
export interface State {
|
||||||
isAuthenticated: boolean;
|
isAuthenticated: boolean;
|
||||||
|
session: CasContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isRoutePublic(route: string) {
|
function isRoutePublic(route: string) {
|
||||||
@@ -44,12 +45,16 @@ export const handler = [
|
|||||||
}
|
}
|
||||||
|
|
||||||
const content = getJwtPayload(cookies["sessionToken"]) as LoginJWT;
|
const content = getJwtPayload(cookies["sessionToken"]) as LoginJWT;
|
||||||
const key = getKey(content.user.uid as string);
|
const key = getKey(content.user.uid);
|
||||||
|
|
||||||
context.state.isAuthenticated = await isJwtValid(
|
context.state.isAuthenticated = await isJwtValid(
|
||||||
cookies["sessionToken"],
|
cookies["sessionToken"],
|
||||||
key,
|
key,
|
||||||
);
|
);
|
||||||
|
const session: CasContent =
|
||||||
|
(getJwtPayload(cookies["sessionToken"]) as LoginJWT).user;
|
||||||
|
|
||||||
|
context.state.session = session;
|
||||||
|
|
||||||
return await context.next();
|
return await context.next();
|
||||||
},
|
},
|
||||||
|
|||||||
+14
-30
@@ -1,10 +1,12 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
import { Handlers } from "$fresh/server.ts";
|
||||||
import { State } from "$root/routes/_middleware.ts";
|
import { State } from "$root/routes/_middleware.ts";
|
||||||
|
import { parse, type RegularTagNode } from "@melvdouc/xml-parser";
|
||||||
import {
|
import {
|
||||||
parse,
|
CasContent,
|
||||||
type RegularTagNode,
|
CasResponse,
|
||||||
type TextNode,
|
CasTagNode,
|
||||||
} from "@melvdouc/xml-parser";
|
LoginJWT,
|
||||||
|
} from "$root/defaults/interfaces.ts";
|
||||||
import { createJwt } from "@popov/jwt";
|
import { createJwt } from "@popov/jwt";
|
||||||
import { setCookie } from "$std/http/cookie.ts";
|
import { setCookie } from "$std/http/cookie.ts";
|
||||||
import { getKey } from "$root/routes/_middleware.ts";
|
import { getKey } from "$root/routes/_middleware.ts";
|
||||||
@@ -12,26 +14,6 @@ import { getKey } from "$root/routes/_middleware.ts";
|
|||||||
const SERVICE = "https://localhost/login";
|
const SERVICE = "https://localhost/login";
|
||||||
const CAS = "https://ident.univ-amu.fr/cas";
|
const CAS = "https://ident.univ-amu.fr/cas";
|
||||||
|
|
||||||
interface CasTagNode extends RegularTagNode {
|
|
||||||
children: [TextNode];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface CasGroupNode extends RegularTagNode {
|
|
||||||
children: CasTagNode[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface CasResponse extends RegularTagNode {
|
|
||||||
children: [TextNode, CasGroupNode];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface LoginJWT {
|
|
||||||
iss: "PolyMPR";
|
|
||||||
iat: number;
|
|
||||||
exp: number;
|
|
||||||
aud: "PolyMPR";
|
|
||||||
user: Record<string, string | string[]>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTag(tag: CasTagNode): [string, string] {
|
function getTag(tag: CasTagNode): [string, string] {
|
||||||
return [
|
return [
|
||||||
tag.tagName.replace("cas:", ""),
|
tag.tagName.replace("cas:", ""),
|
||||||
@@ -43,11 +25,13 @@ function createUserJWT(casResponse: CasResponse): Promise<string> {
|
|||||||
const nodes = casResponse.children[1].children.map(getTag);
|
const nodes = casResponse.children[1].children.map(getTag);
|
||||||
const fullUserInfos: Record<string, string | string[]> = {};
|
const fullUserInfos: Record<string, string | string[]> = {};
|
||||||
|
|
||||||
nodes.forEach(([key, value]) => {
|
nodes.forEach(([key, value]: [string, string]) => {
|
||||||
if (fullUserInfos[key] && Array.isArray(fullUserInfos[key])) {
|
if (typeof fullUserInfos[key] == "string") {
|
||||||
|
fullUserInfos[key] = [fullUserInfos[key]];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(fullUserInfos[key])) {
|
||||||
fullUserInfos[key].push(value);
|
fullUserInfos[key].push(value);
|
||||||
} else if (fullUserInfos[key]) {
|
|
||||||
fullUserInfos[key] = [fullUserInfos[key], value];
|
|
||||||
} else {
|
} else {
|
||||||
fullUserInfos[key] = value;
|
fullUserInfos[key] = value;
|
||||||
}
|
}
|
||||||
@@ -56,12 +40,12 @@ function createUserJWT(casResponse: CasResponse): Promise<string> {
|
|||||||
const now = Math.floor(Date.now() / 1000);
|
const now = Math.floor(Date.now() / 1000);
|
||||||
const oneHour = 60 * 60;
|
const oneHour = 60 * 60;
|
||||||
|
|
||||||
const payload = {
|
const payload: LoginJWT = {
|
||||||
iss: "PolyMPR",
|
iss: "PolyMPR",
|
||||||
iat: now,
|
iat: now,
|
||||||
exp: now + oneHour,
|
exp: now + oneHour,
|
||||||
aud: "PolyMPR",
|
aud: "PolyMPR",
|
||||||
user: fullUserInfos,
|
user: fullUserInfos as unknown as CasContent,
|
||||||
};
|
};
|
||||||
|
|
||||||
const key = getKey(fullUserInfos.uid as string);
|
const key = getKey(fullUserInfos.uid as string);
|
||||||
|
|||||||
@@ -77,15 +77,14 @@ function getIndexContent(_name: string) {
|
|||||||
|
|
||||||
function getPartialIndexContent(name: string) {
|
function getPartialIndexContent(name: string) {
|
||||||
return `
|
return `
|
||||||
import { EmptyObject } from "$root/defaults/interfaces.ts";
|
|
||||||
import {
|
import {
|
||||||
getPartialsConfig,
|
getPartialsConfig,
|
||||||
makePartials,
|
makePartials,
|
||||||
} from "$root/defaults/makePartials.tsx";
|
} from "$root/defaults/makePartials.tsx";
|
||||||
|
import { FreshContext } from "$fresh/server.ts";
|
||||||
|
import { State } from "$root/routes/_middleware.ts";
|
||||||
|
|
||||||
type ${name}IndexProps = EmptyObject;
|
export async function Index(request: Request, context: FreshContext<State>) {
|
||||||
|
|
||||||
export function Index(_props: ${name}IndexProps) {
|
|
||||||
return <h2>Welcome to ${name}.</h2>;
|
return <h2>Welcome to ${name}.</h2>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user