-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to manually checking transaction state #27
Changes from 8 commits
588d883
a241a1d
e5bab1a
b29b3c2
06c4ce7
d77033d
8fed2bf
2e3b535
e1f0822
e649bff
8294dc7
0015778
6a3832f
2291036
0d6d662
6d9b19e
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,16 @@ | ||
'use strict'; | ||
|
||
var object = require('../utils/object'); | ||
var GuardianError = require('./guardian_error'); | ||
|
||
function InvalidStateError(message) { | ||
GuardianError.call(this, { | ||
message: message, | ||
errorCode: 'invalid_state' | ||
}); | ||
} | ||
|
||
InvalidStateError.prototype = object.create(GuardianError.prototype); | ||
InvalidStateError.prototype.contructor = InvalidStateError; | ||
|
||
module.exports = InvalidStateError; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
var object = require('../utils/object'); | ||
var GuardianError = require('./guardian_error'); | ||
|
||
function UnexpectedInputError(message) { | ||
GuardianError.call(this, { | ||
message: message, | ||
errorCode: 'unexpected_input' | ||
}); | ||
} | ||
|
||
UnexpectedInputError.prototype = object.create(GuardianError.prototype); | ||
UnexpectedInputError.prototype.contructor = UnexpectedInputError; | ||
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.
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. Good catch |
||
|
||
module.exports = UnexpectedInputError; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,13 @@ var auth0Ticket = require('./utils/auth0_ticket'); | |
var httpClient = require('./utils/http_client'); | ||
var transactionFactory = require('./transaction/factory'); | ||
var clientFactory = require('./utils/client_factory'); | ||
|
||
var apiTransport = { | ||
polling: 'polling', | ||
manual: 'polling', | ||
socket: 'socket' | ||
}; | ||
|
||
/** | ||
* @public | ||
* | ||
|
@@ -26,7 +33,9 @@ var clientFactory = require('./utils/client_factory'); | |
* @param {string} [options.globalTrackingId] Id used to associate the request | ||
* in the transaction (that includes both Guardian and Auth0-server requests) | ||
* @param {string} options.accountLabel | ||
* @param {socket|polling} [options.transport] | ||
* | ||
* @deprecated @param {string} [options.transport] Use stateCheckingMechanism | ||
* @param {string} [options.stateCheckingMechanism] | ||
* | ||
* @param {SocketClient} [options.dependencies.socketClient] Client factory for socket api | ||
* @param {function(serviceUrl)} [options.dependencies.httpClient] Client factory for http | ||
|
@@ -44,7 +53,7 @@ function auth0GuardianJS(options) { | |
self.httpClient = object.get(options, 'dependencies.httpClient', | ||
httpClient(self.serviceUrl, globalTrackingId)); | ||
|
||
self.transport = options.transport || 'socket'; | ||
self.transport = options.transport || options.state_checking_mechanism || 'socket'; | ||
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. The jsdoc says 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. Good catch |
||
|
||
self.socketClient = clientFactory.create({ | ||
serviceUrl: self.serviceUrl, | ||
|
@@ -81,7 +90,9 @@ auth0GuardianJS.prototype.start = function start(callback) { | |
|
||
self.httpClient.post('/api/start-flow', | ||
self.credentials, | ||
{ state_transport: self.transport }, | ||
// TODO: polling is not a good name for api state checking since | ||
// it could be polling or manual checking | ||
{ state_transport: apiTransport[self.transport] }, | ||
function startTransaction(err, txLegacyData) { | ||
if (err) { | ||
callback(err); | ||
|
@@ -138,19 +149,27 @@ auth0GuardianJS.prototype.start = function start(callback) { | |
* | ||
* @param {string} transactionState.baseUrl | ||
|
||
* @param {undefined|string} options.transport | ||
* @deprecated @param {string} [options.transport] Use stateCheckingMechanism | ||
* @param {string} [options.stateCheckingMechanism] | ||
* | ||
* @param {SocketClient} [options.dependencies.socketClient] Client factory for socket api | ||
* @param {function(serviceUrl)} [options.dependencies.httpClient] Client factory for http | ||
*/ | ||
|
||
auth0GuardianJS.resume = function resume(options, transactionState, callback) { | ||
var txId = jwtToken(transactionState.transactionToken).getDecoded().txid; | ||
var transactionTokenObject = jwtToken(transactionState.transactionToken); | ||
var txId = transactionTokenObject.getDecoded().txid; | ||
|
||
// create httpClient/socketClient | ||
var httpClientInstance = object.get(options, 'dependencies.httpClient', | ||
httpClient(transactionState.baseUrl, txId)); | ||
|
||
var transport = options.transport || 'socket'; | ||
var transport = options.transport || options.stateCheckingMechanism || 'socket'; | ||
|
||
if (transactionTokenObject.isExpired()) { | ||
asyncHelpers.setImmediate(callback, new errors.CredentialsExpiredError()); | ||
return; | ||
} | ||
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. <3 |
||
|
||
var socketClient = clientFactory.create({ | ||
serviceUrl: transactionState.baseUrl, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ var object = require('../utils/object'); | |
var asyncHelpers = require('../utils/async'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var helpers = require('./helpers'); | ||
var events = require('../utils/events'); | ||
|
||
function authVerificationStep(strategy, options) { | ||
var self = object.create(authVerificationStep.prototype); | ||
|
@@ -28,7 +29,16 @@ authVerificationStep.prototype.getMethod = function getMethod() { | |
return this.strategy.method; | ||
}; | ||
|
||
authVerificationStep.prototype.verify = function verify(data) { | ||
/** | ||
* @param {object} data | ||
* @param {function} [acceptedCallback] Triggered once the data has been | ||
* accepted by the service provider, the first arguments indicates if it | ||
* was valid | ||
*/ | ||
authVerificationStep.prototype.verify = function verify(data, acceptedCallback) { | ||
// eslint-disable-next-line no-param-reassign | ||
acceptedCallback = acceptedCallback || function noop() {}; | ||
|
||
var self = this; | ||
|
||
// TODO Move this to the transaction | ||
|
@@ -57,14 +67,27 @@ authVerificationStep.prototype.verify = function verify(data) { | |
verificationPayload = verificationPayload || {}; | ||
|
||
if (err) { | ||
if (acceptedCallback) { | ||
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. can this ever be false? I see this before
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. Good point forgot to remove that 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. Done |
||
events.ignoreError(self, err); | ||
|
||
acceptedCallback(err); | ||
} | ||
|
||
done(err); | ||
return; | ||
} | ||
|
||
done(null, { | ||
var payload = { | ||
// New recovery code if needed (recover) | ||
recoveryCode: verificationPayload.recoveryCode | ||
}); | ||
}; | ||
|
||
// On the one hand we trigger the callback since the data has been | ||
// accepted by the service provider, on the other hand | ||
// we still need to wait for (loginOrRejectTask) to trigger the event. | ||
// loginOrRejectTask might never be triggered in case of transport=manual | ||
acceptedCallback(null, payload); | ||
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. Seems like this actually needs to be always there, so the previous |
||
done(null, payload); | ||
}); | ||
}; | ||
|
||
|
@@ -88,4 +111,10 @@ authVerificationStep.prototype.verify = function verify(data) { | |
}); | ||
}; | ||
|
||
authVerificationStep.prototype.serialize = function serialize() { | ||
var self = this; | ||
|
||
return { method: self.getMethod() }; | ||
}; | ||
|
||
module.exports = authVerificationStep; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
var object = require('../utils/object'); | ||
var asyncHelpers = require('../utils/async'); | ||
var events = require('../utils/events'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var enrollmentBuilder = require('../entities/enrollment'); | ||
var helpers = require('./helpers'); | ||
|
@@ -31,7 +32,18 @@ enrollmentConfirmationStep.prototype.getData = function getData() { | |
return this.strategy.getData(); | ||
}; | ||
|
||
enrollmentConfirmationStep.prototype.confirm = function confirm(data) { | ||
enrollmentConfirmationStep.prototype.getMethod = function getMethod() { | ||
return this.strategy.method; | ||
}; | ||
|
||
/** | ||
* @param {object} data | ||
* @param {function} [acceptedCallback] Triggered once the data has been accepted by | ||
* the service provider, the first argument indicates if it was valid | ||
*/ | ||
enrollmentConfirmationStep.prototype.confirm = function confirm(data, acceptedCallback) { | ||
// eslint-disable-next-line no-param-reassign | ||
acceptedCallback = acceptedCallback || function noop() {}; | ||
var self = this; | ||
|
||
// TODO Move this to the transaction | ||
|
@@ -44,7 +56,21 @@ enrollmentConfirmationStep.prototype.confirm = function confirm(data) { | |
}; | ||
|
||
var confirmTask = function confirmTask(done) { | ||
self.strategy.confirm(data, done); | ||
self.strategy.confirm(data, function confirmationDataAccepted(err, result) { | ||
if (err) { | ||
if (acceptedCallback) { | ||
events.ignoreError(self, err); | ||
|
||
acceptedCallback(err); | ||
} | ||
|
||
done(err); | ||
return; | ||
} | ||
|
||
acceptedCallback(err, { recoveryCode: self.enrollmentAttempt.getRecoveryCode() }); | ||
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. passing the recovery code here don't quite convince me, but, how are you going to get the recovery code otherwise... making |
||
done(err, result); | ||
}); | ||
}; | ||
|
||
asyncHelpers.all([ | ||
|
@@ -73,4 +99,10 @@ enrollmentConfirmationStep.prototype.confirm = function confirm(data) { | |
}); | ||
}; | ||
|
||
enrollmentConfirmationStep.prototype.serialize = function serialize() { | ||
var self = this; | ||
|
||
return { method: self.getMethod() }; | ||
}; | ||
|
||
module.exports = enrollmentConfirmationStep; |
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.
contructor
...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.
Good catch