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
42 changes: 22 additions & 20 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 @@ -81,7 +83,7 @@
'EX',
this.sessionTtlSeconds,
);
await this.addSessionToUserIndex(userId, sid);
await this.addSessionToUserIndex(userId, sid);

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

View workflow job for this annotation

GitHub Actions / validate

Insert `····`
return sid;
}

Expand Down Expand Up @@ -134,21 +136,9 @@
async removeSession(sid: string): Promise<void> {
const session = await this.getSession(sid);
await this.redis.del(this.sessionKey(sid));
if (session) {
await this.removeSessionFromUserIndex(session.userId, sid);
}
}

async addSessionToUserIndex(userId: string, sid: string): Promise<void> {
await this.redis.zadd(`user:sessions:${userId}`, Date.now(), sid);
}

async removeSessionFromUserIndex(userId: string, sid: string): Promise<void> {
await this.redis.zrem(`user:sessions:${userId}`, sid);
}

async getUserSessionIds(userId: string): Promise<string[]> {
return this.redis.zrange(`user:sessions:${userId}`, 0, -1);
if (session) {

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

View workflow job for this annotation

GitHub Actions / validate

Insert `····`
await this.removeSessionFromUserIndex(session.userId, sid);

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

View workflow job for this annotation

GitHub Actions / validate

Insert `····`
}

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

View workflow job for this annotation

GitHub Actions / validate

Insert `····`
}

/**
Expand All @@ -171,10 +161,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 164 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 165 in src/session/session.service.ts

View workflow job for this annotation

GitHub Actions / validate

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

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

View workflow job for this annotation

GitHub Actions / validate

Delete `··`
.exec();

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

View workflow job for this annotation

GitHub Actions / validate

Delete `··`
// Update user's session sorted set

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

View workflow job for this annotation

GitHub Actions / validate

Delete `··`
if (existing) {
const userKey = this.userSessionKey(existing.userId);
await this.redis.multi()
.zrem(userKey, oldSid)
.zadd(userKey, Date.now(), newSid)
.exec();
}

return newSid;
}
Expand Down Expand Up @@ -259,6 +257,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