X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=src%2Fdb%2Fsqlite%2Findex.js;h=2dd75b2214f1962abdb18a541a4628bfeb4d7866;hb=f0bf29c75b0fd405ff92fa76f058e61162b87e43;hp=878a004e3126f67d7b734256881b94fc59314864;hpb=b0103b0d496262c438b40bc20304081dbfe41e73;p=squeep-indie-auther diff --git a/src/db/sqlite/index.js b/src/db/sqlite/index.js index 878a004..2dd75b2 100644 --- a/src/db/sqlite/index.js +++ b/src/db/sqlite/index.js @@ -1,9 +1,10 @@ 'use strict'; const common = require('../../common'); +const Enum = require('../../enum'); const Database = require('../abstract'); const DBErrors = require('../errors'); -const svh = require('../schema-version-helper'); +const { unappliedSchemaVersions } = require('../schema-version-helper'); const SQLite = require('better-sqlite3'); const fs = require('fs'); const path = require('path'); @@ -20,7 +21,7 @@ const schemaVersionsSupported = { }, max: { major: 1, - minor: 0, + minor: 2, patch: 0, }, }; @@ -59,8 +60,8 @@ class DatabaseSQLite extends Database { /** * Boolean to 0/1 representation for SQLite params. - * @param {Boolean} bool - * @returns {Number} + * @param {boolean} bool boolean + * @returns {number} number */ static _booleanToNumeric(bool) { // eslint-disable-next-line security/detect-object-injection @@ -83,7 +84,7 @@ class DatabaseSQLite extends Database { let metaExists = tableExists.get(); if (metaExists === undefined) { const fPath = path.join(__dirname, 'sql', 'schema', 'init.sql'); - // eslint-disable-next-line security/detect-non-literal-fs-filename + const fSql = fs.readFileSync(fPath, { encoding: 'utf8' }); this.db.exec(fSql); metaExists = tableExists.get(); @@ -96,7 +97,7 @@ class DatabaseSQLite extends Database { // Apply migrations const currentSchema = this._currentSchema(); - const migrationsWanted = svh.unappliedSchemaVersions(__dirname, currentSchema, this.schemaVersionsSupported); + const migrationsWanted = unappliedSchemaVersions(__dirname, currentSchema, this.schemaVersionsSupported); this.logger.debug(_scope, 'schema migrations wanted', { migrationsWanted }); migrationsWanted.forEach((v) => { const fPath = path.join(__dirname, 'sql', 'schema', v, 'apply.sql'); @@ -138,7 +139,7 @@ class DatabaseSQLite extends Database { }; }; - // eslint-disable-next-line security/detect-non-literal-fs-filename + for (const f of fs.readdirSync(sqlDir)) { const fPath = path.join(sqlDir, f); const { name: fName, ext: fExt } = path.parse(f); @@ -233,10 +234,13 @@ class DatabaseSQLite extends Database { _purgeTables(really) { if (really) { [ + 'almanac', 'authentication', 'profile', + 'redeemed_ticket', + 'resource', 'token', - ].map((table) => { + ].forEach((table) => { const result = this.db.prepare(`DELETE FROM ${table}`).run(); this.logger.debug(_fileScope('_purgeTables'), 'success', { table, result }); }); @@ -262,6 +266,12 @@ class DatabaseSQLite extends Database { }; } + + static _almanacErrorThrow() { + throw new DBErrors.UnexpectedResult('did not update almanac'); + } + + almanacGetAll(dbCtx) { // eslint-disable-line no-unused-vars const _scope = _fileScope('almanacGetAll'); this.logger.debug(_scope, 'called'); @@ -276,6 +286,23 @@ class DatabaseSQLite extends Database { } + almanacUpsert(dbCtx, event, date) { + const _scope = _fileScope('almanacUpsert'); + this.logger.debug(_scope, 'called', { event, date }); + + try { + const epoch = common.dateToEpoch(date); + const result = this.statement.almanacUpsert.run({ event, epoch }); + if (result.changes != 1) { + this.constructor._almanacErrorThrow(); + } + } catch (e) { + this.logger.error(_scope, 'failed', { error: e, event, date }); + throw e; + } + } + + static _authenticationToNative(authentication) { if (authentication) { authentication.created = new Date(Number(authentication.created) * 1000); @@ -315,17 +342,34 @@ class DatabaseSQLite extends Database { } - authenticationUpsert(dbCtx, identifier, credential) { + authenticationUpsert(dbCtx, identifier, credential, otpKey) { const _scope = _fileScope('authenticationUpsert'); const scrubbedCredential = '*'.repeat((credential || '').length); - this.logger.debug(_scope, 'called', { identifier, scrubbedCredential }); + const scrubbedOTPKey = '*'.repeat((otpKey || '').length); + this.logger.debug(_scope, 'called', { identifier, scrubbedCredential, scrubbedOTPKey }); - let result; try { - result = this.statement.authenticationUpsert.run({ identifier, credential }); + const result = this.statement.authenticationUpsert.run({ identifier, credential, otpKey }); if (result.changes != 1) { throw new DBErrors.UnexpectedResult('did not upsert authentication'); } + } catch (e) { + this.logger.error(_scope, 'failed', { error: e, identifier, scrubbedCredential, scrubbedOTPKey }); + throw e; + } + } + + + authenticationUpdateCredential(dbCtx, identifier, credential) { + const _scope = _fileScope('authenticationUpdateCredential'); + const scrubbedCredential = '*'.repeat((credential || '').length); + this.logger.debug(_scope, 'called', { identifier, scrubbedCredential }); + + try { + const result = this.statement.authenticationUpdateCredential.run({ identifier, credential }); + if (result.changes != 1) { + throw new DBErrors.UnexpectedResult('did not update credential'); + } } catch (e) { this.logger.error(_scope, 'failed', { error: e, identifier, scrubbedCredential }); throw e; @@ -333,6 +377,23 @@ class DatabaseSQLite extends Database { } + authenticationUpdateOTPKey(dbCtx, identifier, otpKey) { + const _scope = _fileScope('authenticationUpdateOTPKey'); + const scrubbedOTPKey = '*'.repeat((otpKey || '').length); + this.logger.debug(_scope, 'called', { identifier, scrubbedOTPKey }); + + try { + const result = this.statement.authenticationUpdateOtpKey.run({ identifier, otpKey }); + if (result.changes != 1) { + throw new DBErrors.UnexpectedResult('did not update otpKey'); + } + } catch (e) { + this.logger.error(_scope, 'failed', { error: e, identifier, scrubbedOTPKey }); + throw e; + } + } + + profileIdentifierInsert(dbCtx, profile, identifier) { const _scope = _fileScope('profileIdentifierInsert'); this.logger.debug(_scope, 'called', { profile, identifier }); @@ -539,7 +600,7 @@ class DatabaseSQLite extends Database { const _scope = _fileScope('scopeCleanup'); this.logger.debug(_scope, 'called', { atLeastMsSinceLast }); - const almanacEvent = 'scopeCleanup'; + const almanacEvent = Enum.AlmanacEntry.ScopeCleanup; try { return this.db.transaction(() => { @@ -558,7 +619,7 @@ class DatabaseSQLite extends Database { // Update the last cleanup time const result = this.statement.almanacUpsert.run({ event: almanacEvent, epoch: nowEpoch }); if (result.changes != 1) { - throw new DBErrors.UnexpectedResult('did not update almanac'); + this.constructor._almanacErrorThrow(); } this.logger.debug(_scope, 'finished', { scopesRemoved, atLeastMsSinceLast }); @@ -617,7 +678,7 @@ class DatabaseSQLite extends Database { const _scope = _fileScope('tokenCleanup'); this.logger.debug(_scope, 'called', { codeLifespanSeconds, atLeastMsSinceLast }); - const almanacEvent = 'tokenCleanup'; + const almanacEvent = Enum.AlmanacEntry.TokenCleanup; try { return this.db.transaction(() => { @@ -636,7 +697,7 @@ class DatabaseSQLite extends Database { // Update the last cleanup time const result = this.statement.almanacUpsert.run({ event: almanacEvent, epoch: nowEpoch }); if (result.changes != 1) { - throw new DBErrors.UnexpectedResult('did not update almanac'); + this.constructor._almanacErrorThrow(); } this.logger.debug(_scope, 'finished', { tokensRemoved, codeLifespanSeconds, atLeastMsSinceLast }); @@ -734,6 +795,66 @@ class DatabaseSQLite extends Database { } } + + ticketRedeemed(dbCtx, redeemedData) { + const _scope = _fileScope('ticketRedeemed'); + this.logger.debug(_scope, 'called', { ...redeemedData }); + + try { + const result = this.statement.ticketRedeemed.run(redeemedData); + if (result.changes != 1) { + throw new DBErrors.UnexpectedResult('did not store redeemed ticket'); + } + } catch (e) { + this.logger.error(_scope, 'failed', { error: e }); + throw e; + } + } + + + ticketTokenPublished(dbCtx, redeemedData) { + const _scope = _fileScope('ticketRedeemed'); + this.logger.debug(_scope, 'called', { ...redeemedData }); + + const almanacEvent = Enum.AlmanacEntry.TicketPublished; + try { + const result = this.statement.ticketTokenPublished.run(redeemedData); + if (result.changes != 1) { + throw new DBErrors.UnexpectedResult('did not store redeemed ticket'); + } + const epoch = common.dateToEpoch(); + const almanacResult = this.statement.almanacUpsert.run({ event: almanacEvent, epoch }); + if (almanacResult.changes != 1) { + this.constructor._almanacErrorThrow(); + } + + } catch (e) { + this.logger.error(_scope, 'failed', { error: e }); + throw e; + } + } + + static _redeemedTicketToNative(redeemedTicket) { + redeemedTicket.created = new Date(Number(redeemedTicket.created) * 1000); + if (redeemedTicket.published) { + redeemedTicket.published = new Date(Number(redeemedTicket.published) * 1000); + } + return redeemedTicket; + } + + ticketTokenGetUnpublished() { + const _scope = _fileScope('ticketTokenGetUnpublished'); + this.logger.debug(_scope, 'called'); + + try { + const unpublished = this.statement.ticketTokenGetUnpublished.all(); + return unpublished.map((x) => DatabaseSQLite._redeemedTicketToNative(x)); + } catch (e) { + this.logger.error(_scope, 'failed', { error: e }); + throw e; + } + } + } -module.exports = DatabaseSQLite; \ No newline at end of file +module.exports = DatabaseSQLite;