Optimized code and wrote documentation
This commit is contained in:
+50
-42
@@ -1,4 +1,4 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { FreshContext, Handlers } from "$fresh/server.ts";
|
||||
import { State } from "$root/defaults/interfaces.ts";
|
||||
import { parse, type RegularTagNode } from "@melvdouc/xml-parser";
|
||||
import {
|
||||
@@ -13,13 +13,23 @@ import { getKey } from "$root/routes/_middleware.ts";
|
||||
|
||||
const CAS = "https://ident.univ-amu.fr/cas";
|
||||
|
||||
function getTag(tag: CasTagNode): [string, string] {
|
||||
/**
|
||||
* Get the tag node value without "cas:" prefix in name.
|
||||
* @param tag The CAS tag node.
|
||||
* @returns The `[name, value]` pair.
|
||||
*/
|
||||
function getTag(tag: CasTagNode): [name: string, value: string] {
|
||||
return [
|
||||
tag.tagName.replace("cas:", ""),
|
||||
tag.children[0].value,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user JWT token with a validity period of one hour.
|
||||
* @param casResponse The CAS reponse parsed from XML.
|
||||
* @returns The user JWT session token.
|
||||
*/
|
||||
function createUserJWT(casResponse: CasResponse): Promise<string> {
|
||||
const nodes = casResponse.children[1].children.map(getTag);
|
||||
const fullUserInfos: Record<string, string | string[]> = {};
|
||||
@@ -28,7 +38,6 @@ function createUserJWT(casResponse: CasResponse): Promise<string> {
|
||||
if (typeof fullUserInfos[key] == "string") {
|
||||
fullUserInfos[key] = [fullUserInfos[key]];
|
||||
}
|
||||
|
||||
if (Array.isArray(fullUserInfos[key])) {
|
||||
fullUserInfos[key].push(value);
|
||||
} else {
|
||||
@@ -37,12 +46,10 @@ function createUserJWT(casResponse: CasResponse): Promise<string> {
|
||||
});
|
||||
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const oneHour = 60 * 60;
|
||||
|
||||
const payload: LoginJWT = {
|
||||
iss: "PolyMPR",
|
||||
iat: now,
|
||||
exp: now + oneHour,
|
||||
exp: now + 0xe10,
|
||||
aud: "PolyMPR",
|
||||
user: fullUserInfos as unknown as CasContent,
|
||||
};
|
||||
@@ -51,51 +58,39 @@ function createUserJWT(casResponse: CasResponse): Promise<string> {
|
||||
return createJwt(payload, key);
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export const handler: Handlers<any, State> = {
|
||||
async GET(request, context) {
|
||||
export const handler: Handlers<null, State> = {
|
||||
/**
|
||||
* Handles all CAS protocol requests.
|
||||
* @param request The incomming HTTP request.
|
||||
* @param context The Fresh context with `State`.
|
||||
* @returns The redirect corresponding to each step of the CAS protocol.
|
||||
*/
|
||||
async GET(
|
||||
request: Request,
|
||||
context: FreshContext<State, null>,
|
||||
): Promise<Response> {
|
||||
const url = new URL(request.url);
|
||||
const ticket = url.searchParams.get("ticket");
|
||||
const service = `${context.url.origin}/login`;
|
||||
|
||||
if (ticket) {
|
||||
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}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
|
||||
setCookie(headers, {
|
||||
name: "sessionToken",
|
||||
value: await createUserJWT(casResponse),
|
||||
});
|
||||
headers.set("Location", "/apps");
|
||||
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers,
|
||||
});
|
||||
}
|
||||
|
||||
if (context.state.isAuthenticated) {
|
||||
if (!ticket) {
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: {
|
||||
Location: "/apps",
|
||||
Location: context.state.isAuthenticated
|
||||
? "/apps"
|
||||
: `${CAS}/login?service=${service}`,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
}
|
||||
|
||||
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: {
|
||||
@@ -103,5 +98,18 @@ export const handler: Handlers<any, State> = {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
|
||||
setCookie(headers, {
|
||||
name: "sessionToken",
|
||||
value: await createUserJWT(casResponse),
|
||||
});
|
||||
headers.set("Location", "/apps");
|
||||
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user