Skip to content

Commit 6ccdf81

Browse files
authored
Merge pull request #25 from vectorize-io/updateNonce
Add nonces to the HTML creation so client side components can call it
2 parents e6d0a18 + ef5936a commit 6ccdf81

8 files changed

Lines changed: 44 additions & 33 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vectorize-io/vectorize-connect",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "A simple package for Google Drive authorization and file selection",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/baseOAuth/ui/picker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ export abstract class BasePicker {
213213
styles: string = '',
214214
head: string = '',
215215
body: string,
216-
scripts: string
216+
scripts: string,
217+
nonce?: string
217218
): string {
218219
return `
219220
<!DOCTYPE html>
@@ -283,7 +284,7 @@ export abstract class BasePicker {
283284
${body}
284285
</div>
285286
</div>
286-
<script>
287+
<script${nonce ? ` nonce="${nonce}"` : ''}>
287288
${scripts}
288289
</script>
289290
</body>

src/dropBoxOAuth/core/oauth.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ export class DropboxOAuth extends BaseOAuth {
9191
public static override async createCallbackResponse(
9292
code: string,
9393
config: DropboxOAuthConfig,
94-
error?: string | OAuthError
94+
error?: string | OAuthError,
95+
nonce?: string
9596
): Promise<Response> {
9697
if (error) {
9798
const errorObj = typeof error === 'string' ? new OAuthError(error, 'CALLBACK_ERROR') : error;
@@ -107,8 +108,8 @@ export class DropboxOAuth extends BaseOAuth {
107108
);
108109

109110
// Use the Dropbox picker template
110-
const htmlContent = DropboxPicker.createPickerHTML(tokens, config, tokens.refresh_token);
111-
111+
const htmlContent = DropboxPicker.createPickerHTML(tokens, config, tokens.refresh_token, undefined, nonce);
112+
112113
return new Response(htmlContent, { headers: { 'Content-Type': 'text/html' } });
113114
} catch (error) {
114115
return this.createErrorResponse(

src/dropBoxOAuth/ui/picker.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ export class DropboxPicker extends BasePicker {
1616
* @returns HTML string for the Dropbox picker interface
1717
*/
1818
createPickerHTML(
19-
tokens: OAuthResponse,
20-
config: DropboxOAuthConfig,
21-
refreshToken: string,
22-
preSelectedFiles?: Record<string, { name: string; mimeType: string }>
19+
tokens: OAuthResponse,
20+
config: DropboxOAuthConfig,
21+
refreshToken: string,
22+
preSelectedFiles?: Record<string, { name: string; mimeType: string }>,
23+
nonce?: string
2324
): string {
2425
const ui = this.getCommonUIElements();
2526

@@ -218,20 +219,22 @@ export class DropboxPicker extends BasePicker {
218219
${ui.fileListContainer}
219220
${ui.submitButtonContainer}
220221
`,
221-
dropboxScripts
222+
dropboxScripts,
223+
nonce
222224
);
223225
}
224226

225227
/**
226228
* Create a static instance for backward compatibility
227229
*/
228230
static createPickerHTML(
229-
tokens: OAuthResponse,
230-
config: DropboxOAuthConfig,
231-
refreshToken: string,
232-
preSelectedFiles?: Record<string, { name: string; mimeType: string }>
231+
tokens: OAuthResponse,
232+
config: DropboxOAuthConfig,
233+
refreshToken: string,
234+
preSelectedFiles?: Record<string, { name: string; mimeType: string }>,
235+
nonce?: string
233236
): string {
234237
const picker = new DropboxPicker();
235-
return picker.createPickerHTML(tokens, config, refreshToken, preSelectedFiles);
238+
return picker.createPickerHTML(tokens, config, refreshToken, preSelectedFiles, nonce);
236239
}
237240
}

