Skip to content

Commit e5b5efb

Browse files
authored
Added option for custom callback token request handler (#50)
1 parent 0b017d0 commit e5b5efb

File tree

4 files changed

+144
-128
lines changed

4 files changed

+144
-128
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ module.exports = {
2323
DiscoveryResponse: require('./lib/discovery/DiscoveryResponse'),
2424
SchemaConnector: require('./lib/SchemaConnector'),
2525
StateRefreshResponse: require('./lib/state/StateRefreshResponse'),
26-
StateUpdateRequest: require('./lib/callbacks/StateUpdateRequest')
26+
StateUpdateRequest: require('./lib/callbacks/StateUpdateRequest'),
27+
STBase: require('./lib/STBase'),
28+
checkFetchStatus: require('./lib/util/checkFetchStatus')
2729
};

lib/SchemaConnector.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const clientSecret = Symbol('private)');
1212
const discoveryHandler = Symbol('private)');
1313
const stateRefreshHandler = Symbol('private)');
1414
const commandHandler = Symbol('private)');
15+
const callbackTokenRequestHandler = Symbol('private)');
1516
const callbackAccessHandler = Symbol('private)');
1617
const integrationDeletedHandler = Symbol('private)');
1718
const interactionResultHandler = Symbol('private)');
@@ -39,6 +40,19 @@ module.exports = class SchemaConnector {
3940

4041
this[callbackAccessHandler] = null;
4142

43+
this[callbackTokenRequestHandler] = (async (clientId, clientSecret, body) => {
44+
const tokenRequest = new AccessTokenRequest(
45+
clientId,
46+
clientSecret,
47+
body.headers.requestId
48+
);
49+
50+
return await tokenRequest.getCallbackToken(
51+
body.callbackUrls.oauthToken,
52+
body.callbackAuthentication.code
53+
);
54+
});
55+
4256
this[integrationDeletedHandler] = ((accessToken, data) => {
4357
console.log('integrationDeletedHandler not defined')
4458
});
@@ -100,13 +114,22 @@ module.exports = class SchemaConnector {
100114
}
101115

102116
/**
103-
* Sets the grant callback access handler
117+
* Sets the handlers called after callback access is granted
104118
*/
105119
callbackAccessHandler(callback) {
106120
this[callbackAccessHandler] = callback;
107121
return this
108122
}
109123

124+
/**
125+
* Overrides the built in handler that requests callback access tokens.
126+
* That handler is expected to return the response of the callback token request.
127+
*/
128+
callbackTokenRequestHandler(callback) {
129+
this[callbackTokenRequestHandler] = callback
130+
return this
131+
}
132+
110133
/**
111134
* Sets integration deleted handler
112135
*/
@@ -239,16 +262,7 @@ module.exports = class SchemaConnector {
239262
if (this[callbackAccessHandler]) {
240263
if (body.callbackAuthentication.clientId === this[clientId] ) {
241264

242-
const tokenRequest = new AccessTokenRequest(
243-
this[clientId],
244-
this[clientSecret],
245-
body.headers.requestId
246-
);
247-
248-
const tokenResponse = await tokenRequest.getCallbackToken(
249-
body.callbackUrls.oauthToken,
250-
body.callbackAuthentication.code
251-
);
265+
const tokenResponse = await this[callbackTokenRequestHandler](this[clientId], this[clientSecret], body)
252266

253267
await this[callbackAccessHandler](body.authentication.token, tokenResponse.callbackAuthentication, body.callbackUrls, body)
254268
}

0 commit comments

Comments
 (0)