| 1 | # Google Drive import |
| 2 | |
| 3 | This optional component connects to Google Identity Services, Google Picker |
| 4 | and the Google Drive API on the import page. It exports one selected native |
| 5 | Google document or spreadsheet into browser memory and submits it through the |
| 6 | app's ordinary file upload form. The original Google file is unchanged. |
| 7 | |
| 8 | The Google access token is held only in page memory and sent only in the |
| 9 | Authorization header to Google's fixed API origin. It is never uploaded to |
| 10 | Oficina, persisted in browser storage, or written to a server database. No |
| 11 | background synchronization or refresh token is involved. Google's authorization |
| 12 | grant can outlive the page; people can remove it from their Google account's |
| 13 | third-party connections. The requested `drive.file` scope grants per-file access |
| 14 | and can authorize modifications, but this component only calls the export API. |
| 15 | |
| 16 | JavaScript is required for the chooser. With scripts disabled, a note explains |
| 17 | how to download from Google and use the app's ordinary file form. Configuring |
| 18 | none of the public Google fields hides the component entirely; partial or |
| 19 | malformed configuration must refuse app startup through `Config.Validate`. |
| 20 | |
| 21 | ## Google Cloud setup |
| 22 | |
| 23 | 1. Create a Google Cloud project and enable **Google Drive API** and **Google |
| 24 | Picker API**. |
| 25 | 2. Configure the OAuth application's branding, audience and support information. |
| 26 | Request only `https://www.googleapis.com/auth/drive.file`. This scope is |
| 27 | non-sensitive. Follow the console's publishing requirements; while in |
| 28 | Testing, add the accounts that will exercise consent as test users. |
| 29 | 3. Create an OAuth **Web application** client. Add each app's exact HTTPS |
| 30 | origin to its authorized JavaScript origins, including the canary used for |
| 31 | verification. Local development can use its exact localhost origin and port. |
| 32 | Google does not accept a wildcard for arbitrary tenant subdomains; register |
| 33 | each deployed origin. This token flow does not use an OAuth redirect endpoint. |
| 34 | 4. Create a browser API key in the same project. Restrict it to Google Picker |
| 35 | API and Google Drive API. Its website restrictions must include the app's |
| 36 | origin (`https://example.test/*`) and `https://docs.google.com/*`, where |
| 37 | Picker's iframe runs. The key is public client configuration. |
| 38 | 5. Record the project's numeric **project number**, not its project name or ID. |
| 39 | It must be the OAuth client's project and is passed to Picker's `setAppId`; |
| 40 | mismatches can make valid selections fail to export. |
| 41 | 6. Set all three app settings together: |
| 42 | |
| 43 | | Setting | Docs | Sheets | |
| 44 | | --- | --- | --- | |
| 45 | | OAuth client ID | `DOCS_GOOGLE_CLIENT_ID` | `SHEETS_GOOGLE_CLIENT_ID` | |
| 46 | | Public API key | `DOCS_GOOGLE_API_KEY` | `SHEETS_GOOGLE_API_KEY` | |
| 47 | | Project number | `DOCS_GOOGLE_APP_ID` | `SHEETS_GOOGLE_APP_ID` | |
| 48 | |
| 49 | There is no client secret setting. Leave production Google configuration unset |
| 50 | until the real consent and import checks below have succeeded on an authorized |
| 51 | canary origin. |
| 52 | |
| 53 | ## App integration |
| 54 | |
| 55 | Pass `Config` through the app's config and validate at startup. Merge |
| 56 | `Templates()` with the app templates (glob `templates/*.html`), then render |
| 57 | `estilo-google-import` with `View{Config, Kind: Docs/Sheets, FormID, InputID, |
| 58 | Labels: Text(locale)}`. The file input must belong to the named same-origin |
| 59 | multipart POST form. Existing hidden fields, including creation tokens, remain |
| 60 | intact. The app owns permissions, CSRF, upload limits, conversion and reports. |
| 61 | |
| 62 | Serve `CSS()` as a stylesheet and link it on the page that renders the |
| 63 | chooser; it spaces the chooser's disclosure, buttons and status on the spacing |
| 64 | scale. Remove any `[estilo-google-import]` rules of the app's own when you link |
| 65 | it: a column gap of the app's would add to its margins. It has no hashed name, |
| 66 | so serve and cache it as the app serves its other estilo stylesheets; the |
| 67 | immutable caching below is the script's. Serve `JS()` as |
| 68 | JavaScript at `JSName()` and load it with a module script only |
| 69 | on configured import pages. The name contains the asset's hash and may be |
| 70 | cached as immutable. Call `Headers(w, ContentSecurityPolicy)` before rendering |
| 71 | the opted-in import page, including a refused POST's re-render. Handle errors |
| 72 | before writing a response. This sets one expanded policy, preserves unrelated |
| 73 | directives, and sets no-store. It adds no COOP header. Both current apps already |
| 74 | allow inline styles; this component adds neither inline-script nor eval access. |
| 75 | |
| 76 | The scripts come directly from Google as Google requires; no CDN library is |
| 77 | vendored or bundled. Loading waits until prerendered pages are activated. |
| 78 | Picker is filtered by native MIME type, and returned shortcuts/other types |
| 79 | are rejected again. Download uses a fixed URL built from a validated file ID, |
| 80 | refuses redirects, and checks actual streamed bytes against `MaxExportBytes` |
| 81 | (10 MiB). Google's export API also applies its own size limit. Files handed to |
| 82 | the upload form use the chosen name with path separators removed and `.docx` |
| 83 | or `.xlsx` appended. Never treat that name or MIME type as server validation. |
| 84 | |
| 85 | ## Verification |
| 86 | |
| 87 | `go test ./googleimport` runs config/policy/template checks and the Node tests |
| 88 | for fixed-origin export, bounded streaming, scope-independent response errors, |
| 89 | and MIME/ID validation. `go test -tags browser ./googleimport` additionally |
| 90 | exercises Chromium and WebKit through real headers, using controlled Google |
| 91 | responses. Point `ESTILO_PLAYWRIGHT` at an installed Playwright package if it |
| 92 | is not resolvable here. `ESTILO_BROWSERS` defaults to `chromium,webkit`. |
| 93 | |
| 94 | Mocked responses prove the integration's behavior, not real account consent. |
| 95 | Before enabling a production origin, a person must connect a permitted Google |
| 96 | account on the configured canary, select a real Google Doc and Sheet, verify |
| 97 | their editable copies and omission reports, and retry after cancelling consent. |
| 98 | Keep representative exports as regression fixtures only with permission to |
| 99 | commit their contents. Real Google libraries were separately checked to load |
| 100 | and create a Picker frame under the proposed CSP; that also is not proof of |
| 101 | live consent or export with a real project. |
| 102 | |
| 103 | Google references: [token model](https://developers.google.com/identity/oauth2/web/guides/use-token-model), |
| 104 | [Picker setup](https://developers.google.com/workspace/drive/picker/guides/web-picker), |
| 105 | [exports](https://developers.google.com/workspace/drive/api/reference/rest/v3/files/export). |
| 106 | |