@t8n/iauth 1.0.2 → 1.0.4
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/index.d.ts +299 -0
- package/index.js +1 -0
- package/package.json +11 -6
- package/titan.json +1 -1
- package/utils/registerExtension.js +0 -44
package/index.d.ts
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package @t8n/iauth
|
|
3
|
+
* Intelligent authentication extension for the TitanPL framework.
|
|
4
|
+
*
|
|
5
|
+
* @description
|
|
6
|
+
* `@t8n/iauth` provides a synchronous authentication system designed for the
|
|
7
|
+
* TitanPL Gravity Runtime. It includes password hashing, JWT authentication,
|
|
8
|
+
* OAuth login, and database-backed user management.
|
|
9
|
+
*
|
|
10
|
+
* This library follows Titan's sync-first architecture and integrates with
|
|
11
|
+
* Titan native APIs.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* import IAuth from "@t8n/iauth"
|
|
15
|
+
*
|
|
16
|
+
* const auth = new IAuth({
|
|
17
|
+
* secret: "supersecret",
|
|
18
|
+
* db: {
|
|
19
|
+
* conn: db,
|
|
20
|
+
* table: "users"
|
|
21
|
+
* }
|
|
22
|
+
* })
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export type OAuthProvider = "google" | "github" | "discord"
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* OAuth provider configuration.
|
|
29
|
+
*/
|
|
30
|
+
export interface OAuthProviderConfig {
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* OAuth client ID issued by the provider.
|
|
34
|
+
*/
|
|
35
|
+
clientId: string
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* OAuth client secret issued by the provider.
|
|
39
|
+
*/
|
|
40
|
+
clientSecret: string
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Redirect URL for OAuth callback.
|
|
44
|
+
*/
|
|
45
|
+
redirect: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Database configuration used by the authentication system.
|
|
50
|
+
*/
|
|
51
|
+
export interface DatabaseConfig {
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Database connection instance.
|
|
55
|
+
*/
|
|
56
|
+
conn: any
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* User table name.
|
|
60
|
+
*/
|
|
61
|
+
table: string
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Identity column used for login.
|
|
65
|
+
*
|
|
66
|
+
* Default: `email`
|
|
67
|
+
*/
|
|
68
|
+
identityField?: string
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Password column name.
|
|
72
|
+
*
|
|
73
|
+
* Default: `password`
|
|
74
|
+
*/
|
|
75
|
+
passwordField?: string
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Authentication configuration options.
|
|
80
|
+
*/
|
|
81
|
+
export interface AuthConfig {
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Secret key used to sign JWT tokens.
|
|
85
|
+
*/
|
|
86
|
+
secret: string
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* JWT token expiration time.
|
|
90
|
+
*
|
|
91
|
+
* Default: `"7d"`
|
|
92
|
+
*/
|
|
93
|
+
exp?: string
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Database configuration.
|
|
97
|
+
*/
|
|
98
|
+
db?: DatabaseConfig
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* OAuth provider configuration.
|
|
102
|
+
*/
|
|
103
|
+
oauth?: Record<OAuthProvider, OAuthProviderConfig>
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Hook executed before login validation.
|
|
107
|
+
*/
|
|
108
|
+
beforeLogin?: (data: any) => void
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Hook executed after successful login.
|
|
112
|
+
*/
|
|
113
|
+
afterLogin?: (result: any) => void
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Titan request object.
|
|
118
|
+
*/
|
|
119
|
+
export interface TitanRequest {
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Request headers.
|
|
123
|
+
*/
|
|
124
|
+
headers?: Record<string, string>
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Request body.
|
|
128
|
+
*/
|
|
129
|
+
body?: any
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* URL query parameters.
|
|
133
|
+
*/
|
|
134
|
+
query?: Record<string, string>
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Successful authentication result.
|
|
139
|
+
*/
|
|
140
|
+
export interface AuthResult {
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Authenticated user object.
|
|
144
|
+
*/
|
|
145
|
+
user: any
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Generated JWT token.
|
|
149
|
+
*/
|
|
150
|
+
token: string
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Authentication error response.
|
|
155
|
+
*/
|
|
156
|
+
export interface AuthError {
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Error message.
|
|
160
|
+
*/
|
|
161
|
+
error: string
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* OAuth helper utilities returned by `auth.oauth()`.
|
|
166
|
+
*/
|
|
167
|
+
export interface OAuthHelper {
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Generate the OAuth login redirect URL.
|
|
171
|
+
*/
|
|
172
|
+
loginUrl(): string
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Exchange OAuth authorization code for access token.
|
|
176
|
+
*
|
|
177
|
+
* @param code Authorization code returned by the provider
|
|
178
|
+
*/
|
|
179
|
+
exchange(code: string): Promise<any>
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Fetch OAuth user profile using access token.
|
|
183
|
+
*
|
|
184
|
+
* @param token OAuth access token
|
|
185
|
+
*/
|
|
186
|
+
profile(token: string): Promise<any>
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Main authentication class for TitanPL applications.
|
|
191
|
+
*
|
|
192
|
+
* Provides password authentication, JWT sessions, OAuth login,
|
|
193
|
+
* and protected route helpers.
|
|
194
|
+
*/
|
|
195
|
+
declare class IAuth {
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Create a new authentication instance.
|
|
199
|
+
*
|
|
200
|
+
* @param config Authentication configuration
|
|
201
|
+
*/
|
|
202
|
+
constructor(config?: AuthConfig)
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Hash a plaintext password using bcrypt.
|
|
206
|
+
*
|
|
207
|
+
* @param password Plaintext password
|
|
208
|
+
* @returns Hashed password
|
|
209
|
+
*/
|
|
210
|
+
hashPassword(password: string): string
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Verify a plaintext password against a stored bcrypt hash.
|
|
214
|
+
*
|
|
215
|
+
* @param password Plaintext password
|
|
216
|
+
* @param hash Stored password hash
|
|
217
|
+
*/
|
|
218
|
+
verifyPassword(password: string, hash: string): boolean
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Generate a signed JWT token.
|
|
222
|
+
*
|
|
223
|
+
* @param payload JWT payload
|
|
224
|
+
*/
|
|
225
|
+
signToken(payload: Record<string, any>): string
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Verify a JWT token.
|
|
229
|
+
*
|
|
230
|
+
* @param token JWT token
|
|
231
|
+
*/
|
|
232
|
+
verifyToken(token: string): Record<string, any> | null
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Extract JWT token from Authorization header.
|
|
236
|
+
*
|
|
237
|
+
* Supports Bearer tokens.
|
|
238
|
+
*
|
|
239
|
+
* @param req Titan request
|
|
240
|
+
*/
|
|
241
|
+
extractToken(req: TitanRequest): string | null
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Get authenticated user from request token.
|
|
245
|
+
*
|
|
246
|
+
* @param req Titan request
|
|
247
|
+
*/
|
|
248
|
+
getUser(req: TitanRequest): Record<string, any> | null
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Protect a route using JWT authentication.
|
|
252
|
+
*
|
|
253
|
+
* @param req Titan request
|
|
254
|
+
*/
|
|
255
|
+
guard(req: TitanRequest): Record<string, any> | AuthError
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Find a user in the database by identity field.
|
|
259
|
+
*
|
|
260
|
+
* @param identity Identity value (email/username)
|
|
261
|
+
*/
|
|
262
|
+
findUser(identity: string): any | null
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Create a new user in the configured database.
|
|
266
|
+
*
|
|
267
|
+
* @param data User data
|
|
268
|
+
*/
|
|
269
|
+
createUser(data: Record<string, any>): any | null
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Register a new user.
|
|
273
|
+
*
|
|
274
|
+
* Automatically hashes password and generates JWT token.
|
|
275
|
+
*
|
|
276
|
+
* @param data User credentials
|
|
277
|
+
*/
|
|
278
|
+
signUp(data: Record<string, any>): AuthResult | AuthError
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Authenticate an existing user.
|
|
282
|
+
*
|
|
283
|
+
* @param data Login credentials
|
|
284
|
+
*/
|
|
285
|
+
signIn(data: Record<string, any>): AuthResult | AuthError
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Access OAuth provider utilities.
|
|
289
|
+
*
|
|
290
|
+
* @param provider OAuth provider name
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* const google = auth.oauth("google")
|
|
294
|
+
* const url = google.loginUrl()
|
|
295
|
+
*/
|
|
296
|
+
oauth(provider: OAuthProvider): OAuthHelper
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export default IAuth
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t8n/iauth",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "An intelligent auth extension for TitanPL framework.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"auth",
|
|
7
7
|
"titan",
|
|
@@ -9,6 +9,11 @@
|
|
|
9
9
|
"titanpl",
|
|
10
10
|
"iauth"
|
|
11
11
|
],
|
|
12
|
+
"files": [
|
|
13
|
+
"index.js",
|
|
14
|
+
"index.d.ts",
|
|
15
|
+
"titan.json"
|
|
16
|
+
],
|
|
12
17
|
"license": "ISC",
|
|
13
18
|
"author": "ezetgalaxy",
|
|
14
19
|
"type": "module",
|
|
@@ -18,9 +23,9 @@
|
|
|
18
23
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
19
24
|
},
|
|
20
25
|
"dependencies": {
|
|
21
|
-
"@titanpl/core": "
|
|
22
|
-
"@titanpl/native": "
|
|
23
|
-
"@titanpl/node": "
|
|
26
|
+
"@titanpl/core": "latest",
|
|
27
|
+
"@titanpl/native": "latest",
|
|
28
|
+
"@titanpl/node": "latest",
|
|
24
29
|
"bcryptjs": "^3.0.3"
|
|
25
30
|
}
|
|
26
|
-
}
|
|
31
|
+
}
|
package/titan.json
CHANGED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
// utils/registerExtension.js
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Safely registers an extension in the global t object
|
|
5
|
-
* @param {string} extensionName - Unique name for the extension
|
|
6
|
-
* @param {any} extensionModule - The extension module/object to register
|
|
7
|
-
* @returns {boolean} True if registration was successful
|
|
8
|
-
*/
|
|
9
|
-
export function registerExtension(extensionName, extensionModule) {
|
|
10
|
-
// Check for global t object
|
|
11
|
-
if (typeof t === 'undefined') {
|
|
12
|
-
console.warn(`[registerExtension] Global 't' object not available. Cannot register: ${extensionName}`);
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// Input validation
|
|
17
|
-
if (!extensionName || typeof extensionName !== 'string') {
|
|
18
|
-
console.error('[registerExtension] Invalid extension name provided');
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Check for naming conflicts
|
|
23
|
-
if (t[extensionName]) {
|
|
24
|
-
console.warn(`[registerExtension] '${extensionName}' already exists in global t object, overwriting`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
// Register the extension
|
|
29
|
-
t[extensionName] = extensionModule;
|
|
30
|
-
|
|
31
|
-
console.log(`[registerExtension] Successfully registered '${extensionName}'`);
|
|
32
|
-
|
|
33
|
-
return true;
|
|
34
|
-
} catch (error) {
|
|
35
|
-
// Structured error reporting
|
|
36
|
-
console.error(`[registerExtension] Failed to register '${extensionName}':`, {
|
|
37
|
-
error: error.message,
|
|
38
|
-
extensionName,
|
|
39
|
-
moduleType: typeof extensionModule
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
}
|