Skip to content

Commit

Permalink
feat(cas): add service auth using tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Jun 18, 2024
1 parent b66a76f commit 0365cf0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/cas/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CAS_HOST, CAS_EXTERNAL_SERVICES } from "~/utils/constants";

/**
* @param cas_token token that can be found using `cas_login` function.
* @param service_url the service URL to authenticate to.
* @returns the URL to authenticate to said service.
*/
export const cas_service = async (cas_token: string, service_url: typeof CAS_EXTERNAL_SERVICES[keyof typeof CAS_EXTERNAL_SERVICES]): Promise<string> => {
const uri = new URL(CAS_HOST + "/cas/login");
uri.searchParams.set("service", service_url);
uri.searchParams.set("gateway", "true");

const response = await fetch(uri.href, {
headers: { "Cookie": "lemonldap=" + cas_token },
// prevent redirects since we want to
// return the redirection url.
redirect: "manual"
});

const redirection = response.headers.get("location");
if (!redirection) throw new Error("No redirection found.");

return redirection;
};
8 changes: 8 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const CAS_HOST = "https://cas.unilim.fr";

export const PROD_ENDPOINT = "https://apis.unilim.fr";
export const DEV_ENDPOINT = "https://apis-dev.unilim.fr";
export const BIOME_PROD_ENDPOINT = "https://biome.unilim.fr";
Expand All @@ -6,3 +8,9 @@ export const BIOME_DEV_ENDPOINT = "https://biome-dev.unilim.fr";
export const OAUTH2_CODE_CHALLENGE_METHOD = "plain";
export const OAUTH2_CODE_CHALLENGE = Math.random().toString(36).substring(2);
export const OAUTH2_STATE = Math.random().toString(36).substring(2);

export const CAS_EXTERNAL_SERVICES = {
IUT_COMMUNITY_MOODLE: "https://community-iut.unilim.fr/login/index.php?authCAS=CAS",
COMMUNITIES_MOODLE: "https://communities.unilim.fr/login/index.php?authCAS=CAS",
GRADING: "https://inscription.unilim.fr/gestion/etudiant/rvn/"
} as const;

0 comments on commit 0365cf0

Please sign in to comment.