Skip to content
Snippets Groups Projects
Commit 10a510c4 authored by Grant's avatar Grant
Browse files

jank: wrap oidc userinfo call (related #80)

parent 07189888
No related branches found
No related tags found
No related merge requests found
...@@ -152,72 +152,82 @@ app.get("/callback", RateLimiter.HIGH, async (req, res) => { ...@@ -152,72 +152,82 @@ app.get("/callback", RateLimiter.HIGH, async (req, res) => {
}); });
} }
const whoami = await OpenID.client.userinfo<{ try {
instance: { const whoami = await OpenID.client.userinfo<{
software: {
name: string;
version: string;
logo_uri?: string;
repository?: string;
homepage?: string;
};
instance: { instance: {
logo_uri?: string; software: {
banner_uri?: string; name: string;
name?: string; version: string;
logo_uri?: string;
repository?: string;
homepage?: string;
};
instance: {
logo_uri?: string;
banner_uri?: string;
name?: string;
};
}; };
}; }>(exchange.access_token);
}>(exchange.access_token);
const [username, hostname] = whoami.sub.split("@"); const [username, hostname] = whoami.sub.split("@");
const instance = await Instance.fromAuth(hostname, whoami.instance.instance); const instance = await Instance.fromAuth(
const instanceBan = await instance.getEffectiveBan(); hostname,
if (instanceBan) { whoami.instance.instance
res.redirect(
"/" +
buildQuery({
TYPE: "banned",
ERROR_DESC: instanceBan.publicNote || undefined,
})
); );
return; const instanceBan = await instance.getEffectiveBan();
} if (instanceBan) {
res.redirect(
"/" +
buildQuery({
TYPE: "banned",
ERROR_DESC: instanceBan.publicNote || undefined,
})
);
return;
}
const sub = [username, hostname].join("@"); const sub = [username, hostname].join("@");
await prisma.user.upsert({ await prisma.user.upsert({
where: { where: {
sub, sub,
}, },
update: { update: {
sub, sub,
display_name: whoami.name, display_name: whoami.name,
picture_url: whoami.picture, picture_url: whoami.picture,
profile_url: whoami.profile, profile_url: whoami.profile,
}, },
create: { create: {
sub, sub,
display_name: whoami.name, display_name: whoami.name,
picture_url: whoami.picture, picture_url: whoami.picture,
profile_url: whoami.profile, profile_url: whoami.profile,
}, },
}); });
req.session.user = { req.session.user = {
service: { service: {
...whoami.instance, ...whoami.instance,
instance: { instance: {
...whoami.instance.instance, ...whoami.instance.instance,
hostname, hostname,
},
}, },
}, user: {
user: { picture_url: whoami.picture,
picture_url: whoami.picture, username,
username, },
}, };
}; req.session.save();
req.session.save(); res.redirect("/");
res.redirect("/"); } catch (e) {
console.error("callback error", e);
res
.status(500)
.json({ success: false, error: "internal error, try again" });
}
}); });
app.get("/canvas/pixel/:x/:y", RateLimiter.HIGH, async (req, res) => { app.get("/canvas/pixel/:x/:y", RateLimiter.HIGH, async (req, res) => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment