From a9d6398634236462a232f94d2ec3f2b5ab1fe4fc Mon Sep 17 00:00:00 2001 From: Ross Stenersen Date: Wed, 10 Jan 2024 14:40:10 -0600 Subject: [PATCH] chore: bump core SDK version --- .changeset/calm-worms-taste.md | 8 +++++ package-lock.json | 28 +++++++-------- packages/cli/package.json | 2 +- packages/edge/package.json | 2 +- packages/edge/src/lib/live-logging.ts | 7 ++-- packages/lib/package.json | 2 +- .../src/__tests__/login-authenticator.test.ts | 36 ++++--------------- packages/lib/src/login-authenticator.ts | 18 ++-------- packages/lib/src/sse-command.ts | 6 ++-- packages/testlib/package.json | 2 +- packages/testlib/src/index.ts | 2 +- 11 files changed, 43 insertions(+), 70 deletions(-) create mode 100644 .changeset/calm-worms-taste.md diff --git a/.changeset/calm-worms-taste.md b/.changeset/calm-worms-taste.md new file mode 100644 index 00000000..6fcba9f5 --- /dev/null +++ b/.changeset/calm-worms-taste.md @@ -0,0 +1,8 @@ +--- +"@smartthings/plugin-cli-edge": patch +"@smartthings/cli-lib": patch +"@smartthings/cli-testlib": patch +"@smartthings/cli": patch +--- + +bump core SDK version diff --git a/package-lock.json b/package-lock.json index 9d083914..848f5de6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4800,9 +4800,9 @@ "link": true }, "node_modules/@smartthings/core-sdk": { - "version": "8.1.1", - "resolved": "https://registry.npmjs.org/@smartthings/core-sdk/-/core-sdk-8.1.1.tgz", - "integrity": "sha512-SyTg33ZpvvS4qkStoWmbLPK8+sxNE0mT/BMOiwqfuAUKlDU0MQAccE/JOY8BNDJtJtMjx2gc+3WQ7FsZS0ZT0g==", + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/@smartthings/core-sdk/-/core-sdk-8.1.2.tgz", + "integrity": "sha512-itj2uTj0CAM/s7YGBNdXnZ/9DqM1byYBBRCue9RFwmAqhNz+msEYPqve8MwGiopmCnsXjE5+E4bT+efBhpNIew==", "dependencies": { "async-mutex": "^0.4.0", "axios": "^0.27.2", @@ -19842,7 +19842,7 @@ "@oclif/plugin-not-found": "^2.3.1", "@oclif/plugin-plugins": "^2.1.0", "@smartthings/cli-lib": "^2.2.3", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "@smartthings/plugin-cli-edge": "^3.3.2", "inquirer": "^8.2.4", "js-yaml": "^4.1.0", @@ -19897,7 +19897,7 @@ "@log4js-node/log4js-api": "^1.0.2", "@oclif/core": "^1.16.3", "@smartthings/cli-lib": "^2.2.3", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "axios": "^0.27.2", "inquirer": "^8.2.4", "js-yaml": "^4.1.0", @@ -19943,7 +19943,7 @@ "dependencies": { "@log4js-node/log4js-api": "^1.0.2", "@oclif/core": "^1.16.3", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "@types/eventsource": "^1.1.9", "axios": "^0.27.2", "chalk": "^4.1.2", @@ -20009,7 +20009,7 @@ "license": "Apache-2.0", "dependencies": { "@smartthings/cli-lib": "^2.2.3", - "@smartthings/core-sdk": "^8.1.1" + "@smartthings/core-sdk": "^8.1.2" }, "devDependencies": { "@types/jest": "^28.1.5", @@ -23971,7 +23971,7 @@ "@oclif/plugin-plugins": "^2.1.0", "@smartthings/cli-lib": "^2.2.3", "@smartthings/cli-testlib": "^2.0.5", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "@smartthings/plugin-cli-edge": "^3.3.2", "@types/inquirer": "^8.2.1", "@types/jest": "^28.1.5", @@ -24012,7 +24012,7 @@ "requires": { "@log4js-node/log4js-api": "^1.0.2", "@oclif/core": "^1.16.3", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "@types/eventsource": "^1.1.9", "@types/express": "^4.17.13", "@types/inquirer": "^8.2.1", @@ -24066,7 +24066,7 @@ "version": "file:packages/testlib", "requires": { "@smartthings/cli-lib": "^2.2.3", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "@types/jest": "^28.1.5", "@types/js-yaml": "^4.0.5", "@types/node": "^18.15.11", @@ -24086,9 +24086,9 @@ } }, "@smartthings/core-sdk": { - "version": "8.1.1", - "resolved": "https://registry.npmjs.org/@smartthings/core-sdk/-/core-sdk-8.1.1.tgz", - "integrity": "sha512-SyTg33ZpvvS4qkStoWmbLPK8+sxNE0mT/BMOiwqfuAUKlDU0MQAccE/JOY8BNDJtJtMjx2gc+3WQ7FsZS0ZT0g==", + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/@smartthings/core-sdk/-/core-sdk-8.1.2.tgz", + "integrity": "sha512-itj2uTj0CAM/s7YGBNdXnZ/9DqM1byYBBRCue9RFwmAqhNz+msEYPqve8MwGiopmCnsXjE5+E4bT+efBhpNIew==", "requires": { "async-mutex": "^0.4.0", "axios": "^0.27.2", @@ -24116,7 +24116,7 @@ "@oclif/core": "^1.16.3", "@smartthings/cli-lib": "^2.2.3", "@smartthings/cli-testlib": "^2.0.5", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "@types/cli-table": "^0.3.0", "@types/eventsource": "^1.1.8", "@types/inquirer": "^8.2.1", diff --git a/packages/cli/package.json b/packages/cli/package.json index 49ee99be..1d78e558 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -77,7 +77,7 @@ "@oclif/plugin-not-found": "^2.3.1", "@oclif/plugin-plugins": "^2.1.0", "@smartthings/cli-lib": "^2.2.3", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "@smartthings/plugin-cli-edge": "^3.3.2", "inquirer": "^8.2.4", "js-yaml": "^4.1.0", diff --git a/packages/edge/package.json b/packages/edge/package.json index d33d845c..b2bcc834 100644 --- a/packages/edge/package.json +++ b/packages/edge/package.json @@ -48,7 +48,7 @@ "@log4js-node/log4js-api": "^1.0.2", "@oclif/core": "^1.16.3", "@smartthings/cli-lib": "^2.2.3", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "axios": "^0.27.2", "inquirer": "^8.2.4", "js-yaml": "^4.1.0", diff --git a/packages/edge/src/lib/live-logging.ts b/packages/edge/src/lib/live-logging.ts index 941ea060..7e01cc8f 100644 --- a/packages/edge/src/lib/live-logging.ts +++ b/packages/edge/src/lib/live-logging.ts @@ -217,20 +217,21 @@ export class LiveLogClient { } private async request(url: string, method: Method = 'GET'): Promise { - const config = await this.config.authenticator.authenticate({ + const authHeaders = await this.config.authenticator.authenticate() + const config = { url: url, method: method, httpsAgent: new https.Agent({ rejectUnauthorized: false }), timeout: this.config.timeout, // eslint-disable-next-line @typescript-eslint/naming-convention - headers: { 'User-Agent': this.config.userAgent }, + headers: { 'User-Agent': this.config.userAgent, ...authHeaders }, transitional: { silentJSONParsing: true, forcedJSONParsing: true, // throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts clarifyTimeoutError: true, }, - }) + } let response try { diff --git a/packages/lib/package.json b/packages/lib/package.json index e1f4d0a2..b5a1e8eb 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -30,7 +30,7 @@ "dependencies": { "@log4js-node/log4js-api": "^1.0.2", "@oclif/core": "^1.16.3", - "@smartthings/core-sdk": "^8.1.1", + "@smartthings/core-sdk": "^8.1.2", "@types/eventsource": "^1.1.9", "axios": "^0.27.2", "chalk": "^4.1.2", diff --git a/packages/lib/src/__tests__/login-authenticator.test.ts b/packages/lib/src/__tests__/login-authenticator.test.ts index 8ce2c25e..bcc6485a 100644 --- a/packages/lib/src/__tests__/login-authenticator.test.ts +++ b/packages/lib/src/__tests__/login-authenticator.test.ts @@ -429,31 +429,12 @@ describe('LoginAuthenticator', () => { }) describe('authenticate', () => { - it('calls generic authenticate', async () => { - readFileMock.mockReturnValueOnce(Buffer.from(JSON.stringify(credentialsFileData))) - - const genericSpy = jest.spyOn(LoginAuthenticator.prototype, 'authenticateGeneric') - - const loginAuthenticator = setupAuthenticator() - const requestConfig = {} - - const response = await loginAuthenticator.authenticate(requestConfig) - - expect(response.headers?.Authorization).toEqual('Bearer access token') - expect(genericSpy).toBeCalledTimes(1) - }) - }) - - describe('authenticateGeneric', () => { it('refreshes token when necessary', async () => { readFileMock.mockReturnValueOnce(Buffer.from(JSON.stringify(refreshableCredentialsFileData))) const loginAuthenticator = setupAuthenticator() - const requestConfig = {} - - const response = await loginAuthenticator.authenticate(requestConfig) - expect(response.headers?.Authorization).toEqual('Bearer access token') + expect(await loginAuthenticator.authenticate()).toStrictEqual({ Authorization: 'Bearer access token' }) expect(postMock).toHaveBeenCalledTimes(1) const postData = postMock.mock.calls[0][1] @@ -472,9 +453,8 @@ describe('LoginAuthenticator', () => { it('includes User-Agent on refresh', async () => { readFileMock.mockReturnValueOnce(Buffer.from(JSON.stringify(refreshableCredentialsFileData))) const loginAuthenticator = setupAuthenticator() - const requestConfig = {} - await loginAuthenticator.authenticate(requestConfig) + await loginAuthenticator.authenticate() expect(postMock).toHaveBeenCalledTimes(1) expect(postMock).toHaveBeenCalledWith( @@ -493,13 +473,11 @@ describe('LoginAuthenticator', () => { postMock.mockResolvedValueOnce(tokenResponse) const loginAuthenticator = setupAuthenticator() - const requestConfig = {} - const promise = loginAuthenticator.authenticate(requestConfig) + const promise = loginAuthenticator.authenticate() await imitateBrowser() - const response = await promise - expect(response.headers?.Authorization).toEqual('Bearer access token') + expect(await promise).toStrictEqual({ Authorization: 'Bearer access token' }) expect(postMock).toHaveBeenCalledTimes(2) const postData = postMock.mock.calls[0][1] @@ -529,13 +507,11 @@ describe('LoginAuthenticator', () => { it('logs in not logged in', async () => { const loginAuthenticator = setupAuthenticator() - const requestConfig = {} - const promise = loginAuthenticator.authenticate(requestConfig) + const promise = loginAuthenticator.authenticate() await imitateBrowser() - const response = await promise - expect(response.headers?.Authorization).toEqual('Bearer access token') + expect(await promise).toStrictEqual({ Authorization: 'Bearer access token' }) expect(getPortMock).toHaveBeenCalledTimes(1) expect(getPortMock).toHaveBeenCalledWith({ port: [61973, 61974, 61975] }) diff --git a/packages/lib/src/login-authenticator.ts b/packages/lib/src/login-authenticator.ts index 44a93716..c9392d15 100644 --- a/packages/lib/src/login-authenticator.ts +++ b/packages/lib/src/login-authenticator.ts @@ -6,7 +6,7 @@ import getPort from 'get-port' import open from 'open' import path from 'path' import qs from 'qs' -import { SmartThingsURLProvider, defaultSmartThingsURLProvider, Authenticator } from '@smartthings/core-sdk' +import { SmartThingsURLProvider, defaultSmartThingsURLProvider, Authenticator, HttpClientHeaders } from '@smartthings/core-sdk' import log4js from '@log4js-node/log4js-api' import { CliUx } from '@oclif/core' @@ -279,19 +279,7 @@ export class LoginAuthenticator implements Authenticator { }) } - async authenticate(requestConfig: AxiosRequestConfig): Promise { - const token = await this.authenticateGeneric() - - return { - ...requestConfig, - headers: { - ...requestConfig.headers, - Authorization: `Bearer ${token}`, - }, - } - } - - async authenticateGeneric(): Promise { + async authenticate(): Promise { this.logger.debug('authentication - enter') // refresh if we have less than an hour left on the auth token if (this.authenticationInfo && this.authenticationInfo.expires.getTime() < Date.now() + 60 * 60 * 1000) { @@ -304,7 +292,7 @@ export class LoginAuthenticator implements Authenticator { } if (this.authenticationInfo) { - return this.authenticationInfo.accessToken + return { Authorization: `Bearer ${this.authenticationInfo.accessToken}` } } throw new Error('unable to obtain user credentials') diff --git a/packages/lib/src/sse-command.ts b/packages/lib/src/sse-command.ts index 580c0d3f..073fc013 100644 --- a/packages/lib/src/sse-command.ts +++ b/packages/lib/src/sse-command.ts @@ -23,9 +23,9 @@ export abstract class SseCommand extends APIC const headers: HttpClientHeaders = { 'User-Agent': this.userAgent } // assume auth is taken care of if passing an initDict - if (!sourceInitDict && this.authenticator.authenticateGeneric) { - const token = await this.authenticator.authenticateGeneric() - sourceInitDict = { headers: { ...headers, 'Authorization': `Bearer ${token}` } } + if (!sourceInitDict) { + const authHeaders = await this.authenticator.authenticate() + sourceInitDict = { headers: { ...headers, ...authHeaders } } } else { sourceInitDict = { ...sourceInitDict, headers: { ...headers, ...sourceInitDict?.headers } } } diff --git a/packages/testlib/package.json b/packages/testlib/package.json index 5dd9165e..62b9862d 100644 --- a/packages/testlib/package.json +++ b/packages/testlib/package.json @@ -29,7 +29,7 @@ }, "dependencies": { "@smartthings/cli-lib": "^2.2.3", - "@smartthings/core-sdk": "^8.1.1" + "@smartthings/core-sdk": "^8.1.2" }, "devDependencies": { "@types/jest": "^28.1.5", diff --git a/packages/testlib/src/index.ts b/packages/testlib/src/index.ts index 094cf187..50771431 100644 --- a/packages/testlib/src/index.ts +++ b/packages/testlib/src/index.ts @@ -42,7 +42,7 @@ jest.spyOn(APICommand.prototype, 'client', 'get').mockReturnValue(new MockSmartT * Perform minimal stubbing required to get CLI commands running under jest */ jest.spyOn(LoginAuthenticator.prototype, 'login').mockImplementation(() => Promise.resolve()) -jest.spyOn(LoginAuthenticator.prototype, 'authenticate').mockImplementation((requestConfig) => Promise.resolve(requestConfig)); +jest.spyOn(LoginAuthenticator.prototype, 'authenticate').mockImplementation(() => Promise.resolve({})); (global as { _credentialsFile?: string })._credentialsFile = 'credentials.json' /**