-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add feature: refresh MSTeams token before it expires #1230
Changes from 9 commits
8ae926b
514d7ec
2553164
bb1f34e
a3944df
5a62829
4f19463
2d94271
ad67ab2
521ed91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
let teamsBot; | ||
let teamsApi; | ||
let config = { | ||
clientId: 'client', | ||
clientSecret: 'secret', | ||
debug: false | ||
}; | ||
|
||
describe('authentication', () => { | ||
beforeEach(() => { | ||
jest.doMock('../../lib/TeamsAPI', () => { | ||
return jest.fn((configuration) => { | ||
return { | ||
getToken: jest.fn((cb) => { | ||
configuration.token = 'token'; | ||
configuration.token_expires_in = '3600'; | ||
cb(null); | ||
}) | ||
}; | ||
}); | ||
}); | ||
|
||
jest.useFakeTimers(); | ||
teamsApi = require('../../lib/TeamsAPI'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetModules(); | ||
jest.clearAllTimers(); | ||
}); | ||
|
||
test('get token', () => { | ||
let bot = require('../../lib/Teams')(config); | ||
expect(bot.api.getToken).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('refresh token before expiry', () => { | ||
let bot = require('../../lib/Teams')(config); | ||
expect(bot.api.getToken).toHaveBeenCalledTimes(1); | ||
jest.runOnlyPendingTimers(); | ||
expect(bot.api.getToken).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
test('token valid for 20 mins should refresh after 10 mins', () => { | ||
teamsApi.mockImplementation(jest.fn((configuration) => { | ||
return { | ||
getToken: jest.fn((cb) => { | ||
configuration.token = 'token'; | ||
configuration.token_expires_in = '20'; | ||
cb(null); | ||
}) | ||
}; | ||
})); | ||
let bot = require('../../lib/Teams')(config); | ||
expect(bot.config.token_expires_in).toBe('20'); | ||
expect(bot.api.getToken).toHaveBeenCalledTimes(1); | ||
jest.runTimersToTime(1000 * 60 * 11); | ||
expect(bot.api.getToken).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,14 +11,17 @@ function TeamsBot(configuration) { | |
var controller = Botkit(configuration || {}); | ||
|
||
controller.api = TeamsAPI(configuration || {}); | ||
|
||
controller.api.getToken(function(err) { | ||
function tokenHandler(err) { | ||
if (err) { | ||
// this is a fatal error - could not create a Teams API client | ||
throw new Error(err); | ||
} | ||
}); | ||
|
||
//use the expires_in value, convert to ms and remove 10 mins in ms | ||
let tokenExpiresIn = 1000 * 60 * (configuration.token_expires_in || 60); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expires_in unit is seconds, for example the value "3600" will denote that token will expire in one hour (60 * 60). Example of |
||
let expiryCheckDelta = 1000 * 60 * 10; | ||
setTimeout(controller.api.getToken, (tokenExpiresIn - expiryCheckDelta), tokenHandler); | ||
}; | ||
controller.api.getToken(tokenHandler); | ||
|
||
controller.defineBot(function(botkit, config) { | ||
var bot = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
configuration.token_expires_in value will be in seconds
https://tools.ietf.org/html/rfc6749#section-4.2.2