Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/config/env.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export const envValidationSchema = Joi.object({
SESSION_LOCK_TTL_MS: Joi.number().integer().default(5000),
SESSION_LOCK_MAX_RETRIES: Joi.number().integer().default(5),
SESSION_LOCK_RETRY_DELAY_MS: Joi.number().integer().default(120),
// Maximum concurrent sessions per user (default 5)
MAX_SESSIONS_PER_USER: Joi.number().integer().min(0).default(5),
STICKY_SESSIONS_REQUIRED: Joi.boolean().default(true),
TRUST_PROXY: Joi.boolean().default(true),

Expand Down
32 changes: 26 additions & 6 deletions src/session/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
private readonly lockTtlMs: number;
private readonly lockRetries: number;
private readonly lockRetryDelayMs: number;
private readonly maxSessionsPerUser: number;

constructor(
@Inject(SESSION_REDIS_CLIENT) private readonly redis: Redis,
private readonly configService: ConfigService,
) {
this.sessionPrefix = this.configService.get<string>('AUTH_SESSION_PREFIX') || 'auth:sess:';
this.maxSessionsPerUser = parseInt(this.configService.get<string>('MAX_SESSIONS_PER_USER') || '5', 10);

Check failure on line 35 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `this.configService.get<string>('MAX_SESSIONS_PER_USER')·||·'5',·10` with `⏎······this.configService.get<string>('MAX_SESSIONS_PER_USER')·||·'5',⏎······10,⏎····`
this.legacySessionPrefix =
this.configService.get<string>('AUTH_SESSION_LEGACY_PREFIX') || 'session:';
this.sessionTtlSeconds = parseInt(
Expand Down Expand Up @@ -119,7 +121,7 @@
'EX',
this.sessionTtlSeconds,
);
await this.addSessionToUserIndex(userId, sid);
await this.addSessionToUserIndex(userId, sid);

Check failure on line 124 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Insert `····`
return sid;
}

Expand Down Expand Up @@ -183,7 +185,13 @@
let deletedCount = 0;

do {
const [nextCursor, keys] = await this.redis.scan(cursor, 'MATCH', pattern, 'COUNT', 100);
const [nextCursor, keys] = await this.redis.scan(

Check failure on line 188 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `⏎········cursor,⏎········'MATCH',⏎········pattern,⏎········'COUNT',⏎········100,⏎······` with `cursor,·'MATCH',·pattern,·'COUNT',·100`
cursor,
'MATCH',
pattern,
'COUNT',
100,
);
cursor = nextCursor;

for (const key of keys) {
Expand Down Expand Up @@ -240,10 +248,18 @@
};

await this.redis
.multi()
.set(this.sessionKey(newSid), JSON.stringify(migrated), 'EX', this.sessionTtlSeconds)
.del(this.sessionKey(oldSid))
.exec();
.multi()

Check failure on line 251 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Delete `··`
.set(this.sessionKey(newSid), JSON.stringify(migrated), 'EX', this.sessionTtlSeconds)

Check failure on line 252 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Delete `··`
.del(this.sessionKey(oldSid))

Check failure on line 253 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Delete `··`
.exec();

Check failure on line 254 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `········` with `······`
// Update user's session sorted set

Check failure on line 255 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Delete `··`
if (existing) {

Check failure on line 256 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Delete `··`
const userKey = this.userSessionKey(existing.userId);

Check failure on line 257 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `········` with `······`
await this.redis.multi()
.zrem(userKey, oldSid)
.zadd(userKey, Date.now(), newSid)
.exec();
}

return newSid;
}
Expand Down Expand Up @@ -328,6 +344,10 @@
await this.redis.eval(releaseScript, 1, lockKey, lockToken);
}

private userSessionKey(userId: string): string {
return `user:sessions:${userId}`;
}

private sessionKey(sid: string): string {
return `${this.sessionPrefix}${sid}`;
}
Expand Down
Loading