-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
mongo-sessions.ts
238 lines (216 loc) · 7.48 KB
/
mongo-sessions.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { type Collection, type Db, type ObjectId, type CreateIndexesOptions } from 'mongodb';
import {
type ConnectionInformations,
type DatabaseInterfaceSessions,
type Session,
} from '@accounts/types';
import { toMongoID } from './utils';
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
export interface MongoSessionsOptions {
/**
* Mongo database object.
*/
database: Db;
/**
* The sessions collection name.
* Default 'sessions'.
*/
sessionCollectionName?: string;
/**
* The timestamps for the sessions collection.
* Default 'createdAt' and 'updatedAt'.
*/
timestamps?: {
createdAt: string;
updatedAt: string;
};
/**
* Should the session collection use _id as string or ObjectId.
* If 'false' must include an 'idSessionProvider'.
* Default 'true'.
*/
convertSessionIdToMongoObjectId?: boolean;
/**
* Function that generates the _id for new Session objects.
* If 'undefined' then 'convertSessionIdToMongoObjectId' must be 'true'.
* Default 'undefined'
*/
idSessionProvider?: () => string | object;
/**
* Function that generate the id for new User objects.
*/
idProvider?: () => string | object;
/**
* Function that generate the date for the timestamps.
* Default to `(date?: Date) => (date ? date.getTime() : Date.now())`.
*/
dateProvider?: (date?: Date) => any;
}
const defaultOptions = {
sessionCollectionName: 'sessions',
timestamps: {
createdAt: 'createdAt',
updatedAt: 'updatedAt',
},
convertSessionIdToMongoObjectId: true,
dateProvider: (date?: Date) => (date ? date.getTime() : Date.now()),
};
export class MongoSessions implements DatabaseInterfaceSessions {
// Merged options that can be used
private options: MongoSessionsOptions & typeof defaultOptions;
// Mongo database object
private database: Db;
// Mongo session collection
private sessionCollection: Collection<
PartialBy<Session & { _id?: string | object }, 'createdAt' | 'updatedAt' | 'id'>
>;
constructor(options: MongoSessionsOptions) {
this.options = {
...defaultOptions,
...options,
timestamps: { ...defaultOptions.timestamps, ...options.timestamps },
};
if (
typeof this.options.idSessionProvider === 'function' &&
this.options.convertSessionIdToMongoObjectId
) {
console.warn(`You have set both "options.idSessionProvider" and "options.convertSessionIdToMongoObjectId = true" which will cause your "options.idSessionProvider" to be ignored.
In order to fix this warning change "options.convertSessionIdToMongoObjectId" to "false" or remove your "options.idSessionProvider" from the configuration.
`);
}
this.database = this.options.database;
this.sessionCollection = this.database.collection(this.options.sessionCollectionName);
}
/**
* Setup the mongo indexes needed for the sessions.
* @param options Options passed to the mongo native `createIndex` method.
*/
public async setupIndexes(
options: Omit<CreateIndexesOptions, 'unique' | 'sparse'> = {}
): Promise<void> {
// Token index used to query a session
await this.sessionCollection.createIndex('token', {
...options,
unique: true,
sparse: true,
});
}
/**
* Create a new session attached to a user.
* @param userId User id of the session.
* @param token Random token used to identify the session.
* @param connection Connection informations related to the session (such as user agent, ip etc..).
* @param extraData Any extra data you would like to add. The data will be added to the root of the object.
*/
public async createSession(
userId: string,
token: string,
connection: ConnectionInformations = {},
extraData?: object
): Promise<string> {
const session = {
userId,
token,
userAgent: connection.userAgent,
ip: connection.ip,
extraData,
valid: true,
[this.options.timestamps.createdAt]: this.options.dateProvider(),
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
};
if (this.options.idSessionProvider && !this.options.convertSessionIdToMongoObjectId) {
session._id = this.options.idSessionProvider();
}
const ret = await this.sessionCollection.insertOne(session);
// keep ret.ops for compatibility with MongoDB 3.X, version 4.X uses insertedId
return ((ret.insertedId ? ret.insertedId : (ret as any).ops[0]._id) as ObjectId).toString();
}
/**
* Get a session by his id.
* @param sessionId Id used to query the session.
*/
public async findSessionById(sessionId: string): Promise<Session | null> {
const _id = this.options.convertSessionIdToMongoObjectId ? toMongoID(sessionId) : sessionId;
const session = await this.sessionCollection.findOne({ _id });
if (session) {
session.id = session._id.toString();
}
return session as Session;
}
/**
* Get a session by his token.
* @param token Token used to query the session.
*/
public async findSessionByToken(token: string): Promise<Session | null> {
const session = await this.sessionCollection.findOne({ token });
if (session) {
session.id = session._id.toString();
}
return session as Session;
}
/**
* Update the session informations, token and connection informations.
* @param sessionId Id used to update the session.
* @param connection Connection informations related to the session (such as user agent, ip etc..).
* @param newToken New token that will replace the existing token for the session.
*/
public async updateSession(
sessionId: string,
connection: ConnectionInformations,
newToken?: string
): Promise<void> {
const updateClause = {
$set: {
userAgent: connection.userAgent,
ip: connection.ip,
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
},
};
if (newToken) {
updateClause.$set.token = newToken;
}
const _id = this.options.convertSessionIdToMongoObjectId ? toMongoID(sessionId) : sessionId;
await this.sessionCollection.updateOne({ _id }, updateClause);
}
/**
* Invalidate a session.
* @param sessionId Id of the session to invalidate.
*/
public async invalidateSession(sessionId: string): Promise<void> {
const _id = this.options.convertSessionIdToMongoObjectId ? toMongoID(sessionId) : sessionId;
await this.sessionCollection.updateOne(
{ _id },
{
$set: {
valid: false,
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
},
}
);
}
/**
* Invalidate all the session of a user.
* @param userId User id of the sessions.
* @param excludedSessionIds Can be used to whitelist some sessions. Eg: close all the sessions except these ones.
*/
public async invalidateAllSessions(userId: string, excludedSessionIds?: string[]): Promise<void> {
const selector: { userId: string; _id?: object } = { userId };
if (excludedSessionIds && excludedSessionIds.length > 0) {
let excludedObjectIds: string[] | ObjectId[] = excludedSessionIds;
if (this.options.convertSessionIdToMongoObjectId) {
excludedObjectIds = excludedSessionIds.map((sessionId) => {
return toMongoID(sessionId);
});
}
selector._id = {
$nin: excludedObjectIds,
};
}
await this.sessionCollection.updateMany(selector, {
$set: {
valid: false,
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
},
});
}
}