connect-dynamodb
Advanced tools
Comparing version 2.0.4 to 2.0.5
@@ -0,1 +1,5 @@ | ||
2.0.5 / 2022-07-25 | ||
* Add option to prevent missing special keys exception (https://github.com/ca98am79/connect-dynamodb/issues/74) | ||
2.0.4 / 2019-11-15 | ||
@@ -2,0 +6,0 @@ |
@@ -38,3 +38,2 @@ /*! | ||
*/ | ||
function DynamoDBStore(options) { | ||
@@ -45,17 +44,18 @@ options = options || {}; | ||
this.hashKey = null == options.hashKey ? 'id' : options.hashKey; | ||
this.readCapacityUnits = null == options.readCapacityUnits ? 5 : parseInt(options.readCapacityUnits,10); | ||
this.writeCapacityUnits = null == options.writeCapacityUnits ? 5 : parseInt(options.writeCapacityUnits,10); | ||
this.readCapacityUnits = null == options.readCapacityUnits ? 5 : parseInt(options.readCapacityUnits, 10); | ||
this.writeCapacityUnits = null == options.writeCapacityUnits ? 5 : parseInt(options.writeCapacityUnits, 10); | ||
this.specialKeys = null == options.specialKeys ? [] : options.specialKeys; | ||
this.skipThrowMissingSpecialKeys = null == options.skipThrowMissingSpecialKeys? false : !!options.specialKeys; | ||
if (options.client) { | ||
this.client = options.client; | ||
} else { | ||
if (options.AWSConfigPath) { | ||
AWS.config.loadFromPath(options.AWSConfigPath); | ||
if (options.AWSConfigPath) { | ||
AWS.config.loadFromPath(options.AWSConfigPath); | ||
} else if (options.AWSConfigJSON) { | ||
AWS.config.update(options.AWSConfigJSON); | ||
} else { | ||
this.AWSRegion = options.AWSRegion || 'us-east-1'; | ||
AWS.config.update({region: this.AWSRegion}); | ||
} | ||
} else { | ||
this.AWSRegion = options.AWSRegion || 'us-east-1'; | ||
AWS.config.update({ region: this.AWSRegion }); | ||
} | ||
this.client = new AWS.DynamoDB(); | ||
@@ -129,4 +129,8 @@ } | ||
if (!(result.Item && result.Item.sess && result.Item.sess.S)) return fn(null, null); | ||
else if (result.Item.expires && now >= result.Item.expires) { | ||
else if (result.Item.expires && now >= +result.Item.expires.N) { | ||
fn(null, null); | ||
} else if (!result.Item.sess) { | ||
// Session isn't on the item for some reason. This seems to happen when | ||
// the session has been destroyed but Dynamo still returns an Item | ||
fn(null, null); | ||
} else { | ||
@@ -156,3 +160,3 @@ var sess = result.Item.sess.S.toString(); | ||
var expires = this.getExpiresValue(sess); | ||
sess = JSON.stringify(sess); | ||
const sessString = JSON.stringify(sess); | ||
@@ -169,3 +173,3 @@ var params = { | ||
sess: { | ||
'S': sess | ||
'S': sessString | ||
} | ||
@@ -175,5 +179,20 @@ } | ||
params.Item[this.hashKey] = { | ||
'S': sid | ||
'S': sid | ||
}; | ||
const missingKeys = []; | ||
this.specialKeys.forEach(key => { | ||
if (typeof sess[key.name] !== 'undefined') { | ||
const item = {}; | ||
item[key.type] = sess[key.name]; | ||
params.Item[key.name] = item; | ||
} else { | ||
missingKeys.push(key.name); | ||
} | ||
}); | ||
if (!this.skipThrowMissingSpecialKeys && missingKeys.length > 0) { | ||
throw Error('Session missing special keys' + JSON.stringify(missingKeys)); | ||
} | ||
this.client.putItem(params, fn); | ||
@@ -250,3 +269,3 @@ }; | ||
}; | ||
this.client.deleteItem(params, fn || function () {}); | ||
this.client.deleteItem(params, fn || function () { }); | ||
}; | ||
@@ -261,4 +280,4 @@ | ||
DynamoDBStore.prototype.getExpiresValue = function (sess) { | ||
var expires = typeof sess.cookie.maxAge === 'number' ? (+new Date()) + sess.cookie.maxAge : (+new Date()) + oneDayInMilliseconds; | ||
return Math.floor(expires / 1000); | ||
var expires = typeof sess.cookie.maxAge === 'number' ? (+new Date()) + sess.cookie.maxAge : (+new Date()) + oneDayInMilliseconds; | ||
return Math.floor(expires / 1000); | ||
} | ||
@@ -272,21 +291,21 @@ | ||
*/ | ||
DynamoDBStore.prototype.touch = function (sid, sess, fn) { | ||
sid = this.prefix + sid; | ||
var expires = this.getExpiresValue(sess); | ||
var params = { | ||
TableName: this.table, | ||
Key: {}, | ||
UpdateExpression: "set expires = :e", | ||
ExpressionAttributeValues:{ | ||
":e": { | ||
N: JSON.stringify(expires) | ||
} | ||
}, | ||
ReturnValues:"UPDATED_NEW" | ||
}; | ||
params.Key[this.hashKey] = { | ||
'S': sid | ||
} | ||
DynamoDBStore.prototype.touch = function (sid, sess, fn) { | ||
sid = this.prefix + sid; | ||
var expires = this.getExpiresValue(sess); | ||
var params = { | ||
TableName: this.table, | ||
Key: {}, | ||
UpdateExpression: "set expires = :e", | ||
ExpressionAttributeValues: { | ||
":e": { | ||
N: JSON.stringify(expires) | ||
} | ||
}, | ||
ReturnValues: "UPDATED_NEW" | ||
}; | ||
params.Key[this.hashKey] = { | ||
'S': sid | ||
} | ||
this.client.updateItem(params, fn || function () {}); | ||
this.client.updateItem(params, fn || function () { }); | ||
}; | ||
@@ -293,0 +312,0 @@ |
{ | ||
"name": "connect-dynamodb", | ||
"description": "DynamoDB session store for Connect", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"author": "Mike Carson <ca98am79@gmail.com> (http://ca98am79.com)", | ||
@@ -6,0 +6,0 @@ "main": "./index.js", |
@@ -44,3 +44,13 @@ # Connect DynamoDB | ||
readCapacityUnits: 25, | ||
writeCapacityUnits: 25 | ||
writeCapacityUnits: 25, | ||
// Optional special keys that will be inserted directly into your table (in addition to remaining in the session) | ||
specialKeys: [ | ||
{ | ||
name: 'userId', // The session key | ||
type: 'S' // The DyanamoDB attribute type | ||
} | ||
], | ||
// Optional skip throw missing special keys in session, if set true | ||
skipThrowMissingSpecialKeys: true, | ||
}; | ||
@@ -71,3 +81,3 @@ | ||
OR | ||
OR | ||
@@ -101,2 +111,37 @@ var app = express(); | ||
## IAM Permissions | ||
Connect DynamoDB requires the following IAM permissions for DynamoDB: | ||
- CreateTable | ||
- PutItem | ||
- DeleteItem | ||
- GetItem | ||
- Scan | ||
- UpdateItem | ||
Sample IAM policy (with least privilege): | ||
_(Replace __\<AWS ACCOUNT ID\>__, __\<TABLE NAME\>__ and __\<SOURCE IP AND BITMASK\>__)._ | ||
``` | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "VisualEditor0", | ||
"Effect": "Allow", | ||
"Action": [ | ||
"dynamodb:CreateTable", | ||
"dynamodb:DescribeTable", | ||
"dynamodb:PutItem", | ||
"dynamodb:DeleteItem", | ||
"dynamodb:GetItem", | ||
"dynamodb:Scan", | ||
"dynamodb:UpdateItem" | ||
], | ||
"Resource": "arn:aws:dynamodb:*:<AWS ACCOUNT ID>:table/<TABLE NAME>" | ||
} | ||
] | ||
} | ||
``` | ||
## License | ||
@@ -103,0 +148,0 @@ |
27628
475
151