diff --git a/src/cas/service.ts b/src/cas/service.ts new file mode 100644 index 0000000..d763336 --- /dev/null +++ b/src/cas/service.ts @@ -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 => { + 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; +}; diff --git a/src/utils/constants.ts b/src/utils/constants.ts index b62b708..12b7773 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -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"; @@ -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;