@shware/security 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/README.md +1 -0
  2. package/dist/auth/index.cjs +158 -0
  3. package/dist/auth/index.cjs.map +1 -0
  4. package/dist/auth/index.d.cts +27 -0
  5. package/dist/auth/index.d.ts +27 -0
  6. package/dist/auth/index.mjs +129 -0
  7. package/dist/auth/index.mjs.map +1 -0
  8. package/dist/auth/types.cjs +19 -0
  9. package/dist/auth/types.cjs.map +1 -0
  10. package/dist/auth/types.d.cts +25 -0
  11. package/dist/auth/types.d.ts +25 -0
  12. package/dist/auth/types.mjs +1 -0
  13. package/dist/auth/types.mjs.map +1 -0
  14. package/dist/core/index.cjs +19 -0
  15. package/dist/core/index.cjs.map +1 -0
  16. package/dist/core/index.d.cts +17 -0
  17. package/dist/core/index.d.ts +17 -0
  18. package/dist/core/index.mjs +1 -0
  19. package/dist/core/index.mjs.map +1 -0
  20. package/dist/index.cjs +41 -0
  21. package/dist/index.cjs.map +1 -0
  22. package/dist/index.d.cts +11 -0
  23. package/dist/index.d.ts +11 -0
  24. package/dist/index.mjs +12 -0
  25. package/dist/index.mjs.map +1 -0
  26. package/dist/oauth2/client.cjs +110 -0
  27. package/dist/oauth2/client.cjs.map +1 -0
  28. package/dist/oauth2/client.d.cts +39 -0
  29. package/dist/oauth2/client.d.ts +39 -0
  30. package/dist/oauth2/client.mjs +74 -0
  31. package/dist/oauth2/client.mjs.map +1 -0
  32. package/dist/oauth2/error.cjs +54 -0
  33. package/dist/oauth2/error.cjs.map +1 -0
  34. package/dist/oauth2/error.d.cts +11 -0
  35. package/dist/oauth2/error.d.ts +11 -0
  36. package/dist/oauth2/error.mjs +28 -0
  37. package/dist/oauth2/error.mjs.map +1 -0
  38. package/dist/oauth2/provider/apple.cjs +85 -0
  39. package/dist/oauth2/provider/apple.cjs.map +1 -0
  40. package/dist/oauth2/provider/apple.d.cts +31 -0
  41. package/dist/oauth2/provider/apple.d.ts +31 -0
  42. package/dist/oauth2/provider/apple.mjs +50 -0
  43. package/dist/oauth2/provider/apple.mjs.map +1 -0
  44. package/dist/oauth2/provider/common.cjs +159 -0
  45. package/dist/oauth2/provider/common.cjs.map +1 -0
  46. package/dist/oauth2/provider/common.d.cts +38 -0
  47. package/dist/oauth2/provider/common.d.ts +38 -0
  48. package/dist/oauth2/provider/common.mjs +119 -0
  49. package/dist/oauth2/provider/common.mjs.map +1 -0
  50. package/dist/oauth2/provider/facebook.cjs +106 -0
  51. package/dist/oauth2/provider/facebook.cjs.map +1 -0
  52. package/dist/oauth2/provider/facebook.d.cts +29 -0
  53. package/dist/oauth2/provider/facebook.d.ts +29 -0
  54. package/dist/oauth2/provider/facebook.mjs +71 -0
  55. package/dist/oauth2/provider/facebook.mjs.map +1 -0
  56. package/dist/oauth2/provider/github.cjs +101 -0
  57. package/dist/oauth2/provider/github.cjs.map +1 -0
  58. package/dist/oauth2/provider/github.d.cts +51 -0
  59. package/dist/oauth2/provider/github.d.ts +51 -0
  60. package/dist/oauth2/provider/github.mjs +66 -0
  61. package/dist/oauth2/provider/github.mjs.map +1 -0
  62. package/dist/oauth2/provider/google.cjs +91 -0
  63. package/dist/oauth2/provider/google.cjs.map +1 -0
  64. package/dist/oauth2/provider/google.d.cts +35 -0
  65. package/dist/oauth2/provider/google.d.ts +35 -0
  66. package/dist/oauth2/provider/google.mjs +56 -0
  67. package/dist/oauth2/provider/google.mjs.map +1 -0
  68. package/dist/oauth2/provider/index.cjs +43 -0
  69. package/dist/oauth2/provider/index.cjs.map +1 -0
  70. package/dist/oauth2/provider/index.d.cts +6 -0
  71. package/dist/oauth2/provider/index.d.ts +6 -0
  72. package/dist/oauth2/provider/index.mjs +14 -0
  73. package/dist/oauth2/provider/index.mjs.map +1 -0
  74. package/dist/oauth2/provider/x.cjs +126 -0
  75. package/dist/oauth2/provider/x.cjs.map +1 -0
  76. package/dist/oauth2/provider/x.d.cts +60 -0
  77. package/dist/oauth2/provider/x.d.ts +60 -0
  78. package/dist/oauth2/provider/x.mjs +97 -0
  79. package/dist/oauth2/provider/x.mjs.map +1 -0
  80. package/dist/oauth2/schema.cjs +2 -0
  81. package/dist/oauth2/schema.cjs.map +1 -0
  82. package/dist/oauth2/schema.d.cts +2 -0
  83. package/dist/oauth2/schema.d.ts +2 -0
  84. package/dist/oauth2/schema.mjs +1 -0
  85. package/dist/oauth2/schema.mjs.map +1 -0
  86. package/dist/oauth2/types.cjs +19 -0
  87. package/dist/oauth2/types.cjs.map +1 -0
  88. package/dist/oauth2/types.d.cts +138 -0
  89. package/dist/oauth2/types.d.ts +138 -0
  90. package/dist/oauth2/types.mjs +1 -0
  91. package/dist/oauth2/types.mjs.map +1 -0
  92. package/dist/oidc/index.cjs +39 -0
  93. package/dist/oidc/index.cjs.map +1 -0
  94. package/dist/oidc/index.d.cts +10 -0
  95. package/dist/oidc/index.d.ts +10 -0
  96. package/dist/oidc/index.mjs +14 -0
  97. package/dist/oidc/index.mjs.map +1 -0
  98. package/dist/session/__test__/redis-session.test.cjs +94 -0
  99. package/dist/session/__test__/redis-session.test.cjs.map +1 -0
  100. package/dist/session/__test__/redis-session.test.d.cts +2 -0
  101. package/dist/session/__test__/redis-session.test.d.ts +2 -0
  102. package/dist/session/__test__/redis-session.test.mjs +70 -0
  103. package/dist/session/__test__/redis-session.test.mjs.map +1 -0
  104. package/dist/session/index.cjs +33 -0
  105. package/dist/session/index.cjs.map +1 -0
  106. package/dist/session/index.d.cts +4 -0
  107. package/dist/session/index.d.ts +4 -0
  108. package/dist/session/index.mjs +7 -0
  109. package/dist/session/index.mjs.map +1 -0
  110. package/dist/session/redis-session.cjs +403 -0
  111. package/dist/session/redis-session.cjs.map +1 -0
  112. package/dist/session/redis-session.d.cts +73 -0
  113. package/dist/session/redis-session.d.ts +73 -0
  114. package/dist/session/redis-session.mjs +377 -0
  115. package/dist/session/redis-session.mjs.map +1 -0
  116. package/dist/session/types.cjs +19 -0
  117. package/dist/session/types.cjs.map +1 -0
  118. package/dist/session/types.d.cts +39 -0
  119. package/dist/session/types.d.ts +39 -0
  120. package/dist/session/types.mjs +1 -0
  121. package/dist/session/types.mjs.map +1 -0
  122. package/dist/utils/http.cjs +63 -0
  123. package/dist/utils/http.cjs.map +1 -0
  124. package/dist/utils/http.d.cts +17 -0
  125. package/dist/utils/http.d.ts +17 -0
  126. package/dist/utils/http.mjs +34 -0
  127. package/dist/utils/http.mjs.map +1 -0
  128. package/package.json +44 -0
