Skip to content

Commit c345761

Browse files
committed
feat: oauth client for signatures
1 parent 2ea9887 commit c345761

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/iut/signatures/index.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { HttpResponse } from "schwi";
2+
import { HttpRequest, HttpRequestRedirection, send } from "schwi";
3+
import { CAS, OAuth2 } from "~cas";
4+
5+
export class Signatures {
6+
public static readonly COOKIE = "session";
7+
8+
public static readonly HOST = "https://signatures.unilim.fr";
9+
public static readonly oauth2 = new OAuth2("signatures", "https://signatures.unilim.fr/callback", ["openid", "profile"]);
10+
11+
public constructor(
12+
public readonly session: string
13+
) {}
14+
15+
public static async fromCAS(cas: CAS): Promise<Signatures> {
16+
let request: HttpRequest, response: HttpResponse;
17+
let session: string, state: string;
18+
19+
{
20+
request = new HttpRequest.Builder(Signatures.HOST + "/login")
21+
.setRedirection(HttpRequestRedirection.MANUAL)
22+
.enableUnauthorizedTLS()
23+
.build();
24+
25+
response = await send(request);
26+
27+
const cookies = response.headers.getSetCookie();
28+
const cookie = cookies.find((cookie) => cookie.startsWith(Signatures.COOKIE + "="));
29+
30+
if (!cookie)
31+
throw new Error("session cookie not found");
32+
33+
session = cookie.split(";")[0].split("=")[1];
34+
35+
const location = new URL(response.headers.get("location")!);
36+
state = location.searchParams.get("state")!;
37+
}
38+
39+
{
40+
const callback = await cas.authorize(Signatures.oauth2, false, state);
41+
42+
request = new HttpRequest.Builder(callback)
43+
.setRedirection(HttpRequestRedirection.MANUAL)
44+
.setCookie(Signatures.COOKIE, session)
45+
.setCookie(CAS.COOKIE, cas.lemonldap)
46+
.enableUnauthorizedTLS()
47+
.build();
48+
49+
response = await send(request);
50+
51+
const cookies = response.headers.getSetCookie();
52+
const cookie = cookies.find((cookie) => cookie.startsWith(Signatures.COOKIE + "="));
53+
54+
if (!cookie)
55+
throw new Error("session cookie not found");
56+
57+
session = cookie.split(";")[0].split("=")[1];
58+
}
59+
60+
return new Signatures(session);
61+
}
62+
}

0 commit comments

Comments
 (0)