@gauts/auth 0.1.3 → 0.2.1
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.
- package/README.md +103 -59
- package/SECURITY.md +1 -3
- package/dist/adapters/hono/index.d.ts +12 -6
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +72 -17
- package/dist/adapters/hono/index.js.map +1 -1
- package/dist/adapters/redis/index.d.ts +5 -1
- package/dist/adapters/redis/index.d.ts.map +1 -1
- package/dist/adapters/redis/index.js +16 -7
- package/dist/adapters/redis/index.js.map +1 -1
- package/dist/auth.d.ts +3 -3
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +21 -15
- package/dist/auth.js.map +1 -1
- package/dist/client/index.d.ts +8 -6
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +4 -11
- package/dist/client/index.js.map +1 -1
- package/dist/config.d.ts +10 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +118 -19
- package/dist/config.js.map +1 -1
- package/dist/errors.d.ts +7 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +4 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/password/index.d.ts +4 -1
- package/dist/password/index.d.ts.map +1 -1
- package/dist/password/index.js +16 -9
- package/dist/password/index.js.map +1 -1
- package/dist/session/schema.d.ts.map +1 -1
- package/dist/session/schema.js +19 -10
- package/dist/session/schema.js.map +1 -1
- package/dist/session/service.d.ts +3 -3
- package/dist/session/service.d.ts.map +1 -1
- package/dist/session/service.js +128 -67
- package/dist/session/service.js.map +1 -1
- package/dist/session/token.d.ts.map +1 -1
- package/dist/session/token.js +6 -2
- package/dist/session/token.js.map +1 -1
- package/dist/session/types.d.ts +52 -25
- package/dist/session/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/auth.js
CHANGED
|
@@ -2,31 +2,37 @@ import { resolvePasswordConfig, resolveSessionConfig } from "./config.js";
|
|
|
2
2
|
import { createError } from "./errors.js";
|
|
3
3
|
import { createPassword } from "./password/index.js";
|
|
4
4
|
import { createSessionService } from "./session/service.js";
|
|
5
|
-
const requireMethods = (name, value,
|
|
5
|
+
const requireMethods = ({ methods, name, value, }) => {
|
|
6
6
|
if (typeof value !== "object" || value === null) {
|
|
7
|
-
throw createError(
|
|
7
|
+
throw createError({
|
|
8
|
+
code: "AUTH_CONFIG_INVALID",
|
|
9
|
+
message: `${name} adapter is invalid.`,
|
|
10
|
+
});
|
|
8
11
|
}
|
|
9
12
|
const adapter = value;
|
|
10
13
|
if (methods.some((method) => typeof adapter[method] !== "function")) {
|
|
11
|
-
throw createError(
|
|
14
|
+
throw createError({
|
|
15
|
+
code: "AUTH_CONFIG_INVALID",
|
|
16
|
+
message: `${name} adapter is invalid.`,
|
|
17
|
+
});
|
|
12
18
|
}
|
|
13
19
|
};
|
|
14
|
-
export const createAuth = ({
|
|
15
|
-
requireMethods(
|
|
16
|
-
"create",
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
export const createAuth = ({ password, records, redis, session, }) => {
|
|
21
|
+
requireMethods({
|
|
22
|
+
methods: ["create", "delete", "exists", "get", "getMany", "keep", "update"],
|
|
23
|
+
name: "Redis",
|
|
24
|
+
value: redis,
|
|
25
|
+
});
|
|
26
|
+
requireMethods({
|
|
27
|
+
methods: ["create", "find", "findActive", "revoke", "updateExpiry"],
|
|
28
|
+
name: "Records",
|
|
29
|
+
value: records,
|
|
30
|
+
});
|
|
25
31
|
return {
|
|
26
32
|
password: createPassword(resolvePasswordConfig(password)),
|
|
27
33
|
session: createSessionService({
|
|
28
|
-
audit,
|
|
29
34
|
config: resolveSessionConfig(session),
|
|
35
|
+
records,
|
|
30
36
|
redis,
|
|
31
37
|
}),
|
|
32
38
|
};
|
package/dist/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAwB,MAAM,qBAAqB,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAwB,MAAM,qBAAqB,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AA4B5D,MAAM,cAAc,GAAG,CAAC,EACtB,OAAO,EACP,IAAI,EACJ,KAAK,GACe,EAAQ,EAAE;IAC9B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,MAAM,WAAW,CAAC;YAChB,IAAI,EAAE,qBAAqB;YAC3B,OAAO,EAAE,GAAG,IAAI,sBAAsB;SACvC,CAAC,CAAC;IACL,CAAC;IAED,MAAM,OAAO,GAAG,KAAgC,CAAC;IAEjD,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,UAAU,CAAC,EAAE,CAAC;QACpE,MAAM,WAAW,CAAC;YAChB,IAAI,EAAE,qBAAqB;YAC3B,OAAO,EAAE,GAAG,IAAI,sBAAsB;SACvC,CAAC,CAAC;IACL,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,UAAU,GAAG,CAAiD,EACzE,QAAQ,EACR,OAAO,EACP,KAAK,EACL,OAAO,GACE,EAAe,EAAE;IAC1B,cAAc,CAAC;QACb,OAAO,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC;QAC3E,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,KAAK;KACb,CAAC,CAAC;IACH,cAAc,CAAC;QACb,OAAO,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,cAAc,CAAC;QACnE,IAAI,EAAE,SAAS;QACf,KAAK,EAAE,OAAO;KACf,CAAC,CAAC;IAEH,OAAO;QACL,QAAQ,EAAE,cAAc,CAAC,qBAAqB,CAAC,QAAQ,CAAC,CAAC;QACzD,OAAO,EAAE,oBAAoB,CAAC;YAC5B,MAAM,EAAE,oBAAoB,CAAC,OAAO,CAAC;YACrC,OAAO;YACP,KAAK;SACN,CAAC;KACH,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
+
import type { SessionValidation } from "../config.js";
|
|
1
2
|
export type SessionClientInput = {
|
|
2
|
-
country?: string | null;
|
|
3
3
|
ip?: string | null;
|
|
4
4
|
platform?: string | null;
|
|
5
5
|
userAgent?: string | null;
|
|
6
6
|
};
|
|
7
|
-
export type SessionIdentityInput = Pick<SessionClientInput, "ip" | "userAgent">;
|
|
8
7
|
export type SessionClient = {
|
|
9
|
-
country: string | null;
|
|
10
8
|
ip: string | null;
|
|
11
9
|
platform: string | null;
|
|
12
10
|
userAgent: string | null;
|
|
13
11
|
};
|
|
14
|
-
|
|
12
|
+
type MatchesClientProps = {
|
|
13
|
+
current: SessionClient;
|
|
14
|
+
stored: SessionClient;
|
|
15
|
+
validation: readonly SessionValidation[];
|
|
16
|
+
};
|
|
15
17
|
export declare const normalizeIp: (input?: string | null) => string | null;
|
|
16
18
|
export declare const normalizeClient: (input: SessionClientInput) => SessionClient;
|
|
17
|
-
export declare const
|
|
18
|
-
export
|
|
19
|
+
export declare const matchesClient: ({ current, stored, validation, }: MatchesClientProps) => boolean;
|
|
20
|
+
export {};
|
|
19
21
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEtD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,EAAE,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IAClB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,CAAC;AAEF,KAAK,kBAAkB,GAAG;IACxB,OAAO,EAAE,aAAa,CAAC;IACvB,MAAM,EAAE,aAAa,CAAC;IACtB,UAAU,EAAE,SAAS,iBAAiB,EAAE,CAAC;CAC1C,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,QAAQ,MAAM,GAAG,IAAI,KAAG,MAAM,GAAG,IAkB5D,CAAC;AAWF,eAAO,MAAM,eAAe,GAAI,OAAO,kBAAkB,KAAG,aAI1D,CAAC;AAEH,eAAO,MAAM,aAAa,GAAI,kCAI3B,kBAAkB,KAAG,OAEvB,CAAC"}
|
package/dist/client/index.js
CHANGED
|
@@ -13,24 +13,17 @@ export const normalizeIp = (input) => {
|
|
|
13
13
|
const normalized = ipaddr.process(value).toString();
|
|
14
14
|
return normalized === "::1" ? "127.0.0.1" : normalized;
|
|
15
15
|
};
|
|
16
|
-
const normalizeCountry = (input) => {
|
|
17
|
-
const value = input?.trim().toUpperCase();
|
|
18
|
-
return value && /^[A-Z]{2}$/.test(value) ? value : null;
|
|
19
|
-
};
|
|
20
16
|
const normalizePlatform = (input) => {
|
|
21
17
|
const value = input?.replace(/^"+|"+$/g, "").trim();
|
|
22
18
|
return value ? value.slice(0, 255) : null;
|
|
23
19
|
};
|
|
24
|
-
const normalizeUserAgent = (input) =>
|
|
20
|
+
const normalizeUserAgent = (input) => input?.length ? input : null;
|
|
25
21
|
export const normalizeClient = (input) => ({
|
|
26
|
-
country: normalizeCountry(input.country),
|
|
27
22
|
ip: normalizeIp(input.ip),
|
|
28
23
|
platform: normalizePlatform(input.platform),
|
|
29
24
|
userAgent: normalizeUserAgent(input.userAgent),
|
|
30
25
|
});
|
|
31
|
-
export const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
});
|
|
35
|
-
export const matchesClient = (stored, current) => stored.ip === current.ip && stored.userAgent === current.userAgent;
|
|
26
|
+
export const matchesClient = ({ current, stored, validation, }) => {
|
|
27
|
+
return validation.every((field) => stored[field] === current[field]);
|
|
28
|
+
};
|
|
36
29
|
//# sourceMappingURL=index.js.map
|
package/dist/client/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,WAAW,CAAC;AAqB/B,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAqB,EAAiB,EAAE;IAClE,IAAI,KAAK,GAAG,KAAK,EAAE,IAAI,EAAE,CAAC;IAE1B,IAAI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AM,CAAC,MAAM,aAAa,GAAG,CAAC,EAC5B,OAAO,EACP,MAAM,EACN,UAAU,GACS,EAAW,EAAE;IAChC,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;AACvE,CAAC,CAAC"}
|
package/dist/config.d.ts
CHANGED
|
@@ -13,29 +13,35 @@ export type BcryptConfig = {
|
|
|
13
13
|
verifyMaxBytes?: number;
|
|
14
14
|
};
|
|
15
15
|
export type PasswordConfig = Argon2idConfig | BcryptConfig;
|
|
16
|
+
export type SessionValidation = "ip" | "platform" | "userAgent";
|
|
16
17
|
export type SessionConfig = {
|
|
17
18
|
max?: number;
|
|
18
|
-
|
|
19
|
+
renewInterval?: number;
|
|
19
20
|
ttl?: number;
|
|
21
|
+
validation?: readonly SessionValidation[];
|
|
20
22
|
};
|
|
21
|
-
|
|
23
|
+
type ResolvedArgon2idConfig = {
|
|
22
24
|
algorithm: "argon2id";
|
|
23
25
|
hashLength: number;
|
|
24
26
|
maxBytes: number;
|
|
25
27
|
memoryCost: number;
|
|
26
28
|
parallelism: number;
|
|
27
29
|
timeCost: number;
|
|
28
|
-
}
|
|
30
|
+
};
|
|
31
|
+
type ResolvedBcryptConfig = {
|
|
29
32
|
algorithm: "bcrypt";
|
|
30
33
|
maxBytes: number;
|
|
31
34
|
rounds: number;
|
|
32
35
|
verifyMaxBytes: number;
|
|
33
36
|
};
|
|
37
|
+
export type ResolvedPasswordConfig = ResolvedArgon2idConfig | ResolvedBcryptConfig;
|
|
34
38
|
export type ResolvedSessionConfig = {
|
|
35
39
|
max: number;
|
|
36
|
-
|
|
40
|
+
renewInterval: number;
|
|
37
41
|
ttl: number;
|
|
42
|
+
validation: readonly SessionValidation[];
|
|
38
43
|
};
|
|
39
44
|
export declare const resolvePasswordConfig: (config?: PasswordConfig) => ResolvedPasswordConfig;
|
|
40
45
|
export declare const resolveSessionConfig: (config?: SessionConfig) => ResolvedSessionConfig;
|
|
46
|
+
export {};
|
|
41
47
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,cAAc,GAAG;IAC3B,SAAS,CAAC,EAAE,UAAU,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,SAAS,EAAE,QAAQ,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,cAAc,GAAG,YAAY,CAAC;AAE3D,MAAM,MAAM,aAAa,GAAG;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,cAAc,GAAG;IAC3B,SAAS,CAAC,EAAE,UAAU,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,SAAS,EAAE,QAAQ,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAA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
15
15
|
};
|
|
16
16
|
const SESSION_DEFAULTS = {
|
|
17
17
|
max: 10,
|
|
18
|
-
|
|
18
|
+
renewInterval: 60 * 60 * 24,
|
|
19
19
|
ttl: 60 * 60 * 24 * 7,
|
|
20
|
+
validation: ["userAgent"],
|
|
20
21
|
};
|
|
21
|
-
const
|
|
22
|
+
const SESSION_VALIDATION = new Set([
|
|
23
|
+
"ip",
|
|
24
|
+
"platform",
|
|
25
|
+
"userAgent",
|
|
26
|
+
]);
|
|
27
|
+
const requireInteger = ({ max, min, name, value }) => {
|
|
22
28
|
if (!Number.isInteger(value) || value < min || value > max) {
|
|
23
|
-
throw createError(
|
|
29
|
+
throw createError({
|
|
30
|
+
code: "AUTH_CONFIG_INVALID",
|
|
31
|
+
message: `${name} must be an integer between ${String(min)} and ${String(max)}.`,
|
|
32
|
+
});
|
|
24
33
|
}
|
|
25
34
|
return value;
|
|
26
35
|
};
|
|
36
|
+
const resolveSessionValidation = (input) => {
|
|
37
|
+
if (!Array.isArray(input)) {
|
|
38
|
+
throw createError({
|
|
39
|
+
code: "AUTH_CONFIG_INVALID",
|
|
40
|
+
message: "session.validation contains an invalid field.",
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
const fields = input;
|
|
44
|
+
if (fields.some((field) => typeof field !== "string" ||
|
|
45
|
+
!SESSION_VALIDATION.has(field))) {
|
|
46
|
+
throw createError({
|
|
47
|
+
code: "AUTH_CONFIG_INVALID",
|
|
48
|
+
message: "session.validation contains an invalid field.",
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
if (new Set(fields).size !== fields.length) {
|
|
52
|
+
throw createError({
|
|
53
|
+
code: "AUTH_CONFIG_INVALID",
|
|
54
|
+
message: "session.validation contains duplicate fields.",
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return fields.map((field) => field);
|
|
58
|
+
};
|
|
27
59
|
export const resolvePasswordConfig = (config = {}) => {
|
|
28
60
|
const input = config;
|
|
29
61
|
if (typeof input !== "object" || input === null) {
|
|
30
|
-
throw createError(
|
|
62
|
+
throw createError({
|
|
63
|
+
code: "AUTH_CONFIG_INVALID",
|
|
64
|
+
message: "Password configuration is invalid.",
|
|
65
|
+
});
|
|
31
66
|
}
|
|
32
67
|
const algorithm = config.algorithm;
|
|
33
|
-
if (algorithm !== undefined &&
|
|
34
|
-
|
|
68
|
+
if (algorithm !== undefined &&
|
|
69
|
+
algorithm !== "argon2id" &&
|
|
70
|
+
algorithm !== "bcrypt") {
|
|
71
|
+
throw createError({
|
|
72
|
+
code: "AUTH_CONFIG_INVALID",
|
|
73
|
+
message: "Password algorithm is invalid.",
|
|
74
|
+
});
|
|
35
75
|
}
|
|
36
76
|
if (config.algorithm === "bcrypt") {
|
|
37
77
|
const resolved = { ...BCRYPT_DEFAULTS, ...config };
|
|
38
78
|
return {
|
|
39
79
|
algorithm: "bcrypt",
|
|
40
|
-
maxBytes: requireInteger(
|
|
41
|
-
|
|
42
|
-
|
|
80
|
+
maxBytes: requireInteger({
|
|
81
|
+
max: 72,
|
|
82
|
+
min: 1,
|
|
83
|
+
name: "password.maxBytes",
|
|
84
|
+
value: resolved.maxBytes,
|
|
85
|
+
}),
|
|
86
|
+
rounds: requireInteger({
|
|
87
|
+
max: 31,
|
|
88
|
+
min: 4,
|
|
89
|
+
name: "password.rounds",
|
|
90
|
+
value: resolved.rounds,
|
|
91
|
+
}),
|
|
92
|
+
verifyMaxBytes: requireInteger({
|
|
93
|
+
max: 1_048_576,
|
|
94
|
+
min: resolved.maxBytes,
|
|
95
|
+
name: "password.verifyMaxBytes",
|
|
96
|
+
value: resolved.verifyMaxBytes,
|
|
97
|
+
}),
|
|
43
98
|
};
|
|
44
99
|
}
|
|
45
100
|
const resolved = { ...ARGON_DEFAULTS, ...config };
|
|
46
101
|
return {
|
|
47
102
|
algorithm: "argon2id",
|
|
48
|
-
hashLength: requireInteger(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
103
|
+
hashLength: requireInteger({
|
|
104
|
+
max: 64,
|
|
105
|
+
min: 16,
|
|
106
|
+
name: "password.hashLength",
|
|
107
|
+
value: resolved.hashLength,
|
|
108
|
+
}),
|
|
109
|
+
maxBytes: requireInteger({
|
|
110
|
+
max: 1_048_576,
|
|
111
|
+
min: 1,
|
|
112
|
+
name: "password.maxBytes",
|
|
113
|
+
value: resolved.maxBytes,
|
|
114
|
+
}),
|
|
115
|
+
memoryCost: requireInteger({
|
|
116
|
+
max: 1_048_576,
|
|
117
|
+
min: 8_192,
|
|
118
|
+
name: "password.memoryCost",
|
|
119
|
+
value: resolved.memoryCost,
|
|
120
|
+
}),
|
|
121
|
+
parallelism: requireInteger({
|
|
122
|
+
max: 16,
|
|
123
|
+
min: 1,
|
|
124
|
+
name: "password.parallelism",
|
|
125
|
+
value: resolved.parallelism,
|
|
126
|
+
}),
|
|
127
|
+
timeCost: requireInteger({
|
|
128
|
+
max: 10,
|
|
129
|
+
min: 1,
|
|
130
|
+
name: "password.timeCost",
|
|
131
|
+
value: resolved.timeCost,
|
|
132
|
+
}),
|
|
53
133
|
};
|
|
54
134
|
};
|
|
55
135
|
export const resolveSessionConfig = (config = {}) => {
|
|
56
136
|
const input = config;
|
|
57
137
|
if (typeof input !== "object" || input === null) {
|
|
58
|
-
throw createError(
|
|
138
|
+
throw createError({
|
|
139
|
+
code: "AUTH_CONFIG_INVALID",
|
|
140
|
+
message: "Session configuration is invalid.",
|
|
141
|
+
});
|
|
59
142
|
}
|
|
60
143
|
const resolved = { ...SESSION_DEFAULTS, ...config };
|
|
61
|
-
const ttl = requireInteger(
|
|
62
|
-
|
|
144
|
+
const ttl = requireInteger({
|
|
145
|
+
max: 60 * 60 * 24 * 365,
|
|
146
|
+
min: 60,
|
|
147
|
+
name: "session.ttl",
|
|
148
|
+
value: resolved.ttl,
|
|
149
|
+
});
|
|
150
|
+
const renewInterval = requireInteger({
|
|
151
|
+
max: ttl - 1,
|
|
152
|
+
min: 1,
|
|
153
|
+
name: "session.renewInterval",
|
|
154
|
+
value: resolved.renewInterval,
|
|
155
|
+
});
|
|
63
156
|
return {
|
|
64
|
-
max: requireInteger(
|
|
65
|
-
|
|
157
|
+
max: requireInteger({
|
|
158
|
+
max: 10_000,
|
|
159
|
+
min: 1,
|
|
160
|
+
name: "session.max",
|
|
161
|
+
value: resolved.max,
|
|
162
|
+
}),
|
|
163
|
+
renewInterval,
|
|
66
164
|
ttl,
|
|
165
|
+
validation: resolveSessionValidation(config.validation ?? SESSION_DEFAULTS.validation),
|
|
67
166
|
};
|
|
68
167
|
};
|
|
69
168
|
//# sourceMappingURL=config.js.map
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AA8D1C,MAAM,cAAc,GAAG;IACrB,SAAS,EAAE,UAAU;IACrB,UAAU,EAAE,EAAE;IACd,QAAQ,EAAE,IAAI;IACd,UAAU,EAAE,MAAM;IAClB,WAAW,EAAE,CAAC;IACd,QAAQ,EAAE,CAAC;CACH,CAAC;AAEX,MAAM,eAAe,GAAG;IACtB,SAAS,EAAE,QAAQ;IACnB,QAAQ,EAAE,EAAE;IACZ,MAAM,EAAE,EAAE;IACV,cAAc,EAAE,EAAE;CACV,CAAC;AAEX,MAAM,gBAAgB,GAAG;IACvB,GAAG,EAAE,EAAE;IACP,aAAa,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE;IAC3B,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;IACrB,UAAU,EAAE,CAAC,WAAW,CAAC;CACjB,CAAC;AAEX,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAoB;IACpD,IAAI;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AAC;IAED,MAAM,QAAQ,GAAG,EAAE,GAAG,gBAAgB,EAAE,GAAG,MAAM,EAAE,CAAC;IAEpD,MAAM,GAAG,GAAG,cAAc,CAAC;QACzB,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG;QACvB,GAAG,EAAE,EAAE;QACP,IAAI,EAAE,aAAa;QACnB,KAAK,EAAE,QAAQ,CAAC,GAAG;KACpB,CAAC,CAAC;IAEH,MAAM,aAAa,GAAG,cAAc,CAAC;QACnC,GAAG,EAAE,GAAG,GAAG,CAAC;QACZ,GAAG,EAAE,CAAC;QACN,IAAI,EAAE,uBAAuB;QAC7B,KAAK,EAAE,QAAQ,CAAC,aAAa;KAC9B,CAAC,CAAC;IAEH,OAAO;QACL,GAAG,EAAE,cAAc,CAAC;YAClB,GAAG,EAAE,MAAM;YACX,GAAG,EAAE,CAAC;YACN,IAAI,EAAE,aAAa;YACnB,KAAK,EAAE,QAAQ,CAAC,GAAG;SACpB,CAAC;QACF,aAAa;QACb,GAAG;QACH,UAAU,EAAE,wBAAwB,CAClC,MAAM,CAAC,UAAU,IAAI,gBAAgB,CAAC,UAAU,CACjD;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
export type AuthErrorCode = "AUTH_CONFIG_INVALID" | "PASSWORD_INPUT_INVALID" | "SESSION_CLIENT_MISMATCH" | "SESSION_DATA_INVALID" | "SESSION_INVALID" | "SESSION_LIMIT_REACHED" | "SESSION_NOT_FOUND" | "REDIS_UNAVAILABLE" | "
|
|
1
|
+
export type AuthErrorCode = "AUTH_CONFIG_INVALID" | "PASSWORD_INPUT_INVALID" | "SESSION_CLIENT_MISMATCH" | "SESSION_DATA_INVALID" | "SESSION_INVALID" | "SESSION_LIMIT_REACHED" | "SESSION_NOT_FOUND" | "REDIS_UNAVAILABLE" | "RECORDS_UNAVAILABLE";
|
|
2
2
|
export type AuthError = Error & {
|
|
3
3
|
code: AuthErrorCode;
|
|
4
4
|
};
|
|
5
|
-
export
|
|
5
|
+
export type AuthErrorInput = {
|
|
6
|
+
cause?: unknown;
|
|
7
|
+
code: AuthErrorCode;
|
|
8
|
+
message: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const createError: ({ cause, code, message, }: AuthErrorInput) => AuthError;
|
|
6
11
|
export declare const isAuthError: (error: unknown) => error is AuthError;
|
|
7
12
|
//# sourceMappingURL=errors.d.ts.map
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GACrB,qBAAqB,GACrB,wBAAwB,GACxB,yBAAyB,GACzB,sBAAsB,GACtB,iBAAiB,GACjB,uBAAuB,GACvB,mBAAmB,GACnB,mBAAmB,GACnB,
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GACrB,qBAAqB,GACrB,wBAAwB,GACxB,yBAAyB,GACzB,sBAAsB,GACtB,iBAAiB,GACjB,uBAAuB,GACvB,mBAAmB,GACnB,mBAAmB,GACnB,qBAAqB,CAAC;AAE1B,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG;IAC9B,IAAI,EAAE,aAAa,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,IAAI,EAAE,aAAa,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,2BAIzB,cAAc,KAAG,SAUnB,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,SAItB,CAAC"}
|
package/dist/errors.js
CHANGED
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAqBA,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,EAC1B,KAAK,EACL,IAAI,EACJ,OAAO,GACQ,EAAa,EAAE;IAC9B,MAAM,KAAK,GAAG,IAAI,KAAK,CACrB,OAAO,EACP,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAC/B,CAAC;IAEf,KAAK,CAAC,IAAI,GAAG,WAAW,CAAC;IACzB,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC;IAElB,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAc,EAAsB,EAAE,CAChE,KAAK,YAAY,KAAK;IACtB,KAAK,CAAC,IAAI,KAAK,WAAW;IAC1B,MAAM,IAAI,KAAK;IACf,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
export { createAuth } from "./auth.js";
|
|
2
2
|
export type { Auth, AuthConfig, AuthDeps } from "./auth.js";
|
|
3
|
-
export type { BcryptConfig, Argon2idConfig, PasswordConfig, SessionConfig } from "./config.js";
|
|
4
|
-
export {
|
|
3
|
+
export type { BcryptConfig, Argon2idConfig, PasswordConfig, SessionConfig, SessionValidation, } from "./config.js";
|
|
4
|
+
export { isAuthError } from "./errors.js";
|
|
5
5
|
export type { AuthError, AuthErrorCode } from "./errors.js";
|
|
6
6
|
export type { PasswordService } from "./password/index.js";
|
|
7
|
-
export {
|
|
8
|
-
export type {
|
|
9
|
-
export type { SessionClient, SessionClientInput, SessionIdentity, SessionIdentityInput, } from "./client/index.js";
|
|
7
|
+
export type { ActiveSession, CreateSessionRecord, CreatedSession, RedisSessionStore, ResolvedSession, Session, SessionInput, SessionRecord, SessionRecords, SessionService, } from "./session/types.js";
|
|
8
|
+
export type { SessionClient, SessionClientInput } from "./client/index.js";
|
|
10
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC5D,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC5D,YAAY,EACV,YAAY,EACZ,cAAc,EACd,cAAc,EACd,aAAa,EACb,iBAAiB,GAClB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5D,YAAY,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAC3D,YAAY,EACV,aAAa,EACb,mBAAmB,EACnB,cAAc,EACd,iBAAiB,EACjB,eAAe,EACf,OAAO,EACP,YAAY,EACZ,aAAa,EACb,cAAc,EACd,cAAc,GACf,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AASvC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/password/index.d.ts
CHANGED
|
@@ -2,7 +2,10 @@ import type { ResolvedPasswordConfig } from "../config.js";
|
|
|
2
2
|
export type PasswordService = {
|
|
3
3
|
algorithm: "argon2id" | "bcrypt";
|
|
4
4
|
hash(password: string): Promise<string>;
|
|
5
|
-
verify(
|
|
5
|
+
verify(input: {
|
|
6
|
+
password: string;
|
|
7
|
+
storedHash: string;
|
|
8
|
+
}): Promise<boolean>;
|
|
6
9
|
};
|
|
7
10
|
export declare const createPassword: (config: ResolvedPasswordConfig) => PasswordService;
|
|
8
11
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/password/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAG3D,MAAM,MAAM,eAAe,GAAG;IAC5B,SAAS,EAAE,UAAU,GAAG,QAAQ,CAAC;IACjC,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACxC,MAAM,CAAC,QAAQ,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/password/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAG3D,MAAM,MAAM,eAAe,GAAG;IAC5B,SAAS,EAAE,UAAU,GAAG,QAAQ,CAAC;IACjC,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACxC,MAAM,CAAC,KAAK,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC3E,CAAC;AAuEF,eAAO,MAAM,cAAc,GACzB,QAAQ,sBAAsB,KAC7B,eAIF,CAAC"}
|
package/dist/password/index.js
CHANGED
|
@@ -2,16 +2,19 @@ import * as argon2 from "argon2";
|
|
|
2
2
|
import { compare as compareBcrypt, hash as hashBcrypt } from "bcryptjs";
|
|
3
3
|
import { createError } from "../errors.js";
|
|
4
4
|
const BCRYPT_PATTERN = /^\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}$/;
|
|
5
|
-
const requirePassword = (password, maxBytes) => {
|
|
5
|
+
const requirePassword = ({ password, maxBytes, }) => {
|
|
6
6
|
const bytes = Buffer.byteLength(password, "utf8");
|
|
7
7
|
if (bytes < 1 || bytes > maxBytes) {
|
|
8
|
-
throw createError(
|
|
8
|
+
throw createError({
|
|
9
|
+
code: "PASSWORD_INPUT_INVALID",
|
|
10
|
+
message: `Password must contain between 1 and ${String(maxBytes)} bytes.`,
|
|
11
|
+
});
|
|
9
12
|
}
|
|
10
13
|
};
|
|
11
14
|
const createArgon2id = (config) => ({
|
|
12
15
|
algorithm: "argon2id",
|
|
13
16
|
hash: async (password) => {
|
|
14
|
-
requirePassword(
|
|
17
|
+
requirePassword({ maxBytes: config.maxBytes, password });
|
|
15
18
|
return argon2.hash(password, {
|
|
16
19
|
hashLength: config.hashLength,
|
|
17
20
|
memoryCost: config.memoryCost,
|
|
@@ -20,8 +23,8 @@ const createArgon2id = (config) => ({
|
|
|
20
23
|
type: argon2.argon2id,
|
|
21
24
|
});
|
|
22
25
|
},
|
|
23
|
-
verify: async (password, storedHash) => {
|
|
24
|
-
requirePassword(
|
|
26
|
+
verify: async ({ password, storedHash }) => {
|
|
27
|
+
requirePassword({ maxBytes: config.maxBytes, password });
|
|
25
28
|
if (!storedHash.startsWith("$argon2id$")) {
|
|
26
29
|
return false;
|
|
27
30
|
}
|
|
@@ -31,16 +34,20 @@ const createArgon2id = (config) => ({
|
|
|
31
34
|
const createBcrypt = (config) => ({
|
|
32
35
|
algorithm: "bcrypt",
|
|
33
36
|
hash: async (password) => {
|
|
34
|
-
requirePassword(
|
|
37
|
+
requirePassword({ maxBytes: config.maxBytes, password });
|
|
35
38
|
return hashBcrypt(password, config.rounds);
|
|
36
39
|
},
|
|
37
|
-
verify: async (password, storedHash) => {
|
|
38
|
-
requirePassword(
|
|
40
|
+
verify: async ({ password, storedHash }) => {
|
|
41
|
+
requirePassword({ maxBytes: config.verifyMaxBytes, password });
|
|
39
42
|
if (!BCRYPT_PATTERN.test(storedHash)) {
|
|
40
43
|
return false;
|
|
41
44
|
}
|
|
42
45
|
return compareBcrypt(password, storedHash);
|
|
43
46
|
},
|
|
44
47
|
});
|
|
45
|
-
export const createPassword = (config) =>
|
|
48
|
+
export const createPassword = (config) => {
|
|
49
|
+
return config.algorithm === "bcrypt"
|
|
50
|
+
? createBcrypt(config)
|
|
51
|
+
: createArgon2id(config);
|
|
52
|
+
};
|
|
46
53
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/password/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,IAAI,IAAI,UAAU,EAAE,MAAM,UAAU,CAAC;AAGxE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAQ3C,MAAM,cAAc,GAAG,sCAAsC,CAAC;AAE9D,MAAM,eAAe,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/password/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,IAAI,IAAI,UAAU,EAAE,MAAM,UAAU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OAAO,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;IAC7C,CAAC;CACF,CAAC,CAAC;AAEH,MAAM,YAAY,GAAG,CACnB,MAAgE,EAC/C,EAAE,CAAC,CAAC;IACrB,SAAS,EAAE,QAAQ;IAEnB,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE;QACvB,eAAe,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC;QAEzD,OAAO,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,EAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE;QACzC,eAAe,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,cAAc,EAAE,QAAQ,EAAE,CAAC,CAAC;QAE/D,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;YACrC,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,aAAa,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;IAC7C,CAAC;CACF,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,MAA8B,EACb,EAAE;IACnB,OAAO,MAAM,CAAC,SAAS,KAAK,QAAQ;QAClC,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC;QACtB,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;AAC7B,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/session/schema.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/session/schema.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAsBzD,eAAO,MAAM,aAAa,GAAI,KAAK,SAAS,MAAM,EAChD,SAAS,aAAa,CAAC,KAAK,CAAC,KAC5B,MAUF,CAAC;AAEF,eAAO,MAAM,YAAY,GAAI,KAAK,SAAS,MAAM,EAC/C,KAAK,MAAM,KACV,aAAa,CAAC,KAAK,CAAC,GAAG,IAuBzB,CAAC;AAEF,eAAO,MAAM,SAAS,GAAI,KAAK,SAAS,MAAM,EAC5C,QAAQ,aAAa,CAAC,KAAK,CAAC,KAC3B,OAAO,CAAC,KAAK,CAQd,CAAC"}
|
package/dist/session/schema.js
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
import { createError } from "../errors.js";
|
|
2
2
|
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3
|
-
const isNullableString = (value) =>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
const isNullableString = (value) => {
|
|
4
|
+
return typeof value === "string" || value === null;
|
|
5
|
+
};
|
|
6
|
+
const isDateString = (value) => {
|
|
7
|
+
return typeof value === "string" && Number.isFinite(Date.parse(value));
|
|
8
|
+
};
|
|
9
|
+
const isClient = (value) => {
|
|
10
|
+
return (isRecord(value) &&
|
|
11
|
+
isNullableString(value.ip) &&
|
|
12
|
+
isNullableString(value.platform) &&
|
|
13
|
+
isNullableString(value.userAgent));
|
|
14
|
+
};
|
|
10
15
|
export const encodeSession = (session) => {
|
|
11
16
|
try {
|
|
12
17
|
return JSON.stringify(session);
|
|
13
18
|
}
|
|
14
19
|
catch (error) {
|
|
15
|
-
throw createError(
|
|
20
|
+
throw createError({
|
|
21
|
+
cause: error,
|
|
22
|
+
code: "SESSION_DATA_INVALID",
|
|
23
|
+
message: "Session data must be JSON serializable.",
|
|
24
|
+
});
|
|
16
25
|
}
|
|
17
26
|
};
|
|
18
27
|
export const parseSession = (raw) => {
|
|
@@ -25,7 +34,7 @@ export const parseSession = (raw) => {
|
|
|
25
34
|
}
|
|
26
35
|
if (!isRecord(value) ||
|
|
27
36
|
typeof value.id !== "string" ||
|
|
28
|
-
typeof value.
|
|
37
|
+
typeof value.accountId !== "string" ||
|
|
29
38
|
!isRecord(value.data) ||
|
|
30
39
|
!isClient(value.client) ||
|
|
31
40
|
!isDateString(value.createdAt) ||
|
|
@@ -36,12 +45,12 @@ export const parseSession = (raw) => {
|
|
|
36
45
|
return value;
|
|
37
46
|
};
|
|
38
47
|
export const toSession = (stored) => ({
|
|
48
|
+
accountId: stored.accountId,
|
|
39
49
|
client: stored.client,
|
|
40
50
|
createdAt: new Date(stored.createdAt),
|
|
41
51
|
data: stored.data,
|
|
42
52
|
expiresAt: new Date(stored.expiresAt),
|
|
43
53
|
id: stored.id,
|
|
44
|
-
subjectId: stored.subjectId,
|
|
45
54
|
touchedAt: new Date(stored.touchedAt),
|
|
46
55
|
});
|
|
47
56
|
//# sourceMappingURL=schema.js.map
|