@qyh213/easyauth-client 1.0.0-beta.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/LICENSE +21 -0
- package/README.md +425 -0
- package/dist/chunk-H65ZLXQJ.mjs +495 -0
- package/dist/client-RPDHpVsj.d.mts +300 -0
- package/dist/client-RPDHpVsj.d.ts +300 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +530 -0
- package/dist/index.mjs +18 -0
- package/dist/next.d.mts +106 -0
- package/dist/next.d.ts +106 -0
- package/dist/next.js +638 -0
- package/dist/next.mjs +124 -0
- package/dist/react.d.mts +68 -0
- package/dist/react.d.ts +68 -0
- package/dist/react.js +733 -0
- package/dist/react.mjs +220 -0
- package/package.json +99 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,530 @@
|
|
|
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/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
EasyAuthClient: () => EasyAuthClient,
|
|
24
|
+
EasyAuthError: () => EasyAuthError,
|
|
25
|
+
LocalStorageTokenStorage: () => LocalStorageTokenStorage,
|
|
26
|
+
MemoryTokenStorage: () => MemoryTokenStorage,
|
|
27
|
+
ScopeError: () => ScopeError,
|
|
28
|
+
VERSION: () => VERSION
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/types.ts
|
|
33
|
+
var EasyAuthError = class extends Error {
|
|
34
|
+
constructor(message, code, statusCode) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.code = code;
|
|
37
|
+
this.statusCode = statusCode;
|
|
38
|
+
this.name = "EasyAuthError";
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var ScopeError = class extends EasyAuthError {
|
|
42
|
+
constructor(message, requiredScopes, providedScopes) {
|
|
43
|
+
super(message, "INSUFFICIENT_SCOPES", 403);
|
|
44
|
+
this.requiredScopes = requiredScopes;
|
|
45
|
+
this.providedScopes = providedScopes;
|
|
46
|
+
this.name = "ScopeError";
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var _LocalStorageTokenStorage = class _LocalStorageTokenStorage {
|
|
50
|
+
getToken() {
|
|
51
|
+
if (typeof window === "undefined") return null;
|
|
52
|
+
return localStorage.getItem(_LocalStorageTokenStorage.KEY);
|
|
53
|
+
}
|
|
54
|
+
setToken(token) {
|
|
55
|
+
if (typeof window === "undefined") return;
|
|
56
|
+
localStorage.setItem(_LocalStorageTokenStorage.KEY, token);
|
|
57
|
+
}
|
|
58
|
+
removeToken() {
|
|
59
|
+
if (typeof window === "undefined") return;
|
|
60
|
+
localStorage.removeItem(_LocalStorageTokenStorage.KEY);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
_LocalStorageTokenStorage.KEY = "easy_auth_token";
|
|
64
|
+
var LocalStorageTokenStorage = _LocalStorageTokenStorage;
|
|
65
|
+
var MemoryTokenStorage = class {
|
|
66
|
+
constructor() {
|
|
67
|
+
this.token = null;
|
|
68
|
+
}
|
|
69
|
+
getToken() {
|
|
70
|
+
return this.token;
|
|
71
|
+
}
|
|
72
|
+
setToken(token) {
|
|
73
|
+
this.token = token;
|
|
74
|
+
}
|
|
75
|
+
removeToken() {
|
|
76
|
+
this.token = null;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/client.ts
|
|
81
|
+
var EasyAuthClient = class {
|
|
82
|
+
constructor(config, tokenStorage) {
|
|
83
|
+
this.config = {
|
|
84
|
+
apiKey: "",
|
|
85
|
+
defaultTokenExpiry: 24,
|
|
86
|
+
...config
|
|
87
|
+
};
|
|
88
|
+
this.tokenStorage = tokenStorage || new LocalStorageTokenStorage();
|
|
89
|
+
}
|
|
90
|
+
// ============ HTTP Utilities ============
|
|
91
|
+
async request(endpoint, options = {}, requireAuth = false) {
|
|
92
|
+
const url = `${this.config.baseUrl}${endpoint}`;
|
|
93
|
+
const headers = {
|
|
94
|
+
"Content-Type": "application/json",
|
|
95
|
+
...options.headers || {}
|
|
96
|
+
};
|
|
97
|
+
if (this.config.apiKey && !requireAuth) {
|
|
98
|
+
headers["Authorization"] = `Bearer ${this.config.apiKey}`;
|
|
99
|
+
}
|
|
100
|
+
if (requireAuth) {
|
|
101
|
+
const token = this.getAccessToken();
|
|
102
|
+
if (token) {
|
|
103
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
const response = await fetch(url, {
|
|
108
|
+
...options,
|
|
109
|
+
headers
|
|
110
|
+
});
|
|
111
|
+
const data = await response.json();
|
|
112
|
+
if (!response.ok) {
|
|
113
|
+
if (response.status === 403 && data.required && data.provided) {
|
|
114
|
+
throw new ScopeError(
|
|
115
|
+
data.error || "Insufficient scopes",
|
|
116
|
+
data.required,
|
|
117
|
+
data.provided
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
throw new EasyAuthError(
|
|
121
|
+
data.error || `HTTP ${response.status}`,
|
|
122
|
+
data.code || "UNKNOWN_ERROR",
|
|
123
|
+
response.status
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
return data;
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (error instanceof EasyAuthError) {
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
throw new EasyAuthError(
|
|
132
|
+
error instanceof Error ? error.message : "Network error",
|
|
133
|
+
"NETWORK_ERROR"
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// ============ Configuration ============
|
|
138
|
+
/**
|
|
139
|
+
* Update the client's service configuration
|
|
140
|
+
*/
|
|
141
|
+
configure(config) {
|
|
142
|
+
this.config = { ...this.config, ...config };
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Get current configuration (without sensitive data)
|
|
146
|
+
*/
|
|
147
|
+
getConfig() {
|
|
148
|
+
const { apiKey: _, ...rest } = this.config;
|
|
149
|
+
return rest;
|
|
150
|
+
}
|
|
151
|
+
// ============ Token Management ============
|
|
152
|
+
/**
|
|
153
|
+
* Get the stored access token
|
|
154
|
+
*/
|
|
155
|
+
getAccessToken() {
|
|
156
|
+
return this.tokenStorage.getToken();
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Store an access token
|
|
160
|
+
*/
|
|
161
|
+
setAccessToken(token) {
|
|
162
|
+
this.tokenStorage.setToken(token);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Remove the stored access token (logout)
|
|
166
|
+
*/
|
|
167
|
+
clearAccessToken() {
|
|
168
|
+
this.tokenStorage.removeToken();
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Check if user is logged in
|
|
172
|
+
*/
|
|
173
|
+
isLoggedIn() {
|
|
174
|
+
return !!this.getAccessToken();
|
|
175
|
+
}
|
|
176
|
+
// ============ Scope Validation ============
|
|
177
|
+
/**
|
|
178
|
+
* Introspect a token or API key and validate required scopes
|
|
179
|
+
*/
|
|
180
|
+
async introspect(request) {
|
|
181
|
+
return this.request("/api/v1/introspect", {
|
|
182
|
+
method: "POST",
|
|
183
|
+
body: JSON.stringify(request)
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Validate the current token with optional scope checking
|
|
188
|
+
*/
|
|
189
|
+
async validateWithScopes(requiredScopes) {
|
|
190
|
+
const token = this.getAccessToken();
|
|
191
|
+
if (!token) {
|
|
192
|
+
return { valid: false, error: "No token provided" };
|
|
193
|
+
}
|
|
194
|
+
if (!this.config.apiKey) {
|
|
195
|
+
return { valid: false, error: "Service API key not configured" };
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
const serviceId = this.config.apiKey.split(".")[0];
|
|
199
|
+
const result = await this.introspect({
|
|
200
|
+
serviceId,
|
|
201
|
+
token,
|
|
202
|
+
requiredScopes
|
|
203
|
+
});
|
|
204
|
+
return {
|
|
205
|
+
valid: result.valid,
|
|
206
|
+
type: result.type === "bearer" ? "token" : "api_key",
|
|
207
|
+
scopes: result.scopes,
|
|
208
|
+
userId: result.userId,
|
|
209
|
+
email: result.email,
|
|
210
|
+
error: result.error
|
|
211
|
+
};
|
|
212
|
+
} catch (error) {
|
|
213
|
+
if (error instanceof ScopeError) {
|
|
214
|
+
return {
|
|
215
|
+
valid: false,
|
|
216
|
+
error: error.message,
|
|
217
|
+
scopes: error.providedScopes
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
if (error instanceof EasyAuthError && error.statusCode === 401) {
|
|
221
|
+
return { valid: false, error: "Invalid or expired token" };
|
|
222
|
+
}
|
|
223
|
+
throw error;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Check if the current user has all the required scopes
|
|
228
|
+
* Throws ScopeError if scopes are insufficient
|
|
229
|
+
*/
|
|
230
|
+
async requireScopes(...requiredScopes) {
|
|
231
|
+
const result = await this.validateWithScopes(requiredScopes);
|
|
232
|
+
if (!result.valid) {
|
|
233
|
+
throw new EasyAuthError(
|
|
234
|
+
result.error || "Authentication required",
|
|
235
|
+
"UNAUTHORIZED",
|
|
236
|
+
401
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
const hasScopes = requiredScopes.every(
|
|
240
|
+
(scope) => result.scopes?.includes(scope)
|
|
241
|
+
);
|
|
242
|
+
if (!hasScopes) {
|
|
243
|
+
throw new ScopeError(
|
|
244
|
+
`Required scopes: ${requiredScopes.join(", ")}`,
|
|
245
|
+
requiredScopes,
|
|
246
|
+
result.scopes || []
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Execute a function only if the user has the required scopes
|
|
253
|
+
*/
|
|
254
|
+
async withScope(scopes, fn) {
|
|
255
|
+
await this.requireScopes(...scopes);
|
|
256
|
+
return fn();
|
|
257
|
+
}
|
|
258
|
+
// ============ Authentication ============
|
|
259
|
+
/**
|
|
260
|
+
* Login a user and store the access token
|
|
261
|
+
*/
|
|
262
|
+
async login(credentials) {
|
|
263
|
+
if (!this.config.apiKey) {
|
|
264
|
+
throw new EasyAuthError(
|
|
265
|
+
"API key required for login. Configure the client first.",
|
|
266
|
+
"MISSING_API_KEY"
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
const response = await this.request("/api/v1/auth/login", {
|
|
270
|
+
method: "POST",
|
|
271
|
+
body: JSON.stringify({
|
|
272
|
+
email: credentials.email,
|
|
273
|
+
password: credentials.password,
|
|
274
|
+
expires_in_hours: credentials.expiresInHours || this.config.defaultTokenExpiry,
|
|
275
|
+
scopes: credentials.scopes
|
|
276
|
+
})
|
|
277
|
+
});
|
|
278
|
+
const token = {
|
|
279
|
+
accessToken: response.access_token,
|
|
280
|
+
tokenType: response.token_type,
|
|
281
|
+
expiresIn: response.expires_in,
|
|
282
|
+
expiresAt: response.expires_at,
|
|
283
|
+
userId: response.user_id,
|
|
284
|
+
email: response.email,
|
|
285
|
+
scopes: response.scopes
|
|
286
|
+
};
|
|
287
|
+
this.setAccessToken(token.accessToken);
|
|
288
|
+
return token;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Logout the current user
|
|
292
|
+
*/
|
|
293
|
+
async logout() {
|
|
294
|
+
const token = this.getAccessToken();
|
|
295
|
+
if (token) {
|
|
296
|
+
try {
|
|
297
|
+
await this.request("/api/v1/auth/logout", {
|
|
298
|
+
method: "POST"
|
|
299
|
+
});
|
|
300
|
+
} catch (error) {
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
this.clearAccessToken();
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Validate the current token or an API key
|
|
307
|
+
* @deprecated Use validateWithScopes instead
|
|
308
|
+
*/
|
|
309
|
+
async validate(_credential) {
|
|
310
|
+
return this.validateWithScopes();
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Refresh the current token
|
|
314
|
+
*/
|
|
315
|
+
async refreshToken() {
|
|
316
|
+
const response = await this.request("/api/v1/auth/refresh", {
|
|
317
|
+
method: "POST"
|
|
318
|
+
}, true);
|
|
319
|
+
const token = {
|
|
320
|
+
accessToken: response.access_token,
|
|
321
|
+
tokenType: response.token_type,
|
|
322
|
+
expiresIn: response.expires_in,
|
|
323
|
+
expiresAt: response.expires_at,
|
|
324
|
+
userId: "",
|
|
325
|
+
// Refresh doesn't return user info
|
|
326
|
+
email: ""
|
|
327
|
+
};
|
|
328
|
+
this.setAccessToken(token.accessToken);
|
|
329
|
+
return token;
|
|
330
|
+
}
|
|
331
|
+
// ============ User Management ============
|
|
332
|
+
/**
|
|
333
|
+
* Create a new user (requires admin API key)
|
|
334
|
+
*/
|
|
335
|
+
async createUser(request) {
|
|
336
|
+
const response = await this.request("/api/v1/users/create", {
|
|
337
|
+
method: "POST",
|
|
338
|
+
body: JSON.stringify(request)
|
|
339
|
+
});
|
|
340
|
+
return {
|
|
341
|
+
userId: response.user_id,
|
|
342
|
+
email: response.email,
|
|
343
|
+
createdAt: response.created_at
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* List all users for the service
|
|
348
|
+
*/
|
|
349
|
+
async listUsers() {
|
|
350
|
+
const response = await this.request("/api/v1/users/list");
|
|
351
|
+
return response.users;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Delete a user
|
|
355
|
+
*/
|
|
356
|
+
async deleteUser(userId) {
|
|
357
|
+
await this.request("/api/v1/users/delete", {
|
|
358
|
+
method: "POST",
|
|
359
|
+
body: JSON.stringify({ user_id: userId })
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Update user password
|
|
364
|
+
*/
|
|
365
|
+
async updatePassword(userId, oldPassword, newPassword) {
|
|
366
|
+
await this.request("/api/v1/users/password", {
|
|
367
|
+
method: "POST",
|
|
368
|
+
body: JSON.stringify({
|
|
369
|
+
user_id: userId,
|
|
370
|
+
old_password: oldPassword,
|
|
371
|
+
new_password: newPassword
|
|
372
|
+
})
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
// ============ Scope Management ============
|
|
376
|
+
/**
|
|
377
|
+
* List custom scopes for the service
|
|
378
|
+
*/
|
|
379
|
+
async listScopes() {
|
|
380
|
+
const response = await this.request("/api/v1/scopes/list");
|
|
381
|
+
return response.scopes;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Create a custom scope
|
|
385
|
+
*/
|
|
386
|
+
async createScope(request) {
|
|
387
|
+
const response = await this.request("/api/v1/scopes/create", {
|
|
388
|
+
method: "POST",
|
|
389
|
+
body: JSON.stringify(request)
|
|
390
|
+
});
|
|
391
|
+
return {
|
|
392
|
+
scopeId: response.scope_id,
|
|
393
|
+
name: response.name,
|
|
394
|
+
key: response.key,
|
|
395
|
+
description: response.description,
|
|
396
|
+
createdAt: response.created_at
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Delete a custom scope
|
|
401
|
+
*/
|
|
402
|
+
async deleteScope(scopeId) {
|
|
403
|
+
await this.request("/api/v1/scopes/delete", {
|
|
404
|
+
method: "POST",
|
|
405
|
+
body: JSON.stringify({ scope_id: scopeId })
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
// ============ API Key Management ============
|
|
409
|
+
/**
|
|
410
|
+
* List all API keys for the service
|
|
411
|
+
*/
|
|
412
|
+
async listApiKeys() {
|
|
413
|
+
const response = await this.request("/api/v1/keys/list");
|
|
414
|
+
return response.apiKeys;
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Create a new API key with optional custom scopes
|
|
418
|
+
*/
|
|
419
|
+
async createApiKey(request) {
|
|
420
|
+
const response = await this.request("/api/v1/keys/create", {
|
|
421
|
+
method: "POST",
|
|
422
|
+
body: JSON.stringify({
|
|
423
|
+
name: request.name,
|
|
424
|
+
scope: request.scope || "service",
|
|
425
|
+
scopes: request.scopes,
|
|
426
|
+
key_type: request.keyType || "service",
|
|
427
|
+
tps_limit: request.tpsLimit || 100
|
|
428
|
+
})
|
|
429
|
+
});
|
|
430
|
+
return {
|
|
431
|
+
keyId: response.key_id,
|
|
432
|
+
apiKey: response.api_key,
|
|
433
|
+
name: response.name,
|
|
434
|
+
scope: response.scope,
|
|
435
|
+
scopes: response.scopes
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Revoke an API key
|
|
440
|
+
*/
|
|
441
|
+
async revokeApiKey(keyId) {
|
|
442
|
+
await this.request("/api/v1/keys/revoke", {
|
|
443
|
+
method: "POST",
|
|
444
|
+
body: JSON.stringify({ key_id: keyId })
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
// ============ Service Management ============
|
|
448
|
+
/**
|
|
449
|
+
* Onboard a new service (requires master admin key)
|
|
450
|
+
*/
|
|
451
|
+
async onboardService(request, masterAdminKey) {
|
|
452
|
+
const response = await this.request("/api/v1/services/onboard", {
|
|
453
|
+
method: "POST",
|
|
454
|
+
headers: {
|
|
455
|
+
"x-onboarding-admin-key": masterAdminKey
|
|
456
|
+
},
|
|
457
|
+
body: JSON.stringify({
|
|
458
|
+
service_name: request.serviceName,
|
|
459
|
+
owner_email: request.ownerEmail
|
|
460
|
+
})
|
|
461
|
+
});
|
|
462
|
+
return {
|
|
463
|
+
serviceId: response.service_id,
|
|
464
|
+
serviceName: response.service_name,
|
|
465
|
+
ownerEmail: response.owner_email,
|
|
466
|
+
adminKeyId: response.admin_key_id,
|
|
467
|
+
serviceAdminApiKey: response.service_admin_api_key,
|
|
468
|
+
createdAt: response.created_at
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Delete a service (requires master admin key)
|
|
473
|
+
*/
|
|
474
|
+
async deleteService(serviceId, masterAdminKey) {
|
|
475
|
+
await this.request("/api/v1/services/deboard", {
|
|
476
|
+
method: "POST",
|
|
477
|
+
headers: {
|
|
478
|
+
"x-onboarding-admin-key": masterAdminKey
|
|
479
|
+
},
|
|
480
|
+
body: JSON.stringify({ service_id: serviceId })
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
// ============ Webhook Management ============
|
|
484
|
+
/**
|
|
485
|
+
* List webhooks
|
|
486
|
+
*/
|
|
487
|
+
async listWebhooks() {
|
|
488
|
+
const response = await this.request(
|
|
489
|
+
"/api/v1/webhooks/list"
|
|
490
|
+
);
|
|
491
|
+
return response.webhooks;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Register a webhook
|
|
495
|
+
*/
|
|
496
|
+
async registerWebhook(request) {
|
|
497
|
+
const response = await this.request("/api/v1/webhooks/register", {
|
|
498
|
+
method: "POST",
|
|
499
|
+
body: JSON.stringify(request)
|
|
500
|
+
});
|
|
501
|
+
return {
|
|
502
|
+
webhookId: response.webhook_id,
|
|
503
|
+
url: response.url,
|
|
504
|
+
events: response.events,
|
|
505
|
+
active: response.active,
|
|
506
|
+
createdAt: response.created_at
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Delete a webhook
|
|
511
|
+
*/
|
|
512
|
+
async deleteWebhook(webhookId) {
|
|
513
|
+
await this.request("/api/v1/webhooks/delete", {
|
|
514
|
+
method: "POST",
|
|
515
|
+
body: JSON.stringify({ webhook_id: webhookId })
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
// src/index.ts
|
|
521
|
+
var VERSION = "1.0.0";
|
|
522
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
523
|
+
0 && (module.exports = {
|
|
524
|
+
EasyAuthClient,
|
|
525
|
+
EasyAuthError,
|
|
526
|
+
LocalStorageTokenStorage,
|
|
527
|
+
MemoryTokenStorage,
|
|
528
|
+
ScopeError,
|
|
529
|
+
VERSION
|
|
530
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EasyAuthClient,
|
|
3
|
+
EasyAuthError,
|
|
4
|
+
LocalStorageTokenStorage,
|
|
5
|
+
MemoryTokenStorage,
|
|
6
|
+
ScopeError
|
|
7
|
+
} from "./chunk-H65ZLXQJ.mjs";
|
|
8
|
+
|
|
9
|
+
// src/index.ts
|
|
10
|
+
var VERSION = "1.0.0";
|
|
11
|
+
export {
|
|
12
|
+
EasyAuthClient,
|
|
13
|
+
EasyAuthError,
|
|
14
|
+
LocalStorageTokenStorage,
|
|
15
|
+
MemoryTokenStorage,
|
|
16
|
+
ScopeError,
|
|
17
|
+
VERSION
|
|
18
|
+
};
|
package/dist/next.d.mts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
import { e as EasyAuthConfig } from './client-RPDHpVsj.mjs';
|
|
4
|
+
export { A as ApiKey, a as AuthToken, C as CreateApiKeyRequest, b as CreateApiKeyResponse, c as CreateScopeRequest, d as CreateUserRequest, E as EasyAuthClient, f as EasyAuthError, I as IntrospectRequest, g as IntrospectResponse, L as LocalStorageTokenStorage, h as LoginCredentials, M as MemoryTokenStorage, O as OnboardRequest, i as OnboardResponse, R as RegisterWebhookRequest, S as Scope, j as ScopeError, k as Service, T as TokenStorage, U as User, l as UserListResponse, V as ValidationResult, W as Webhook } from './client-RPDHpVsj.mjs';
|
|
5
|
+
|
|
6
|
+
interface MiddlewareOptions {
|
|
7
|
+
/** Base URL of Easy Auth service */
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
/** Service API Key */
|
|
10
|
+
apiKey: string;
|
|
11
|
+
/** Paths that require authentication */
|
|
12
|
+
protectedPaths?: string[];
|
|
13
|
+
/** Paths that require specific scopes: path -> scopes[] */
|
|
14
|
+
scopedPaths?: Record<string, string[]>;
|
|
15
|
+
/** Paths that are public (no auth required) */
|
|
16
|
+
publicPaths?: string[];
|
|
17
|
+
/** Login page path */
|
|
18
|
+
loginPath?: string;
|
|
19
|
+
/** Forbidden page path (insufficient scopes) */
|
|
20
|
+
forbiddenPath?: string;
|
|
21
|
+
}
|
|
22
|
+
interface MiddlewareContext {
|
|
23
|
+
valid: boolean;
|
|
24
|
+
userId?: string;
|
|
25
|
+
email?: string;
|
|
26
|
+
scopes?: string[];
|
|
27
|
+
error?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create a Next.js middleware with scope-based protection
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // middleware.ts
|
|
34
|
+
* import { createEasyAuthMiddleware } from "@easyauth/client/next";
|
|
35
|
+
*
|
|
36
|
+
* export const middleware = createEasyAuthMiddleware({
|
|
37
|
+
* baseUrl: process.env.EASY_AUTH_URL!,
|
|
38
|
+
* apiKey: process.env.EASY_AUTH_API_KEY!,
|
|
39
|
+
* protectedPaths: ["/dashboard", "/api/protected"],
|
|
40
|
+
* scopedPaths: {
|
|
41
|
+
* "/api/users": ["users:read"],
|
|
42
|
+
* "/api/users/create": ["users:write"],
|
|
43
|
+
* "/api/billing": ["billing:read"],
|
|
44
|
+
* "/api/admin": ["admin"],
|
|
45
|
+
* },
|
|
46
|
+
* publicPaths: ["/", "/login", "/register"],
|
|
47
|
+
* });
|
|
48
|
+
*/
|
|
49
|
+
declare function createEasyAuthMiddleware(options: MiddlewareOptions): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
50
|
+
/**
|
|
51
|
+
* Higher-order component to protect a page with required scopes
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // pages/admin.tsx
|
|
55
|
+
* import { withScope } from "@easyauth/client/next";
|
|
56
|
+
*
|
|
57
|
+
* function AdminPage() {
|
|
58
|
+
* return <div>Admin Only</div>;
|
|
59
|
+
* }
|
|
60
|
+
*
|
|
61
|
+
* export default withScope(AdminPage, ["admin"], {
|
|
62
|
+
* baseUrl: process.env.NEXT_PUBLIC_EASY_AUTH_URL!,
|
|
63
|
+
* apiKey: process.env.NEXT_PUBLIC_EASY_AUTH_API_KEY!,
|
|
64
|
+
* });
|
|
65
|
+
*/
|
|
66
|
+
declare function withScope<P extends object>(Component: React.ComponentType<P>, _requiredScopes: string[], _config: EasyAuthConfig): (props: P) => react_jsx_runtime.JSX.Element;
|
|
67
|
+
/**
|
|
68
|
+
* Server-side helper to validate scopes in API routes
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* // app/api/users/route.ts
|
|
72
|
+
* import { validateScopes } from "@easyauth/client/next";
|
|
73
|
+
*
|
|
74
|
+
* export async function GET(request: Request) {
|
|
75
|
+
* const result = await validateScopes(request, {
|
|
76
|
+
* baseUrl: process.env.EASY_AUTH_URL!,
|
|
77
|
+
* apiKey: process.env.EASY_AUTH_API_KEY!,
|
|
78
|
+
* requiredScopes: ["users:read"],
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* if (!result.valid) {
|
|
82
|
+
* return new Response(JSON.stringify({ error: result.error }), {
|
|
83
|
+
* status: result.status || 401,
|
|
84
|
+
* });
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* // Proceed with the request
|
|
88
|
+
* return Response.json({ users: [] });
|
|
89
|
+
* }
|
|
90
|
+
*/
|
|
91
|
+
declare function validateScopes(request: Request, options: {
|
|
92
|
+
baseUrl: string;
|
|
93
|
+
apiKey: string;
|
|
94
|
+
requiredScopes: string[];
|
|
95
|
+
}): Promise<{
|
|
96
|
+
valid: true;
|
|
97
|
+
userId: string;
|
|
98
|
+
email: string;
|
|
99
|
+
scopes: string[];
|
|
100
|
+
} | {
|
|
101
|
+
valid: false;
|
|
102
|
+
error: string;
|
|
103
|
+
status: number;
|
|
104
|
+
}>;
|
|
105
|
+
|
|
106
|
+
export { EasyAuthConfig, type MiddlewareContext, type MiddlewareOptions, createEasyAuthMiddleware, validateScopes, withScope };
|