Require enough scope

This commit is contained in:
2025-04-22 10:10:29 +03:00
parent 79fbaa9f43
commit d62b2ad86d

View File

@@ -1,10 +1,28 @@
import { omit } from "radash";
import { get, omit } from "radash";
const requiredScope = ["read", "activity:write", "activity:read_all"];
const hasEnoughScope = (scope: string) => {
const permissions = scope.split(",");
return requiredScope.every((p) => permissions.includes(p));
};
export default defineOAuthStravaEventHandler({
config: {
scope: ["read,activity:read_all,activity:write"],
scope: [requiredScope.join(",")],
approvalPrompt: "force",
},
onSuccess: async (event, auth) => {
const query = getQuery(event);
const scope = get(query, "scope", "");
if (!hasEnoughScope(scope)) {
throw createError({
statusCode: 403,
message: "Insufficient scope",
});
}
const userPayload = {
id: auth.user.id,
name: `${auth.user.firstname} ${auth.user.lastname}`,
@@ -15,10 +33,6 @@ export default defineOAuthStravaEventHandler({
avatar: auth.user.profile,
};
await setUserSession(event, {
user: userPayload,
});
const db = useDrizzle();
const [user] = await db
@@ -61,6 +75,10 @@ export default defineOAuthStravaEventHandler({
})
.onConflictDoNothing();
await setUserSession(event, {
user: userPayload,
});
sendRedirect(event, "/");
},
});