Skip to content

Commit 36d2eb3

Browse files
authored
fix: Construct proper authenticator type (#157)
1 parent 34f97b0 commit 36d2eb3

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib/util/smart-app-context.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
'use strict'
22

33
const i18n = require('i18n')
4-
const {SmartThingsClient, SequentialRefreshTokenAuthenticator} = require('@smartthings/core-sdk')
4+
const {
5+
SmartThingsClient,
6+
BearerTokenAuthenticator,
7+
RefreshTokenAuthenticator,
8+
SequentialRefreshTokenAuthenticator} = require('@smartthings/core-sdk')
9+
510
const TokenStore = require('./token-store')
611

712
module.exports = class SmartAppContext {
@@ -87,11 +92,25 @@ module.exports = class SmartAppContext {
8792
}
8893

8994
if (this.authToken) {
90-
const authenticator = new SequentialRefreshTokenAuthenticator(
91-
this.authToken,
92-
new TokenStore(this.installedAppId, app._contextStore, app._clientId, app._clientSecret),
93-
this.apiMutex
94-
)
95+
const authenticator = app._contextStore ?
96+
(this.apiMutex ?
97+
98+
// Authenticator for non-lifecycle invocations which forces refresh requests to be
99+
// sequential to reduce the chance of overwriting a valid refresh token with an
100+
// invalid one
101+
new SequentialRefreshTokenAuthenticator(
102+
this.authToken,
103+
new TokenStore(this.installedAppId, app._contextStore, app._clientId, app._clientSecret),
104+
this.apiMutex) :
105+
106+
// Not currently used by the SDK but here for logical consistency in case a path is added that
107+
// supports parallel refreshes
108+
new RefreshTokenAuthenticator(this.authToken,
109+
new TokenStore(this.installedAppId, app._contextStore, app._clientId, app._clientSecret))) :
110+
111+
// Authenticator for lifecycle event invocations. Refresh unnecessary since token is always valid
112+
new BearerTokenAuthenticator(this.authToken)
113+
95114
const config = {
96115
locationId: this.locationId,
97116
installedAppId: this.installedAppId

test/unit/smartapp-context-spec.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
const assert = require('assert').strict
44
const {expect} = require('chai')
5+
const {
6+
BearerTokenAuthenticator,
7+
SequentialRefreshTokenAuthenticator} = require('@smartthings/core-sdk')
58
const SmartApp = require('../../lib/smart-app')
69
const SmartAppContext = require('../../lib/util/smart-app-context')
710

@@ -75,8 +78,9 @@ describe('smartapp-context-spec', () => {
7578
})
7679

7780
const ctx = await app.withContext('d692699d-e7a6-400d-a0b7-d5be96e7a564')
78-
expect(ctx).to.be.instanceof(SmartAppContext)
7981

82+
expect(ctx).to.be.instanceof(SmartAppContext)
83+
expect(ctx.api.config.authenticator).to.be.instanceof(SequentialRefreshTokenAuthenticator)
8084
assert.equal(installData.installedApp.installedAppId, ctx.installedAppId)
8185
assert.equal(installData.installedApp.locationId, ctx.locationId)
8286
assert.equal(installData.authToken, ctx.authToken)
@@ -86,7 +90,6 @@ describe('smartapp-context-spec', () => {
8690
it('endpoint app with context object', async () => {
8791
const params = {
8892
authToken: 'xxx',
89-
refreshToken: 'yyy',
9093
installedAppId: 'aaa',
9194
locationId: 'bbb',
9295
locale: 'en',
@@ -95,14 +98,17 @@ describe('smartapp-context-spec', () => {
9598

9699
const ctx = await app.withContext(params)
97100

101+
expect(ctx.api.config.authenticator).to.be.instanceof(BearerTokenAuthenticator)
98102
assert.equal(params.installedAppId, ctx.installedAppId)
99103
assert.equal(params.locationId, ctx.locationId)
100104
assert.equal(params.authToken, ctx.authToken)
101-
assert.equal(params.refreshToken, ctx.refreshToken)
102105
assert.equal(params.locale, ctx.event.locale)
103106
})
104107

105108
it('api app with context object', async () => {
109+
const contextStore = new ContextStore()
110+
app.contextStore(contextStore)
111+
106112
const params = {
107113
authToken: 'xxx',
108114
refreshToken: 'yyy',
@@ -112,6 +118,7 @@ describe('smartapp-context-spec', () => {
112118

113119
const ctx = await app.withContext(params)
114120

121+
expect(ctx.api.config.authenticator).to.be.instanceof(SequentialRefreshTokenAuthenticator)
115122
assert.equal(params.installedAppId, ctx.installedAppId)
116123
assert.equal(params.locationId, ctx.locationId)
117124
assert.equal(params.authToken, ctx.authToken)

0 commit comments

Comments
 (0)