All files / src/utils oauth.ts

100% Statements 66/66
95.65% Branches 22/23
100% Functions 6/6
100% Lines 66/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258      66x             66x               66x           66x               66x       66x               66x                                           68x   68x                           62x 33x     33x 33x   13x     20x     19x 19x       60x     60x 60x 40x   60x   60x 60x 60x 60x   60x           60x 20x     20x 20x 10x   20x       30x 30x         30x             30x                         30x 20x     20x 20x   10x     10x     10x 10x       66x                   30x           30x 10x 10x     20x 20x       66x           66x                   30x   30x           30x                 30x 10x 10x     10x 10x         20x             20x 10x 10x    
import { z } from 'zod'
 
// OAuth Token schemas
export const TokenExchangeParamsSchema = z.object({
  clientId: z.string(),
  clientSecret: z.string(),
  code: z.string(),
  redirectUri: z.string()
})
 
export const TokenResponseSchema = z.object({
  access_token: z.string(),
  refresh_token: z.string().optional(),
  expires_in: z.number(),
  scope: z.string().optional(),
  token_type: z.string()
})
 
export const TokenErrorResponseSchema = z.object({
  error: z.string(),
  error_description: z.string().optional()
})
 
// Google Drive API schemas
export const DriveFileSchema = z.object({
  id: z.string(),
  name: z.string(),
  webViewLink: z.string().optional().nullable(),
  createdTime: z.string().optional(),
  mimeType: z.string().optional()
})
 
export const DriveFilesListResponseSchema = z.object({
  files: z.array(DriveFileSchema)
})
 
export const DriveListFilesParamsSchema = z.object({
  parentFolderId: z.string(),
  mimeType: z.string().optional(),
  accessToken: z.string(),
  orderBy: z.string().optional(),
  fields: z.string().optional()
})
 
export const DriveErrorResponseSchema = z.object({
  error: z.object({
    errors: z.array(z.object({
      domain: z.string(),
      reason: z.string(),
      message: z.string()
    })).optional(),
    code: z.number(),
    message: z.string()
  })
})
 
// Type exports
export type TokenExchangeParams = z.infer<typeof TokenExchangeParamsSchema>
export type TokenResponse = z.infer<typeof TokenResponseSchema>
export type TokenErrorResponse = z.infer<typeof TokenErrorResponseSchema>
export type DriveFile = z.infer<typeof DriveFileSchema>
export type DriveFilesListResponse = z.infer<typeof DriveFilesListResponseSchema>
export type DriveListFilesParams = z.infer<typeof DriveListFilesParamsSchema>
export type DriveErrorResponse = z.infer<typeof DriveErrorResponseSchema>
 
export async function exchangeCodeForTokens(params: TokenExchangeParams): Promise<TokenResponse> {
  const validatedParams = TokenExchangeParamsSchema.parse(params)
  
  const tokenResponse = await fetch('https://oauth2.googleapis.com/token', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: new URLSearchParams({
      client_id: validatedParams.clientId,
      client_secret: validatedParams.clientSecret,
      code: validatedParams.code,
      grant_type: 'authorization_code',
      redirect_uri: validatedParams.redirectUri
    })
  })
 
  if (!tokenResponse.ok) {
    const errorText = await tokenResponse.text()
    let errorData: TokenErrorResponse
    
    try {
      errorData = TokenErrorResponseSchema.parse(JSON.parse(errorText))
    } catch {
      throw new Error(`Token exchange failed: ${errorText}`)
    }
    
    throw new Error(`OAuth error: ${errorData.error}${errorData.error_description ? ` - ${errorData.error_description}` : ''}`)
  }
 
  const tokens = await tokenResponse.json()
  return TokenResponseSchema.parse(tokens)
}
 
export async function listDriveFiles(params: DriveListFilesParams): Promise<DriveFilesListResponse> {
  const validatedParams = DriveListFilesParamsSchema.parse(params)
  
  // Build query based on parameters
  const queryParts = [`'${validatedParams.parentFolderId}' in parents`, 'trashed=false']
  if (validatedParams.mimeType) {
    queryParts.push(`mimeType='${validatedParams.mimeType}'`)
  }
  const query = queryParts.join(' and ')
  
  const listUrl = new URL('https://www.googleapis.com/drive/v3/files')
  listUrl.searchParams.append('q', query)
  listUrl.searchParams.append('fields', validatedParams.fields || 'files(id,name,webViewLink,createdTime)')
  listUrl.searchParams.append('orderBy', validatedParams.orderBy || 'name')
  
  const listResponse = await fetch(listUrl.toString(), {
    headers: {
      'Authorization': `Bearer ${validatedParams.accessToken}`
    }
  })
 
  if (!listResponse.ok) {
    const errorText = await listResponse.text()
    let errorData: DriveErrorResponse
    
    try {
      errorData = DriveErrorResponseSchema.parse(JSON.parse(errorText))
      throw new Error(`Drive API error: ${errorData.error.message}`)
    } catch (parseError) {
      throw new Error(`Failed to list files: ${errorText}`)
    }
  }
 
  const result = await listResponse.json()
  return DriveFilesListResponseSchema.parse(result)
}
 
