-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(microsoft-excel): add SharePoint drive support for Excel integration #4162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9669ca4
feat(microsoft-excel): add SharePoint drive support for Excel integra…
waleedlatif1 6162253
fix(microsoft-excel): address PR review comments
waleedlatif1 a3c93ee
fix(microsoft-excel): validate driveId in files route
waleedlatif1 780fa90
fix(microsoft-excel): unblock OneDrive users and validate driveId in …
waleedlatif1 f18af3c
fix(microsoft-excel): validate driveId in getItemBasePath utility
waleedlatif1 65308e4
fix(microsoft-excel): use centralized input validation
waleedlatif1 2884587
lint
waleedlatif1 649c3e6
improvement(microsoft-excel): add File Source dropdown to control Sha…
waleedlatif1 8b1c88c
fix(microsoft-excel): fix canonical param test failures
waleedlatif1 326114d
fix(microsoft-excel): address PR review feedback for SharePoint drive…
waleedlatif1 3be18ca
fix(microsoft-excel): use validateMicrosoftGraphId for driveId valida…
waleedlatif1 12231db
fix(microsoft-excel): use validatePathSegment with strict pattern for…
waleedlatif1 8148260
lint
waleedlatif1 16ad6ce
fix(microsoft-excel): reorder driveId before spreadsheetId in v1 block
waleedlatif1 d1b8778
fix(microsoft-excel): clear manualDriveId when fileSource changes
waleedlatif1 def6e90
refactor(microsoft-excel): use getItemBasePath in sheets route to rem…
waleedlatif1 5334c2b
lint
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { authorizeCredentialUse } from '@/lib/auth/credential-access' | ||
| import { validatePathSegment, validateSharePointSiteId } from '@/lib/core/security/input-validation' | ||
| import { generateRequestId } from '@/lib/core/utils/request' | ||
| import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| const logger = createLogger('MicrosoftExcelDrivesAPI') | ||
|
|
||
| interface GraphDrive { | ||
| id: string | ||
| name: string | ||
| driveType: string | ||
| webUrl?: string | ||
| } | ||
|
|
||
| /** | ||
| * List document libraries (drives) for a SharePoint site. | ||
| * Used by the microsoft.excel.drives selector to let users pick | ||
| * which drive contains their Excel file. | ||
| */ | ||
| export async function POST(request: NextRequest) { | ||
| const requestId = generateRequestId() | ||
|
|
||
| try { | ||
| const body = await request.json() | ||
| const { credential, workflowId, siteId, driveId } = body | ||
|
|
||
| if (!credential) { | ||
| logger.warn(`[${requestId}] Missing credential in request`) | ||
| return NextResponse.json({ error: 'Credential is required' }, { status: 400 }) | ||
| } | ||
|
|
||
| if (!siteId) { | ||
| logger.warn(`[${requestId}] Missing siteId in request`) | ||
| return NextResponse.json({ error: 'Site ID is required' }, { status: 400 }) | ||
| } | ||
|
|
||
| const siteIdValidation = validateSharePointSiteId(siteId, 'siteId') | ||
| if (!siteIdValidation.isValid) { | ||
| logger.warn(`[${requestId}] Invalid siteId format`) | ||
| return NextResponse.json({ error: siteIdValidation.error }, { status: 400 }) | ||
| } | ||
|
|
||
| const authz = await authorizeCredentialUse(request, { | ||
| credentialId: credential, | ||
| workflowId, | ||
| }) | ||
| if (!authz.ok || !authz.credentialOwnerUserId) { | ||
| return NextResponse.json({ error: authz.error || 'Unauthorized' }, { status: 403 }) | ||
| } | ||
|
|
||
| const accessToken = await refreshAccessTokenIfNeeded( | ||
| credential, | ||
| authz.credentialOwnerUserId, | ||
| requestId | ||
| ) | ||
| if (!accessToken) { | ||
| logger.warn(`[${requestId}] Failed to obtain valid access token`) | ||
| return NextResponse.json( | ||
| { error: 'Failed to obtain valid access token', authRequired: true }, | ||
| { status: 401 } | ||
| ) | ||
| } | ||
|
|
||
| // Single-drive lookup when driveId is provided (used by fetchById) | ||
| if (driveId) { | ||
| const driveIdValidation = validatePathSegment(driveId, { | ||
| paramName: 'driveId', | ||
| customPattern: /^[a-zA-Z0-9!_-]+$/, | ||
| }) | ||
| if (!driveIdValidation.isValid) { | ||
| return NextResponse.json({ error: driveIdValidation.error }, { status: 400 }) | ||
| } | ||
|
|
||
| const url = `https://graph.microsoft.com/v1.0/sites/${siteId}/drives/${driveId}?$select=id,name,driveType,webUrl` | ||
| const response = await fetch(url, { | ||
| headers: { Authorization: `Bearer ${accessToken}` }, | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| const errorData = await response | ||
| .json() | ||
| .catch(() => ({ error: { message: 'Unknown error' } })) | ||
| return NextResponse.json( | ||
| { error: errorData.error?.message || 'Failed to fetch drive' }, | ||
| { status: response.status } | ||
| ) | ||
| } | ||
|
|
||
| const data: GraphDrive = await response.json() | ||
| return NextResponse.json( | ||
| { drive: { id: data.id, name: data.name, driveType: data.driveType } }, | ||
| { status: 200 } | ||
| ) | ||
| } | ||
|
|
||
| // List all drives for the site | ||
| const url = `https://graph.microsoft.com/v1.0/sites/${siteId}/drives?$select=id,name,driveType,webUrl` | ||
waleedlatif1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const response = await fetch(url, { | ||
| headers: { | ||
| Authorization: `Bearer ${accessToken}`, | ||
| }, | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| const errorData = await response.json().catch(() => ({ error: { message: 'Unknown error' } })) | ||
| logger.error(`[${requestId}] Microsoft Graph API error fetching drives`, { | ||
| status: response.status, | ||
| error: errorData.error?.message, | ||
| }) | ||
| return NextResponse.json( | ||
| { error: errorData.error?.message || 'Failed to fetch drives' }, | ||
| { status: response.status } | ||
| ) | ||
| } | ||
|
|
||
| const data = await response.json() | ||
| const drives = (data.value || []).map((drive: GraphDrive) => ({ | ||
| id: drive.id, | ||
| name: drive.name, | ||
| driveType: drive.driveType, | ||
| })) | ||
|
|
||
| logger.info(`[${requestId}] Successfully fetched ${drives.length} drives for site ${siteId}`) | ||
| return NextResponse.json({ drives }, { status: 200 }) | ||
| } catch (error) { | ||
| logger.error(`[${requestId}] Error fetching drives`, error) | ||
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.