-
Notifications
You must be signed in to change notification settings - Fork 56
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
chore: add csrf token handling #289
base: master
Are you sure you want to change the base?
Changes from all commits
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,4 @@ | ||
var Runner = require('../../../Runner'); | ||
var restServiceMock = require('./apiServiceMock'); | ||
|
||
Runner.startApp(restServiceMock); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var logger = require('../logger')(3); | ||
|
||
var CSRF_HEADER = 'x-csrf-token'; | ||
|
||
function CsrfAuthenticator(options){ | ||
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. I am also fine with declarative setup like request.authenticate({crsf: {user,pass,url}) 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 is no user or pass that could be associated with csrf 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 is for sure, the call for getting the crsf token should be authenticated, see the reference impl: https://github.wdf.sap.corp/I533952/uiveri5_iscoper/blob/9f08dcbbf05b692184aa619266ea1b3904ad8e61/iscoperPlugin.js#L43 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. thanks for this link but as you know I've had it for 2 months and looked at it every day since. if you want to add support for authentication, it would be a separate topic. besides, user:auth in the url is accepted https://visionmedia.github.io/superagent/#authentication |
||
options = options || {}; | ||
this.user = options.user; | ||
this.pass = options.pass; | ||
this.csrfHeader = options.csrfHeader; | ||
this.csrfFetchUrl = options.csrfFetchUrl; | ||
} | ||
|
||
CsrfAuthenticator.prototype.authenticate = function () { | ||
var that = this; | ||
if (this.csrfFetchUrl) { | ||
return request.get(this.csrfFetchUrl) | ||
.set(CSRF_HEADER, 'Fetch') | ||
.do() | ||
.then(function (res) { | ||
if (res.headers[CSRF_HEADER]) { | ||
that.csrfHeader = res.headers[CSRF_HEADER]; | ||
} else { | ||
logger.error('Cannot generate CSRF token: missing X-CSRF-Token header'); | ||
} | ||
}).catch(function (err) { | ||
logger.error('Error in generating CSRF token. Details: ' + err); | ||
}); | ||
} | ||
}; | ||
|
||
CsrfAuthenticator.prototype.modifyCall = function (req) { | ||
if (this.csrfHeader) { | ||
req.set(CSRF_HEADER, this.csrfHeader); | ||
} | ||
// superagent will add the cookies e.g. _csrf cookie | ||
return req; | ||
}; | ||
|
||
module.exports = CsrfAuthenticator; |
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 idea to use standard module
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.
what is the standard module?