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

Commit 155d2ce

Browse files
committed
refactor(db): clients.secret to clients.hashedSecret, remove clients.whitelisted
Closes #155 Closes #267
1 parent 52854d6 commit 155d2ce

File tree

10 files changed

+33
-38
lines changed

10 files changed

+33
-38
lines changed

lib/db/memory.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const MAX_TTL = config.get('expiration.accessToken');
1919
* clients: {
2020
* <id>: {
2121
* id: <id>,
22-
* secret: <string>,
22+
* hashedSecret: <string>,
2323
* name: <string>,
2424
* imageUri: <string>,
2525
* redirectUri: <string>,
@@ -133,7 +133,7 @@ MemoryStore.prototype = {
133133
client.canGrant = !!client.canGrant;
134134
client.trusted = !!client.trusted;
135135
this.clients[hex] = client;
136-
client.secret = client.hashedSecret;
136+
client.hashedSecret = client.hashedSecret;
137137
return P.resolve(client);
138138
},
139139
updateClient: function updateClient(client) {
@@ -149,7 +149,7 @@ MemoryStore.prototype = {
149149
if (key === 'id') {
150150
// nothing
151151
} else if (key === 'hashedSecret') {
152-
old.secret = buf(client[key]);
152+
old.hashedSecret = buf(client[key]);
153153
} else if (client[key] !== undefined) {
154154
old[key] = client[key];
155155
}

lib/db/mysql/index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ MysqlStore.connect = function mysqlConnect(options) {
112112

113113
const QUERY_CLIENT_REGISTER =
114114
'INSERT INTO clients ' +
115-
'(id, name, imageUri, secret, redirectUri, termsUri, privacyUri, ' +
116-
' whitelisted, trusted, canGrant) ' +
117-
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
115+
'(id, name, imageUri, hashedSecret, redirectUri, termsUri, privacyUri, ' +
116+
' trusted, canGrant) ' +
117+
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);';
118118
const QUERY_CLIENT_DEVELOPER_INSERT =
119119
'INSERT INTO clientDevelopers ' +
120120
'(rowId, developerId, clientId) ' +
@@ -135,17 +135,17 @@ const QUERY_DEVELOPER_INSERT =
135135
'VALUES (?, ?);';
136136
const QUERY_CLIENT_GET = 'SELECT * FROM clients WHERE id=?';
137137
const QUERY_CLIENT_LIST = 'SELECT id, name, redirectUri, imageUri, ' +
138-
'termsUri, privacyUri, canGrant, whitelisted, trusted ' +
138+
'termsUri, privacyUri, canGrant, trusted ' +
139139
'FROM clients, clientDevelopers, developers ' +
140140
'WHERE clients.id = clientDevelopers.clientId AND ' +
141141
'developers.developerId = clientDevelopers.developerId AND ' +
142142
'developers.email =?;';
143143
const QUERY_CLIENT_UPDATE = 'UPDATE clients SET ' +
144144
'name=COALESCE(?, name), imageUri=COALESCE(?, imageUri), ' +
145-
'secret=COALESCE(?, secret), redirectUri=COALESCE(?, redirectUri), ' +
145+
'hashedSecret=COALESCE(?, hashedSecret), ' +
146+
'redirectUri=COALESCE(?, redirectUri), ' +
146147
'termsUri=COALESCE(?, termsUri), privacyUri=COALESCE(?, privacyUri), ' +
147-
'whitelisted=COALESCE(?, whitelisted), trusted=COALESCE(?, trusted), ' +
148-
'canGrant=COALESCE(?, canGrant) ' +
148+
'trusted=COALESCE(?, trusted), canGrant=COALESCE(?, canGrant) ' +
149149
'WHERE id=?';
150150
const QUERY_CLIENT_DELETE = 'DELETE FROM clients WHERE id=?';
151151
const QUERY_CODE_INSERT =
@@ -215,8 +215,7 @@ MysqlStore.prototype = {
215215
client.redirectUri,
216216
client.termsUri || '',
217217
client.privacyUri || '',
218-
!!client.trusted, // XXX TODO: we have duplicate columns while we're
219-
!!client.trusted, // in the process of renaming whitelisted=>trusted.
218+
!!client.trusted,
220219
!!client.canGrant
221220
]).then(function() {
222221
logger.debug('registerClient.success', { id: hex(id) });
@@ -299,7 +298,7 @@ MysqlStore.prototype = {
299298
if (!client.id) {
300299
return P.reject(new Error('Update client needs an id'));
301300
}
302-
var secret = client.hashedSecret || client.secret || null;
301+
var secret = client.hashedSecret;
303302
if (secret) {
304303
secret = buf(secret);
305304
}
@@ -311,8 +310,7 @@ MysqlStore.prototype = {
311310
client.redirectUri,
312311
client.termsUri,
313312
client.privacyUri,
314-
client.trusted, // XXX TODO: we have duplicate columns while we're
315-
client.trusted, // in the process of renaming whitelisted => trusted.
313+
client.trusted,
316314
client.canGrant,
317315

318316
// WHERE

lib/db/mysql/patch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// Update this if you add a new patch, and don't forget to update
77
// the documentation for the current schema in ../schema.sql.
88

9-
module.exports.level = 6;
9+
module.exports.level = 7;
1010

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Change clients.secret to clients.hashedSecret
2+
-- Drop whitelisted column
3+
4+
ALTER TABLE clients CHANGE secret hashedSecret BINARY(32);
5+
ALTER TABLE clients DROP COLUMN whitelisted;
6+
7+
UPDATE dbMetadata SET value = '7' WHERE name = 'schema-patch-level';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Change clients.hashedSecret to clients.secret
2+
-- (commented out to avoid accidentally running this in production...)
3+
4+
-- ALTER TABLE clients CHANGE hashedSecret secret BINARY(32);
5+
-- ALTER TABLE clients ADD COLUMN whitelisted BOOLEAN DEFAULT FALSE;
6+
-- UPDATE clients SET whitelisted=truested;
7+
8+
-- UPDATE dbMetadata SET value = '6' WHERE name = 'schema-patch-level';

lib/db/mysql/schema.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99

1010
CREATE TABLE IF NOT EXISTS clients (
1111
id BINARY(8) PRIMARY KEY,
12-
secret BINARY(32) NOT NULL,
12+
hashedSecret BINARY(32) NOT NULL,
1313
name VARCHAR(256) NOT NULL,
1414
imageUri VARCHAR(256) NOT NULL,
1515
redirectUri VARCHAR(256) NOT NULL,
1616
termsUri VARCHAR(256) NOT NULL,
1717
privacyUri VARCHAR(256) NOT NULL,
18-
whitelisted BOOLEAN DEFAULT FALSE,
1918
canGrant BOOLEAN DEFAULT FALSE,
2019
createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
2120
trusted BOOLEAN DEFAULT FALSE

lib/routes/client/list.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ function serialize(client) {
1818
terms_uri: client.termsUri,
1919
privacy_uri: client.privacyUri,
2020
can_grant: client.canGrant,
21-
// XXX TODO: a future PR will remove legacy "whitelisted" attr
22-
whitelisted: client.trusted,
2321
trusted: client.trusted
2422
};
2523
}
@@ -40,8 +38,6 @@ module.exports = {
4038
terms_uri: Joi.string().allow('').required(),
4139
privacy_uri: Joi.string().allow('').required(),
4240
can_grant: Joi.boolean().required(),
43-
// XXX TODO: a future PR will remove legacy "whitelisted" attr
44-
whitelisted: Joi.boolean().required(),
4541
trusted: Joi.boolean().required()
4642
})
4743
)

lib/routes/client/register.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ module.exports = {
2525
terms_uri: Joi.string().max(256).allow(''),
2626
privacy_uri: Joi.string().max(256).allow(''),
2727
can_grant: Joi.boolean(),
28-
// XXX TODO: a future PR will remove legacy "whitelisted" property
29-
whitelisted: Joi.boolean(),
3028
trusted: Joi.boolean()
3129
}
3230
},
@@ -40,8 +38,6 @@ module.exports = {
4038
terms_uri: Joi.string().required().allow(''),
4139
privacy_uri: Joi.string().required().allow(''),
4240
can_grant: Joi.boolean().required(),
43-
// XXX TODO: a future PR will remove legacy "whitelisted" property
44-
whitelisted: Joi.boolean().required(),
4541
trusted: Joi.boolean().required()
4642
}
4743
},
@@ -57,11 +53,7 @@ module.exports = {
5753
termsUri: payload.terms_uri || '',
5854
privacyUri: payload.privacy_uri || '',
5955
canGrant: !!payload.can_grant,
60-
// XXX TODO: a future PR will remove legacy "whitelisted" property.
61-
// Accept both for now for API b/w compat.
62-
trusted: !!(typeof payload.trusted !== 'undefined' ?
63-
payload.trusted :
64-
payload.whitelisted)
56+
trusted: !!payload.trusted
6557
};
6658
var developerEmail = req.auth.credentials.email;
6759
var developerId = null;
@@ -91,8 +83,6 @@ module.exports = {
9183
terms_uri: client.termsUri,
9284
privacy_uri: client.privacyUri,
9385
can_grant: client.canGrant,
94-
// XXX TODO: a future PR will remove legacy "whitelisted" property
95-
whitelisted: client.trusted,
9686
trusted: client.trusted
9787
}).code(201);
9888
}, reply);

