Skip to content

Commit

Permalink
Permission logging
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Jan 7, 2025
1 parent e28fb72 commit 816f9be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 3 additions & 1 deletion apps/web/utils/actions/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ export const checkPermissionsAction = withActionInstrumentation(
"checkPermissions",
async () => {
const session = await auth();
if (!session?.user.id) return { error: "Not logged in" };
if (!session?.user.email) return { error: "Not logged in" };

try {
const token = await getGmailAccessToken(session);
if (!token.token) return { error: "No Gmail access token" };

const { hasAllPermissions, error } = await checkGmailPermissions(
token.token,
session.user.email,
);
if (error) return { error };

Expand Down Expand Up @@ -66,6 +67,7 @@ export const adminCheckPermissionsAction = withActionInstrumentation(

const { hasAllPermissions, error } = await checkGmailPermissions(
token.token,
email,
);
if (error) return { error };
return { hasAllPermissions };
Expand Down
28 changes: 21 additions & 7 deletions apps/web/utils/gmail/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { SCOPES } from "@/utils/auth";
import { createScopedLogger } from "@/utils/logger";

export async function checkGmailPermissions(accessToken: string): Promise<{
const logger = createScopedLogger("Gmail Permissions");

export async function checkGmailPermissions(
accessToken: string,
email: string,
): Promise<{
hasAllPermissions: boolean;
missingScopes: string[];
error?: string;
}> {
if (!accessToken) {
console.error("No access token available");
logger.error("No access token available", { email });
return {
hasAllPermissions: false,
missingScopes: SCOPES,
Expand All @@ -22,7 +28,10 @@ export async function checkGmailPermissions(accessToken: string): Promise<{
const data = await response.json();

if (data.error) {
console.error("Error checking Gmail permissions:", data.error);
logger.error("Error checking Gmail permissions:", {
email,
error: data.error,
});
return {
hasAllPermissions: false,
missingScopes: SCOPES, // Assume all scopes are missing if we can't check
Expand All @@ -35,12 +44,17 @@ export async function checkGmailPermissions(accessToken: string): Promise<{
(scope) => !grantedScopes.includes(scope),
);

return {
hasAllPermissions: missingScopes.length === 0,
const hasAllPermissions = missingScopes.length === 0;

logger.info("Gmail permissions check", {
email,
hasAllPermissions,
missingScopes,
};
});

return { hasAllPermissions, missingScopes };
} catch (error) {
console.error("Error checking Gmail permissions:", error);
logger.error("Error checking Gmail permissions:", { email, error });
return {
hasAllPermissions: false,
missingScopes: SCOPES, // Assume all scopes are missing if we can't check
Expand Down

1 comment on commit 816f9be

@vercel
Copy link

@vercel vercel bot commented on 816f9be Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.