-
Notifications
You must be signed in to change notification settings - Fork 309
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
Get access token before importing users #267
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b0353d0
get access token before importing users
Floppy 4359fef
remove the hardcoded auth token in test
Floppy 60f44b1
mock tokenProvider to get an accessToken
Floppy 2512c31
update token tests to include the right token
Floppy 44e2a3b
DRY up token value
Floppy af160e2
Merge branch 'master' into auth-before-import
luisrudge a2fec66
move error check to start of request handler
Floppy e221130
fix error message spacing and check in test
Floppy 429da3b
add check for total request failure
Floppy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,7 @@ JobsManager.prototype.get = function(params, cb) { | |
|
||
/** | ||
* Given a path to a file and a connection id, create a new job that imports the | ||
* users contained in the file or JSON string and associate them with the given | ||
* users contained in the file or JSON string and associate them with the given | ||
* connection. | ||
* | ||
* @method importUsers | ||
|
@@ -135,42 +135,45 @@ JobsManager.prototype.importUsers = function(data, cb) { | |
var url = options.baseUrl + '/jobs/users-imports'; | ||
var method = 'POST'; | ||
|
||
var promise = new Promise(function(resolve, reject) { | ||
request( | ||
{ | ||
url: url, | ||
method: method, | ||
headers: headers, | ||
formData: { | ||
users: { | ||
value: data.users_json ? Buffer.from(data.users_json) : fs.createReadStream(data.users), | ||
options: { | ||
filename: data.users_json ? 'users.json' : data.users, | ||
} | ||
}, | ||
connection_id: data.connection_id | ||
var promise = options.tokenProvider.getAccessToken().then(function(access_token) { | ||
return new Promise(function(resolve, reject) { | ||
request( | ||
{ | ||
url: url, | ||
method: method, | ||
headers: extend({ Authorization: `Bearer ${access_token}` }, headers), | ||
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 token is then added to the headers. |
||
formData: { | ||
users: { | ||
value: data.users_json | ||
? Buffer.from(data.users_json) | ||
: fs.createReadStream(data.users), | ||
options: { | ||
filename: data.users_json ? 'users.json' : data.users | ||
} | ||
}, | ||
connection_id: data.connection_id | ||
} | ||
}, | ||
function(err, res) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
// `superagent` uses the error parameter in callback on http errors. | ||
// the following code is intended to keep that behaviour (https://github.com/visionmedia/superagent/blob/master/lib/node/response.js#L170) | ||
var type = (res.statusCode / 100) | 0; | ||
var isErrorResponse = 4 === type || 5 === type; | ||
if (isErrorResponse) { | ||
var error = new Error('cannot ' + method + ' ' + url + ' (' + res.statusCode + ')'); | ||
error.status = res.statusCode; | ||
error.method = method; | ||
error.text = res.text; | ||
reject(error); | ||
} | ||
resolve(res); | ||
} | ||
}, | ||
function(err, res) { | ||
// `superagent` uses the error parameter in callback on http errors. | ||
// the following code is intended to keep that behaviour (https://github.com/visionmedia/superagent/blob/master/lib/node/response.js#L170) | ||
var type = (res.statusCode / 100) | 0; | ||
var isErrorResponse = 4 === type || 5 === type; | ||
if (isErrorResponse) { | ||
var error = new Error('cannot ' + method + url + ' (' + res.statusCode + ')'); | ||
error.status = res.statusCode; | ||
error.method = method; | ||
error.text = res.text; | ||
reject(error); | ||
} | ||
|
||
if (err) { | ||
reject(err); | ||
} | ||
|
||
resolve(res); | ||
} | ||
); | ||
); | ||
}); | ||
}); | ||
|
||
// Don't return a promise if a callback was given. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Before the existing request promise, we call
getAccessToken
from the provider we were passed on creation. This is then passed a function that wraps the existing code.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.
I think this is the right way to do it, but I'm not an expert with the various ways this codebase handles authentication, so if it should do something different as well / instead, please let me know.