Skip to content

Commit

Permalink
🌱 Adds support for autoPush url param in poll and verify functions (#91)
Browse files Browse the repository at this point in the history
Resolves: OKTA-155565
  • Loading branch information
gowthamidommety-okta authored Feb 16, 2018
1 parent 36b347f commit 3d70ed9
Show file tree
Hide file tree
Showing 4 changed files with 1,085 additions and 14 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,20 @@ questionFactor.verify({
});
```

###### [OKTA push](https://developer.okta.com/docs/api/resources/authn.html#verify-push-factor)

- `autoPush` - Optional parameter to send a push notification immediately the next time `verify` is called on a push factor

```javascript
var pushFactor = transaction.factors.find(function(factor) {
return factor.provider === 'OKTA' && factor.factorType === 'push';
});

pushFactor.verify({
autoPush: true
});
```

###### [All other factors](http://developer.okta.com/docs/api/resources/authn.html#verify-factor)

```javascript
Expand Down Expand Up @@ -1044,19 +1058,25 @@ The user must verify the factor-specific challenge.
#### [verify(options)](http://developer.okta.com/docs/api/resources/authn.html#verify-factor)

- `passCode` - OTP sent to device
- `autoPush` - Optional parameter to send a push notification immediately the next time `verify` is called on a push factor

```javascript
transaction.verify({
passCode: '615243'
passCode: '615243',
autoPush: true
});
```

#### [poll()](http://developer.okta.com/docs/api/resources/authn.html#activate-push-factor)
#### [poll(options)](http://developer.okta.com/docs/api/resources/authn.html#activate-push-factor)

Poll until factorResult is not WAITING. Throws AuthPollStopError if prev or cancel is called.
- `autoPush` - Optional parameter to send a push notification immediately the next time `verify` is called on a push factor

Poll until factorResult is not WAITING. Throws AuthPollStopError if prev, resend, or cancel is called.

```javascript
transaction.poll();
transaction.poll({
autoPush: true
});
```

#### [prev()](http://developer.okta.com/docs/api/resources/authn.html#previous-transaction-state)
Expand Down
49 changes: 39 additions & 10 deletions lib/tx.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable complexity */
/* eslint-disable complexity, max-statements */
var http = require('./http');
var util = require('./util');
var Q = require('q');
Expand All @@ -7,7 +7,8 @@ var AuthPollStopError = require('./errors/AuthPollStopError');
var config = require('./config');

function addStateToken(res, options) {
var builtArgs = util.clone(options) || {};
var builtArgs = {};
util.extend(builtArgs, options);

// Add the stateToken if one isn't passed and we have one
if (!builtArgs.stateToken && res.stateToken) {
Expand Down Expand Up @@ -59,12 +60,14 @@ function getPollFn(sdk, res, ref) {
return function (options) {
var delay;
var rememberDevice;
var autoPush;

if (util.isNumber(options)) {
delay = options;
} else if (util.isObject(options)) {
delay = options.delay;
rememberDevice = options.rememberDevice;
autoPush = options.autoPush;
}

if (!delay && delay !== 0) {
Expand All @@ -74,10 +77,22 @@ function getPollFn(sdk, res, ref) {
// Get the poll function
var pollLink = util.getLink(res, 'next', 'poll');
function pollFn() {
var href = pollLink.href;
var opts = {};
if (typeof autoPush === 'function') {
try {
opts.autoPush = !!autoPush();
}
catch (e) {
return Q.reject(new AuthSdkError('AutoPush resulted in an error.'));
}
}
else if (autoPush !== undefined && autoPush !== null) {
opts.autoPush = !!autoPush;
}
if (rememberDevice) {
href += '?rememberDevice=true';
opts.rememberDevice = true;
}
var href = pollLink.href + util.toQueryParams(opts);
return http.post(sdk, href, getStateToken(res), {
saveAuthnState: false
});
Expand All @@ -87,12 +102,10 @@ function getPollFn(sdk, res, ref) {

var retryCount = 0;
var recursivePoll = function () {

// If the poll was manually stopped during the delay
if (!ref.isPolling) {
return Q.reject(new AuthPollStopError());
}

return pollFn()
.then(function (pollRes) {
// Reset our retry counter on success
Expand Down Expand Up @@ -180,21 +193,37 @@ function link2fn(sdk, res, obj, link, ref) {
});
}

var href = link.href;
var params = {};
var autoPush = data.autoPush;
if (autoPush !== undefined) {
if (typeof autoPush === 'function') {
try {
params.autoPush = !!autoPush();
}
catch (e) {
return Q.reject(new AuthSdkError('AutoPush resulted in an error.'));
}
}
else if (autoPush !== null) {
params.autoPush = !!autoPush;
}
data = util.omit(data, 'autoPush');
}

if (data.rememberDevice !== undefined) {
if (data.rememberDevice) {
href += '?rememberDevice=true';
params.rememberDevice = true;
}
data = util.omit(data, 'rememberDevice');

} else if (data.profile &&
data.profile.updatePhone !== undefined) {
if (data.profile.updatePhone) {
href += '?updatePhone=true';
params.updatePhone = true;
}
data.profile = util.omit(data.profile, 'updatePhone');
}

var href = link.href + util.toQueryParams(params);
return postToTransaction(sdk, href, data);
};
}
Expand Down
Loading

0 comments on commit 3d70ed9

Please sign in to comment.