@magda/authentication-plugin-sdk 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +305 -264
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -96,12 +96,6 @@ return /******/ (function(modules) { // webpackBootstrap
96
96
  /************************************************************************/
97
97
  /******/ ([
98
98
  /* 0 */
99
- /***/ (function(module, exports) {
100
-
101
- module.exports = require("util");
102
-
103
- /***/ }),
104
- /* 1 */
105
99
  /***/ (function(module, exports, __webpack_require__) {
106
100
 
107
101
  /*!
@@ -116,7 +110,7 @@ module.exports = require("util");
116
110
 
117
111
  var callSiteToString = __webpack_require__(34).callSiteToString
118
112
  var eventListenerCount = __webpack_require__(34).eventListenerCount
119
- var relative = __webpack_require__(2).relative
113
+ var relative = __webpack_require__(1).relative
120
114
 
121
115
  /**
122
116
  * Module exports.
@@ -629,11 +623,17 @@ function DeprecationError (namespace, message, stack) {
629
623
 
630
624
 
631
625
  /***/ }),
632
- /* 2 */
626
+ /* 1 */
633
627
  /***/ (function(module, exports) {
634
628
 
635
629
  module.exports = require("path");
636
630
 
631
+ /***/ }),
632
+ /* 2 */
633
+ /***/ (function(module, exports) {
634
+
635
+ module.exports = require("util");
636
+
637
637
  /***/ }),
638
638
  /* 3 */
639
639
  /***/ (function(module, exports) {
@@ -668,7 +668,7 @@ module.exports = require("buffer");
668
668
  var Buffer = __webpack_require__(55).Buffer
669
669
  var contentDisposition = __webpack_require__(56);
670
670
  var contentType = __webpack_require__(16);
671
- var deprecate = __webpack_require__(1)('express');
671
+ var deprecate = __webpack_require__(0)('express');
672
672
  var flatten = __webpack_require__(23);
673
673
  var mime = __webpack_require__(32).mime;
674
674
  var etag = __webpack_require__(58);
@@ -2076,7 +2076,7 @@ function ContentType (type) {
2076
2076
  * @private
2077
2077
  */
2078
2078
 
2079
- var deprecate = __webpack_require__(1)('http-errors')
2079
+ var deprecate = __webpack_require__(0)('http-errors')
2080
2080
  var setPrototypeOf = __webpack_require__(10)
2081
2081
  var statuses = __webpack_require__(11)
2082
2082
  var inherits = __webpack_require__(81)
@@ -3372,7 +3372,7 @@ function getBasicNodeMethods() {
3372
3372
 
3373
3373
  var createError = __webpack_require__(133)
3374
3374
  var debug = __webpack_require__(136)('send')
3375
- var deprecate = __webpack_require__(1)('send')
3375
+ var deprecate = __webpack_require__(0)('send')
3376
3376
  var destroy = __webpack_require__(140)
3377
3377
  var encodeUrl = __webpack_require__(21)
3378
3378
  var escapeHtml = __webpack_require__(22)
@@ -3383,10 +3383,10 @@ var mime = __webpack_require__(141)
3383
3383
  var ms = __webpack_require__(143)
3384
3384
  var onFinished = __webpack_require__(20)
3385
3385
  var parseRange = __webpack_require__(60)
3386
- var path = __webpack_require__(2)
3386
+ var path = __webpack_require__(1)
3387
3387
  var statuses = __webpack_require__(11)
3388
3388
  var Stream = __webpack_require__(13)
3389
- var util = __webpack_require__(0)
3389
+ var util = __webpack_require__(2)
3390
3390
 
3391
3391
  /**
3392
3392
  * Path function references.
@@ -5111,7 +5111,7 @@ function unpipe(stream) {
5111
5111
  */
5112
5112
 
5113
5113
  var db = __webpack_require__(108)
5114
- var extname = __webpack_require__(2).extname
5114
+ var extname = __webpack_require__(1).extname
5115
5115
 
5116
5116
  /**
5117
5117
  * Module variables.
@@ -5793,7 +5793,7 @@ var Layer = __webpack_require__(50);
5793
5793
  var methods = __webpack_require__(31);
5794
5794
  var mixin = __webpack_require__(25);
5795
5795
  var debug = __webpack_require__(15)('express:router');
5796
- var deprecate = __webpack_require__(1)('express');
5796
+ var deprecate = __webpack_require__(0)('express');
5797
5797
  var flatten = __webpack_require__(23);
5798
5798
  var parseUrl = __webpack_require__(8);
5799
5799
  var setPrototypeOf = __webpack_require__(10)
@@ -7484,7 +7484,7 @@ module.exports.parse = parse
7484
7484
  * @private
7485
7485
  */
7486
7486
 
7487
- var basename = __webpack_require__(2).basename
7487
+ var basename = __webpack_require__(1).basename
7488
7488
  var Buffer = __webpack_require__(132).Buffer
7489
7489
 
7490
7490
  /**
@@ -9138,7 +9138,7 @@ function coerce(val) {
9138
9138
  * Module dependencies.
9139
9139
  */
9140
9140
 
9141
- var relative = __webpack_require__(2).relative
9141
+ var relative = __webpack_require__(1).relative
9142
9142
 
9143
9143
  /**
9144
9144
  * Module exports.
@@ -9847,7 +9847,7 @@ Cookie.prototype = {
9847
9847
  var Cookie = __webpack_require__(64)
9848
9848
  var EventEmitter = __webpack_require__(29).EventEmitter
9849
9849
  var Session = __webpack_require__(66)
9850
- var util = __webpack_require__(0)
9850
+ var util = __webpack_require__(2)
9851
9851
 
9852
9852
  /**
9853
9853
  * Module exports.
@@ -13797,7 +13797,7 @@ removedMiddlewares.forEach(function (name) {
13797
13797
  * @private
13798
13798
  */
13799
13799
 
13800
- var deprecate = __webpack_require__(1)('body-parser')
13800
+ var deprecate = __webpack_require__(0)('body-parser')
13801
13801
 
13802
13802
  /**
13803
13803
  * Cache of loaded parsers.
@@ -14330,7 +14330,7 @@ module.exports = JSON.parse("{\"100\":\"Continue\",\"101\":\"Switching Protocols
14330
14330
  /***/ (function(module, exports, __webpack_require__) {
14331
14331
 
14332
14332
  try {
14333
- var util = __webpack_require__(0);
14333
+ var util = __webpack_require__(2);
14334
14334
  if (typeof util.inherits !== 'function') throw '';
14335
14335
  module.exports = util.inherits;
14336
14336
  } catch (e) {
@@ -14725,7 +14725,7 @@ function plural(ms, n, name) {
14725
14725
  */
14726
14726
 
14727
14727
  var tty = __webpack_require__(12);
14728
- var util = __webpack_require__(0);
14728
+ var util = __webpack_require__(2);
14729
14729
 
14730
14730
  /**
14731
14731
  * This is the Node.js implementation of `debug()`.
@@ -18543,7 +18543,7 @@ var bytes = __webpack_require__(9)
18543
18543
  var contentType = __webpack_require__(16)
18544
18544
  var createError = __webpack_require__(17)
18545
18545
  var debug = __webpack_require__(18)('body-parser:urlencoded')
18546
- var deprecate = __webpack_require__(1)('body-parser')
18546
+ var deprecate = __webpack_require__(0)('body-parser')
18547
18547
  var read = __webpack_require__(19)
18548
18548
  var typeis = __webpack_require__(14)
18549
18549
 
@@ -19452,10 +19452,10 @@ var http = __webpack_require__(24);
19452
19452
  var compileETag = __webpack_require__(5).compileETag;
19453
19453
  var compileQueryParser = __webpack_require__(5).compileQueryParser;
19454
19454
  var compileTrust = __webpack_require__(5).compileTrust;
19455
- var deprecate = __webpack_require__(1)('express');
19455
+ var deprecate = __webpack_require__(0)('express');
19456
19456
  var flatten = __webpack_require__(23);
19457
19457
  var merge = __webpack_require__(25);
19458
- var resolve = __webpack_require__(2).resolve;
19458
+ var resolve = __webpack_require__(1).resolve;
19459
19459
  var setPrototypeOf = __webpack_require__(10)
19460
19460
  var slice = Array.prototype.slice;
19461
19461
 
@@ -20784,7 +20784,7 @@ function plural(ms, n, name) {
20784
20784
  */
20785
20785
 
20786
20786
  var tty = __webpack_require__(12);
20787
- var util = __webpack_require__(0);
20787
+ var util = __webpack_require__(2);
20788
20788
 
20789
20789
  /**
20790
20790
  * This is the Node.js implementation of `debug()`.
@@ -21387,7 +21387,7 @@ function plural(ms, n, name) {
21387
21387
  */
21388
21388
 
21389
21389
  var tty = __webpack_require__(12);
21390
- var util = __webpack_require__(0);
21390
+ var util = __webpack_require__(2);
21391
21391
 
21392
21392
  /**
21393
21393
  * This is the Node.js implementation of `debug()`.
@@ -22363,7 +22363,7 @@ module.exports = function (str, opts) {
22363
22363
  */
22364
22364
 
22365
22365
  var debug = __webpack_require__(15)('express:view');
22366
- var path = __webpack_require__(2);
22366
+ var path = __webpack_require__(1);
22367
22367
  var fs = __webpack_require__(3);
22368
22368
 
22369
22369
  /**
@@ -22632,7 +22632,7 @@ SafeBuffer.allocUnsafeSlow = function (size) {
22632
22632
  * @private
22633
22633
  */
22634
22634
 
22635
- var deprecate = __webpack_require__(1)('http-errors')
22635
+ var deprecate = __webpack_require__(0)('http-errors')
22636
22636
  var setPrototypeOf = __webpack_require__(10)
22637
22637
  var statuses = __webpack_require__(11)
22638
22638
  var inherits = __webpack_require__(134)
@@ -22891,7 +22891,7 @@ function populateConstructorExports (exports, codes, HttpError) {
22891
22891
  /***/ (function(module, exports, __webpack_require__) {
22892
22892
 
22893
22893
  try {
22894
- var util = __webpack_require__(0);
22894
+ var util = __webpack_require__(2);
22895
22895
  /* istanbul ignore next */
22896
22896
  if (typeof util.inherits !== 'function') throw '';
22897
22897
  module.exports = util.inherits;
@@ -23308,7 +23308,7 @@ function plural(ms, n, name) {
23308
23308
  */
23309
23309
 
23310
23310
  var tty = __webpack_require__(12);
23311
- var util = __webpack_require__(0);
23311
+ var util = __webpack_require__(2);
23312
23312
 
23313
23313
  /**
23314
23314
  * This is the Node.js implementation of `debug()`.
@@ -23639,7 +23639,7 @@ function onOpenClose() {
23639
23639
  /* 141 */
23640
23640
  /***/ (function(module, exports, __webpack_require__) {
23641
23641
 
23642
- var path = __webpack_require__(2);
23642
+ var path = __webpack_require__(1);
23643
23643
  var fs = __webpack_require__(3);
23644
23644
 
23645
23645
  function Mime() {
@@ -24707,7 +24707,7 @@ function parse (header) {
24707
24707
  */
24708
24708
 
24709
24709
  var accepts = __webpack_require__(147);
24710
- var deprecate = __webpack_require__(1)('express');
24710
+ var deprecate = __webpack_require__(0)('express');
24711
24711
  var isIP = __webpack_require__(6).isIP;
24712
24712
  var typeis = __webpack_require__(14);
24713
24713
  var http = __webpack_require__(24);
@@ -26469,13 +26469,13 @@ function splitParameters(str) {
26469
26469
 
26470
26470
  var Buffer = __webpack_require__(55).Buffer
26471
26471
  var contentDisposition = __webpack_require__(56);
26472
- var deprecate = __webpack_require__(1)('express');
26472
+ var deprecate = __webpack_require__(0)('express');
26473
26473
  var encodeUrl = __webpack_require__(21);
26474
26474
  var escapeHtml = __webpack_require__(22);
26475
26475
  var http = __webpack_require__(24);
26476
26476
  var isAbsolute = __webpack_require__(5).isAbsolute;
26477
26477
  var onFinished = __webpack_require__(20);
26478
- var path = __webpack_require__(2);
26478
+ var path = __webpack_require__(1);
26479
26479
  var statuses = __webpack_require__(11)
26480
26480
  var merge = __webpack_require__(25);
26481
26481
  var sign = __webpack_require__(27).sign;
@@ -27776,7 +27776,7 @@ function vary (res, field) {
27776
27776
  var encodeUrl = __webpack_require__(21)
27777
27777
  var escapeHtml = __webpack_require__(22)
27778
27778
  var parseUrl = __webpack_require__(8)
27779
- var resolve = __webpack_require__(2).resolve
27779
+ var resolve = __webpack_require__(1).resolve
27780
27780
  var send = __webpack_require__(32)
27781
27781
  var url = __webpack_require__(46)
27782
27782
 
@@ -29102,7 +29102,7 @@ function plural(ms, n, name) {
29102
29102
  */
29103
29103
 
29104
29104
  var tty = __webpack_require__(12);
29105
- var util = __webpack_require__(0);
29105
+ var util = __webpack_require__(2);
29106
29106
 
29107
29107
  /**
29108
29108
  * This is the Node.js implementation of `debug()`.
@@ -29729,7 +29729,7 @@ function generateRandomBytes(size, attempts, callback) {
29729
29729
  */
29730
29730
 
29731
29731
  var Store = __webpack_require__(65)
29732
- var util = __webpack_require__(0)
29732
+ var util = __webpack_require__(2)
29733
29733
 
29734
29734
  /**
29735
29735
  * Shim setImmediate for node.js < 0.10
@@ -47474,286 +47474,327 @@ function signedCookies (obj, secret) {
47474
47474
  /***/ (function(module, exports, __webpack_require__) {
47475
47475
 
47476
47476
  "use strict";
47477
+ // @ts-check
47478
+ /// <reference types="node" />
47477
47479
 
47478
47480
 
47479
- const util = __webpack_require__(0);
47480
- const oneDay = 86400;
47481
- const DEFAULT_PRUNE_INTERVAL_IN_SECONDS = 60 * 5;
47482
47481
 
47483
- const currentTimestamp = function () {
47484
- return Math.ceil(Date.now() / 1000);
47485
- };
47482
+ const DEFAULT_PRUNE_INTERVAL_IN_SECONDS = 60 * 15;
47483
+ const ONE_DAY = 86400;
47484
+
47485
+ /** @typedef {*} ExpressSession */
47486
+ /** @typedef {*} ExpressSessionStore */
47487
+ /** @typedef {import('pg').Pool} Pool */
47488
+
47489
+ /** @returns {number} */
47490
+ const currentTimestamp = () => Math.ceil(Date.now() / 1000);
47486
47491
 
47487
47492
  /**
47488
- * See
47489
47493
  * @see https://www.postgresql.org/docs/9.5/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
47494
+ * @param {string} value
47495
+ * @returns {string}
47490
47496
  */
47491
47497
  const escapePgIdentifier = (value) => value.replace(/"/g, '""');
47492
47498
 
47493
- module.exports = function (session) {
47494
- const Store = session.Store || session.session.Store;
47499
+ /** @typedef {(err: Error|null) => void} SimpleErrorCallback */
47495
47500
 
47496
- const PGStore = function (options) {
47497
- options = options || {};
47498
- Store.call(this, options);
47501
+ /** @typedef {{ cookie: { maxAge: number, [property: string]: any }, [property: string]: any }} SessionObject */
47499
47502
 
47500
- this.schemaName = options.schemaName ? escapePgIdentifier(options.schemaName) : null;
47501
- this.tableName = options.tableName ? escapePgIdentifier(options.tableName) : 'session';
47503
+ /** @typedef {(delay: number) => number} PGStorePruneDelayRandomizer */
47504
+ /** @typedef {Object<string, any>} PGStoreQueryResult */
47505
+ /** @typedef {(err: Error|null, firstRow?: PGStoreQueryResult) => void} PGStoreQueryCallback */
47502
47506
 
47503
- if (!this.schemaName && this.tableName.includes('"."')) {
47504
- console.warn('DEPRECATION WARNING: Schema should be provided through its dedicated "schemaName" option rather than through "tableName"');
47505
- this.tableName = this.tableName.replace(/^([^"]+)""\.""([^"]+)$/, '$1"."$2');
47506
- }
47507
+ /**
47508
+ * @typedef PGStoreOptions
47509
+ * @property {string} [schemaName]
47510
+ * @property {string} [tableName]
47511
+ * @property {number} [ttl]
47512
+ * @property {typeof console.error} [errorLog]
47513
+ * @property {Pool} [pool]
47514
+ * @property {*} [pgPromise]
47515
+ * @property {string} [conString]
47516
+ * @property {*} [conObject]
47517
+ * @property {false|number} [pruneSessionInterval]
47518
+ * @property {false|PGStorePruneDelayRandomizer} [pruneSessionRandomizedInterval]
47519
+ */
47507
47520
 
47508
- this.ttl = options.ttl;
47521
+ /**
47522
+ * @param {ExpressSession} session
47523
+ * @returns {ExpressSessionStore}
47524
+ */
47525
+ module.exports = function (session) {
47526
+ /** @type {ExpressSessionStore} */
47527
+ const Store = session.Store ||
47528
+ // @ts-ignore
47529
+ session.session.Store;
47509
47530
 
47510
- this.errorLog = options.errorLog || console.error.bind(console);
47531
+ class PGStore extends Store {
47532
+ /** @param {PGStoreOptions} options */
47533
+ constructor (options = {}) {
47534
+ super(options);
47511
47535
 
47512
- if (options.pool !== undefined) {
47513
- this.pool = options.pool;
47514
- this.ownsPg = false;
47515
- } else if (options.pgPromise !== undefined) {
47516
- if (typeof options.pgPromise.query !== 'function') {
47517
- throw new Error('`pgPromise` config must point to an existing and configured instance of pg-promise pointing at your database');
47518
- }
47519
- this.pgPromise = options.pgPromise;
47520
- this.ownsPg = false;
47521
- } else {
47522
- const conString = options.conString || process.env.DATABASE_URL;
47523
- let conObject = options.conObject;
47524
-
47525
- if (!conObject) {
47526
- conObject = {};
47536
+ this.schemaName = options.schemaName ? escapePgIdentifier(options.schemaName) : undefined;
47537
+ /** @type {string} */
47538
+ this.tableName = options.tableName ? escapePgIdentifier(options.tableName) : 'session';
47527
47539
 
47528
- if (conString) {
47529
- conObject.connectionString = conString;
47530
- }
47540
+ if (!this.schemaName && this.tableName.includes('"."')) {
47541
+ console.warn('DEPRECATION WARNING: Schema should be provided through its dedicated "schemaName" option rather than through "tableName"');
47542
+ this.tableName = this.tableName.replace(/^([^"]+)""\.""([^"]+)$/, '$1"."$2');
47531
47543
  }
47532
47544
 
47533
- this.pool = new (__webpack_require__(71)).Pool(conObject);
47534
- this.pool.on('error', err => {
47535
- this.errorLog('PG Pool error:', err.message);
47536
- });
47537
- this.ownsPg = true;
47538
- }
47545
+ this.ttl = options.ttl;
47539
47546
 
47540
- if (options.pruneSessionInterval === false) {
47541
- this.pruneSessionInterval = false;
47542
- } else {
47543
- this.pruneSessionInterval = (options.pruneSessionInterval || DEFAULT_PRUNE_INTERVAL_IN_SECONDS) * 1000;
47544
- if (options.pruneSessionRandomizedInterval !== false) {
47545
- this.pruneSessionRandomizedInterval = (
47546
- options.pruneSessionRandomizedInterval ||
47547
- // Results in at least 50% of the specified interval and at most 150%. Makes it so that multiple instances doesn't all prune at the same time.
47548
- (delay => Math.ceil(delay / 2 + delay * Math.random()))
47549
- );
47550
- }
47551
- setImmediate(() => { this.pruneSessions(); });
47552
- }
47553
- };
47547
+ this.errorLog = options.errorLog || console.error.bind(console);
47554
47548
 
47555
- /**
47556
- * Inherit from `Store`.
47557
- */
47549
+ if (options.pool !== undefined) {
47550
+ this.pool = options.pool;
47551
+ this.ownsPg = false;
47552
+ } else if (options.pgPromise !== undefined) {
47553
+ if (typeof options.pgPromise.query !== 'function') {
47554
+ throw new TypeError('`pgPromise` config must point to an existing and configured instance of pg-promise pointing at your database');
47555
+ }
47556
+ this.pgPromise = options.pgPromise;
47557
+ this.ownsPg = false;
47558
+ } else {
47559
+ const conString = options.conString || process.env.DATABASE_URL;
47560
+ let conObject = options.conObject;
47558
47561
 
47559
- util.inherits(PGStore, Store);
47562
+ if (!conObject) {
47563
+ conObject = {};
47560
47564
 
47561
- /**
47562
- * Closes the session store
47563
- *
47564
- * Currently only stops the automatic pruning, if any, from continuing
47565
- *
47566
- * @access public
47567
- */
47565
+ if (conString) {
47566
+ conObject.connectionString = conString;
47567
+ }
47568
+ }
47568
47569
 
47569
- PGStore.prototype.close = function () {
47570
- this.closed = true;
47570
+ this.pool = new (__webpack_require__(71)).Pool(conObject);
47571
+ this.pool.on('error', err => {
47572
+ this.errorLog('PG Pool error:', err.message);
47573
+ });
47574
+ this.ownsPg = true;
47575
+ }
47571
47576
 
47572
- if (this.pruneTimer) {
47573
- clearTimeout(this.pruneTimer);
47574
- this.pruneTimer = undefined;
47577
+ if (options.pruneSessionInterval === false) {
47578
+ /** @type {false|number} */
47579
+ this.pruneSessionInterval = false;
47580
+ } else {
47581
+ /** @type {false|number} */
47582
+ this.pruneSessionInterval = (options.pruneSessionInterval || DEFAULT_PRUNE_INTERVAL_IN_SECONDS) * 1000;
47583
+ if (options.pruneSessionRandomizedInterval !== false) {
47584
+ this.pruneSessionRandomizedInterval = (
47585
+ options.pruneSessionRandomizedInterval ||
47586
+ // Results in at least 50% of the specified interval and at most 150%. Makes it so that multiple instances doesn't all prune at the same time.
47587
+ (delay => Math.ceil(delay / 2 + delay * Math.random()))
47588
+ );
47589
+ }
47590
+ setImmediate(() => { this.pruneSessions(); });
47591
+ }
47575
47592
  }
47576
47593
 
47577
- if (this.ownsPg) {
47578
- this.pool.end();
47579
- }
47580
- };
47594
+ /**
47595
+ * Closes the session store
47596
+ *
47597
+ * Currently only stops the automatic pruning, if any, from continuing
47598
+ *
47599
+ * @access public
47600
+ */
47601
+ close () {
47602
+ this.closed = true;
47581
47603
 
47582
- /**
47583
- * Get a new prune delay
47584
- *
47585
- * @return {number} the quoted schema + table for use in queries
47586
- * @access private
47587
- */
47604
+ if (this.pruneTimer) {
47605
+ clearTimeout(this.pruneTimer);
47606
+ this.pruneTimer = undefined;
47607
+ }
47588
47608
 
47589
- PGStore.prototype.getPruneDelay = function () {
47590
- const delay = this.pruneSessionInterval;
47609
+ if (this.ownsPg) {
47610
+ this.pool.end();
47611
+ }
47612
+ }
47591
47613
 
47592
- if (!delay) throw new Error('Can not calculate delay when pruning is inactivated');
47593
- if (this.pruneSessionRandomizedInterval) return this.pruneSessionRandomizedInterval(delay);
47614
+ /**
47615
+ * Get a new prune delay
47616
+ *
47617
+ * @returns {number} the quoted schema + table for use in queries
47618
+ * @access private
47619
+ */
47620
+ getPruneDelay () {
47621
+ const delay = this.pruneSessionInterval;
47594
47622
 
47595
- return delay;
47596
- };
47623
+ if (!delay) throw new Error('Can not calculate delay when pruning is inactivated');
47624
+ if (this.pruneSessionRandomizedInterval) return this.pruneSessionRandomizedInterval(delay);
47597
47625
 
47598
- /**
47599
- * Does garbage collection for expired session in the database
47600
- *
47601
- * @param {Function} [fn] - standard Node.js callback called on completion
47602
- * @access public
47603
- */
47626
+ return delay;
47627
+ }
47604
47628
 
47605
- PGStore.prototype.pruneSessions = function (fn) {
47606
- this.query('DELETE FROM ' + this.quotedTable() + ' WHERE expire < to_timestamp($1)', [currentTimestamp()], err => {
47607
- if (fn && typeof fn === 'function') {
47608
- return fn(err);
47609
- }
47629
+ /**
47630
+ * Does garbage collection for expired session in the database
47631
+ *
47632
+ * @param {SimpleErrorCallback} [fn] - standard Node.js callback called on completion
47633
+ * @access public
47634
+ */
47635
+ pruneSessions (fn) {
47636
+ this.query('DELETE FROM ' + this.quotedTable() + ' WHERE expire < to_timestamp($1)', [currentTimestamp()], err => {
47637
+ if (fn && typeof fn === 'function') {
47638
+ return fn(err);
47639
+ }
47610
47640
 
47611
- if (err) {
47612
- this.errorLog('Failed to prune sessions:', err.message);
47613
- }
47641
+ if (err) {
47642
+ this.errorLog('Failed to prune sessions:', err.message);
47643
+ }
47614
47644
 
47615
- if (this.pruneSessionInterval && !this.closed) {
47616
- if (this.pruneTimer) {
47617
- clearTimeout(this.pruneTimer);
47645
+ if (this.pruneSessionInterval && !this.closed) {
47646
+ if (this.pruneTimer) {
47647
+ clearTimeout(this.pruneTimer);
47648
+ }
47649
+ this.pruneTimer = setTimeout(
47650
+ () => { this.pruneSessions(); },
47651
+ this.getPruneDelay()
47652
+ );
47653
+ this.pruneTimer.unref();
47618
47654
  }
47619
- this.pruneTimer = setTimeout(
47620
- () => { this.pruneSessions(); },
47621
- this.getPruneDelay()
47622
- );
47623
- this.pruneTimer.unref();
47624
- }
47625
- });
47626
- };
47655
+ });
47656
+ }
47627
47657
 
47628
- /**
47629
- * Get the quoted table.
47630
- *
47631
- * @return {String} the quoted schema + table for use in queries
47632
- * @access private
47633
- */
47658
+ /**
47659
+ * Get the quoted table.
47660
+ *
47661
+ * @returns {string} the quoted schema + table for use in queries
47662
+ * @access private
47663
+ */
47664
+ quotedTable () {
47665
+ let result = '"' + this.tableName + '"';
47634
47666
 
47635
- PGStore.prototype.quotedTable = function () {
47636
- let result = '"' + this.tableName + '"';
47667
+ if (this.schemaName) {
47668
+ result = '"' + this.schemaName + '".' + result;
47669
+ }
47637
47670
 
47638
- if (this.schemaName) {
47639
- result = '"' + this.schemaName + '".' + result;
47671
+ return result;
47640
47672
  }
47641
47673
 
47642
- return result;
47643
- };
47644
-
47645
- /**
47646
- * Figure out when a session should expire
47647
- *
47648
- * @param {Number} [maxAge] - the maximum age of the session cookie
47649
- * @return {Number} the unix timestamp, in seconds
47650
- * @access private
47651
- */
47652
-
47653
- PGStore.prototype.getExpireTime = function (maxAge) {
47654
- let ttl = this.ttl;
47674
+ /**
47675
+ * Figure out when a session should expire
47676
+ *
47677
+ * @param {number} [maxAge] - the maximum age of the session cookie
47678
+ * @returns {number} the unix timestamp, in seconds
47679
+ * @access private
47680
+ */
47681
+ getExpireTime (maxAge) {
47682
+ let ttl = this.ttl;
47655
47683
 
47656
- ttl = ttl || (typeof maxAge === 'number' ? maxAge / 1000 : oneDay);
47657
- ttl = Math.ceil(ttl + currentTimestamp());
47684
+ ttl = ttl || (typeof maxAge === 'number' ? maxAge / 1000 : ONE_DAY);
47685
+ ttl = Math.ceil(ttl + currentTimestamp());
47658
47686
 
47659
- return ttl;
47660
- };
47687
+ return ttl;
47688
+ }
47661
47689
 
47662
- /**
47663
- * Query the database.
47664
- *
47665
- * @param {String} query - the database query to perform
47666
- * @param {(Array|Function)} [params] - the parameters of the query or the callback function
47667
- * @param {Function} [fn] - standard Node.js callback returning the resulting rows
47668
- * @access private
47669
- */
47690
+ /**
47691
+ * Query the database.
47692
+ *
47693
+ * @param {string} query - the database query to perform
47694
+ * @param {any[]|PGStoreQueryCallback} [params] - the parameters of the query or the callback function
47695
+ * @param {PGStoreQueryCallback} [fn] - standard Node.js callback returning the resulting rows
47696
+ * @access private
47697
+ */
47698
+ query (query, params, fn) {
47699
+ if (typeof params === 'function') {
47700
+ if (fn) throw new Error('Two callback functions set at once');
47701
+ fn = params;
47702
+ params = [];
47703
+ }
47670
47704
 
47671
- PGStore.prototype.query = function (query, params, fn) {
47672
- if (!fn && typeof params === 'function') {
47673
- fn = params;
47674
- params = [];
47705
+ if (this.pgPromise) {
47706
+ this.pgPromise.query(query, params || [])
47707
+ .then(
47708
+ /** @param {PGStoreQueryResult} res */
47709
+ // eslint-disable-next-line unicorn/no-null
47710
+ res => { fn && fn(null, res && res[0] ? res[0] : undefined); }
47711
+ )
47712
+ .catch(
47713
+ /** @param {Error} err */
47714
+ err => { fn && fn(err); }
47715
+ );
47716
+ } else {
47717
+ this.pool.query(
47718
+ query,
47719
+ params || [],
47720
+ (err, res) => { fn && fn(err, res && res.rows[0] ? res.rows[0] : undefined); }
47721
+ );
47722
+ }
47675
47723
  }
47676
47724
 
47677
- if (this.pgPromise) {
47678
- this.pgPromise.query(query, params || [])
47679
- .then(function (res) { fn && fn(null, res && res[0] ? res[0] : false); })
47680
- .catch(function (err) { fn && fn(err, false); });
47681
- } else {
47682
- this.pool.query(query, params || [], function (err, res) {
47683
- if (fn) { fn(err, res && res.rows[0] ? res.rows[0] : false); }
47725
+ /**
47726
+ * Attempt to fetch session by the given `sid`.
47727
+ *
47728
+ * @param {string} sid the session id
47729
+ * @param {(err: Error|null, firstRow?: PGStoreQueryResult) => void} fn – a standard Node.js callback returning the parsed session object
47730
+ * @access public
47731
+ */
47732
+ get (sid, fn) {
47733
+ this.query('SELECT sess FROM ' + this.quotedTable() + ' WHERE sid = $1 AND expire >= to_timestamp($2)', [sid, currentTimestamp()], (err, data) => {
47734
+ if (err) { return fn(err); }
47735
+ // eslint-disable-next-line unicorn/no-null
47736
+ if (!data) { return fn(null); }
47737
+ try {
47738
+ // eslint-disable-next-line unicorn/no-null
47739
+ return fn(null, (typeof data.sess === 'string') ? JSON.parse(data.sess) : data.sess);
47740
+ } catch (err_) {
47741
+ return this.destroy(sid, fn);
47742
+ }
47684
47743
  });
47685
47744
  }
47686
- };
47687
-
47688
- /**
47689
- * Attempt to fetch session by the given `sid`.
47690
- *
47691
- * @param {String} sid – the session id
47692
- * @param {Function} fn – a standard Node.js callback returning the parsed session object
47693
- * @access public
47694
- */
47695
-
47696
- PGStore.prototype.get = function (sid, fn) {
47697
- this.query('SELECT sess FROM ' + this.quotedTable() + ' WHERE sid = $1 AND expire >= to_timestamp($2)', [sid, currentTimestamp()], (err, data) => {
47698
- if (err) { return fn(err); }
47699
- if (!data) { return fn(); }
47700
- try {
47701
- return fn(null, (typeof data.sess === 'string') ? JSON.parse(data.sess) : data.sess);
47702
- } catch (e) {
47703
- return this.destroy(sid, fn);
47704
- }
47705
- });
47706
- };
47707
-
47708
- /**
47709
- * Commit the given `sess` object associated with the given `sid`.
47710
- *
47711
- * @param {String} sid – the session id
47712
- * @param {Object} sess – the session object to store
47713
- * @param {Function} fn – a standard Node.js callback returning the parsed session object
47714
- * @access public
47715
- */
47716
-
47717
- PGStore.prototype.set = function (sid, sess, fn) {
47718
- const expireTime = this.getExpireTime(sess.cookie.maxAge);
47719
- const query = 'INSERT INTO ' + this.quotedTable() + ' (sess, expire, sid) SELECT $1, to_timestamp($2), $3 ON CONFLICT (sid) DO UPDATE SET sess=$1, expire=to_timestamp($2) RETURNING sid';
47720
-
47721
- this.query(query, [sess, expireTime, sid], function (err) {
47722
- if (fn) { fn.call(this, err); }
47723
- });
47724
- };
47725
47745
 
47726
- /**
47727
- * Destroy the session associated with the given `sid`.
47728
- *
47729
- * @param {String} sid – the session id
47730
- * @access public
47731
- */
47746
+ /**
47747
+ * Commit the given `sess` object associated with the given `sid`.
47748
+ *
47749
+ * @param {string} sid – the session id
47750
+ * @param {SessionObject} sess – the session object to store
47751
+ * @param {SimpleErrorCallback} fn – a standard Node.js callback returning the parsed session object
47752
+ * @access public
47753
+ */
47754
+ set (sid, sess, fn) {
47755
+ const expireTime = this.getExpireTime(sess.cookie.maxAge);
47756
+ const query = 'INSERT INTO ' + this.quotedTable() + ' (sess, expire, sid) SELECT $1, to_timestamp($2), $3 ON CONFLICT (sid) DO UPDATE SET sess=$1, expire=to_timestamp($2) RETURNING sid';
47732
47757
 
47733
- PGStore.prototype.destroy = function (sid, fn) {
47734
- this.query('DELETE FROM ' + this.quotedTable() + ' WHERE sid = $1', [sid], function (err) {
47735
- if (fn) { fn(err); }
47736
- });
47737
- };
47758
+ this.query(
47759
+ query,
47760
+ [sess, expireTime, sid],
47761
+ err => { fn && fn(err); }
47762
+ );
47763
+ }
47738
47764
 
47739
- /**
47740
- * Touch the given session object associated with the given session ID.
47741
- *
47742
- * @param {String} sid – the session id
47743
- * @param {Object} sess – the session object to store
47744
- * @param {Function} fn – a standard Node.js callback returning the parsed session object
47745
- * @access public
47746
- */
47765
+ /**
47766
+ * Destroy the session associated with the given `sid`.
47767
+ *
47768
+ * @param {string} sid – the session id
47769
+ * @param {SimpleErrorCallback} fna standard Node.js callback returning the parsed session object
47770
+ * @access public
47771
+ */
47772
+ destroy (sid, fn) {
47773
+ this.query(
47774
+ 'DELETE FROM ' + this.quotedTable() + ' WHERE sid = $1',
47775
+ [sid],
47776
+ err => { fn && fn(err); }
47777
+ );
47778
+ }
47747
47779
 
47748
- PGStore.prototype.touch = function (sid, sess, fn) {
47749
- const expireTime = this.getExpireTime(sess.cookie.maxAge);
47780
+ /**
47781
+ * Touch the given session object associated with the given session ID.
47782
+ *
47783
+ * @param {string} sid – the session id
47784
+ * @param {SessionObject} sess – the session object to store
47785
+ * @param {SimpleErrorCallback} fn – a standard Node.js callback returning the parsed session object
47786
+ * @access public
47787
+ */
47788
+ touch (sid, sess, fn) {
47789
+ const expireTime = this.getExpireTime(sess.cookie.maxAge);
47750
47790
 
47751
- this.query(
47752
- 'UPDATE ' + this.quotedTable() + ' SET expire = to_timestamp($1) WHERE sid = $2 RETURNING sid',
47753
- [expireTime, sid],
47754
- function (err) { fn(err); }
47755
- );
47756
- };
47791
+ this.query(
47792
+ 'UPDATE ' + this.quotedTable() + ' SET expire = to_timestamp($1) WHERE sid = $2 RETURNING sid',
47793
+ [expireTime, sid],
47794
+ err => { fn && fn(err); }
47795
+ );
47796
+ }
47797
+ }
47757
47798
 
47758
47799
  return PGStore;
47759
47800
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@magda/authentication-plugin-sdk",
3
3
  "description": "MAGDA Authentication Plugin SDK",
4
- "version": "2.0.0",
4
+ "version": "2.0.1",
5
5
  "scripts": {
6
6
  "prebuild": "rimraf dist tsconfig.tsbuildinfo",
7
7
  "build": "webpack && api-extractor run -l",
@@ -12,8 +12,8 @@
12
12
  "main": "dist/index.js",
13
13
  "types": "dist/index.d.ts",
14
14
  "devDependencies": {
15
- "@magda/auth-api-client": "^2.0.0",
16
- "@magda/typescript-common": "^2.0.0",
15
+ "@magda/auth-api-client": "^2.0.1",
16
+ "@magda/typescript-common": "^2.0.1",
17
17
  "@types/express": "^4.17.6",
18
18
  "@types/express-session": "^1.15.15",
19
19
  "@types/lodash": "^4.14.74",
@@ -26,7 +26,7 @@
26
26
  "webpack-cli": "^3.3.10"
27
27
  },
28
28
  "dependencies": {
29
- "connect-pg-simple": "^6.0.1",
29
+ "connect-pg-simple": "^6.2.1",
30
30
  "cookie-parser": "^1.4.5",
31
31
  "express": "^4.17.1",
32
32
  "express-session": "^1.17.1",