Require enough scope
This commit is contained in:
@@ -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({
|
export default defineOAuthStravaEventHandler({
|
||||||
config: {
|
config: {
|
||||||
scope: ["read,activity:read_all,activity:write"],
|
scope: [requiredScope.join(",")],
|
||||||
|
approvalPrompt: "force",
|
||||||
},
|
},
|
||||||
onSuccess: async (event, auth) => {
|
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 = {
|
const userPayload = {
|
||||||
id: auth.user.id,
|
id: auth.user.id,
|
||||||
name: `${auth.user.firstname} ${auth.user.lastname}`,
|
name: `${auth.user.firstname} ${auth.user.lastname}`,
|
||||||
@@ -15,10 +33,6 @@ export default defineOAuthStravaEventHandler({
|
|||||||
avatar: auth.user.profile,
|
avatar: auth.user.profile,
|
||||||
};
|
};
|
||||||
|
|
||||||
await setUserSession(event, {
|
|
||||||
user: userPayload,
|
|
||||||
});
|
|
||||||
|
|
||||||
const db = useDrizzle();
|
const db = useDrizzle();
|
||||||
|
|
||||||
const [user] = await db
|
const [user] = await db
|
||||||
@@ -61,6 +75,10 @@ export default defineOAuthStravaEventHandler({
|
|||||||
})
|
})
|
||||||
.onConflictDoNothing();
|
.onConflictDoNothing();
|
||||||
|
|
||||||
|
await setUserSession(event, {
|
||||||
|
user: userPayload,
|
||||||
|
});
|
||||||
|
|
||||||
sendRedirect(event, "/");
|
sendRedirect(event, "/");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user