Skip to content

Commit d8c1b4d

Browse files
committed
use token instead of sessionId inside functions
1 parent 265b31e commit d8c1b4d

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

packages/common/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface ImpersonateReturnType {
5151
export interface SessionType {
5252
sessionId: string;
5353
userId: string;
54+
token: string;
5455
valid: boolean;
5556
userAgent?: string;
5657
createdAt: string;

packages/server/src/accounts-server.ts

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export interface TokenRecord {
2828
reason: string;
2929
}
3030

31+
export interface JwtData {
32+
token: string;
33+
isImpersonated: boolean;
34+
}
35+
3136
const defaultOptions = {
3237
tokenSecret: 'secret',
3338
tokenConfigs: {
@@ -181,7 +186,12 @@ export class AccountsServer {
181186
const { ip, userAgent } = infos;
182187

183188
try {
184-
const sessionId = await this.db.createSession(user.id, ip, userAgent);
189+
// TODO get a random token
190+
const token = '';
191+
const sessionId = await this.db.createSession(user.id, token, {
192+
ip,
193+
userAgent,
194+
});
185195
const { accessToken, refreshToken } = this.createTokens(sessionId);
186196

187197
const loginResult = {
@@ -256,10 +266,15 @@ export class AccountsServer {
256266
return { authorized: false };
257267
}
258268

269+
// TODO get a random token
270+
const token = '';
259271
const newSessionId = await this.db.createSession(
260272
impersonatedUser.id,
261-
ip,
262-
userAgent,
273+
token,
274+
{
275+
ip,
276+
userAgent,
277+
},
263278
{ impersonatorUserId: user.id }
264279
);
265280
const impersonationTokens = this.createTokens(newSessionId, true);
@@ -302,22 +317,24 @@ export class AccountsServer {
302317
throw new AccountsError('An accessToken and refreshToken are required');
303318
}
304319

305-
let sessionId;
320+
let sessionToken: string;
306321
try {
307322
jwt.verify(refreshToken, this.options.tokenSecret);
308-
const decodedAccessToken: any = jwt.verify(
323+
const decodedAccessToken = jwt.verify(
309324
accessToken,
310325
this.options.tokenSecret,
311326
{
312327
ignoreExpiration: true,
313328
}
314-
);
315-
sessionId = decodedAccessToken.data.sessionId;
329+
) as { data: JwtData };
330+
sessionToken = decodedAccessToken.data.token;
316331
} catch (err) {
317332
throw new AccountsError('Tokens are not valid');
318333
}
319334

320-
const session: SessionType = await this.db.findSessionById(sessionId);
335+
const session: SessionType = await this.db.findSessionByToken(
336+
sessionToken
337+
);
321338
if (!session) {
322339
throw new AccountsError('Session not found');
323340
}
@@ -327,11 +344,11 @@ export class AccountsServer {
327344
if (!user) {
328345
throw new AccountsError('User not found', { id: session.userId });
329346
}
330-
const tokens = this.createTokens(sessionId);
331-
await this.db.updateSession(sessionId, ip, userAgent);
347+
const tokens = this.createTokens(sessionToken);
348+
await this.db.updateSession(sessionToken, { ip, userAgent });
332349

333350
const result = {
334-
sessionId,
351+
sessionId: session.sessionId,
335352
user: this.sanitizeUser(user),
336353
tokens,
337354
};
@@ -353,20 +370,21 @@ export class AccountsServer {
353370

354371
/**
355372
* @description Refresh a user token.
356-
* @param {string} sessionId - User session id.
373+
* @param {string} token - User session token.
357374
* @param {boolean} isImpersonated - Should be true if impersonating another user.
358375
* @returns {Promise<Object>} - Return a new accessToken and refreshToken.
359376
*/
360377
public createTokens(
361-
sessionId: string,
378+
token: string,
362379
isImpersonated: boolean = false
363380
): TokensType {
364381
const { tokenSecret, tokenConfigs } = this.options;
382+
const jwtData: JwtData = {
383+
token,
384+
isImpersonated,
385+
};
365386
const accessToken = generateAccessToken({
366-
data: {
367-
sessionId,
368-
isImpersonated,
369-
},
387+
data: jwtData,
370388
secret: tokenSecret,
371389
config: tokenConfigs.accessToken || {},
372390
});
@@ -453,25 +471,30 @@ export class AccountsServer {
453471
}
454472
}
455473

474+
/**
475+
* @description Find a session by his token.
476+
* @param {string} accessToken
477+
* @returns {Promise<SessionType>} - Return a session.
478+
*/
456479
public async findSessionByAccessToken(
457480
accessToken: string
458481
): Promise<SessionType> {
459482
if (!isString(accessToken)) {
460483
throw new AccountsError('An accessToken is required');
461484
}
462485

463-
let sessionId;
486+
let sessionToken: string;
464487
try {
465-
const decodedAccessToken: any = jwt.verify(
488+
const decodedAccessToken = jwt.verify(
466489
accessToken,
467490
this.options.tokenSecret
468-
);
469-
sessionId = decodedAccessToken.data.sessionId;
491+
) as { data: JwtData };
492+
sessionToken = decodedAccessToken.data.token;
470493
} catch (err) {
471494
throw new AccountsError('Tokens are not valid');
472495
}
473496

474-
const session: SessionType = await this.db.findSessionById(sessionId);
497+
const session: SessionType = await this.db.findSessionByToken(sessionToken);
475498
if (!session) {
476499
throw new AccountsError('Session not found');
477500
}

packages/server/src/types.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,17 @@ export interface DBInterface {
111111
): Promise<void>;
112112

113113
// Session related operations
114-
findSessionById(sessionId: string): Promise<SessionType | null>;
114+
findSessionByToken(token: string): Promise<SessionType | null>;
115115
createSession(
116116
userId: string,
117-
ip?: string,
118-
userAgent?: string,
117+
token: string,
118+
connection: ConnectionInformationsType,
119119
extraData?: object
120120
): Promise<string>;
121121
updateSession(
122-
sessionId: string,
123-
ip?: string,
124-
userAgent?: string
122+
token: string,
123+
connection: ConnectionInformationsType
125124
): Promise<void>;
126-
invalidateSession(sessionId: string): Promise<void>;
125+
invalidateSession(token: string): Promise<void>;
127126
invalidateAllSessions(userId: string): Promise<void>;
128127
}

0 commit comments

Comments
 (0)