@technomoron/api-server-base 1.1.13 → 2.0.0-beta.10

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 (116) hide show
  1. package/README.txt +25 -2
  2. package/dist/cjs/api-server-base.cjs +448 -111
  3. package/dist/cjs/api-server-base.d.ts +91 -34
  4. package/dist/cjs/auth-api/auth-module.d.ts +105 -0
  5. package/dist/cjs/auth-api/auth-module.js +1180 -0
  6. package/dist/cjs/auth-api/compat-auth-storage.d.ts +57 -0
  7. package/dist/cjs/auth-api/compat-auth-storage.js +128 -0
  8. package/dist/cjs/auth-api/mem-auth-store.d.ts +68 -0
  9. package/dist/cjs/auth-api/mem-auth-store.js +141 -0
  10. package/dist/cjs/{auth-module.d.ts → auth-api/module.d.ts} +7 -7
  11. package/dist/cjs/{auth-module.cjs → auth-api/module.js} +1 -1
  12. package/dist/cjs/auth-api/sql-auth-store.d.ts +77 -0
  13. package/dist/cjs/auth-api/sql-auth-store.js +172 -0
  14. package/dist/cjs/auth-api/storage.d.ts +38 -0
  15. package/dist/cjs/{auth-storage.cjs → auth-api/storage.js} +17 -7
  16. package/dist/cjs/auth-api/types.d.ts +34 -0
  17. package/dist/cjs/auth-api/types.js +2 -0
  18. package/dist/cjs/index.cjs +41 -7
  19. package/dist/cjs/index.d.ts +29 -5
  20. package/dist/cjs/oauth/base.d.ts +10 -0
  21. package/dist/cjs/oauth/base.js +6 -0
  22. package/dist/cjs/oauth/memory.d.ts +16 -0
  23. package/dist/cjs/oauth/memory.js +99 -0
  24. package/dist/cjs/oauth/models.d.ts +45 -0
  25. package/dist/cjs/oauth/models.js +58 -0
  26. package/dist/cjs/oauth/sequelize.d.ts +68 -0
  27. package/dist/cjs/oauth/sequelize.js +210 -0
  28. package/dist/cjs/oauth/types.d.ts +50 -0
  29. package/dist/cjs/oauth/types.js +3 -0
  30. package/dist/cjs/passkey/base.d.ts +16 -0
  31. package/dist/cjs/passkey/base.js +6 -0
  32. package/dist/cjs/passkey/memory.d.ts +27 -0
  33. package/dist/cjs/passkey/memory.js +86 -0
  34. package/dist/cjs/passkey/models.d.ts +25 -0
  35. package/dist/cjs/passkey/models.js +115 -0
  36. package/dist/cjs/passkey/sequelize.d.ts +55 -0
  37. package/dist/cjs/passkey/sequelize.js +220 -0
  38. package/dist/cjs/passkey/service.d.ts +20 -0
  39. package/dist/cjs/passkey/service.js +356 -0
  40. package/dist/cjs/passkey/types.d.ts +78 -0
  41. package/dist/cjs/passkey/types.js +2 -0
  42. package/dist/cjs/token/base.d.ts +38 -0
  43. package/dist/cjs/token/base.js +114 -0
  44. package/dist/cjs/token/memory.d.ts +19 -0
  45. package/dist/cjs/token/memory.js +149 -0
  46. package/dist/cjs/token/sequelize.d.ts +58 -0
  47. package/dist/cjs/token/sequelize.js +404 -0
  48. package/dist/cjs/token/types.d.ts +27 -0
  49. package/dist/cjs/token/types.js +2 -0
  50. package/dist/cjs/user/base.d.ts +26 -0
  51. package/dist/cjs/user/base.js +45 -0
  52. package/dist/cjs/user/memory.d.ts +35 -0
  53. package/dist/cjs/user/memory.js +173 -0
  54. package/dist/cjs/user/sequelize.d.ts +41 -0
  55. package/dist/cjs/user/sequelize.js +182 -0
  56. package/dist/cjs/user/types.d.ts +11 -0
  57. package/dist/cjs/user/types.js +2 -0
  58. package/dist/esm/api-server-base.d.ts +91 -34
  59. package/dist/esm/api-server-base.js +447 -110
  60. package/dist/esm/auth-api/auth-module.d.ts +105 -0
  61. package/dist/esm/auth-api/auth-module.js +1178 -0
  62. package/dist/esm/auth-api/compat-auth-storage.d.ts +57 -0
  63. package/dist/esm/auth-api/compat-auth-storage.js +124 -0
  64. package/dist/esm/auth-api/mem-auth-store.d.ts +68 -0
  65. package/dist/esm/auth-api/mem-auth-store.js +137 -0
  66. package/dist/esm/{auth-module.d.ts → auth-api/module.d.ts} +7 -7
  67. package/dist/esm/{auth-module.js → auth-api/module.js} +1 -1
  68. package/dist/esm/auth-api/sql-auth-store.d.ts +77 -0
  69. package/dist/esm/auth-api/sql-auth-store.js +168 -0
  70. package/dist/esm/auth-api/storage.d.ts +38 -0
  71. package/dist/esm/{auth-storage.js → auth-api/storage.js} +15 -5
  72. package/dist/esm/auth-api/types.d.ts +34 -0
  73. package/dist/esm/auth-api/types.js +1 -0
  74. package/dist/esm/index.d.ts +29 -5
  75. package/dist/esm/index.js +19 -2
  76. package/dist/esm/oauth/base.d.ts +10 -0
  77. package/dist/esm/oauth/base.js +2 -0
  78. package/dist/esm/oauth/memory.d.ts +16 -0
  79. package/dist/esm/oauth/memory.js +92 -0
  80. package/dist/esm/oauth/models.d.ts +45 -0
  81. package/dist/esm/oauth/models.js +51 -0
  82. package/dist/esm/oauth/sequelize.d.ts +68 -0
  83. package/dist/esm/oauth/sequelize.js +199 -0
  84. package/dist/esm/oauth/types.d.ts +50 -0
  85. package/dist/esm/oauth/types.js +2 -0
  86. package/dist/esm/passkey/base.d.ts +16 -0
  87. package/dist/esm/passkey/base.js +2 -0
  88. package/dist/esm/passkey/memory.d.ts +27 -0
  89. package/dist/esm/passkey/memory.js +82 -0
  90. package/dist/esm/passkey/models.d.ts +25 -0
  91. package/dist/esm/passkey/models.js +108 -0
  92. package/dist/esm/passkey/sequelize.d.ts +55 -0
  93. package/dist/esm/passkey/sequelize.js +216 -0
  94. package/dist/esm/passkey/service.d.ts +20 -0
  95. package/dist/esm/passkey/service.js +319 -0
  96. package/dist/esm/passkey/types.d.ts +78 -0
  97. package/dist/esm/passkey/types.js +1 -0
  98. package/dist/esm/token/base.d.ts +38 -0
  99. package/dist/esm/token/base.js +107 -0
  100. package/dist/esm/token/memory.d.ts +19 -0
  101. package/dist/esm/token/memory.js +145 -0
  102. package/dist/esm/token/sequelize.d.ts +58 -0
  103. package/dist/esm/token/sequelize.js +400 -0
  104. package/dist/esm/token/types.d.ts +27 -0
  105. package/dist/esm/token/types.js +1 -0
  106. package/dist/esm/user/base.d.ts +26 -0
  107. package/dist/esm/user/base.js +38 -0
  108. package/dist/esm/user/memory.d.ts +35 -0
  109. package/dist/esm/user/memory.js +169 -0
  110. package/dist/esm/user/sequelize.d.ts +41 -0
  111. package/dist/esm/user/sequelize.js +176 -0
  112. package/dist/esm/user/types.d.ts +11 -0
  113. package/dist/esm/user/types.js +1 -0
  114. package/package.json +13 -3
  115. package/dist/cjs/auth-storage.d.ts +0 -133
  116. package/dist/esm/auth-storage.d.ts +0 -133
