This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
index.js
746 lines (672 loc) · 22.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const path = require('path');
const buf = require('buf').hex;
const hex = require('buf').to.hex;
const mysql = require('mysql');
const MysqlPatcher = require('mysql-patcher');
const config = require('../../config');
const encrypt = require('../../encrypt');
const helpers = require('../helpers');
const P = require('../../promise');
const Scope = require('../../scope');
const unique = require('../../unique');
const patch = require('./patch');
const MAX_TTL = config.get('expiration.accessToken');
const REQUIRED_SQL_MODES = [
'STRICT_ALL_TABLES',
'NO_ENGINE_SUBSTITUTION'
];
const REQUIRED_CHARSET = 'UTF8MB4_UNICODE_CI';
// logger is not const to support mocking in the unit tests
var logger = require('../../logging')('db.mysql');
function MysqlStore(options) {
if (options.charset && options.charset !== REQUIRED_CHARSET) {
logger.warn('createDatabase.invalidCharset', { charset: options.charset });
throw new Error('You cannot use any charset besides ' + REQUIRED_CHARSET);
} else {
options.charset = REQUIRED_CHARSET;
}
options.typeCast = function(field, next) {
if (field.type === 'TINY' && field.length === 1) {
return field.string() === '1';
}
return next();
};
logger.info('pool.create', { options: options });
var pool = this._pool = mysql.createPool(options);
pool.on('enqueue', function() {
logger.info('pool.enqueue', {
queueLength: pool._connectionQueue && pool._connectionQueue.length
});
});
}
// Apply patches up to the current patch level.
// This will also create the DB if it is missing.
function updateDbSchema(patcher) {
logger.verbose('updateDbSchema', patcher.options);
var d = P.defer();
patcher.patch(function(err) {
if (err) {
logger.error('updateDbSchema', err);
return d.reject(err);
}
d.resolve();
});
return d.promise;
}
// Sanity-check that we're working with a compatible patch level.
function checkDbPatchLevel(patcher) {
logger.verbose('checkDbPatchLevel', patcher.options);
var d = P.defer();
patcher.readDbPatchLevel(function(err) {
if (err) {
return d.reject(err);
}
// We can run if we're at or above some patch level. Should be
// equal at initial deployment, and may be one or more higher
// later on, due to database changes in preparation for the next
// release.
if (patcher.currentPatchLevel >= patch.level) {
return d.resolve();
}
err = 'unexpected db patch level: ' + patcher.currentPatchLevel;
return d.reject(new Error(err));
});
return d.promise;
}
MysqlStore.connect = function mysqlConnect(options) {
if (options.logger) {
logger = options.logger;
}
options.createDatabase = options.createSchema;
options.dir = path.join(__dirname, 'patches');
options.metaTable = 'dbMetadata';
options.patchKey = 'schema-patch-level';
options.patchLevel = patch.level;
options.mysql = mysql;
var patcher = new MysqlPatcher(options);
return P.promisify(patcher.connect, patcher)().then(function() {
if (options.createSchema) {
return updateDbSchema(patcher);
}
}).then(function() {
return checkDbPatchLevel(patcher);
}).catch(function(error) {
logger.error('checkDbPatchLevel', error);
throw error;
}).finally(function () {
return P.promisify(patcher.end, patcher)();
}).then(function() {
return new MysqlStore(options);
});
};
const QUERY_CLIENT_REGISTER =
'INSERT INTO clients ' +
'(id, name, imageUri, hashedSecret, hashedSecretPrevious, redirectUri,' +
'trusted, allowedScopes, canGrant, publicClient) ' +
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
const QUERY_CLIENT_DEVELOPER_INSERT =
'INSERT INTO clientDevelopers ' +
'(rowId, developerId, clientId) ' +
'VALUES (?, ?, ?);';
const QUERY_CLIENT_DEVELOPER_LIST_BY_CLIENT_ID =
'SELECT developers.email, developers.createdAt ' +
'FROM clientDevelopers, developers ' +
'WHERE clientDevelopers.developerId = developers.developerId ' +
'AND clientDevelopers.clientId=?;';
const QUERY_DEVELOPER_OWNS_CLIENT =
'SELECT clientDevelopers.rowId ' +
'FROM clientDevelopers, developers ' +
'WHERE developers.developerId = clientDevelopers.developerId ' +
'AND developers.email =? AND clientDevelopers.clientId =?;';
const QUERY_DEVELOPER_INSERT =
'INSERT INTO developers ' +
'(developerId, email) ' +
'VALUES (?, ?);';
const QUERY_CLIENT_GET = 'SELECT * FROM clients WHERE id=?';
const QUERY_CLIENT_LIST = 'SELECT id, name, redirectUri, imageUri, ' +
'canGrant, publicClient, trusted ' +
'FROM clients, clientDevelopers, developers ' +
'WHERE clients.id = clientDevelopers.clientId AND ' +
'developers.developerId = clientDevelopers.developerId AND ' +
'developers.email =?;';
const QUERY_PUBLIC_CLIENTS_LIST = 'SELECT * FROM clients WHERE publicClient = 1 OR canGrant = 1;';
const QUERY_CLIENT_UPDATE = 'UPDATE clients SET ' +
'name=COALESCE(?, name), imageUri=COALESCE(?, imageUri), ' +
'hashedSecret=COALESCE(?, hashedSecret), ' +
'hashedSecretPrevious=COALESCE(?, hashedSecretPrevious), ' +
'redirectUri=COALESCE(?, redirectUri), ' +
'trusted=COALESCE(?, trusted), allowedScopes=COALESCE(?, allowedScopes), ' +
'canGrant=COALESCE(?, canGrant) ' +
'WHERE id=?';
const QUERY_CLIENT_DELETE = 'DELETE FROM clients WHERE id=?';
const QUERY_CODE_INSERT =
'INSERT INTO codes (clientId, userId, email, scope, authAt, amr, aal, offline, code, codeChallengeMethod, codeChallenge, keysJwe) ' +
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
const QUERY_ACCESS_TOKEN_INSERT =
'INSERT INTO tokens (clientId, userId, email, scope, type, expiresAt, ' +
'token) VALUES (?, ?, ?, ?, ?, ?, ?)';
const QUERY_REFRESH_TOKEN_INSERT =
'INSERT INTO refreshTokens (clientId, userId, email, scope, token) VALUES ' +
'(?, ?, ?, ?, ?)';
const QUERY_ACCESS_TOKEN_FIND = 'SELECT * FROM tokens WHERE token=?';
const QUERY_REFRESH_TOKEN_FIND = 'SELECT * FROM refreshTokens where token=?';
const QUERY_REFRESH_TOKEN_LAST_USED_UPDATE = 'UPDATE refreshTokens SET lastUsedAt=? WHERE token=?';
const QUERY_CODE_FIND = 'SELECT * FROM codes WHERE code=?';
const QUERY_CODE_DELETE = 'DELETE FROM codes WHERE code=?';
const QUERY_ACCESS_TOKEN_DELETE = 'DELETE FROM tokens WHERE token=?';
const QUERY_REFRESH_TOKEN_DELETE = 'DELETE FROM refreshTokens WHERE token=?';
const QUERY_ACCESS_TOKEN_DELETE_USER = 'DELETE FROM tokens WHERE userId=?';
const QUERY_DELETE_ACCESS_TOKEN_FOR_PUBLIC_CLIENTS = 'DELETE FROM tokens WHERE userId=? AND clientId IN (?);';
const QUERY_DELETE_REFRESH_TOKEN_FOR_PUBLIC_CLIENTS = 'DELETE FROM refreshTokens WHERE userId=? AND clientId IN (?);';
const QUERY_REFRESH_TOKEN_DELETE_USER =
'DELETE FROM refreshTokens WHERE userId=?';
const QUERY_CODE_DELETE_USER = 'DELETE FROM codes WHERE userId=?';
const QUERY_DEVELOPER = 'SELECT * FROM developers WHERE email=?';
const QUERY_DEVELOPER_DELETE = 'DELETE FROM developers WHERE email=?';
const QUERY_PURGE_EXPIRED_TOKENS = 'DELETE FROM tokens WHERE clientId NOT IN (?) AND expiresAt < NOW() LIMIT ?;';
// Token management by uid.
// Returns the most recent token used with a client name and client id.
// Does not include clients that canGrant.
const QUERY_ACTIVE_CLIENT_TOKENS_BY_UID =
'SELECT tokens.clientId, tokens.createdAt, tokens.scope, clients.name ' +
'FROM tokens LEFT OUTER JOIN clients ON clients.id = tokens.clientId ' +
'WHERE tokens.userId=? AND tokens.expiresAt > NOW() AND clients.canGrant = 0;';
const DELETE_ACTIVE_TOKENS_BY_CLIENT_AND_UID =
'DELETE FROM tokens WHERE clientId=? AND userId=?';
const DELETE_ACTIVE_REFRESH_TOKENS_BY_CLIENT_AND_UID =
'DELETE FROM refreshTokens WHERE clientId=? AND userId=?';
// Scope queries
const QUERY_SCOPE_FIND =
'SELECT * ' +
'FROM scopes ' +
'WHERE scopes.scope=?;';
const QUERY_SCOPES_INSERT =
'INSERT INTO scopes (scope, hasScopedKeys) ' +
'VALUES (?, ?);';
function firstRow(rows) {
return rows[0];
}
function releaseConn(connection) {
connection.release();
}
MysqlStore.prototype = {
ping: function ping() {
logger.debug('ping');
// see bluebird.using():
// https://github.com/petkaantonov/bluebird/blob/master/API.md#resource-management
return P.using(this._getConnection(), function(conn) {
return new P(function(resolve, reject) {
conn.ping(function(err) {
if (err) {
logger.error('ping:', err);
reject(err);
} else {
resolve();
}
});
});
});
},
// createdAt is DEFAULT NOW() in the schema.sql
registerClient: function registerClient(client) {
var id;
if (client.id) {
id = buf(client.id);
} else {
id = unique.id();
}
logger.debug('registerClient', { name: client.name, id: hex(id) });
return this._write(QUERY_CLIENT_REGISTER, [
id,
client.name,
client.imageUri || '',
buf(client.hashedSecret),
client.hashedSecretPrevious ? buf(client.hashedSecretPrevious) : null,
client.redirectUri,
!! client.trusted,
client.allowedScopes ? client.allowedScopes : null,
!! client.canGrant,
!! client.publicClient
]).then(function() {
logger.debug('registerClient.success', { id: hex(id) });
client.id = id;
return client;
});
},
registerClientDeveloper: function regClientDeveloper(developerId, clientId) {
if (! developerId || ! clientId) {
var err = new Error('Owner registration requires user and developer id');
return P.reject(err);
}
var rowId = unique.id();
logger.debug('registerClientDeveloper', {
rowId: rowId,
developerId: developerId,
clientId: clientId
});
return this._write(QUERY_CLIENT_DEVELOPER_INSERT, [
buf(rowId),
buf(developerId),
buf(clientId)
]);
},
getClientDevelopers: function getClientDevelopers (clientId) {
if (! clientId) {
return P.reject(new Error('Client id is required'));
}
return this._read(QUERY_CLIENT_DEVELOPER_LIST_BY_CLIENT_ID, [
buf(clientId)
]);
},
activateDeveloper: function activateDeveloper(email) {
if (! email) {
return P.reject(new Error('Email is required'));
}
var developerId = unique.developerId();
logger.debug('activateDeveloper', { developerId: developerId });
return this._write(QUERY_DEVELOPER_INSERT, [
developerId, email
]).then(function () {
return this.getDeveloper(email);
}.bind(this));
},
getDeveloper: function(email) {
if (! email) {
return P.reject(new Error('Email is required'));
}
return this._readOne(QUERY_DEVELOPER, [
email
]);
},
removeDeveloper: function(email) {
if (! email) {
return P.reject(new Error('Email is required'));
}
return this._write(QUERY_DEVELOPER_DELETE, [
email
]);
},
developerOwnsClient: function devOwnsClient(developerEmail, clientId) {
return this._readOne(QUERY_DEVELOPER_OWNS_CLIENT, [
developerEmail, buf(clientId)
]).then(function(result) {
if (result) {
return P.resolve(true);
} else {
return P.reject(false);
}
});
},
updateClient: function updateClient(client) {
if (! client.id) {
return P.reject(new Error('Update client needs an id'));
}
var secret = client.hashedSecret;
if (secret) {
secret = buf(secret);
}
var secretPrevious = client.hashedSecretPrevious;
if (secretPrevious) {
secretPrevious = buf(secretPrevious);
}
return this._write(QUERY_CLIENT_UPDATE, [
// VALUES
client.name,
client.imageUri,
secret,
secretPrevious,
client.redirectUri,
client.trusted,
client.allowedScopes,
client.canGrant,
// WHERE
buf(client.id)
]);
},
getClient: function getClient(id) {
return this._readOne(QUERY_CLIENT_GET, [buf(id)]);
},
getClients: function getClients(email) {
return this._read(QUERY_CLIENT_LIST, [ email ]);
},
removeClient: function removeClient(id) {
return this._write(QUERY_CLIENT_DELETE, [buf(id)]);
},
generateCode: function generateCode(codeObj) {
var code = unique.code();
var hash = encrypt.hash(code);
return this._write(QUERY_CODE_INSERT, [
codeObj.clientId,
codeObj.userId,
codeObj.email,
codeObj.scope.join(' '),
codeObj.authAt,
codeObj.amr ? codeObj.amr.join(',') : null,
codeObj.aal || null,
!! codeObj.offline,
hash,
codeObj.codeChallengeMethod,
codeObj.codeChallenge,
codeObj.keysJwe
]).then(function() {
return code;
});
},
getCode: function getCode(code) {
var hash = encrypt.hash(code);
return this._readOne(QUERY_CODE_FIND, [hash]).then(function(code) {
if (code) {
code.scope = code.scope.split(' ');
if (code.amr !== null) {
code.amr = code.amr.split(',');
}
}
return code;
});
},
removeCode: function removeCode(code) {
var hash = encrypt.hash(code);
return this._write(QUERY_CODE_DELETE, [hash]);
},
generateAccessToken: function generateAccessToken(vals) {
var t = {
clientId: buf(vals.clientId),
userId: buf(vals.userId),
email: vals.email,
scope: Scope(vals.scope),
token: unique.token(),
type: 'bearer',
expiresAt: vals.expiresAt || new Date(Date.now() + (vals.ttl * 1000 || MAX_TTL))
};
return this._write(QUERY_ACCESS_TOKEN_INSERT, [
t.clientId,
t.userId,
t.email,
t.scope.toString(),
t.type,
t.expiresAt,
encrypt.hash(t.token)
]).then(function() {
return t;
});
},
/**
* Get an access token by token id
* @param id Token Id
* @returns {*}
*/
getAccessToken: function getAccessToken(id) {
return this._readOne(QUERY_ACCESS_TOKEN_FIND, [buf(id)]).then(function(t) {
if (t) {
t.scope = t.scope.split(' ');
}
return t;
});
},
/**
* Remove token by token id
* @param id
* @returns {*}
*/
removeAccessToken: function removeAccessToken(id) {
return this._write(QUERY_ACCESS_TOKEN_DELETE, [buf(id)]);
},
/**
* Get all services that have have non-expired tokens
* @param {String} uid User ID as hex
* @returns {Promise}
*/
getActiveClientsByUid: function getActiveClientsByUid(uid) {
return this._read(QUERY_ACTIVE_CLIENT_TOKENS_BY_UID, [
buf(uid)
]).then(function(activeClientTokens) {
return helpers.aggregateActiveClients(activeClientTokens);
});
},
/**
* Delete all tokens for some clientId and uid.
*
* @param {String} clientId Client ID
* @param {String} uid User Id as Hex
* @returns {Promise}
*/
deleteActiveClientTokens: function deleteActiveClientTokens(clientId, uid) {
const deleteTokens = this._write(DELETE_ACTIVE_TOKENS_BY_CLIENT_AND_UID, [
buf(clientId),
buf(uid)
]);
const deleteRefreshTokens = this._write(DELETE_ACTIVE_REFRESH_TOKENS_BY_CLIENT_AND_UID, [
buf(clientId),
buf(uid)
]);
return P.all([
deleteTokens,
deleteRefreshTokens
]);
},
generateRefreshToken: function generateRefreshToken(vals) {
var t = {
clientId: vals.clientId,
userId: vals.userId,
email: vals.email,
scope: Scope(vals.scope)
};
var token = unique.token();
var hash = encrypt.hash(token);
return this._write(QUERY_REFRESH_TOKEN_INSERT, [
t.clientId,
t.userId,
t.email,
t.scope.toString(),
hash
]).then(function() {
t.token = token;
return t;
});
},
getRefreshToken: function getRefreshToken(token) {
return this._readOne(QUERY_REFRESH_TOKEN_FIND, [buf(token)])
.then(function(t) {
if (t) {
t.scope = t.scope.split(' ');
}
return t;
});
},
usedRefreshToken: function usedRefreshToken(token) {
var now = new Date();
return this._write(QUERY_REFRESH_TOKEN_LAST_USED_UPDATE, [
now,
// WHERE
token
]);
},
removeRefreshToken: function removeRefreshToken(id) {
return this._write(QUERY_REFRESH_TOKEN_DELETE, [buf(id)]);
},
getEncodingInfo: function getEncodingInfo() {
var info = {};
var self = this;
var qry = 'SHOW VARIABLES LIKE "%character\\_set\\_%"';
return this._read(qry).then(function(rows) {
rows.forEach(function(row) {
info[row.Variable_name] = row.Value;
});
qry = 'SHOW VARIABLES LIKE "%collation\\_%"';
return self._read(qry).then(function(rows) {
rows.forEach(function(row) {
info[row.Variable_name] = row.Value;
});
return info;
});
});
},
purgeExpiredTokens: function purgeExpiredTokens(numberOfTokens, delaySeconds, ignoreClientId){
var self = this;
if (! ignoreClientId) {
throw new Error('empty ignoreClientId');
}
if (! Array.isArray(ignoreClientId)) {
ignoreClientId = [ ignoreClientId ];
}
var clientIds = ignoreClientId.map(function (id) {
return self.getClient(id);
});
return P.all(clientIds)
.then(function (results) {
// This ensures that purgeExpiredTokens can not be called with an
// unknown ignoreClientId(s).
results.forEach(function (ignoreClient) {
if (! ignoreClient) {
throw new Error('unknown ignoreClientId ' + ignoreClientId);
}
});
})
.then(function () {
var deleteBatchSize = 200;
if (numberOfTokens <= deleteBatchSize) {
deleteBatchSize = numberOfTokens;
}
var message = '';
var deletedItems = 0;
var promiseWhile = P.method(function () {
if (deletedItems >= numberOfTokens) {
message = 'deletedItems >= numberOfTokens';
logger.info('purgeExpiredTokens', {
message: message,
deletedItems: deletedItems,
numberOfTokens: numberOfTokens,
deleteBatchSize: deleteBatchSize
});
return;
}
var clientIn = ignoreClientId.map(function(id) {
return buf(id);
});
return self._write(QUERY_PURGE_EXPIRED_TOKENS, [clientIn, deleteBatchSize])
.then(function (res) {
logger.info('purgeExpiredTokens', { affectedRows: res.affectedRows });
// Break loop if no items were effected by delete.
// All expired tokens have been deleted.
if (res.affectedRows === 0) {
message = '0 affectedRows. Bailing out.';
logger.info('purgeExpiredTokens', { message: message });
return;
}
deletedItems = deletedItems + res.affectedRows;
return P.delay(delaySeconds * 1000)
.then(function () {
return promiseWhile();
});
});
});
return promiseWhile();
})
.then(function() {
logger.info('purgeExpiredTokens', { message: 'completed' });
});
},
removeUser: function removeUser(userId) {
// TODO this should be a transaction or stored procedure
var id = buf(userId);
return this._write(QUERY_ACCESS_TOKEN_DELETE_USER, [id])
.then(this._write.bind(this, QUERY_REFRESH_TOKEN_DELETE_USER, [id]))
.then(this._write.bind(this, QUERY_CODE_DELETE_USER, [id]));
},
/**
* Removes user's tokens and refreshTokens for canGrant and publicClient clients
*
* @param userId
* @returns {Promise}
*/
removePublicAndCanGrantTokens: function removePublicAndCanGrantTokens(userId) {
const uid = buf(userId);
return this._read(QUERY_PUBLIC_CLIENTS_LIST).then((_clients) => {
const clientIds = _clients.map((client) => client.id);
return this._write(QUERY_DELETE_ACCESS_TOKEN_FOR_PUBLIC_CLIENTS, [uid, clientIds])
.then(() => this._write(QUERY_DELETE_REFRESH_TOKEN_FOR_PUBLIC_CLIENTS, [uid, clientIds]));
});
},
getScope: function getScope (scope) {
return this._readOne(QUERY_SCOPE_FIND, [scope]);
},
registerScope: function registerScope (scope) {
return this._write(QUERY_SCOPES_INSERT, [
scope.scope,
scope.hasScopedKeys
]);
},
_write: function _write(sql, params) {
return this._query(sql, params);
},
_read: function _read(sql, params) {
return this._query(sql, params);
},
_readOne: function _readOne(sql, params) {
return this._read(sql, params).then(firstRow);
},
_getConnection: function _getConnection() {
// see bluebird.using()/disposer():
// https://github.com/petkaantonov/bluebird/blob/master/API.md#resource-management
//
// tl;dr: using() and disposer() ensures that the dispose method will
// ALWAYS be called at the end of the promise stack, regardless of
// various errors thrown. So this should ALWAYS release the connection.
var pool = this._pool;
return new P(function(resolve, reject) {
pool.getConnection(function(err, conn) {
if (err) {
return reject(err);
}
if (conn._fxa_initialized) {
return resolve(conn);
}
// Enforce sane defaults on every new connection.
// These *should* be set by the database by default, but it's nice
// to have an additional layer of protection here.
conn.query('SELECT @@sql_mode AS mode', function(err, rows) {
if (err) {
return reject(err);
}
var modes = rows[0]['mode'].split(',');
var needToSetMode = false;
REQUIRED_SQL_MODES.forEach(function(requiredMode) {
if (modes.indexOf(requiredMode) === -1) {
modes.push(requiredMode);
needToSetMode = true;
}
});
if (! needToSetMode) {
conn._fxa_initialized = true;
return resolve(conn);
}
var mode = modes.join(',');
conn.query('SET SESSION sql_mode = \'' + mode + '\'', function(err) {
if (err) {
return reject(err);
}
conn._fxa_initialized = true;
return resolve(conn);
});
});
});
}).disposer(releaseConn);
},
_query: function _query(sql, params) {
return P.using(this._getConnection(), function(conn) {
return new P(function(resolve, reject) {
conn.query(sql, params || [], function(err, results) {
if (err) {
reject(err);
} else {
resolve(results);
}
});
});
});
}
};
module.exports = MysqlStore;