@openfn/language-gmail 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -341,14 +341,25 @@ getMessageById($.data.find(m => m.report).messageId, {
341
341
  });
342
342
  ```
343
343
 
344
- # Acquiring an access token
344
+ # Authentication
345
345
 
346
- The Gmail adaptor implicitly uses the Gmail account of the Google account that
347
- is used to authenticate the application.
346
+ This adaptor supports two authentication methods: **OAuth2** and **Service Account**.
347
+
348
+ ## OAuth2
349
+
350
+ **On app.openfn.org:** Click **Sign in with Gmail** in the credential setup form. Authentication is handled for you and no manual token management is needed.
351
+
352
+ **Using the OpenFn CLI locally:** Use the [gcloud CLI](https://cloud.google.com/sdk/docs/install) to print a temporary access token:
353
+
354
+ ```bash
355
+ gcloud auth print-access-token
356
+ ```
357
+
358
+ The Gmail adaptor implicitly uses the Gmail account of the Google account that is used to authenticate the application.
348
359
 
349
360
  Allowing the Gmail adaptor to access a Gmail account is a multi-step process.
350
361
 
351
- ## Create an OAuth 2.0 client ID
362
+ ### Create an OAuth 2.0 client ID
352
363
 
353
364
  Follow the instructions are found here:
354
365
  https://support.google.com/googleapi/answer/6158849
@@ -365,7 +376,7 @@ https://support.google.com/googleapi/answer/6158849
365
376
  - On the resulting popup screen, find and click "DOWNLOAD JSON" and save this
366
377
  file to a secure location.
367
378
 
368
- ## Retrieve an access token
379
+ ### Retrieve an access token
369
380
 
370
381
  - Navigate to
371
382
  [OAuth 2.0 Playground](https://developers.google.com/oauthplayground/).
@@ -395,7 +406,7 @@ https://support.google.com/googleapi/answer/6158849
395
406
  token before it expires** or manually refresh it using the **Refresh access
396
407
  token** button.
397
408
 
398
- ## Configure OpenFn CLI to find the access token
409
+ ### Configure OpenFn CLI to find the access token
399
410
 
400
411
  The Gmail adaptor looks for the access token in the configuration section under
401
412
  `access_token`.
@@ -418,3 +429,20 @@ Example configuration using a workflow:
418
429
  ]
419
430
  }
420
431
  ```
432
+
433
+ ## Service Account
434
+
435
+ Service accounts allow the adaptor to access Gmail without user interaction,
436
+ making them well-suited for automated workflows.
437
+
438
+ > **Important:** Gmail service account access requires **Google Workspace**.
439
+ > It does **not** work with personal Gmail accounts (`@gmail.com`). If your
440
+ > organisation uses personal Google accounts, use the OAuth2 method instead.
441
+
442
+ To create a service account and JSON key file, follow the
443
+ [Google Cloud service account documentation](https://cloud.google.com/iam/docs/service-accounts-create).
444
+ Your OpenFn credential requires the `client_email` and `private_key` fields from the downloaded JSON key file. You can also optionally provide a `subject` field for impersonation -- see [Google's domain-wide delegation guide](https://developers.google.com/identity/protocols/oauth2/service-account#delegatingauthority) for details.
445
+
446
+ For enabling domain-wide delegation and authorising the required Gmail API
447
+ scopes, see the
448
+ [Google Workspace domain-wide delegation guide](https://support.google.com/a/answer/162106).
@@ -5,15 +5,35 @@
5
5
  "access_token": {
6
6
  "title": "Access Token",
7
7
  "type": "string",
8
- "description": "Your Gmail access token",
8
+ "description": "Your Gmail OAuth2 access token",
9
9
  "writeOnly": true,
10
10
  "minLength": 1,
11
11
  "examples": [
12
12
  "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjlGWERwYmhax0rZNRlQyU3ZRdVhoODQ2WVR3RUlCdyIsI"
13
13
  ]
14
+ },
15
+ "private_key": {
16
+ "title": "Private Key",
17
+ "type": "string",
18
+ "description": "RSA private key from the Google service account JSON key file. Required for service accounts",
19
+ "writeOnly": true,
20
+ "minLength": 1,
21
+ "examples": ["-----BEGIN RSA PRIVATE KEY-----\nMIIEow..."]
22
+ },
23
+ "client_email": {
24
+ "title": "Client Email",
25
+ "type": "string",
26
+ "description": "Service account email address (e.g. my-app@project-id.iam.gserviceaccount.com). Required for service accounts",
27
+ "minLength": 1,
28
+ "examples": ["my-app@project-id.iam.gserviceaccount.com"]
29
+ },
30
+ "subject": {
31
+ "title": "Subject Email",
32
+ "type": "string",
33
+ "description": "Gmail user to impersonate via domain-wide delegation (e.g. user@yourdomain.com). For service account",
34
+ "examples": ["user@yourdomain.com"]
14
35
  }
15
36
  },
16
37
  "type": "object",
17
- "additionalProperties": true,
18
- "required": ["access_token"]
38
+ "additionalProperties": true
19
39
  }
package/dist/index.cjs CHANGED
@@ -279,10 +279,33 @@ async function parseArchiveAttachment(attachment) {
279
279
  content
280
280
  };
281
281
  }
