-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathaccess-tokens.js
68 lines (61 loc) · 1.65 KB
/
access-tokens.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
'use strict';
const moment = require('moment');
const DynamoDb = require('@cumulus/aws-client/DynamoDb');
const Manager = require('./base');
const { accessToken: accessTokenSchema } = require('../lib/schemas');
class AccessToken extends Manager {
constructor(params = {}) {
super({
tableName: params.tableName || process.env.AccessTokensTable,
tableHash: { name: 'accessToken', type: 'S' },
schema: accessTokenSchema,
});
}
/**
* Gets the item if found. If the record does not exist
* the function throws RecordDoesNotExist error
*
* Enforces strongly consistent reads for the DynamoDB get operation.
*
* @param {Object} item - the item to search for
* @returns {Promise} The record found
*/
get(item) {
return DynamoDb.get({
tableName: this.tableName,
item,
client: this.dynamodbDocClient,
getParams: {
ConsistentRead: true,
},
});
}
/**
* Get the default expiration time for an access token.
*
* @returns {number} - the expiration timestamp, in seconds
*/
_getDefaultExpirationTime() {
const currentTimeInSecs = moment().unix();
const oneHourInSecs = 60 * 60;
return currentTimeInSecs + oneHourInSecs;
}
/**
* Create the access token record.
*
* @param {Object} item - the access token record
* @returns {Promise<Object>} the created record
* @see #constructor
* @see Manager#create
*/
create(item) {
const record = item.expirationTime
? item
: {
...item,
expirationTime: this._getDefaultExpirationTime(),
};
return super.create(record);
}
}
module.exports = AccessToken;