src/googleDriveOAuth/core/oauth.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export class GoogleDriveOAuth extends BaseOAuth {
9696
public static override async createCallbackResponse(
9797
code: string,
9898
config: GoogleDriveOAuthConfig,
99-
error?: string | OAuthError
99+
error?: string | OAuthError,
100+
nonce?: string
100101
): Promise<Response> {
101102
if (error) {
102103
const errorObj = typeof error === 'string' ? new OAuthError(error, 'CALLBACK_ERROR') : error;
@@ -112,8 +113,8 @@ export class GoogleDriveOAuth extends BaseOAuth {
112113
);
113114

114115
// Use the Google Drive picker template
115-
const htmlContent = GoogleDrivePicker.createPickerHTML(tokens, config, tokens.refresh_token);
116-
116+
const htmlContent = GoogleDrivePicker.createPickerHTML(tokens, config, tokens.refresh_token, undefined, nonce);
117+
117118
return new Response(htmlContent, { headers: { 'Content-Type': 'text/html' } });
118119
} catch (error) {
119120
return this.createErrorResponse(

src/googleDriveOAuth/ui/picker.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ export class GoogleDrivePicker extends BasePicker {
1616
* @returns HTML string for the Google Drive picker interface
1717
*/
1818
createPickerHTML(
19-
tokens: OAuthResponse,
20-
config: GoogleDriveOAuthConfig,
21-
refreshToken: string,
22-
preSelectedFiles?: Record<string, { name: string; mimeType: string }>
19+
tokens: OAuthResponse,
20+
config: GoogleDriveOAuthConfig,
21+
refreshToken: string,
22+
preSelectedFiles?: Record<string, { name: string; mimeType: string }>,
23+
nonce?: string
2324
): string {
2425
const ui = this.getCommonUIElements();
2526

@@ -155,20 +156,22 @@ export class GoogleDrivePicker extends BasePicker {
155156
${ui.fileListContainer}
156157
${ui.submitButtonContainer}
157158
`,
158-
googleDriveScripts
159+
googleDriveScripts,
160+
nonce
159161
);
160162
}
161163

162164
/**
163165
* Create a static instance for backward compatibility
164166
*/
165167
static createPickerHTML(
166-
tokens: OAuthResponse,
167-
config: GoogleDriveOAuthConfig,
168-
refreshToken: string,
169-
preSelectedFiles?: Record<string, { name: string; mimeType: string }>
168+
tokens: OAuthResponse,
169+
config: GoogleDriveOAuthConfig,
170+
refreshToken: string,
171+
preSelectedFiles?: Record<string, { name: string; mimeType: string }>,
172+
nonce?: string
170173
): string {
171174
const picker = new GoogleDrivePicker();
172-
return picker.createPickerHTML(tokens, config, refreshToken, preSelectedFiles);
175+
return picker.createPickerHTML(tokens, config, refreshToken, preSelectedFiles, nonce);
173176
}
174177
}

src/googleDriveOAuth/utils/validation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { OAuthConfig, OAuthError, ConfigurationError } from '../types';
1+
import { OAuthError, ConfigurationError } from '../../baseOAuth/types';
2+
import { GoogleDriveOAuthConfig } from '../types';
23

34
/**
45
* Validates the OAuth configuration
56
* @param config The OAuth configuration to validate
67
* @throws ConfigurationError if the configuration is invalid
78
*/
8-
export function validateConfig(config: OAuthConfig): void {
9+
export function validateConfig(config: GoogleDriveOAuthConfig): void {
910
if (!config.clientId) {
1011
throw new ConfigurationError('Client ID is required');
1112
}

src/notionOAuth/ui/picker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export class NotionPicker {
1919
tokens: any,
2020
config: NotionOAuthConfig,
2121
accessToken: string,
22-
existingSelection?: Record<string, { title: string; pageId: string; parentType?: string }>
22+
existingSelection?: Record<string, { title: string; pageId: string; parentType?: string }>,
23+
nonce?: string
2324
): string {
2425
// Convert existing selection to JSON string for embedding in the HTML
2526
const existingSelectionStr = existingSelection
@@ -412,7 +413,7 @@ export class NotionPicker {
412413
</div>
413414
</div>
414415
415-
<script>
416+
<script${nonce ? ` nonce="${nonce}"` : ''}>
416417
// Store selected items
417418
const selectedItems = ${existingSelectionStr};
418419
let dataLoaded = false;

0 commit comments

Comments
 (0)