@@ -0,0 +1,38 @@
1
+ import type { Token } from './types.js';
2
+ import type { DecodeOptions, SignOptions, VerifyOptions } from 'jsonwebtoken';
3
+ export interface JwtSignResult {
4
+ success: boolean;
5
+ token?: string;
6
+ error?: string;
7
+ }
8
+ export interface JwtVerifyResult<T> {
9
+ success: boolean;
10
+ data?: T;
11
+ expired?: boolean;
12
+ error?: string;
13
+ }
14
+ export interface JwtDecodeResult<T> {
15
+ success: boolean;
16
+ data?: T;
17
+ error?: string;
18
+ }
19
+ export declare abstract class TokenStore {
20
+ abstract save(record: Token): Promise<void>;
21
+ abstract get(query: Partial<Token>, opts?: {
22
+ includeExpired?: boolean;
23
+ }): Promise<Token | null>;
24
+ abstract delete(query: Partial<Token>): Promise<number>;
25
+ abstract update(update: Partial<Token> & {
26
+ refreshToken: string;
27
+ }): Promise<boolean>;
28
+ abstract list(userId: string | number, opts?: {
29
+ limit?: number;
30
+ offset?: number;
31
+ includeExpired?: boolean;
32
+ }): Promise<Token[]>;
33
+ abstract close(): Promise<void>;
34
+ normalizeToken(token: Partial<Token>): Token;
35
+ jwtSign(payload: any, secret: string, expiresInSeconds: number, options?: SignOptions): JwtSignResult;
36
+ jwtVerify<T>(token: string, secret: string, options?: VerifyOptions): JwtVerifyResult<T>;
37
+ jwtDecode<T>(token: string, options?: DecodeOptions): JwtDecodeResult<T>;
38
+ }
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TokenStore = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ function normalizeScope(scope) {
9
+ if (!scope) {
10
+ return undefined;
11
+ }
12
+ if (Array.isArray(scope)) {
13
+ return scope.filter((entry) => typeof entry === 'string' && entry.length > 0);
14
+ }
15
+ return scope
16
+ .split(/\s+/)
17
+ .map((entry) => entry.trim())
18
+ .filter((entry) => entry.length > 0);
19
+ }
20
+ function normalizeRefreshTtlSeconds(value) {
21
+ if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
22
+ return Math.floor(value);
23
+ }
24
+ if (typeof value === 'string') {
25
+ const parsed = Number(value);
26
+ if (Number.isFinite(parsed) && parsed > 0) {
27
+ return Math.floor(parsed);
28
+ }
29
+ }
30
+ return undefined;
31
+ }
32
+ function normalizeTokenInternal(input) {
33
+ if (!input.refreshToken) {
34
+ throw new Error('refreshToken is required');
35
+ }
36
+ if (!input.accessToken) {
37
+ throw new Error('accessToken is required');
38
+ }
39
+ if (input.userId === undefined || input.userId === null) {
40
+ throw new Error('userId is required');
41
+ }
42
+ const userId = String(input.userId);
43
+ const ruid = input.ruid === undefined || input.ruid === null ? undefined : String(input.ruid);
44
+ const expires = input.expires ? new Date(input.expires) : new Date(Date.now() + 30 * 24 * 60 * 60 * 1000);
45
+ const issuedAt = input.issuedAt ? new Date(input.issuedAt) : new Date();
46
+ const lastSeenAt = input.lastSeenAt ? new Date(input.lastSeenAt) : issuedAt;
47
+ const scope = normalizeScope(input.scope);
48
+ const refreshTtlSeconds = normalizeRefreshTtlSeconds(input.refreshTtlSeconds);
49
+ const status = input.status === 'revoked' ? 'revoked' : expires.getTime() < Date.now() ? 'expired' : 'active';
50
+ const sessionCookie = typeof input.sessionCookie === 'boolean' ? input.sessionCookie : false;
51
+ return {
52
+ ...input,
53
+ accessToken: input.accessToken,
54
+ refreshToken: input.refreshToken,
55
+ userId,
56
+ domain: typeof input.domain === 'string' ? input.domain : '',
57
+ fingerprint: typeof input.fingerprint === 'string' ? input.fingerprint : '',
58
+ label: typeof input.label === 'string' ? input.label : '',
59
+ browser: typeof input.browser === 'string' ? input.browser : '',
60
+ device: typeof input.device === 'string' ? input.device : '',
61
+ ip: typeof input.ip === 'string' ? input.ip : '',
62
+ os: typeof input.os === 'string' ? input.os : '',
63
+ loginType: typeof input.loginType === 'string' && input.loginType.length > 0 ? input.loginType : undefined,
64
+ scope,
65
+ refreshTtlSeconds,
66
+ expires,
67
+ issuedAt,
68
+ lastSeenAt,
69
+ status,
70
+ ruid,
71
+ sessionCookie
72
+ };
73
+ }
74
+ class TokenStore {
75
+ // Instance helpers
76
+ normalizeToken(token) {
77
+ return normalizeTokenInternal(token);
78
+ }
79
+ jwtSign(payload, secret, expiresInSeconds, options) {
80
+ const opts = { ...(options ?? {}), expiresIn: expiresInSeconds };
81
+ try {
82
+ const token = jsonwebtoken_1.default.sign(payload, secret, opts);
83
+ return { success: true, token };
84
+ }
85
+ catch (error) {
86
+ return { success: false, error: error instanceof Error ? error.message : String(error) };
87
+ }
88
+ }
89
+ jwtVerify(token, secret, options) {
90
+ try {
91
+ const data = jsonwebtoken_1.default.verify(token, secret, options ?? {});
92
+ return { success: true, data };
93
+ }
94
+ catch (error) {
95
+ if (error instanceof jsonwebtoken_1.default.TokenExpiredError) {
96
+ return { success: false, expired: true, error: 'Token expired' };
97
+ }
98
+ return { success: false, expired: false, error: error instanceof Error ? error.message : String(error) };
99
+ }
100
+ }
101
+ jwtDecode(token, options) {
102
+ try {
103
+ const data = jsonwebtoken_1.default.decode(token, options ?? {});
104
+ if (data === null) {
105
+ return { success: false, error: 'Invalid token format' };
106
+ }
107
+ return { success: true, data };
108
+ }
109
+ catch (error) {
110
+ return { success: false, error: error instanceof Error ? error.message : String(error) };
111
+ }
112
+ }
113
+ }
114
+ exports.TokenStore = TokenStore;
@@ -0,0 +1,19 @@
1
+ import { TokenStore } from './base.js';
2
+ import type { Token } from './types.js';
3
+ export declare class MemoryTokenStore extends TokenStore {
4
+ private readonly tokens;
5
+ save(record: Token): Promise<void>;
6
+ get(query: Partial<Token>, opts?: {
7
+ includeExpired?: boolean;
8
+ }): Promise<Token | null>;
9
+ delete(query: Partial<Token>): Promise<number>;
10
+ update(params: Partial<Token> & {
11
+ refreshToken: string;
12
+ }): Promise<boolean>;
13
+ list(userId: string | number, opts?: {
14
+ limit?: number;
15
+ offset?: number;
16
+ includeExpired?: boolean;
17
+ }): Promise<Token[]>;
18
+ close(): Promise<void>;
19
+ }
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryTokenStore = void 0;
4
+ const base_js_1 = require("./base.js");
5
+ function comparableUserId(value) {
6
+ if (value === undefined || value === null) {
7
+ return undefined;
8
+ }
9
+ return String(value);
10
+ }
11
+ function cloneToken(record) {
12
+ // this.normalizeToken is not available in static context; caller passes through instance.
13
+ // cloning handled via store instance methods.
14
+ const normalized = record;
15
+ return {
16
+ ...normalized,
17
+ scope: normalized.scope ? [...normalized.scope] : undefined
18
+ };
19
+ }
20
+ function matchesQuery(record, query, includeExpired) {
21
+ if (query.refreshToken && record.refreshToken !== query.refreshToken) {
22
+ return false;
23
+ }
24
+ if (query.accessToken && record.accessToken !== query.accessToken) {
25
+ return false;
26
+ }
27
+ if (query.userId !== undefined && comparableUserId(record.userId) !== comparableUserId(query.userId)) {
28
+ return false;
29
+ }
30
+ if (query.clientId && record.clientId !== query.clientId) {
31
+ return false;
32
+ }
33
+ if (query.domain !== undefined && (record.domain ?? '') !== (query.domain ?? '')) {
34
+ return false;
35
+ }
36
+ if (query.fingerprint !== undefined && (record.fingerprint ?? '') !== (query.fingerprint ?? '')) {
37
+ return false;
38
+ }
39
+ if (query.loginType !== undefined && record.loginType !== (query.loginType ?? undefined)) {
40
+ return false;
41
+ }
42
+ if (query.label && record.label !== query.label) {
43
+ return false;
44
+ }
45
+ if (!includeExpired && (record.expires ?? new Date(0)).getTime() < Date.now()) {
46
+ return false;
47
+ }
48
+ return true;
49
+ }
50
+ class MemoryTokenStore extends base_js_1.TokenStore {
51
+ constructor() {
52
+ super(...arguments);
53
+ this.tokens = [];
54
+ }
55
+ async save(record) {
56
+ const stored = this.normalizeToken(record);
57
+ const normalizedUserId = comparableUserId(stored.userId);
58
+ const domainProvided = record.domain !== undefined;
59
+ const fingerprintProvided = record.fingerprint !== undefined;
60
+ for (let index = this.tokens.length - 1; index >= 0; index -= 1) {
61
+ const existing = this.tokens[index];
62
+ if (comparableUserId(existing.userId) !== normalizedUserId) {
63
+ continue;
64
+ }
65
+ if (record.clientId && existing.clientId !== record.clientId) {
66
+ continue;
67
+ }
68
+ if (domainProvided && existing.domain !== stored.domain) {
69
+ continue;
70
+ }
71
+ if (fingerprintProvided && existing.fingerprint !== stored.fingerprint) {
72
+ continue;
73
+ }
74
+ this.tokens.splice(index, 1);
75
+ }
76
+ this.tokens.push(stored);
77
+ }
78
+ async get(query, opts) {
79
+ if (!query.refreshToken && !query.accessToken && query.userId === undefined) {
80
+ throw new Error('At least one token lookup field must be provided');
81
+ }
82
+ const includeExpired = opts?.includeExpired ?? false;
83
+ const record = this.tokens.find((token) => matchesQuery(token, query, includeExpired));
84
+ return record ? cloneToken(record) : null;
85
+ }
86
+ async delete(query) {
87
+ if (!query.refreshToken && !query.accessToken && query.userId === undefined && !query.clientId) {
88
+ return 0;
89
+ }
90
+ let removed = 0;
91
+ for (let index = this.tokens.length - 1; index >= 0; index -= 1) {
92
+ if (matchesQuery(this.tokens[index], query, true)) {
93
+ this.tokens.splice(index, 1);
94
+ removed += 1;
95
+ }
96
+ }
97
+ return removed;
98
+ }
99
+ async update(params) {
100
+ const token = this.tokens.find((record) => {
101
+ if (record.refreshToken !== params.refreshToken) {
102
+ return false;
103
+ }
104
+ if (params.clientId && record.clientId !== params.clientId) {
105
+ return false;
106
+ }
107
+ return true;
108
+ });
109
+ if (!token) {
110
+ return false;
111
+ }
112
+ const merged = { ...token };
113
+ const maybeAssign = (key) => {
114
+ const value = params[key];
115
+ if (value !== undefined) {
116
+ merged[key] = value;
117
+ }
118
+ };
119
+ maybeAssign('accessToken');
120
+ maybeAssign('expires');
121
+ maybeAssign('scope');
122
+ maybeAssign('label');
123
+ maybeAssign('domain');
124
+ maybeAssign('fingerprint');
125
+ maybeAssign('browser');
126
+ maybeAssign('device');
127
+ maybeAssign('ip');
128
+ maybeAssign('os');
129
+ maybeAssign('refreshTtlSeconds');
130
+ maybeAssign('loginType');
131
+ maybeAssign('issuedAt');
132
+ maybeAssign('lastSeenAt');
133
+ maybeAssign('sessionCookie');
134
+ const normalized = this.normalizeToken(merged);
135
+ Object.assign(token, normalized);
136
+ return true;
137
+ }
138
+ async list(userId, opts = {}) {
139
+ const includeExpired = opts.includeExpired ?? false;
140
+ const filtered = this.tokens.filter((token) => matchesQuery(token, { userId: comparableUserId(userId) }, includeExpired));
141
+ const offset = opts.offset ?? 0;
142
+ const limit = opts.limit ?? filtered.length;
143
+ return filtered.slice(offset, offset + limit).map(cloneToken);
144
+ }
145
+ async close() {
146
+ return;
147
+ }
148
+ }
149
+ exports.MemoryTokenStore = MemoryTokenStore;
@@ -0,0 +1,58 @@
1
+ import { CreationOptional, Model, type InferAttributes, type InferCreationAttributes, type ModelStatic, type Sequelize } from 'sequelize';
2
+ import { TokenStore } from './base.js';
3
+ import type { Token } from './types.js';
4
+ declare class TokenModel extends Model<InferAttributes<TokenModel>, InferCreationAttributes<TokenModel>> implements InferAttributes<TokenModel> {
5
+ token_id: CreationOptional<number>;
6
+ user_id: string;
7
+ real_user_id: CreationOptional<string | null>;
8
+ expires: Date;
9
+ issued_at: CreationOptional<Date>;
10
+ last_seen_at: CreationOptional<Date>;
11
+ access: string;
12
+ refresh: string;
13
+ domain: CreationOptional<string>;
14
+ fingerprint: CreationOptional<string>;
15
+ label: CreationOptional<string>;
16
+ browser: CreationOptional<string>;
17
+ device: CreationOptional<string>;
18
+ ip: CreationOptional<string>;
19
+ os: CreationOptional<string>;
20
+ client_id: CreationOptional<string | null>;
21
+ scope: CreationOptional<string>;
22
+ login_type: CreationOptional<string>;
23
+ refresh_ttl_seconds: CreationOptional<number | null>;
24
+ session_cookie: CreationOptional<boolean>;
25
+ }
26
+ export type TokenAttributes = InferAttributes<TokenModel>;
27
+ export type TokenCreationAttributes = InferCreationAttributes<TokenModel>;
28
+ export interface SequelizeTokenStoreOptions {
29
+ sequelize: Sequelize;
30
+ tokenModel?: ModelStatic<TokenModel>;
31
+ tokenModelFactory?: (sequelize: Sequelize) => ModelStatic<TokenModel>;
32
+ }
33
+ export declare class SequelizeTokenStore extends TokenStore {
34
+ readonly Tokens: ModelStatic<TokenModel>;
35
+ constructor(options: SequelizeTokenStoreOptions);
36
+ save(record: Token): Promise<void>;
37
+ get(query: Partial<Token>, opts?: {
38
+ includeExpired?: boolean;
39
+ }): Promise<Token | null>;
40
+ delete(query: Partial<Token>): Promise<number>;
41
+ update(params: Partial<Token> & {
42
+ refreshToken: string;
43
+ }): Promise<boolean>;
44
+ list(userId: string | number, opts?: {
45
+ limit?: number;
46
+ offset?: number;
47
+ includeExpired?: boolean;
48
+ }): Promise<Token[]>;
49
+ close(): Promise<void>;
50
+ private normalizeUserId;
51
+ private resolveRealUserId;
52
+ private encodeStringArray;
53
+ private decodeStringArray;
54
+ private encodeScope;
55
+ private decodeScope;
56
+ private toTokenRecord;
57
+ }
58
+ export {};