Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit 5dfd5f8

Browse files
author
Shane Tomlinson
committed
fix(security): Fix the endpoints for /securityEvents.
There were problems with the auth-server integration. * convert GET /securityEvents to GET /securityEvents/:id/ip/:ipAddr to be more RESTful and work with the auth-server's pool get API * for POST /securityEvents, pass the body and query parameters, fixing the former attempt to pass the param's id and body. * in fxa-auth-db-server/index.js, add 3 helper functions so it's easy to know what data is passed to the db methods. fixes #171
1 parent b090367 commit 5dfd5f8

File tree

4 files changed

+68
-61
lines changed

4 files changed

+68
-61
lines changed

fxa-auth-db-server/index.js

Lines changed: 65 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function createServer(db) {
1313

1414
function reply(fn) {
1515
return function (req, res, next) {
16-
fn.call(db, req.params.id, req.body)
16+
fn(req.params, req.body, req.query)
1717
.then(
1818
handleSuccess.bind(null, req, res),
1919
handleError.bind(null, req, res)
@@ -22,15 +22,22 @@ function createServer(db) {
2222
}
2323
}
2424

25-
function replyNoParams(fn) {
26-
return function (req, res, next) {
27-
fn.call(db, req.body, req.query)
28-
.then(
29-
handleSuccess.bind(null, req, res),
30-
handleError.bind(null, req, res)
31-
)
32-
.done(next, next)
33-
}
25+
function withIdAndBody(fn) {
26+
return reply(function (params, body, query) {
27+
return fn.call(db, params.id, body)
28+
})
29+
}
30+
31+
function withBodyAndQuery(fn) {
32+
return reply(function (params, body, query) {
33+
return fn.call(db, body, query)
34+
})
35+
}
36+
37+
function withParams(fn) {
38+
return reply(function (params, body, query) {
39+
return fn.call(db, params)
40+
})
3441
}
3542

3643
var api = restify.createServer()
@@ -44,54 +51,54 @@ function createServer(db) {
4451
'uaDeviceType'
4552
]))
4653

47-
api.get('/account/:id', reply(db.account))
48-
api.del('/account/:id', reply(db.deleteAccount))
49-
api.put('/account/:id', reply(db.createAccount))
50-
api.get('/account/:id/devices', reply(db.accountDevices))
51-
api.post('/account/:id/checkPassword', reply(db.checkPassword))
52-
api.post('/account/:id/reset', reply(db.resetAccount))
53-
api.post('/account/:id/verifyEmail', reply(db.verifyEmail))
54-
api.post('/account/:id/locale', reply(db.updateLocale))
55-
api.get('/account/:id/sessions', reply(db.sessions))
56-
57-
api.get('/sessionToken/:id', reply(db.sessionToken))
58-
api.del('/sessionToken/:id', reply(db.deleteSessionToken))
59-
api.put('/sessionToken/:id', reply(db.createSessionToken))
60-
api.post('/sessionToken/:id/update', reply(db.updateSessionToken))
61-
api.get('/sessionToken/:id/device', reply(db.sessionWithDevice))
62-
63-
api.get('/keyFetchToken/:id', reply(db.keyFetchToken))
64-
api.del('/keyFetchToken/:id', reply(db.deleteKeyFetchToken))
65-
api.put('/keyFetchToken/:id', reply(db.createKeyFetchToken))
66-
67-
api.get('/sessionToken/:id/verified', reply(db.sessionTokenWithVerificationStatus))
68-
api.get('/keyFetchToken/:id/verified', reply(db.keyFetchTokenWithVerificationStatus))
69-
api.post('/tokens/:id/verify', reply(db.verifyTokens))
70-
71-
api.get('/accountResetToken/:id', reply(db.accountResetToken))
72-
api.del('/accountResetToken/:id', reply(db.deleteAccountResetToken))
73-
74-
api.get('/passwordChangeToken/:id', reply(db.passwordChangeToken))
75-
api.del('/passwordChangeToken/:id', reply(db.deletePasswordChangeToken))
76-
api.put('/passwordChangeToken/:id', reply(db.createPasswordChangeToken))
77-
78-
api.get('/passwordForgotToken/:id', reply(db.passwordForgotToken))
79-
api.del('/passwordForgotToken/:id', reply(db.deletePasswordForgotToken))
80-
api.put('/passwordForgotToken/:id', reply(db.createPasswordForgotToken))
81-
api.post('/passwordForgotToken/:id/update', reply(db.updatePasswordForgotToken))
82-
api.post('/passwordForgotToken/:id/verified', reply(db.forgotPasswordVerified))
83-
84-
api.get('/verificationReminders', replyNoParams(db.fetchReminders))
85-
api.post('/verificationReminders', replyNoParams(db.createVerificationReminder))
86-
api.del('/verificationReminders', replyNoParams(db.deleteReminder))
87-
88-
api.get('/securityEvents', reply(db.securityEvents))
89-
api.post('/securityEvents', reply(db.createSecurityEvent))
90-
91-
api.get('/emailRecord/:id', reply(db.emailRecord))
92-
api.head('/emailRecord/:id', reply(db.accountExists))
93-
94-
api.get('/__heartbeat__', reply(db.ping))
54+
api.get('/account/:id', withIdAndBody(db.account))
55+
api.del('/account/:id', withIdAndBody(db.deleteAccount))
56+
api.put('/account/:id', withIdAndBody(db.createAccount))
57+
api.get('/account/:id/devices', withIdAndBody(db.accountDevices))
58+
api.post('/account/:id/checkPassword', withIdAndBody(db.checkPassword))
59+
api.post('/account/:id/reset', withIdAndBody(db.resetAccount))
60+
api.post('/account/:id/verifyEmail', withIdAndBody(db.verifyEmail))
61+
api.post('/account/:id/locale', withIdAndBody(db.updateLocale))
62+
api.get('/account/:id/sessions', withIdAndBody(db.sessions))
63+
64+
api.get('/sessionToken/:id', withIdAndBody(db.sessionToken))
65+
api.del('/sessionToken/:id', withIdAndBody(db.deleteSessionToken))
66+
api.put('/sessionToken/:id', withIdAndBody(db.createSessionToken))
67+
api.post('/sessionToken/:id/update', withIdAndBody(db.updateSessionToken))
68+
api.get('/sessionToken/:id/device', withIdAndBody(db.sessionWithDevice))
69+
70+
api.get('/keyFetchToken/:id', withIdAndBody(db.keyFetchToken))
71+
api.del('/keyFetchToken/:id', withIdAndBody(db.deleteKeyFetchToken))
72+
api.put('/keyFetchToken/:id', withIdAndBody(db.createKeyFetchToken))
73+
74+
api.get('/sessionToken/:id/verified', withIdAndBody(db.sessionTokenWithVerificationStatus))
75+
api.get('/keyFetchToken/:id/verified', withIdAndBody(db.keyFetchTokenWithVerificationStatus))
76+
api.post('/tokens/:id/verify', withIdAndBody(db.verifyTokens))
77+
78+
api.get('/accountResetToken/:id', withIdAndBody(db.accountResetToken))
79+
api.del('/accountResetToken/:id', withIdAndBody(db.deleteAccountResetToken))
80+
81+
api.get('/passwordChangeToken/:id', withIdAndBody(db.passwordChangeToken))
82+
api.del('/passwordChangeToken/:id', withIdAndBody(db.deletePasswordChangeToken))
83+
api.put('/passwordChangeToken/:id', withIdAndBody(db.createPasswordChangeToken))
84+
85+
api.get('/passwordForgotToken/:id', withIdAndBody(db.passwordForgotToken))
86+
api.del('/passwordForgotToken/:id', withIdAndBody(db.deletePasswordForgotToken))
87+
api.put('/passwordForgotToken/:id', withIdAndBody(db.createPasswordForgotToken))
88+
api.post('/passwordForgotToken/:id/update', withIdAndBody(db.updatePasswordForgotToken))
89+
api.post('/passwordForgotToken/:id/verified', withIdAndBody(db.forgotPasswordVerified))
90+
91+
api.get('/verificationReminders', withBodyAndQuery(db.fetchReminders))
92+
api.post('/verificationReminders', withBodyAndQuery(db.createVerificationReminder))
93+
api.del('/verificationReminders', withBodyAndQuery(db.deleteReminder))
94+
95+
api.get('/securityEvents/:id/ip/:ipAddr', withParams(db.securityEvents))
96+
api.post('/securityEvents', withBodyAndQuery(db.createSecurityEvent))
97+
98+
api.get('/emailRecord/:id', withIdAndBody(db.emailRecord))
99+
api.head('/emailRecord/:id', withIdAndBody(db.accountExists))
100+
101+
api.get('/__heartbeat__', withIdAndBody(db.ping))
95102

96103
api.put(
97104
'/account/:uid/device/:deviceId',

fxa-auth-db-server/test/backend/db_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ module.exports = function(config, DB) {
14891489
function query (uid, addr, cb) {
14901490
return function () {
14911491
return db.securityEvents({
1492-
uid: uid,
1492+
id: uid,
14931493
ipAddr: addr
14941494
})
14951495
.then(cb)

lib/db/mem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ module.exports = function (log, error) {
853853
}
854854

855855
Memory.prototype.securityEvents = function (where) {
856-
var key = where.uid.toString('hex')
856+
var key = where.id.toString('hex')
857857
var events = securityEvents[key] || []
858858
var addr = where.ipAddr
859859
if (ip.isV4Format(addr)) {

lib/db/mysql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ module.exports = function (log, error) {
11141114

11151115
var FETCH_SECURITY_EVENTS = 'CALL fetchSecurityEvents_1(?, ?)'
11161116
MySql.prototype.securityEvents = function (where) {
1117-
var uid = where.uid
1117+
var uid = where.id
11181118

11191119
var ipAddr = ipHmac(this.ipHmacKey, uid, where.ipAddr)
11201120
return this.read(FETCH_SECURITY_EVENTS, [uid, ipAddr])

0 commit comments

Comments
 (0)