282
- function createConnection(state) {
283
- const { access_token } = state.configuration;
284
- const auth = new import_googleapis.google.auth.OAuth2();
285
- auth.credentials = { access_token };
282
+ async function createConnection(state) {
283
+ const {
284
+ access_token,
285
+ private_key,
286
+ client_email,
287
+ subject,
288
+ scopes = []
289
+ } = state.configuration;
290
+ const mandatoryScopes = [
291
+ "https://mail.google.com/",
292
+ "https://www.googleapis.com/auth/userinfo.email",
293
+ "https://www.googleapis.com/auth/userinfo.profile",
294
+ "openid"
295
+ ];
296
+ let auth;
297
+ if (private_key && client_email) {
298
+ auth = new import_googleapis.google.auth.JWT({
299
+ email: client_email,
300
+ key: private_key,
301
+ scopes: [...mandatoryScopes, ...scopes],
302
+ subject
303
+ });
304
+ await auth.authorize();
305
+ } else {
306
+ auth = new import_googleapis.google.auth.OAuth2();
307
+ auth.credentials = { access_token };
308
+ }
286
309
  gmail = import_googleapis.google.gmail({ version: "v1", auth });
287
310
  return state;
288
311
  }
@@ -579,6 +602,8 @@ function execute(...operations) {
579
602
  data: null
580
603
  };
581
604
  return (state) => {
605
+ var _a, _b;
606
+ const isServiceAccount = ((_a = state.configuration) == null ? void 0 : _a.private_key) && ((_b = state.configuration) == null ? void 0 : _b.client_email);
582
607
  return (0, import_language_common.execute)(
583
608
  createConnection,
584
609
  ...operations,
@@ -586,7 +611,7 @@ function execute(...operations) {
586
611
  )({
587
612
  ...initialState,
588
613
  ...state,
589
- configuration: (0, import_util.normalizeOauthConfig)(state.configuration)
614
+ configuration: isServiceAccount ? state.configuration : (0, import_util.normalizeOauthConfig)(state.configuration)
590
615
  });
591
616
  };
592
617
  }
package/dist/index.js CHANGED
@@ -241,10 +241,33 @@ async function parseArchiveAttachment(attachment) {
241
241
  content
242
242
  };
243
243
  }
244
- function createConnection(state) {
245
- const { access_token } = state.configuration;
246
- const auth = new google.auth.OAuth2();
247
- auth.credentials = { access_token };
244
+ async function createConnection(state) {
245
+ const {
246
+ access_token,
247
+ private_key,
248
+ client_email,
249
+ subject,
250
+ scopes = []
251
+ } = state.configuration;
252
+ const mandatoryScopes = [
253
+ "https://mail.google.com/",
254
+ "https://www.googleapis.com/auth/userinfo.email",
255
+ "https://www.googleapis.com/auth/userinfo.profile",
256
+ "openid"
257
+ ];
258
+ let auth;
259
+ if (private_key && client_email) {
260
+ auth = new google.auth.JWT({
261
+ email: client_email,
262
+ key: private_key,
263
+ scopes: [...mandatoryScopes, ...scopes],
264
+ subject
265
+ });
266
+ await auth.authorize();
267
+ } else {
268
+ auth = new google.auth.OAuth2();
269
+ auth.credentials = { access_token };
270
+ }
248
271
  gmail = google.gmail({ version: "v1", auth });
249
272
  return state;
250
273
  }
@@ -556,6 +579,8 @@ function execute(...operations) {
556
579
  data: null
557
580
  };
558
581
  return (state) => {
582
+ var _a, _b;
583
+ const isServiceAccount = ((_a = state.configuration) == null ? void 0 : _a.private_key) && ((_b = state.configuration) == null ? void 0 : _b.client_email);
559
584
  return commonExecute(
560
585
  createConnection,
561
586
  ...operations,
@@ -563,7 +588,7 @@ function execute(...operations) {
563
588
  )({
564
589
  ...initialState,
565
590
  ...state,
566
- configuration: normalizeOauthConfig(state.configuration)
591
+ configuration: isServiceAccount ? state.configuration : normalizeOauthConfig(state.configuration)
567
592
  });
568
593
  };
569
594
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openfn/language-gmail",
3
3
  "label": "Gmail",
4
- "version": "3.1.0",
4
+ "version": "3.2.0",
5
5
  "badge": "Community",
6
6
  "description": "OpenFn gmail adaptor",
7
7
  "type": "module",
@@ -31,8 +31,8 @@
31
31
  "assertion-error": "2.0.0",
32
32
  "chai": "4.3.6",
33
33
  "deep-eql": "4.1.1",
34
- "mocha": "^10.8.2",
35
34
  "rimraf": "3.0.2",
35
+ "sinon": "^19.0.4",
36
36
  "undici": "^7.28.0"
37
37
  },
38
38
  "repository": {
package/types/Utils.d.ts CHANGED
@@ -18,5 +18,5 @@ export function buildMessageContent(userId: any, messageId: any, contentIndicato
18
18
  messageId: any;
19
19
  }>;
20
20
  export function buildAndSendMessage(message: any): Promise<any>;
21
- export function createConnection(state: any): any;
21
+ export function createConnection(state: any): Promise<any>;
22
22
  export function removeConnection(state: any): any;