-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore-service.ts
210 lines (186 loc) · 4.81 KB
/
store-service.ts
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
import { AccessLog, Message, Secret, UserSettings } from '../models';
import { calculateTTL } from '../utils';
const { v4: uuidv4 } = require('uuid');
const AWS = require('aws-sdk');
AWS.config.update({
region: 'us-west-2',
});
if (process.env.local) {
AWS.config.update({
endpoint: 'http://localhost:8000',
});
}
const docClient = new AWS.DynamoDB.DocumentClient();
interface ICreateSecret {
workspaceId: string;
authorId: string;
channelId?: string;
title: any;
encrypted: any;
users: any;
expiry: number;
onetime: boolean;
conversation?: any;
}
async function createSecret({
workspaceId,
authorId,
channelId,
title,
encrypted,
users,
expiry,
onetime,
conversation,
}: ICreateSecret) {
try {
const uuid = uuidv4();
const expiryEpoch = calculateTTL(expiry);
const messageItem: Message = {
workspaceId,
uuid,
channelId,
title,
authorId,
users,
expiry: expiryEpoch,
onetime,
conversation,
createdAt: new Date().toISOString(),
};
const secretItem: Secret = {
uuid,
encrypted,
ttl: expiryEpoch,
};
await docClient
.put({
TableName: 'Secret',
Item: secretItem,
})
.promise();
await docClient
.put({
TableName: 'Message',
Item: messageItem,
})
.promise();
return { uuid };
} catch (err) {
console.error(err);
return undefined;
}
}
export type SecretRetrievedType = {
secret: Secret | undefined;
message: Message | undefined;
createdAt: Date;
};
async function retrieveSecret(uuid: string, workspaceId: string): Promise<SecretRetrievedType> {
const secret = await docClient
.get({
TableName: 'Secret',
Key: { uuid },
})
.promise();
const message = await docClient
.get({
TableName: 'Message',
Key: { uuid, workspaceId },
})
.promise();
const results: SecretRetrievedType = {
createdAt: new Date(message.Item?.createdAt),
secret: (secret && secret.Item) || undefined,
message: (secret && message.Item) || undefined,
};
return results;
}
interface ICreateAuditTrail {
uuid: string;
userId: string;
valid?: boolean;
}
async function createAuditTrail({ uuid: secretUuid, userId, valid = true }: ICreateAuditTrail) {
const uuid = uuidv4();
const auditItem: AccessLog = {
uuid,
secretUuid,
userId,
valid,
createdAt: new Date().toISOString(),
};
await docClient
.put({
TableName: 'Access',
Item: auditItem,
})
.promise();
return {};
}
async function retrieveAuditTrail(uuid: string) {
const params = {
TableName: 'Access',
IndexName: 'secretIdIndex',
KeyConditionExpression: 'secretUuid = :s',
ExpressionAttributeValues: { ':s': uuid },
};
const data = await docClient.query(params).promise();
const Items = data.Items;
return Items as AccessLog[];
}
async function deleteMessage(uuid: string) {
const params = {
TableName: 'Secret',
Key: {
uuid: uuid,
},
};
return await docClient.delete(params).promise();
}
async function listAllSecrets(teamId: string, channelId: string) {
const params = {
TableName: 'Message',
IndexName: 'ChannelIndex',
KeyConditionExpression: 'workspaceId = :t and channelId = :c',
ExpressionAttributeValues: { ':t': teamId, ':c': channelId },
};
const data = await docClient.query(params).promise();
const Items = data.Items;
return Items as Message[];
}
async function saveUserSettings({ workspaceId, userId, defaultExpiry, defaultOneTime, defaultTitle }: UserSettings) {
const userSettingItem: UserSettings = {
workspaceId,
userId,
defaultExpiry,
defaultOneTime,
defaultTitle,
};
await docClient
.put({
TableName: 'UserSettings',
Item: userSettingItem,
})
.promise();
return {};
}
async function getUserSettings(workspaceId: string, userId: string) {
const settings = await docClient
.get({
TableName: 'UserSettings',
Key: { workspaceId, userId },
})
.promise();
return settings && settings.Item ? (settings.Item as UserSettings) : undefined;
}
export {
createSecret,
retrieveSecret,
createAuditTrail,
retrieveAuditTrail,
deleteMessage,
listAllSecrets,
saveUserSettings,
getUserSettings,
};