// Token refresh
export async function refreshAccessToken(params: TokenExchangeParams): Promise<TokenResponse> {
  const refreshParams = {
    clientId: params.clientId,
    clientSecret: params.clientSecret,
    refreshToken: params.code, // In refresh flow, 'code' contains the refresh token
    grantType: 'refresh_token' as const
  }
  
  const tokenResponse = await fetch('https://oauth2.googleapis.com/token', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: new URLSearchParams({
      client_id: refreshParams.clientId,
      client_secret: refreshParams.clientSecret,
      refresh_token: refreshParams.refreshToken,
      grant_type: refreshParams.grantType
    })
  })
 
  if (!tokenResponse.ok) {
    const errorText = await tokenResponse.text()
    let errorData: TokenErrorResponse
    
    try {
      errorData = TokenErrorResponseSchema.parse(JSON.parse(errorText))
    } catch {
      throw new Error(`Token refresh failed: ${errorText}`)
    }
    
    throw new Error(`OAuth error: ${errorData.error}${errorData.error_description ? ` - ${errorData.error_description}` : ''}`)
  }
 
  const tokens = await tokenResponse.json()
  return TokenResponseSchema.parse(tokens)
}
 
// Get file info
export const DriveFileInfoSchema = z.object({
  id: z.string(),
  name: z.string(),
  mimeType: z.string(),
  parents: z.array(z.string()).optional()
})
 
export type DriveFileInfo = z.infer<typeof DriveFileInfoSchema>
 
export async function getDriveFileInfo(fileId: string, accessToken: string, fields: string = 'id,name,mimeType,parents'): Promise<DriveFileInfo> {
  const response = await fetch(`https://www.googleapis.com/drive/v3/files/${fileId}?fields=${fields}`, {
    headers: {
      'Authorization': `Bearer ${accessToken}`
    }
  })
  
  if (!response.ok) {
    const errorText = await response.text()
    throw new Error(`Failed to get file info: ${errorText}`)
  }
  
  const data = await response.json()
  return DriveFileInfoSchema.parse(data)
}
 
// Create folder
export const CreateFolderParamsSchema = z.object({
  name: z.string(),
  parentId: z.string(),
  accessToken: z.string()
})
 
export const CreateFolderResponseSchema = z.object({
  id: z.string(),
  name: z.string(),
  webViewLink: z.string().optional()
})
 
export type CreateFolderParams = z.infer<typeof CreateFolderParamsSchema>
export type CreateFolderResponse = z.infer<typeof CreateFolderResponseSchema>
 
export async function createDriveFolder(params: CreateFolderParams): Promise<CreateFolderResponse> {
  const validatedParams = CreateFolderParamsSchema.parse(params)
  
  const folderMetadata = {
    name: validatedParams.name,
    mimeType: 'application/vnd.google-apps.folder',
    parents: [validatedParams.parentId]
  }
 
  const createResponse = await fetch('https://www.googleapis.com/drive/v3/files?fields=id,name,webViewLink', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${validatedParams.accessToken}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(folderMetadata)
  })
 
  if (!createResponse.ok) {
    const errorText = await createResponse.text()
    throw new Error(`Failed to create folder: ${errorText}`)
  }
 
  const folder = await createResponse.json()
  return CreateFolderResponseSchema.parse(folder)
}
 
// Delete file
export async function deleteDriveFile(fileId: string, accessToken: string): Promise<void> {
  const deleteResponse = await fetch(`https://www.googleapis.com/drive/v3/files/${fileId}`, {
    method: 'DELETE',
    headers: {
      'Authorization': `Bearer ${accessToken}`
    }
  })
 
  if (!deleteResponse.ok) {
    const errorText = await deleteResponse.text()
    throw new Error(`Failed to delete file: ${errorText}`)
  }
}