@@ -0,0 +1,403 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/session/redis-session.ts
21
+ var redis_session_exports = {};
22
+ __export(redis_session_exports, {
23
+ PRINCIPAL_NAME_INDEX_NAME: () => PRINCIPAL_NAME_INDEX_NAME,
24
+ RedisIndexedSessionRepository: () => RedisIndexedSessionRepository
25
+ });
26
+ module.exports = __toCommonJS(redis_session_exports);
27
+ var import_crypto = require("crypto");
28
+ var DEFAULT_MAX_INACTIVE_INTERVAL = 1800;
29
+ var CREATION_TIME_KEY = "creationTime";
30
+ var LAST_ACCESSED_TIME_KEY = "lastAccessedTime";
31
+ var MAX_INACTIVE_INTERVAL_KEY = "maxInactiveInterval";
32
+ var ATTRIBUTE_PREFIX = "sessionAttr:";
33
+ var SESSION_EXPIRES_PREFIX = "expires:";
34
+ var SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT";
35
+ var PRINCIPAL_NAME_INDEX_NAME = `org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME`;
36
+ function generateId() {
37
+ return (0, import_crypto.randomUUID)();
38
+ }
39
+ var MapSession = class {
40
+ id;
41
+ sessionAttrs = /* @__PURE__ */ new Map();
42
+ creationTime = Date.now();
43
+ lastAccessedTime = this.creationTime;
44
+ maxInactiveInterval = DEFAULT_MAX_INACTIVE_INTERVAL;
45
+ constructor(input) {
46
+ if (typeof input === "undefined") {
47
+ this.id = generateId();
48
+ } else if (typeof input === "string") {
49
+ this.id = input;
50
+ } else {
51
+ const session = input;
52
+ this.id = session.getId();
53
+ for (const attrName of session.getAttributeNames()) {
54
+ const attrValue = session.getAttribute(attrName);
55
+ if (attrValue !== null) {
56
+ this.setAttribute(attrName, attrValue);
57
+ }
58
+ }
59
+ this.lastAccessedTime = session.getLastAccessedTime();
60
+ this.creationTime = session.getCreationTime();
61
+ this.maxInactiveInterval = session.getMaxInactiveInterval();
62
+ }
63
+ }
64
+ setCreationTime(creationTime) {
65
+ this.creationTime = creationTime;
66
+ }
67
+ // override
68
+ getId() {
69
+ return this.id;
70
+ }
71
+ changeSessionId() {
72
+ const changedId = generateId();
73
+ this.id = changedId;
74
+ return changedId;
75
+ }
76
+ getAttribute(name) {
77
+ return this.sessionAttrs.get(name) ?? null;
78
+ }
79
+ getAttributeNames() {
80
+ return Array.from(this.sessionAttrs.keys());
81
+ }
82
+ setAttribute(name, value) {
83
+ if (value === null) {
84
+ this.removeAttribute(name);
85
+ } else {
86
+ this.sessionAttrs.set(name, value);
87
+ }
88
+ }
89
+ removeAttribute(name) {
90
+ this.sessionAttrs.delete(name);
91
+ }
92
+ getCreationTime() {
93
+ return this.creationTime;
94
+ }
95
+ getLastAccessedTime() {
96
+ return this.lastAccessedTime;
97
+ }
98
+ setLastAccessedTime(lastAccessedTime) {
99
+ this.lastAccessedTime = lastAccessedTime;
100
+ }
101
+ getMaxInactiveInterval() {
102
+ return this.maxInactiveInterval;
103
+ }
104
+ setMaxInactiveInterval(interval) {
105
+ this.maxInactiveInterval = interval;
106
+ }
107
+ isExpired() {
108
+ if (this.maxInactiveInterval < 0) return false;
109
+ return Date.now() - this.maxInactiveInterval * 1e3 - this.lastAccessedTime >= 0;
110
+ }
111
+ };
112
+ function resolveIndexesFor(session) {
113
+ const principalName = session.getAttribute(PRINCIPAL_NAME_INDEX_NAME);
114
+ if (principalName !== null) return principalName;
115
+ const jsonString = session.getAttribute(SPRING_SECURITY_CONTEXT);
116
+ if (typeof jsonString === "string") {
117
+ const context = JSON.parse(jsonString);
118
+ return context?.authentication?.name ?? context.authentication?.principal?.name ?? null;
119
+ }
120
+ return null;
121
+ }
122
+ var RedisSession = class {
123
+ cached;
124
+ isNew;
125
+ delta = /* @__PURE__ */ new Map();
126
+ originalSessionId;
127
+ originalPrincipalName;
128
+ originalLastAccessTime = null;
129
+ constructor(cached, isNew) {
130
+ this.cached = cached;
131
+ this.isNew = isNew;
132
+ this.originalSessionId = cached.getId();
133
+ this.originalPrincipalName = resolveIndexesFor(this);
134
+ if (isNew) {
135
+ this.delta.set(CREATION_TIME_KEY, cached.getCreationTime());
136
+ this.delta.set(LAST_ACCESSED_TIME_KEY, cached.getLastAccessedTime());
137
+ this.delta.set(MAX_INACTIVE_INTERVAL_KEY, cached.getMaxInactiveInterval());
138
+ this.cached.getAttributeNames().forEach((name) => {
139
+ const value = this.cached.getAttribute(name);
140
+ if (value) this.delta.set(this.getSessionAttrNameKey(name), value);
141
+ });
142
+ }
143
+ }
144
+ getSessionAttrNameKey(name) {
145
+ return ATTRIBUTE_PREFIX + name;
146
+ }
147
+ // override
148
+ setLastAccessedTime(lastAccessedTime) {
149
+ this.cached.setLastAccessedTime(lastAccessedTime);
150
+ this.delta.set(LAST_ACCESSED_TIME_KEY, lastAccessedTime);
151
+ }
152
+ isExpired() {
153
+ return this.cached.isExpired();
154
+ }
155
+ getCreationTime() {
156
+ return this.cached.getCreationTime();
157
+ }
158
+ getId() {
159
+ return this.cached.getId();
160
+ }
161
+ changeSessionId() {
162
+ return this.cached.changeSessionId();
163
+ }
164
+ getLastAccessedTime() {
165
+ return this.cached.getLastAccessedTime();
166
+ }
167
+ setMaxInactiveInterval(interval) {
168
+ this.cached.setMaxInactiveInterval(interval);
169
+ this.delta.set(MAX_INACTIVE_INTERVAL_KEY, interval);
170
+ }
171
+ getMaxInactiveInterval() {
172
+ return this.cached.getMaxInactiveInterval();
173
+ }
174
+ getAttribute(name) {
175
+ return this.cached.getAttribute(name);
176
+ }
177
+ getAttributeNames() {
178
+ return this.cached.getAttributeNames();
179
+ }
180
+ setAttribute(name, value) {
181
+ this.cached.setAttribute(name, value);
182
+ this.delta.set(this.getSessionAttrNameKey(name), value);
183
+ }
184
+ removeAttribute(name) {
185
+ this.cached.removeAttribute(name);
186
+ this.delta.set(this.getSessionAttrNameKey(name), null);
187
+ }
188
+ };
189
+ var SortedSetRedisSessionExpirationStore = class {
190
+ redis;
191
+ namespace;
192
+ expirationsKey;
193
+ cleanupCount = 100;
194
+ constructor(redis, namespace) {
195
+ this.redis = redis;
196
+ this.namespace = namespace;
197
+ this.expirationsKey = `${this.namespace}:sessions:expirations`;
198
+ }
199
+ getSessionKey(sessionId) {
200
+ return `${this.namespace}:sessions:${sessionId}`;
201
+ }
202
+ async save(session) {
203
+ const expirationInMillis = session.getLastAccessedTime() + session.getMaxInactiveInterval() * 1e3;
204
+ await this.redis.zadd(this.expirationsKey, expirationInMillis, session.getId());
205
+ }
206
+ async remove(sessionId) {
207
+ await this.redis.zrem(this.expirationsKey, sessionId);
208
+ }
209
+ async rename(session) {
210
+ if (session.originalSessionId === null || session.originalSessionId === session.getId()) return;
211
+ await this.remove(session.originalSessionId);
212
+ await this.save(session);
213
+ }
214
+ async cleanupExpiredSessions() {
215
+ const sessionIds = await this.redis.zrevrangebyscore(
216
+ this.expirationsKey,
217
+ Date.now(),
218
+ 0,
219
+ "LIMIT",
220
+ 0,
221
+ this.cleanupCount
222
+ );
223
+ if (sessionIds.length === 0) return;
224
+ for (const sessionId of sessionIds) {
225
+ const sessionKey = this.getSessionKey(sessionId);
226
+ await this.redis.exists(sessionKey);
227
+ }
228
+ }
229
+ };
230
+ var RedisIndexedSessionRepository = class {
231
+ redis;
232
+ namespace;
233
+ expirationStore;
234
+ defaultMaxInactiveInterval = 1800;
235
+ constructor(redis, namespace = "spring:session") {
236
+ this.redis = redis;
237
+ this.namespace = namespace;
238
+ this.expirationStore = new SortedSetRedisSessionExpirationStore(redis, namespace);
239
+ }
240
+ setDefaultMaxInactiveInterval(interval) {
241
+ this.defaultMaxInactiveInterval = interval;
242
+ }
243
+ getSessionKey(sessionId) {
244
+ return `${this.namespace}:sessions:${sessionId}`;
245
+ }
246
+ getExpiredKey(sessionId) {
247
+ return `${this.namespace}:sessions:expires:${sessionId}`;
248
+ }
249
+ getSessionAttrNameKey(name) {
250
+ return ATTRIBUTE_PREFIX + name;
251
+ }
252
+ getPrincipalKey(principalName) {
253
+ return `${this.namespace}:index:${PRINCIPAL_NAME_INDEX_NAME}:${principalName}`;
254
+ }
255
+ async getSession(id, allowExpired) {
256
+ const key = this.getSessionKey(id);
257
+ const entries = await this.redis.hgetall(key);
258
+ if (!entries || Object.keys(entries).length === 0) return null;
259
+ const loaded = new MapSession(id);
260
+ if (!entries.creationTime) throw new Error("creationTime attribute not found");
261
+ if (!entries.lastAccessedTime) throw new Error("lastAccessedTime attribute not found");
262
+ if (!entries.maxInactiveInterval) throw new Error("maxInactiveInterval attribute not found");
263
+ loaded.setCreationTime(Number(entries.creationTime));
264
+ loaded.setLastAccessedTime(Number(entries.lastAccessedTime));
265
+ loaded.setMaxInactiveInterval(Number(entries.maxInactiveInterval));
266
+ for (const [key2, value] of Object.entries(entries)) {
267
+ if (key2.startsWith(ATTRIBUTE_PREFIX)) {
268
+ loaded.setAttribute(key2.slice(ATTRIBUTE_PREFIX.length), value);
269
+ }
270
+ }
271
+ if (!allowExpired && loaded.isExpired()) return null;
272
+ const result = new RedisSession(loaded, false);
273
+ result.originalLastAccessTime = loaded.getLastAccessedTime();
274
+ return result;
275
+ }
276
+ async saveDelta(session) {
277
+ if (session.delta.size === 0) return;
278
+ const sessionId = session.getId();
279
+ const delta = /* @__PURE__ */ new Map();
280
+ const removes = [];
281
+ session.delta.forEach((value, key) => {
282
+ if (value !== null) delta.set(key, value);
283
+ else removes.push(key);
284
+ });
285
+ if (delta.size !== 0) {
286
+ await this.redis.hset(this.getSessionKey(sessionId), delta);
287
+ }
288
+ if (removes.length !== 0) {
289
+ await this.redis.hdel(this.getSessionKey(sessionId), ...removes);
290
+ }
291
+ const principalSessionKey = this.getSessionAttrNameKey(PRINCIPAL_NAME_INDEX_NAME);
292
+ const securityPrincipalSessionKey = this.getSessionAttrNameKey(SPRING_SECURITY_CONTEXT);
293
+ if (session.delta.has(principalSessionKey) || session.delta.has(securityPrincipalSessionKey)) {
294
+ if (session.originalPrincipalName !== null) {
295
+ const originalPrincipalRedisKey = this.getPrincipalKey(session.originalPrincipalName);
296
+ await this.redis.srem(originalPrincipalRedisKey, sessionId);
297
+ }
298
+ const principal = resolveIndexesFor(session);
299
+ session.originalPrincipalName = principal;
300
+ if (principal !== null) {
301
+ const principalRedisKey = this.getPrincipalKey(principal);
302
+ await this.redis.sadd(principalRedisKey, sessionId);
303
+ }
304
+ }
305
+ if (session.isNew) {
306
+ session.isNew = false;
307
+ }
308
+ const sessionExpireInSeconds = session.getMaxInactiveInterval();
309
+ const keyToExpire = SESSION_EXPIRES_PREFIX + session.getId();
310
+ const sessionKey = this.getSessionKey(keyToExpire);
311
+ if (sessionExpireInSeconds < 0) {
312
+ await this.redis.append(sessionKey, "");
313
+ await this.redis.persist(sessionKey);
314
+ await this.redis.persist(this.getSessionKey(session.getId()));
315
+ } else if (sessionExpireInSeconds === 0) {
316
+ await this.redis.del(sessionKey);
317
+ } else {
318
+ await this.redis.append(sessionKey, "");
319
+ await this.redis.expire(sessionKey, sessionExpireInSeconds);
320
+ }
321
+ if (sessionExpireInSeconds > 0) {
322
+ const fiveMinutesAfterExpires = sessionExpireInSeconds + 5 * 60;
323
+ await this.redis.expire(this.getSessionKey(session.getId()), fiveMinutesAfterExpires);
324
+ }
325
+ await this.expirationStore.save(session);
326
+ session.delta.clear();
327
+ }
328
+ async saveChangeSessionId(session) {
329
+ const sessionId = session.getId();
330
+ if (sessionId === session.originalSessionId) return;
331
+ if (!session.isNew) {
332
+ const originalSessionIdKey = this.getSessionKey(session.originalSessionId);
333
+ const sessionIdKey = this.getSessionKey(sessionId);
334
+ const originalExpiredKey = this.getExpiredKey(session.originalSessionId);
335
+ const expiredKey = this.getExpiredKey(sessionId);
336
+ await this.redis.rename(originalSessionIdKey, sessionIdKey);
337
+ await this.redis.rename(originalExpiredKey, expiredKey);
338
+ if (session.originalPrincipalName !== null) {
339
+ const originalPrincipalRedisKey = this.getPrincipalKey(session.originalPrincipalName);
340
+ await this.redis.srem(originalPrincipalRedisKey, session.originalSessionId);
341
+ await this.redis.sadd(originalPrincipalRedisKey, sessionId);
342
+ }
343
+ await this.expirationStore.rename(session);
344
+ }
345
+ session.originalSessionId = sessionId;
346
+ }
347
+ // override
348
+ createSession() {
349
+ const cached = new MapSession();
350
+ cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
351
+ return new RedisSession(cached, true);
352
+ }
353
+ async save(session) {
354
+ await this.saveChangeSessionId(session);
355
+ await this.saveDelta(session);
356
+ }
357
+ async findById(sessionId) {
358
+ return this.getSession(sessionId, false);
359
+ }
360
+ async deleteById(sessionId) {
361
+ const session = await this.getSession(sessionId, true);
362
+ if (!session) return;
363
+ const principal = resolveIndexesFor(session);
364
+ if (principal !== null) {
365
+ await this.redis.srem(this.getPrincipalKey(principal), sessionId);
366
+ }
367
+ await this.expirationStore.remove(sessionId);
368
+ const expiredKey = this.getExpiredKey(sessionId);
369
+ await this.redis.del(expiredKey);
370
+ session.setMaxInactiveInterval(0);
371
+ await this.save(session);
372
+ }
373
+ async findByIndexNameAndIndexValue(indexName, indexValue) {
374
+ if (indexName !== PRINCIPAL_NAME_INDEX_NAME) {
375
+ return /* @__PURE__ */ new Map();
376
+ }
377
+ const principalKey = this.getPrincipalKey(indexValue);
378
+ const sessionIds = await this.redis.smembers(principalKey);
379
+ if (!sessionIds || sessionIds.length === 0) {
380
+ return /* @__PURE__ */ new Map();
381
+ }
382
+ const sessions = /* @__PURE__ */ new Map();
383
+ for (const sessionId of sessionIds) {
384
+ const session = await this.findById(sessionId);
385
+ if (session !== null) {
386
+ sessions.set(sessionId, session);
387
+ }
388
+ }
389
+ return sessions;
390
+ }
391
+ async findByPrincipalName(principalName) {
392
+ return this.findByIndexNameAndIndexValue(PRINCIPAL_NAME_INDEX_NAME, principalName);
393
+ }
394
+ async cleanupExpiredSessions() {
395
+ await this.expirationStore.cleanupExpiredSessions();
396
+ }
397
+ };
398
+ // Annotate the CommonJS export names for ESM import in node:
399
+ 0 && (module.exports = {
400
+ PRINCIPAL_NAME_INDEX_NAME,
401
+ RedisIndexedSessionRepository
402
+ });
403
+ //# sourceMappingURL=redis-session.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/session/redis-session.ts"],"sourcesContent":["import { randomUUID } from 'crypto';\nimport type { Redis } from 'ioredis';\nimport type { Namespace, SecurityContext, Session, SessionRepository } from './types';\n\nconst DEFAULT_MAX_INACTIVE_INTERVAL = 1800;\n\nconst CREATION_TIME_KEY = 'creationTime';\nconst LAST_ACCESSED_TIME_KEY = 'lastAccessedTime';\nconst MAX_INACTIVE_INTERVAL_KEY = 'maxInactiveInterval';\nconst ATTRIBUTE_PREFIX = 'sessionAttr:';\nconst SESSION_EXPIRES_PREFIX = 'expires:';\n\nconst SPRING_SECURITY_CONTEXT = 'SPRING_SECURITY_CONTEXT';\nexport const PRINCIPAL_NAME_INDEX_NAME = `org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME`;\n\nfunction generateId() {\n return randomUUID();\n}\n\nclass MapSession implements Session {\n private id: string;\n private sessionAttrs = new Map<string, string | number>();\n private creationTime = Date.now();\n private lastAccessedTime = this.creationTime;\n private maxInactiveInterval = DEFAULT_MAX_INACTIVE_INTERVAL;\n\n constructor(id?: string);\n constructor(session: Session);\n constructor(input?: string | Session) {\n if (typeof input === 'undefined') {\n this.id = generateId();\n } else if (typeof input === 'string') {\n this.id = input;\n } else {\n const session = input;\n this.id = session.getId();\n\n for (const attrName of session.getAttributeNames()) {\n const attrValue = session.getAttribute(attrName);\n if (attrValue !== null) {\n this.setAttribute(attrName, attrValue);\n }\n }\n\n this.lastAccessedTime = session.getLastAccessedTime();\n this.creationTime = session.getCreationTime();\n this.maxInactiveInterval = session.getMaxInactiveInterval();\n }\n }\n\n setCreationTime(creationTime: number) {\n this.creationTime = creationTime;\n }\n\n // override\n getId() {\n return this.id;\n }\n\n changeSessionId() {\n const changedId = generateId();\n this.id = changedId;\n return changedId;\n }\n\n getAttribute(name: string) {\n return this.sessionAttrs.get(name) ?? null;\n }\n\n getAttributeNames(): string[] {\n return Array.from(this.sessionAttrs.keys());\n }\n\n setAttribute(name: string, value: string | number) {\n if (value === null) {\n this.removeAttribute(name);\n } else {\n this.sessionAttrs.set(name, value);\n }\n }\n\n removeAttribute(name: string) {\n this.sessionAttrs.delete(name);\n }\n\n getCreationTime() {\n return this.creationTime;\n }\n\n getLastAccessedTime() {\n return this.lastAccessedTime;\n }\n\n setLastAccessedTime(lastAccessedTime: number) {\n this.lastAccessedTime = lastAccessedTime;\n }\n\n getMaxInactiveInterval() {\n return this.maxInactiveInterval;\n }\n\n setMaxInactiveInterval(interval: number) {\n this.maxInactiveInterval = interval;\n }\n\n isExpired() {\n if (this.maxInactiveInterval < 0) return false;\n return Date.now() - this.maxInactiveInterval * 1000 - this.lastAccessedTime >= 0;\n }\n}\n\nfunction resolveIndexesFor(session: Session): string | null {\n const principalName = session.getAttribute(PRINCIPAL_NAME_INDEX_NAME) as string | null;\n if (principalName !== null) return principalName;\n\n const jsonString = session.getAttribute(SPRING_SECURITY_CONTEXT);\n if (typeof jsonString === 'string') {\n const context = JSON.parse(jsonString) as SecurityContext;\n return context?.authentication?.name ?? context.authentication?.principal?.name ?? null;\n }\n return null;\n}\n\nclass RedisSession implements Session {\n private readonly cached: MapSession;\n\n public isNew: boolean;\n public delta = new Map<string, string | number | null>();\n public originalSessionId: string;\n public originalPrincipalName: string | null;\n public originalLastAccessTime: number | null = null;\n\n constructor(cached: MapSession, isNew: boolean) {\n this.cached = cached;\n this.isNew = isNew;\n this.originalSessionId = cached.getId();\n this.originalPrincipalName = resolveIndexesFor(this);\n\n if (isNew) {\n this.delta.set(CREATION_TIME_KEY, cached.getCreationTime());\n this.delta.set(LAST_ACCESSED_TIME_KEY, cached.getLastAccessedTime());\n this.delta.set(MAX_INACTIVE_INTERVAL_KEY, cached.getMaxInactiveInterval());\n this.cached.getAttributeNames().forEach((name) => {\n const value = this.cached.getAttribute(name);\n if (value) this.delta.set(this.getSessionAttrNameKey(name), value);\n });\n }\n }\n\n getSessionAttrNameKey(name: string) {\n return ATTRIBUTE_PREFIX + name;\n }\n\n // override\n setLastAccessedTime(lastAccessedTime: number) {\n this.cached.setLastAccessedTime(lastAccessedTime);\n this.delta.set(LAST_ACCESSED_TIME_KEY, lastAccessedTime);\n }\n\n isExpired() {\n return this.cached.isExpired();\n }\n\n getCreationTime() {\n return this.cached.getCreationTime();\n }\n\n getId() {\n return this.cached.getId();\n }\n\n changeSessionId() {\n return this.cached.changeSessionId();\n }\n\n getLastAccessedTime() {\n return this.cached.getLastAccessedTime();\n }\n\n setMaxInactiveInterval(interval: number) {\n this.cached.setMaxInactiveInterval(interval);\n this.delta.set(MAX_INACTIVE_INTERVAL_KEY, interval);\n }\n\n getMaxInactiveInterval() {\n return this.cached.getMaxInactiveInterval();\n }\n\n getAttribute(name: string) {\n return this.cached.getAttribute(name);\n }\n\n getAttributeNames() {\n return this.cached.getAttributeNames();\n }\n\n setAttribute(name: string, value: string) {\n this.cached.setAttribute(name, value);\n this.delta.set(this.getSessionAttrNameKey(name), value);\n }\n\n removeAttribute(name: string) {\n this.cached.removeAttribute(name);\n this.delta.set(this.getSessionAttrNameKey(name), null);\n }\n}\n\ninterface RedisSessionExpirationStore {\n save(session: RedisSession): Promise<void>;\n remove(sessionId: string): Promise<void>;\n rename(session: RedisSession): Promise<void>;\n cleanupExpiredSessions(): Promise<void>;\n}\n\nclass SortedSetRedisSessionExpirationStore implements RedisSessionExpirationStore {\n private readonly redis: Redis;\n private readonly namespace: Namespace;\n private readonly expirationsKey: string;\n\n private cleanupCount = 100;\n\n constructor(redis: Redis, namespace: Namespace) {\n this.redis = redis;\n this.namespace = namespace;\n this.expirationsKey = `${this.namespace}:sessions:expirations`;\n }\n\n getSessionKey(sessionId: string) {\n return `${this.namespace}:sessions:${sessionId}`;\n }\n\n async save(session: RedisSession) {\n const expirationInMillis =\n session.getLastAccessedTime() + session.getMaxInactiveInterval() * 1000;\n await this.redis.zadd(this.expirationsKey, expirationInMillis, session.getId());\n }\n\n async remove(sessionId: string) {\n await this.redis.zrem(this.expirationsKey, sessionId);\n }\n\n async rename(session: RedisSession) {\n if (session.originalSessionId === null || session.originalSessionId === session.getId()) return;\n await this.remove(session.originalSessionId);\n await this.save(session);\n }\n\n async cleanupExpiredSessions() {\n const sessionIds = await this.redis.zrevrangebyscore(\n this.expirationsKey,\n Date.now(),\n 0,\n 'LIMIT',\n 0,\n this.cleanupCount\n );\n if (sessionIds.length === 0) return;\n for (const sessionId of sessionIds) {\n const sessionKey = this.getSessionKey(sessionId);\n // Checks if the session exists. By trying to access the session we only trigger a deletion\n // if the TTL is expired.\n await this.redis.exists(sessionKey);\n }\n }\n}\n\n// 15549130 <spring:session:sessions:uuid, hash<session>> + 300s\n// 15549147 <spring:session:sessions:expirations, sorted_set<uuid>> + 300s\n// 15548839 <spring:session:sessions:expires:uuid, string<empty>>\nexport class RedisIndexedSessionRepository implements SessionRepository<RedisSession> {\n private readonly redis: Redis;\n private readonly namespace: Namespace;\n private readonly expirationStore: RedisSessionExpirationStore;\n\n private defaultMaxInactiveInterval = 1800;\n\n constructor(redis: Redis, namespace: Namespace = 'spring:session') {\n this.redis = redis;\n this.namespace = namespace;\n this.expirationStore = new SortedSetRedisSessionExpirationStore(redis, namespace);\n }\n\n setDefaultMaxInactiveInterval(interval: number) {\n this.defaultMaxInactiveInterval = interval;\n }\n\n private getSessionKey(sessionId: string) {\n return `${this.namespace}:sessions:${sessionId}`;\n }\n\n private getExpiredKey(sessionId: string) {\n return `${this.namespace}:sessions:expires:${sessionId}`;\n }\n\n private getSessionAttrNameKey(name: string) {\n return ATTRIBUTE_PREFIX + name;\n }\n\n private getPrincipalKey(principalName: string) {\n return `${this.namespace}:index:${PRINCIPAL_NAME_INDEX_NAME}:${principalName}`;\n }\n\n private async getSession(id: string, allowExpired: boolean): Promise<RedisSession | null> {\n const key = this.getSessionKey(id);\n const entries = await this.redis.hgetall(key);\n if (!entries || Object.keys(entries).length === 0) return null;\n const loaded = new MapSession(id);\n if (!entries.creationTime) throw new Error('creationTime attribute not found');\n if (!entries.lastAccessedTime) throw new Error('lastAccessedTime attribute not found');\n if (!entries.maxInactiveInterval) throw new Error('maxInactiveInterval attribute not found');\n\n loaded.setCreationTime(Number(entries.creationTime));\n loaded.setLastAccessedTime(Number(entries.lastAccessedTime));\n loaded.setMaxInactiveInterval(Number(entries.maxInactiveInterval));\n\n for (const [key, value] of Object.entries(entries)) {\n if (key.startsWith(ATTRIBUTE_PREFIX)) {\n loaded.setAttribute(key.slice(ATTRIBUTE_PREFIX.length), value);\n }\n }\n\n if (!allowExpired && loaded.isExpired()) return null;\n const result = new RedisSession(loaded, false);\n result.originalLastAccessTime = loaded.getLastAccessedTime();\n return result;\n }\n\n private async saveDelta(session: RedisSession) {\n if (session.delta.size === 0) return;\n const sessionId = session.getId();\n const delta = new Map<string, string | number>();\n const removes: string[] = [];\n session.delta.forEach((value, key) => {\n if (value !== null) delta.set(key, value);\n else removes.push(key);\n });\n\n if (delta.size !== 0) {\n await this.redis.hset(this.getSessionKey(sessionId), delta);\n }\n if (removes.length !== 0) {\n await this.redis.hdel(this.getSessionKey(sessionId), ...removes);\n }\n\n const principalSessionKey = this.getSessionAttrNameKey(PRINCIPAL_NAME_INDEX_NAME);\n const securityPrincipalSessionKey = this.getSessionAttrNameKey(SPRING_SECURITY_CONTEXT);\n if (session.delta.has(principalSessionKey) || session.delta.has(securityPrincipalSessionKey)) {\n if (session.originalPrincipalName !== null) {\n const originalPrincipalRedisKey = this.getPrincipalKey(session.originalPrincipalName);\n await this.redis.srem(originalPrincipalRedisKey, sessionId);\n }\n const principal = resolveIndexesFor(session);\n session.originalPrincipalName = principal;\n if (principal !== null) {\n const principalRedisKey = this.getPrincipalKey(principal);\n await this.redis.sadd(principalRedisKey, sessionId);\n }\n }\n\n if (session.isNew) {\n // here your can send session created message to channel\n session.isNew = false;\n }\n\n const sessionExpireInSeconds = session.getMaxInactiveInterval();\n\n // createShadowKey start\n const keyToExpire = SESSION_EXPIRES_PREFIX + session.getId();\n const sessionKey = this.getSessionKey(keyToExpire);\n\n if (sessionExpireInSeconds < 0) {\n await this.redis.append(sessionKey, '');\n await this.redis.persist(sessionKey);\n await this.redis.persist(this.getSessionKey(session.getId()));\n } else if (sessionExpireInSeconds === 0) {\n await this.redis.del(sessionKey);\n } else {\n await this.redis.append(sessionKey, '');\n await this.redis.expire(sessionKey, sessionExpireInSeconds);\n }\n // createShadowKey end\n\n if (sessionExpireInSeconds > 0) {\n const fiveMinutesAfterExpires = sessionExpireInSeconds + 5 * 60;\n await this.redis.expire(this.getSessionKey(session.getId()), fiveMinutesAfterExpires);\n }\n\n await this.expirationStore.save(session);\n session.delta.clear();\n }\n\n private async saveChangeSessionId(session: RedisSession) {\n const sessionId = session.getId();\n if (sessionId === session.originalSessionId) return;\n if (!session.isNew) {\n const originalSessionIdKey = this.getSessionKey(session.originalSessionId);\n const sessionIdKey = this.getSessionKey(sessionId);\n const originalExpiredKey = this.getExpiredKey(session.originalSessionId);\n const expiredKey = this.getExpiredKey(sessionId);\n\n await this.redis.rename(originalSessionIdKey, sessionIdKey);\n await this.redis.rename(originalExpiredKey, expiredKey);\n\n if (session.originalPrincipalName !== null) {\n const originalPrincipalRedisKey = this.getPrincipalKey(session.originalPrincipalName);\n await this.redis.srem(originalPrincipalRedisKey, session.originalSessionId);\n await this.redis.sadd(originalPrincipalRedisKey, sessionId);\n }\n await this.expirationStore.rename(session);\n }\n session.originalSessionId = sessionId;\n }\n\n // override\n createSession() {\n const cached = new MapSession();\n cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);\n return new RedisSession(cached, true);\n }\n\n async save(session: RedisSession) {\n await this.saveChangeSessionId(session);\n await this.saveDelta(session);\n }\n\n async findById(sessionId: string): Promise<RedisSession | null> {\n return this.getSession(sessionId, false);\n }\n\n async deleteById(sessionId: string) {\n const session = await this.getSession(sessionId, true);\n if (!session) return;\n // 1. cleanup index\n const principal = resolveIndexesFor(session);\n if (principal !== null) {\n await this.redis.srem(this.getPrincipalKey(principal), sessionId);\n }\n // 2. delete in set\n await this.expirationStore.remove(sessionId);\n // 3. delete expired key\n const expiredKey = this.getExpiredKey(sessionId);\n await this.redis.del(expiredKey);\n\n session.setMaxInactiveInterval(0);\n await this.save(session);\n }\n\n async findByIndexNameAndIndexValue(indexName: string, indexValue: string) {\n if (indexName !== PRINCIPAL_NAME_INDEX_NAME) {\n return new Map<string, RedisSession>();\n }\n\n const principalKey = this.getPrincipalKey(indexValue);\n const sessionIds = await this.redis.smembers(principalKey);\n if (!sessionIds || sessionIds.length === 0) {\n return new Map<string, RedisSession>();\n }\n const sessions = new Map<string, RedisSession>();\n for (const sessionId of sessionIds) {\n const session = await this.findById(sessionId);\n if (session !== null) {\n sessions.set(sessionId, session);\n }\n }\n return sessions;\n }\n\n async findByPrincipalName(principalName: string) {\n return this.findByIndexNameAndIndexValue(PRINCIPAL_NAME_INDEX_NAME, principalName);\n }\n\n async cleanupExpiredSessions() {\n await this.expirationStore.cleanupExpiredSessions();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA2B;AAI3B,IAAM,gCAAgC;AAEtC,IAAM,oBAAoB;AAC1B,IAAM,yBAAyB;AAC/B,IAAM,4BAA4B;AAClC,IAAM,mBAAmB;AACzB,IAAM,yBAAyB;AAE/B,IAAM,0BAA0B;AACzB,IAAM,4BAA4B;AAEzC,SAAS,aAAa;AACpB,aAAO,0BAAW;AACpB;AAEA,IAAM,aAAN,MAAoC;AAAA,EAC1B;AAAA,EACA,eAAe,oBAAI,IAA6B;AAAA,EAChD,eAAe,KAAK,IAAI;AAAA,EACxB,mBAAmB,KAAK;AAAA,EACxB,sBAAsB;AAAA,EAI9B,YAAY,OAA0B;AACpC,QAAI,OAAO,UAAU,aAAa;AAChC,WAAK,KAAK,WAAW;AAAA,IACvB,WAAW,OAAO,UAAU,UAAU;AACpC,WAAK,KAAK;AAAA,IACZ,OAAO;AACL,YAAM,UAAU;AAChB,WAAK,KAAK,QAAQ,MAAM;AAExB,iBAAW,YAAY,QAAQ,kBAAkB,GAAG;AAClD,cAAM,YAAY,QAAQ,aAAa,QAAQ;AAC/C,YAAI,cAAc,MAAM;AACtB,eAAK,aAAa,UAAU,SAAS;AAAA,QACvC;AAAA,MACF;AAEA,WAAK,mBAAmB,QAAQ,oBAAoB;AACpD,WAAK,eAAe,QAAQ,gBAAgB;AAC5C,WAAK,sBAAsB,QAAQ,uBAAuB;AAAA,IAC5D;AAAA,EACF;AAAA,EAEA,gBAAgB,cAAsB;AACpC,SAAK,eAAe;AAAA,EACtB;AAAA;AAAA,EAGA,QAAQ;AACN,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,kBAAkB;AAChB,UAAM,YAAY,WAAW;AAC7B,SAAK,KAAK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BAA6B,WAAmB,YAAoB;AACxE,QAAI,cAAc,2BAA2B;AAC3C,aAAO,oBAAI,IAA0B;AAAA,IACvC;AAEA,UAAM,eAAe,KAAK,gBAAgB,UAAU;AACpD,UAAM,aAAa,MAAM,KAAK,MAAM,SAAS,YAAY;AACzD,QAAI,CAAC,cAAc,WAAW,WAAW,GAAG;AAC1C,aAAO,oBAAI,IAA0B;AAAA,IACvC;AACA,UAAM,WAAW,oBAAI,IAA0B;AAC/C,eAAW,aAAa,YAAY;AAClC,YAAM,UAAU,MAAM,KAAK,SAAS,SAAS;AAC7C,UAAI,YAAY,MAAM;AACpB,iBAAS,IAAI,WAAW,OAAO;AAAA,MACjC;AAAA,IACF;AACA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,oBAAoB,eAAuB;AAC/C,WAAO,KAAK,6BAA6B,2BAA2B,aAAa;AAAA,EACnF;AAAA,EAEA,MAAM,yBAAyB;AAC7B,UAAM,KAAK,gBAAgB,uBAAuB;AAAA,EACpD;AACF;","names":["key"]}
@@ -0,0 +1,73 @@
1
+ import { SessionRepository, Session, Namespace } from './types.cjs';
2
+ import { Redis } from 'ioredis';
3
+ import '../core/index.cjs';
4
+
5
+ declare const PRINCIPAL_NAME_INDEX_NAME = "org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME";
6
+ declare class MapSession implements Session {
7
+ private id;
8
+ private sessionAttrs;
9
+ private creationTime;
10
+ private lastAccessedTime;
11
+ private maxInactiveInterval;
12
+ constructor(id?: string);
13
+ constructor(session: Session);
14
+ setCreationTime(creationTime: number): void;
15
+ getId(): string;
16
+ changeSessionId(): `${string}-${string}-${string}-${string}-${string}`;
17
+ getAttribute(name: string): string | number | null;
18
+ getAttributeNames(): string[];
19
+ setAttribute(name: string, value: string | number): void;
20
+ removeAttribute(name: string): void;
21
+ getCreationTime(): number;
22
+ getLastAccessedTime(): number;
23
+ setLastAccessedTime(lastAccessedTime: number): void;
24
+ getMaxInactiveInterval(): number;
25
+ setMaxInactiveInterval(interval: number): void;
26
+ isExpired(): boolean;
27
+ }
28
+ declare class RedisSession implements Session {
29
+ private readonly cached;
30
+ isNew: boolean;
31
+ delta: Map<string, string | number | null>;
32
+ originalSessionId: string;
33
+ originalPrincipalName: string | null;
34
+ originalLastAccessTime: number | null;
35
+ constructor(cached: MapSession, isNew: boolean);
36
+ getSessionAttrNameKey(name: string): string;
37
+ setLastAccessedTime(lastAccessedTime: number): void;
38
+ isExpired(): boolean;
39
+ getCreationTime(): number;
40
+ getId(): string;
41
+ changeSessionId(): `${string}-${string}-${string}-${string}-${string}`;
42
+ getLastAccessedTime(): number;
43
+ setMaxInactiveInterval(interval: number): void;
44
+ getMaxInactiveInterval(): number;
45
+ getAttribute(name: string): string | number | null;
46
+ getAttributeNames(): string[];
47
+ setAttribute(name: string, value: string): void;
48
+ removeAttribute(name: string): void;
49
+ }
50
+ declare class RedisIndexedSessionRepository implements SessionRepository<RedisSession> {
51
+ private readonly redis;
52
+ private readonly namespace;
53
+ private readonly expirationStore;
54
+ private defaultMaxInactiveInterval;
55
+ constructor(redis: Redis, namespace?: Namespace);
56
+ setDefaultMaxInactiveInterval(interval: number): void;
57
+ private getSessionKey;
58
+ private getExpiredKey;
59
+ private getSessionAttrNameKey;
60
+ private getPrincipalKey;
61
+ private getSession;
62
+ private saveDelta;
63
+ private saveChangeSessionId;
64
+ createSession(): RedisSession;
65
+ save(session: RedisSession): Promise<void>;
66
+ findById(sessionId: string): Promise<RedisSession | null>;
67
+ deleteById(sessionId: string): Promise<void>;
68
+ findByIndexNameAndIndexValue(indexName: string, indexValue: string): Promise<Map<string, RedisSession>>;
69
+ findByPrincipalName(principalName: string): Promise<Map<string, RedisSession>>;
70
+ cleanupExpiredSessions(): Promise<void>;
71
+ }
72
+
73
+ export { PRINCIPAL_NAME_INDEX_NAME, RedisIndexedSessionRepository };
@@ -0,0 +1,73 @@
1
+ import { SessionRepository, Session, Namespace } from './types.js';
2
+ import { Redis } from 'ioredis';
3
+ import '../core/index.js';
4
+
5
+ declare const PRINCIPAL_NAME_INDEX_NAME = "org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME";
6
+ declare class MapSession implements Session {
7
+ private id;
8
+ private sessionAttrs;
9
+ private creationTime;
10
+ private lastAccessedTime;
11
+ private maxInactiveInterval;
12
+ constructor(id?: string);
13
+ constructor(session: Session);
14
+ setCreationTime(creationTime: number): void;
15
+ getId(): string;
16
+ changeSessionId(): `${string}-${string}-${string}-${string}-${string}`;
17
+ getAttribute(name: string): string | number | null;
18
+ getAttributeNames(): string[];
19
+ setAttribute(name: string, value: string | number): void;
20
+ removeAttribute(name: string): void;
21
+ getCreationTime(): number;
22
+ getLastAccessedTime(): number;
23
+ setLastAccessedTime(lastAccessedTime: number): void;
24
+ getMaxInactiveInterval(): number;
25
+ setMaxInactiveInterval(interval: number): void;
26
+ isExpired(): boolean;
27
+ }
28
+ declare class RedisSession implements Session {
29
+ private readonly cached;
30
+ isNew: boolean;
31
+ delta: Map<string, string | number | null>;
32
+ originalSessionId: string;
33
+ originalPrincipalName: string | null;
34
+ originalLastAccessTime: number | null;
35
+ constructor(cached: MapSession, isNew: boolean);
36
+ getSessionAttrNameKey(name: string): string;
37
+ setLastAccessedTime(lastAccessedTime: number): void;
38
+ isExpired(): boolean;
39
+ getCreationTime(): number;
40
+ getId(): string;
41
+ changeSessionId(): `${string}-${string}-${string}-${string}-${string}`;
42
+ getLastAccessedTime(): number;
43
+ setMaxInactiveInterval(interval: number): void;
44
+ getMaxInactiveInterval(): number;
45
+ getAttribute(name: string): string | number | null;
46
+ getAttributeNames(): string[];
47
+ setAttribute(name: string, value: string): void;
48
+ removeAttribute(name: string): void;
49
+ }
50
+ declare class RedisIndexedSessionRepository implements SessionRepository<RedisSession> {
51
+ private readonly redis;
52
+ private readonly namespace;
53
+ private readonly expirationStore;
54
+ private defaultMaxInactiveInterval;
55
+ constructor(redis: Redis, namespace?: Namespace);
56
+ setDefaultMaxInactiveInterval(interval: number): void;
57
+ private getSessionKey;
58
+ private getExpiredKey;
59
+ private getSessionAttrNameKey;
60
+ private getPrincipalKey;
61
+ private getSession;
62
+ private saveDelta;
63
+ private saveChangeSessionId;
64
+ createSession(): RedisSession;
65
+ save(session: RedisSession): Promise<void>;
66
+ findById(sessionId: string): Promise<RedisSession | null>;
67
+ deleteById(sessionId: string): Promise<void>;
68
+ findByIndexNameAndIndexValue(indexName: string, indexValue: string): Promise<Map<string, RedisSession>>;
69
+ findByPrincipalName(principalName: string): Promise<Map<string, RedisSession>>;
70
+ cleanupExpiredSessions(): Promise<void>;
71
+ }
72
+
73
+ export { PRINCIPAL_NAME_INDEX_NAME, RedisIndexedSessionRepository };