lib/routes/token.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function confirmClient(id, secret) {
2828
}
2929

3030
var submitted = hex(encrypt.hash(buf(secret)));
31-
var stored = hex(client.secret);
31+
var stored = hex(client.hashedSecret);
3232
if (submitted !== stored) {
3333
logger.info('client.mismatchSecret', { client: id });
3434
logger.verbose('client.mismatchSecret.details', {

test/api.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ describe('/v1', function() {
881881
hashedSecret: encrypt.hash(secret2),
882882
redirectUri: 'https://example.domain',
883883
imageUri: 'https://example.foo.domain/logo.png',
884-
whitelisted: true
884+
trusted: true
885885
};
886886
return db.registerClient(client2).then(function(c) {
887887
id2 = c.id.toString('hex');
@@ -1256,9 +1256,6 @@ describe('/v1', function() {
12561256
assert(client.image_uri === '');
12571257
assert(client.can_grant === false);
12581258
assert(client.trusted === false);
1259-
// XXX TODO: future PR will remove legacy "whitelisted" attr,
1260-
// it's here for now for API b/w compat
1261-
assert(client.whitelisted === false);
12621259
return db.getClient(client.id).then(function(klient) {
12631260
assert.equal(klient.id.toString('hex'), client.id);
12641261
assert.equal(klient.name, client.name);

0 commit comments

Comments
 (0)