Skip to content
This repository was archived by the owner on Jul 7, 2024. It is now read-only.

Commit 4daa739

Browse files
committed
userinfo lookup
1 parent 665c6e4 commit 4daa739

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

main.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ app.get(`${API_PREFIX}/login`, async (c) => {
4444

4545
app.get(`${API_PREFIX}/callback`, async (c) => {
4646
const accessToken = await getOIDCAccessToken(c.req.query("code")!);
47-
console.log(accessToken);
47+
const data = await getUserinfo(accessToken);
4848

49-
50-
const [_header, _payload, _signature] = decode(accessToken);
49+
const [_header, _payload, _signature] = decode(data);
5150
console.debug(_payload);
5251
const { preferred_username: username, groups: oidc_groups } = _payload as {
5352
preferred_username: string;
@@ -74,7 +73,11 @@ app.get(`${API_PREFIX}/callback`, async (c) => {
7473
return c.redirect("/");
7574
});
7675

77-
let oidcConfig: { authorization_endpoint: string; token_endpoint: string };
76+
let oidcConfig: {
77+
authorization_endpoint: string;
78+
token_endpoint: string;
79+
userinfo_endpoint: string;
80+
};
7881
async function getOIDCConfig() {
7982
if (!oidcConfig) {
8083
const resp = await fetch(OIDC_CONFIG_URL);
@@ -101,6 +104,14 @@ async function getOIDCAccessToken(code: string) {
101104
return json.access_token as string;
102105
}
103106

107+
async function getUserinfo(accessToken: string) {
108+
const config = await getOIDCConfig();
109+
const resp = await fetch(config.userinfo_endpoint, {
110+
headers: { Authorization: `Bearer ${accessToken}` },
111+
});
112+
return await resp.json();
113+
}
114+
104115
async function createToken() {
105116
const resp = await fetch(`${SFTPGO_WEB_URL}/api/v2/token`, {
106117
headers: { Authorization: `Basic ${SFTPGO_ADMIN_BASICAUTH}` },

0 commit comments

Comments
 (0)