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

Commit

Permalink
userinfo lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Apr 10, 2024
1 parent 665c6e4 commit 4daa739
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ app.get(`${API_PREFIX}/login`, async (c) => {

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


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

let oidcConfig: { authorization_endpoint: string; token_endpoint: string };
let oidcConfig: {
authorization_endpoint: string;
token_endpoint: string;
userinfo_endpoint: string;
};
async function getOIDCConfig() {
if (!oidcConfig) {
const resp = await fetch(OIDC_CONFIG_URL);
Expand All @@ -101,6 +104,14 @@ async function getOIDCAccessToken(code: string) {
return json.access_token as string;
}

async function getUserinfo(accessToken: string) {
const config = await getOIDCConfig();
const resp = await fetch(config.userinfo_endpoint, {
headers: { Authorization: `Bearer ${accessToken}` },
});
return await resp.json();
}

async function createToken() {
const resp = await fetch(`${SFTPGO_WEB_URL}/api/v2/token`, {
headers: { Authorization: `Basic ${SFTPGO_ADMIN_BASICAUTH}` },
Expand Down

0 comments on commit 4daa739

Please sign in to comment.