@adobe/spacecat-shared-google-client 1.2.1 → 1.2.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-google-client-v1.2.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-google-client-v1.2.1...@adobe/spacecat-shared-google-client-v1.2.2) (2024-08-26)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * authentication token refresh ([#344](https://github.com/adobe/spacecat-shared/issues/344)) ([37cb442](https://github.com/adobe/spacecat-shared/commit/37cb442c7837241780af3fede345db6be66e72e2))
7
+
1
8
  # [@adobe/spacecat-shared-google-client-v1.2.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-google-client-v1.2.0...@adobe/spacecat-shared-google-client-v1.2.1) (2024-08-24)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-google-client",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Shared modules of the Spacecat Services - Google Client",
5
5
  "type": "module",
6
6
  "engines": {
package/src/index.js CHANGED
@@ -24,19 +24,54 @@ import {
24
24
  import { fetch as httpFetch } from './utils.js';
25
25
 
26
26
  export default class GoogleClient {
27
+ constructor(config, log = console) {
28
+ this.log = log;
29
+ this.authClient = new OAuth2Client(
30
+ config.clientId,
31
+ config.clientSecret,
32
+ config.redirectUri,
33
+ );
34
+
35
+ this.authClient.setCredentials({
36
+ access_token: config.accessToken,
37
+ refresh_token: config.refreshToken,
38
+ token_type: config.tokenType,
39
+ expiry_date: config.expiryDate,
40
+ });
41
+
42
+ this.expiryDate = config.expiryDate;
43
+ this.siteUrl = config.siteUrl;
44
+ this.baseUrl = config.baseUrl;
45
+ }
46
+
47
+ async #refreshTokenIfExpired() {
48
+ if (this.authClient.credentials.expiry_date < Date.now()) {
49
+ try {
50
+ const { credentials } = await this.authClient.refreshAccessToken();
51
+ this.authClient.setCredentials({
52
+ access_token: credentials.access_token,
53
+ expiry_date: credentials.expiry_date,
54
+ });
55
+ } catch (error) {
56
+ this.log.error('Failed to refresh token:', error);
57
+ throw error;
58
+ }
59
+ }
60
+ }
61
+
27
62
  static async createFrom(context, baseURL) {
28
63
  if (!isValidUrl(baseURL)) {
29
64
  throw new Error('Error creating GoogleClient: Invalid base URL');
30
65
  }
31
66
 
67
+ const customerSecret = resolveCustomerSecretsName(baseURL, context);
68
+ const client = new SecretsManagerClient({});
69
+
32
70
  try {
33
- const customerSecret = resolveCustomerSecretsName(baseURL, context);
34
- const client = new SecretsManagerClient({});
35
- const command = new GetSecretValueCommand({
36
- SecretId: customerSecret,
37
- });
71
+ const command = new GetSecretValueCommand({ SecretId: customerSecret });
38
72
  const response = await client.send(command);
39
73
  const secrets = JSON.parse(response.SecretString);
74
+
40
75
  const config = {
41
76
  accessToken: secrets.access_token,
42
77
  refreshToken: secrets.refresh_token,
@@ -48,45 +83,13 @@ export default class GoogleClient {
48
83
  clientSecret: context.env.GOOGLE_CLIENT_SECRET,
49
84
  redirectUri: context.env.GOOGLE_REDIRECT_URI,
50
85
  };
51
- return new GoogleClient(config, context.log);
52
- } catch (error) {
53
- throw new Error(`Error creating GoogleClient: ${error.message}`);
54
- }
55
- }
56
-
57
- constructor(config, log = console) {
58
- const authClient = new OAuth2Client(
59
- config.clientId,
60
- config.clientSecret,
61
- config.redirectUri,
62
- );
63
-
64
- if (!config.accessToken) {
65
- throw new Error('Missing access token in secret');
66
- }
67
-
68
- if (!config.refreshToken) {
69
- throw new Error('Missing refresh token in secret');
70
- }
71
86
 
72
- authClient.setCredentials({
73
- access_token: config.accessToken,
74
- refresh_token: config.refreshToken,
75
- token_type: config.tokenType,
76
- });
77
- this.authClient = authClient;
78
- this.expiryDate = config.expiryDate;
79
- this.siteUrl = config.siteUrl;
80
- this.baseUrl = config.baseUrl;
81
- this.log = log;
82
- }
87
+ const googleClient = new GoogleClient(config, context.log);
88
+ await googleClient.#refreshTokenIfExpired();
83
89
 
84
- async #refreshTokenIfExpired() {
85
- if (new Date(this.expiryDate).getTime() < Date.now()) {
86
- const { credentials } = await this.authClient.refreshAccessToken();
87
- this.authClient.setCredentials({
88
- access_token: credentials.access_token,
89
- });
90
+ return googleClient;
91
+ } catch (error) {
92
+ throw new Error(`Error creating GoogleClient: ${error.message}`);
90
93
  }
91
94
  }
92
95