@levrbet/shared 0.1.95 → 0.1.97
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/dist/core/types/auth.types.d.ts +25 -2
- package/dist/core/types/auth.types.js +4 -2
- package/dist/core/types/auth.types.js.map +1 -1
- package/dist/server/config/cloudflare.d.ts +30 -0
- package/dist/server/config/cloudflare.js +42 -0
- package/dist/server/config/cloudflare.js.map +1 -0
- package/dist/server/config/constants.js +2 -2
- package/dist/server/config/constants.js.map +1 -1
- package/dist/server/config/dotenv.d.ts +3 -7
- package/dist/server/config/dotenv.js +5 -14
- package/dist/server/config/dotenv.js.map +1 -1
- package/dist/server/config/index.d.ts +1 -0
- package/dist/server/config/index.js +1 -0
- package/dist/server/config/index.js.map +1 -1
- package/dist/server/config/urls.js +2 -2
- package/dist/server/config/urls.js.map +1 -1
- package/dist/server/contracts/instances.d.ts +1 -1
- package/dist/server/contracts/instances.js +3 -3
- package/dist/server/contracts/instances.js.map +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/middleware/auth/cloudflare.handler.d.ts +7 -0
- package/dist/server/middleware/auth/cloudflare.handler.js +31 -0
- package/dist/server/middleware/auth/cloudflare.handler.js.map +1 -0
- package/dist/server/middleware/auth/hmac.handler.d.ts +19 -0
- package/dist/server/middleware/auth/hmac.handler.js +129 -0
- package/dist/server/middleware/auth/hmac.handler.js.map +1 -0
- package/dist/server/middleware/auth/index.d.ts +3 -0
- package/dist/server/middleware/auth/index.js +4 -0
- package/dist/server/middleware/auth/index.js.map +1 -0
- package/dist/server/middleware/auth/privy.handler.d.ts +8 -0
- package/dist/server/middleware/auth/privy.handler.js +29 -0
- package/dist/server/middleware/auth/privy.handler.js.map +1 -0
- package/dist/server/middleware/index.d.ts +1 -3
- package/dist/server/middleware/index.js +1 -3
- package/dist/server/middleware/index.js.map +1 -1
- package/dist/server/middleware/multiAuth.examples.d.ts +43 -0
- package/dist/server/middleware/multiAuth.examples.js +114 -0
- package/dist/server/middleware/multiAuth.examples.js.map +1 -0
- package/dist/server/middleware/multiAuth.middleware.d.ts +31 -0
- package/dist/server/middleware/multiAuth.middleware.js +82 -0
- package/dist/server/middleware/multiAuth.middleware.js.map +1 -0
- package/dist/server/services/cloudflare.service.d.ts +12 -0
- package/dist/server/services/cloudflare.service.js +21 -0
- package/dist/server/services/cloudflare.service.js.map +1 -0
- package/dist/server/services/index.d.ts +2 -0
- package/dist/server/services/index.js +2 -0
- package/dist/server/services/index.js.map +1 -1
- package/dist/server/services/privy.service.js.map +1 -0
- package/dist/server/types/express.types.d.ts +27 -0
- package/dist/server/types/express.types.js +2 -0
- package/dist/server/types/express.types.js.map +1 -0
- package/dist/server/types/index.d.ts +1 -0
- package/dist/server/types/index.js +2 -0
- package/dist/server/types/index.js.map +1 -0
- package/package.json +1 -1
- package/dist/server/auth/index.d.ts +0 -2
- package/dist/server/auth/index.js +0 -3
- package/dist/server/auth/index.js.map +0 -1
- package/dist/server/auth/kms.d.ts +0 -34
- package/dist/server/auth/kms.js +0 -122
- package/dist/server/auth/kms.js.map +0 -1
- package/dist/server/auth/middleware/index.d.ts +0 -17
- package/dist/server/auth/middleware/index.js +0 -62
- package/dist/server/auth/middleware/index.js.map +0 -1
- package/dist/server/auth/privy.service.js.map +0 -1
- package/dist/server/middleware/apiKey.middleware.d.ts +0 -2
- package/dist/server/middleware/apiKey.middleware.js +0 -67
- package/dist/server/middleware/apiKey.middleware.js.map +0 -1
- package/dist/server/middleware/mutiAuth.middleware.d.ts +0 -1
- package/dist/server/middleware/mutiAuth.middleware.js +0 -3
- package/dist/server/middleware/mutiAuth.middleware.js.map +0 -1
- package/dist/server/middleware/privy.middleware.d.ts +0 -3
- package/dist/server/middleware/privy.middleware.js +0 -43
- package/dist/server/middleware/privy.middleware.js.map +0 -1
- /package/dist/server/{auth → services}/privy.service.d.ts +0 -0
- /package/dist/server/{auth → services}/privy.service.js +0 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-Auth Middleware Usage Examples
|
|
3
|
+
*
|
|
4
|
+
* This file demonstrates how to use the new multiAuth middleware
|
|
5
|
+
* to support multiple authentication methods in your routes.
|
|
6
|
+
*/
|
|
7
|
+
declare const router: import("express-serve-static-core").Router;
|
|
8
|
+
/**
|
|
9
|
+
* Client-side usage:
|
|
10
|
+
*
|
|
11
|
+
* Clients MUST include the x-levr-auth-type header to indicate which auth method they're using:
|
|
12
|
+
*
|
|
13
|
+
* For Privy User:
|
|
14
|
+
* ```
|
|
15
|
+
* headers: {
|
|
16
|
+
* 'x-levr-auth-type': 'privyUser',
|
|
17
|
+
* 'privy-access-token': 'token...',
|
|
18
|
+
* 'privy-id-token': 'token...',
|
|
19
|
+
* 'eth-address': '0x...' // optional
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* For HMAC API Key:
|
|
24
|
+
* ```
|
|
25
|
+
* headers: {
|
|
26
|
+
* 'x-levr-auth-type': 'hmac',
|
|
27
|
+
* 'Authorization': 'HMAC <api-key-id>:<signature>',
|
|
28
|
+
* 'x-request-timestamp': '1234567890',
|
|
29
|
+
* 'x-request-nonce': 'random-string' // optional
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* For Service HMAC:
|
|
34
|
+
* ```
|
|
35
|
+
* headers: {
|
|
36
|
+
* 'x-levr-auth-type': 'serviceHmac',
|
|
37
|
+
* 'Authorization': 'HMAC <api-key-id>:<signature>',
|
|
38
|
+
* 'x-request-timestamp': '1234567890',
|
|
39
|
+
* 'x-request-nonce': 'random-string' // optional
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export default router;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-Auth Middleware Usage Examples
|
|
3
|
+
*
|
|
4
|
+
* This file demonstrates how to use the new multiAuth middleware
|
|
5
|
+
* to support multiple authentication methods in your routes.
|
|
6
|
+
*/
|
|
7
|
+
import { Router } from "express";
|
|
8
|
+
import { ApiKeyScope, LevrAuth, LevrService } from "../../core";
|
|
9
|
+
import { multiAuth } from "./multiAuth.middleware";
|
|
10
|
+
const router = Router();
|
|
11
|
+
// Example 1: Simple route supporting both Privy user auth and HMAC API keys
|
|
12
|
+
// Auto-enforces Write scope for POST requests
|
|
13
|
+
router.post("/orders", multiAuth({
|
|
14
|
+
methods: [LevrAuth.PRIVY_USER, LevrAuth.HMAC],
|
|
15
|
+
}), (req, res) => {
|
|
16
|
+
// Access authenticated user data
|
|
17
|
+
if (req.privyUser) {
|
|
18
|
+
console.log("Authenticated via Privy:", req.privyUser.id);
|
|
19
|
+
}
|
|
20
|
+
else if (req.apiUser) {
|
|
21
|
+
console.log("Authenticated via API key:", req.apiUser.id);
|
|
22
|
+
}
|
|
23
|
+
console.log("Auth method used:", req.authMethod);
|
|
24
|
+
console.log("ETH address:", req.ethAddress);
|
|
25
|
+
res.json({ success: true });
|
|
26
|
+
});
|
|
27
|
+
// Example 2: Admin-only route
|
|
28
|
+
router.delete("/admin/users/:id", multiAuth({
|
|
29
|
+
methods: [LevrAuth.PRIVY_ADMIN],
|
|
30
|
+
}), (req, res) => {
|
|
31
|
+
// Only Privy admins can access this
|
|
32
|
+
console.log("Admin user:", req.privyUser?.id);
|
|
33
|
+
res.json({ success: true });
|
|
34
|
+
});
|
|
35
|
+
// Example 3: Read-only route supporting multiple auth methods
|
|
36
|
+
// Disable auto-enforcement of Write scope for POST
|
|
37
|
+
router.post("/search", multiAuth({
|
|
38
|
+
methods: [LevrAuth.PRIVY_USER, LevrAuth.HMAC],
|
|
39
|
+
requiredScopes: [ApiKeyScope.Read],
|
|
40
|
+
autoEnforceWriteScope: false, // POST doesn't require Write scope
|
|
41
|
+
}), (req, res) => {
|
|
42
|
+
// This POST route only needs Read scope
|
|
43
|
+
res.json({ results: [] });
|
|
44
|
+
});
|
|
45
|
+
// Example 4: Service-to-service authentication
|
|
46
|
+
// Only Orderbook and OracleCore services can access
|
|
47
|
+
router.post("/internal/sync-markets", multiAuth({
|
|
48
|
+
methods: [LevrAuth.SERVICE_HMAC],
|
|
49
|
+
allowedServices: [LevrService.Orderbook, LevrService.OracleCore],
|
|
50
|
+
}), (req, res) => {
|
|
51
|
+
// Only specific services can access
|
|
52
|
+
console.log("Service:", req.apiUser?.service);
|
|
53
|
+
res.json({ synced: true });
|
|
54
|
+
});
|
|
55
|
+
// Example 5: Mixed service and user auth with explicit scopes
|
|
56
|
+
router.put("/markets/:id", multiAuth({
|
|
57
|
+
methods: [LevrAuth.PRIVY_ADMIN, LevrAuth.SERVICE_HMAC],
|
|
58
|
+
requiredScopes: [ApiKeyScope.Write], // Explicitly require Write
|
|
59
|
+
allowedServices: [LevrService.Orderbook, LevrService.Lab],
|
|
60
|
+
}), (req, res) => {
|
|
61
|
+
// Either admin users or specific services can update
|
|
62
|
+
if (req.privyUser) {
|
|
63
|
+
console.log("Admin updating market:", req.privyUser.id);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
console.log("Service updating market:", req.apiUser?.service);
|
|
67
|
+
}
|
|
68
|
+
res.json({ updated: true });
|
|
69
|
+
});
|
|
70
|
+
// Example 6: GET route with optional auth
|
|
71
|
+
// Even GET requests can require specific scopes if needed
|
|
72
|
+
router.get("/private-data", multiAuth({
|
|
73
|
+
methods: [LevrAuth.PRIVY_USER, LevrAuth.HMAC],
|
|
74
|
+
requiredScopes: [ApiKeyScope.Read],
|
|
75
|
+
}), (req, res) => {
|
|
76
|
+
res.json({ data: "sensitive information" });
|
|
77
|
+
});
|
|
78
|
+
/**
|
|
79
|
+
* Client-side usage:
|
|
80
|
+
*
|
|
81
|
+
* Clients MUST include the x-levr-auth-type header to indicate which auth method they're using:
|
|
82
|
+
*
|
|
83
|
+
* For Privy User:
|
|
84
|
+
* ```
|
|
85
|
+
* headers: {
|
|
86
|
+
* 'x-levr-auth-type': 'privyUser',
|
|
87
|
+
* 'privy-access-token': 'token...',
|
|
88
|
+
* 'privy-id-token': 'token...',
|
|
89
|
+
* 'eth-address': '0x...' // optional
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* For HMAC API Key:
|
|
94
|
+
* ```
|
|
95
|
+
* headers: {
|
|
96
|
+
* 'x-levr-auth-type': 'hmac',
|
|
97
|
+
* 'Authorization': 'HMAC <api-key-id>:<signature>',
|
|
98
|
+
* 'x-request-timestamp': '1234567890',
|
|
99
|
+
* 'x-request-nonce': 'random-string' // optional
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* For Service HMAC:
|
|
104
|
+
* ```
|
|
105
|
+
* headers: {
|
|
106
|
+
* 'x-levr-auth-type': 'serviceHmac',
|
|
107
|
+
* 'Authorization': 'HMAC <api-key-id>:<signature>',
|
|
108
|
+
* 'x-request-timestamp': '1234567890',
|
|
109
|
+
* 'x-request-nonce': 'random-string' // optional
|
|
110
|
+
* }
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export default router;
|
|
114
|
+
//# sourceMappingURL=multiAuth.examples.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multiAuth.examples.js","sourceRoot":"","sources":["../../../src/server/middleware/multiAuth.examples.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAElD,MAAM,MAAM,GAAG,MAAM,EAAE,CAAA;AAEvB,4EAA4E;AAC5E,8CAA8C;AAC9C,MAAM,CAAC,IAAI,CACP,SAAS,EACT,SAAS,CAAC;IACN,OAAO,EAAE,CAAC,QAAQ,CAAC,UAAU,E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IAAI,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAA;AAC/B,CAAC,CACJ,CAAA;AAED,0CAA0C;AAC1C,0DAA0D;AAC1D,MAAM,CAAC,GAAG,CACN,eAAe,EACf,SAAS,CAAC;IACN,OAAO,EAAE,CAAC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,IAAI,CAAC;IAC7C,cAAc,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC;CACrC,CAAC,EACF,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;IACT,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,uBAAuB,EAAE,CAAC,CAAA;AAC/C,CAAC,CACJ,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,eAAe,MAAM,CAAA"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { NextFunction, Request, Response } from "express";
|
|
2
|
+
import { MultiAuthOptions } from "../../core";
|
|
3
|
+
import "../types/express.types";
|
|
4
|
+
/**
|
|
5
|
+
* Multi-auth middleware factory
|
|
6
|
+
*
|
|
7
|
+
* Creates Express middleware that supports multiple authentication methods.
|
|
8
|
+
* The client specifies which method to use via the x-levr-auth-type header.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Support both user and API key auth, require Write scope
|
|
13
|
+
* app.post('/orders',
|
|
14
|
+
* multiAuth({
|
|
15
|
+
* methods: [LevrAuth.PRIVY_USER, LevrAuth.HMAC],
|
|
16
|
+
* requiredScopes: [ApiKeyScope.Write]
|
|
17
|
+
* }),
|
|
18
|
+
* orderController.create
|
|
19
|
+
* )
|
|
20
|
+
*
|
|
21
|
+
* // Service-to-service only, must be from Orderbook or OracleCore service
|
|
22
|
+
* app.post('/internal/sync',
|
|
23
|
+
* multiAuth({
|
|
24
|
+
* methods: [LevrAuth.SERVICE_HMAC],
|
|
25
|
+
* requiredServices: [LevrService.Orderbook, LevrService.OracleCore]
|
|
26
|
+
* }),
|
|
27
|
+
* internalController.sync
|
|
28
|
+
* )
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function multiAuth(options: MultiAuthOptions): (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { LEVR_AUTH_HEADER, LevrAuth, PrivyRole } from "../../core";
|
|
2
|
+
import "../types/express.types";
|
|
3
|
+
import { handleCloudflareAuth, handleHmacAuth, handlePrivyAuth, handleServiceHmacAuth } from "./auth";
|
|
4
|
+
/**
|
|
5
|
+
* Multi-auth middleware factory
|
|
6
|
+
*
|
|
7
|
+
* Creates Express middleware that supports multiple authentication methods.
|
|
8
|
+
* The client specifies which method to use via the x-levr-auth-type header.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Support both user and API key auth, require Write scope
|
|
13
|
+
* app.post('/orders',
|
|
14
|
+
* multiAuth({
|
|
15
|
+
* methods: [LevrAuth.PRIVY_USER, LevrAuth.HMAC],
|
|
16
|
+
* requiredScopes: [ApiKeyScope.Write]
|
|
17
|
+
* }),
|
|
18
|
+
* orderController.create
|
|
19
|
+
* )
|
|
20
|
+
*
|
|
21
|
+
* // Service-to-service only, must be from Orderbook or OracleCore service
|
|
22
|
+
* app.post('/internal/sync',
|
|
23
|
+
* multiAuth({
|
|
24
|
+
* methods: [LevrAuth.SERVICE_HMAC],
|
|
25
|
+
* requiredServices: [LevrService.Orderbook, LevrService.OracleCore]
|
|
26
|
+
* }),
|
|
27
|
+
* internalController.sync
|
|
28
|
+
* )
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function multiAuth(options) {
|
|
32
|
+
return async (req, res, next) => {
|
|
33
|
+
const requestedAuthType = req.header(LEVR_AUTH_HEADER);
|
|
34
|
+
// If no auth type specified, reject
|
|
35
|
+
if (!requestedAuthType) {
|
|
36
|
+
res.status(400).json({
|
|
37
|
+
error: `Missing ${LEVR_AUTH_HEADER} header`,
|
|
38
|
+
supportedMethods: options.methods,
|
|
39
|
+
});
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
// Check if requested auth method is supported by this route
|
|
43
|
+
if (!options.methods.includes(requestedAuthType)) {
|
|
44
|
+
res.status(400).json({
|
|
45
|
+
error: `Unsupported auth method: ${requestedAuthType}`,
|
|
46
|
+
supportedMethods: options.methods,
|
|
47
|
+
});
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
let result;
|
|
51
|
+
// Route to appropriate auth handler
|
|
52
|
+
switch (requestedAuthType) {
|
|
53
|
+
case LevrAuth.HMAC:
|
|
54
|
+
result = await handleHmacAuth(req, options);
|
|
55
|
+
break;
|
|
56
|
+
case LevrAuth.SERVICE_HMAC:
|
|
57
|
+
result = await handleServiceHmacAuth(req, options);
|
|
58
|
+
break;
|
|
59
|
+
case LevrAuth.PRIVY_USER:
|
|
60
|
+
result = await handlePrivyAuth(req, PrivyRole.User, LevrAuth.PRIVY_USER);
|
|
61
|
+
break;
|
|
62
|
+
case LevrAuth.PRIVY_ADMIN:
|
|
63
|
+
result = await handlePrivyAuth(req, PrivyRole.Admin, LevrAuth.PRIVY_ADMIN);
|
|
64
|
+
break;
|
|
65
|
+
case LevrAuth.CLOUDFLARE:
|
|
66
|
+
result = await handleCloudflareAuth(req);
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
result = {
|
|
70
|
+
success: false,
|
|
71
|
+
error: `Unknown auth method: ${requestedAuthType}`,
|
|
72
|
+
statusCode: 400,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (!result.success) {
|
|
76
|
+
res.status(result.statusCode || 401).json({ error: result.error });
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
next();
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=multiAuth.middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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ACtB,MAAM,GAAG,MAAM,qBAAqB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;gBAClD,MAAK;YAET,KAAK,QAAQ,CAAC,UAAU;gBACpB,MAAM,GAAG,MAAM,eAAe,CAAC,GAAG,EAAE,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAA;gBACxE,MAAK;YAET,KAAK,QAAQ,CAAC,WAAW;gBACrB,MAAM,GAAG,MAAM,eAAe,CAAC,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAA;gBAC1E,MAAK;YAET,KAAK,QAAQ,CAAC,UAAU;gBACpB,MAAM,GAAG,MAAM,oBAAoB,CAAC,GAAG,CAAC,CAAA;gBACxC,MAAK;YAET;gBACI,MAAM,GAAG;oBACL,OAAO,EAAE,KAAK;oBACd,KAAK,EAAE,wBAAwB,iBAAiB,EAAE;oBAClD,UAAU,EAAE,GAAG;iBAClB,CAAA;QACT,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YAClB,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAA;YAClE,OAAM;QACV,CAAC;QAED,IAAI,EAAE,CAAA;IACV,CAAC,CAAA;AACL,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type JWTPayload, type JWTVerifyResult } from "jose";
|
|
2
|
+
declare class CloudflareAuthService {
|
|
3
|
+
/**
|
|
4
|
+
* Verify a Cloudflare Access JWT token.
|
|
5
|
+
*
|
|
6
|
+
* @throws Error if CloudflareConfig has not been initialized
|
|
7
|
+
* @throws Error if JWT verification fails
|
|
8
|
+
*/
|
|
9
|
+
verifyJwt(jwt: string): Promise<JWTVerifyResult<JWTPayload>>;
|
|
10
|
+
}
|
|
11
|
+
export declare const cloudflareAuthService: CloudflareAuthService;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { jwtVerify } from "jose";
|
|
2
|
+
import { cloudflareConfig } from "../config/cloudflare";
|
|
3
|
+
class CloudflareAuthService {
|
|
4
|
+
/**
|
|
5
|
+
* Verify a Cloudflare Access JWT token.
|
|
6
|
+
*
|
|
7
|
+
* @throws Error if CloudflareConfig has not been initialized
|
|
8
|
+
* @throws Error if JWT verification fails
|
|
9
|
+
*/
|
|
10
|
+
async verifyJwt(jwt) {
|
|
11
|
+
const { jwks, issuer, audience } = cloudflareConfig.config;
|
|
12
|
+
return jwtVerify(jwt, jwks, {
|
|
13
|
+
issuer,
|
|
14
|
+
audience,
|
|
15
|
+
algorithms: ["RS256"],
|
|
16
|
+
typ: "JWT",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export const cloudflareAuthService = new CloudflareAuthService();
|
|
21
|
+
//# sourceMappingURL=cloudflare.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cloudflare.service.js","sourceRoot":"","sources":["../../../src/server/services/cloudflare.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAyC,MAAM,MAAM,CAAA;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAEvD,MAAM,qBAAqB;IACvB;;;;;OAKG;IACH,KAAK,CAAC,SAAS,CAAC,GAAW;QACvB,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,gBAAgB,CAAC,MAAM,CAAA;QAE1D,OAAO,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE;YACxB,MAAM;YACN,QAAQ;YACR,UAAU,EAAE,CAAC,OAAO,CAAC;YACrB,GAAG,EAAE,KAAK;SACb,CAAC,CAAA;IACN,CAAC;CACJ;AAED,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,qBAAqB,EAAE,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"privy.service.js","sourceRoot":"","sources":["../../../src/server/services/privy.service.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,WAAW,EAAqB,MAAM,gBAAgB,CAAA;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAW,UAAU,EAAE,MAAM,MAAM,CAAA;AAC1C,OAAO,CAAC,MAAM,KAAK,CAAA;AACnB,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,EACH,kBAAkB,EAClB,4BAA4B,EAC5B,YAAY,EACZ,sBAAsB,GACzB,MAAM,4BAA4B,CAAA;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AASlC,MAAM,YAAY;IAAlB;QAsBI;;;WAGG;QACH,sBAAiB,GAAG,CAAC,IAAe,EAAuB,EAAE;YACzD,IAAI,OAAO,GAAwB,SAAS,CAAA;YAE5C,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;gBACzC,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,CAAC,UAAU,KAAK,UAAU;oBAAE,SAAQ;gBAC5E,OAAO,GAAG,OAAO,CAAC,OAAkB,CAAA;gBACpC,IAAI,OAAO,CAAC,kBAAkB,KAAK,OAAO;oBAAE,MAAK;YACrD,CAAC;YAED,OAAO,OAAO,CAAC,CAAC,CAAC,UAAU,CAAC,OAAO,CAA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,CAAA;QACvB,CAAC;IACL,CAAC;IA+BO,eAAe;QACnB,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC;YACpB,gBAAgB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;YACnC,sBAAsB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;SAC5C,CAAC,CAAA;QACF,MAAM,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QAExE,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC;YAChC,KAAK,EAAE,YAAY;YACnB,SAAS,EAAE,gBAAgB;YAC3B,kBAAkB,EAAE,sBAAsB;SAC7C,CAAC,CAAA;QAEF,MAAM,gBAAgB,GAAG,IAAI,WAAW,CAAC;YACrC,KAAK,EAAE,kBAAkB;YACzB,SAAS,EAAE,sBAAsB;YACjC,kBAAkB,EAAE,4BAA4B;SACnD,CAAC,CAAA;QAEF,OAAO;YACH,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,WAAW;YAC7B,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,gBAAgB;SACtC,CAAA;IACL,CAAC;CACJ;AAxBW;IADP,OAAO,EAAE;mDAwBT;AAGL,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,YAAY,EAAE,CAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { User as PrivyUser } from "@privy-io/node";
|
|
2
|
+
import type { Address } from "viem";
|
|
3
|
+
import type { LevrService } from "../../core";
|
|
4
|
+
declare module "express-serve-static-core" {
|
|
5
|
+
interface Request {
|
|
6
|
+
/**
|
|
7
|
+
* Authenticated Privy user (set by PRIVY_USER or PRIVY_ADMIN auth)
|
|
8
|
+
*/
|
|
9
|
+
privyUser?: PrivyUser;
|
|
10
|
+
/**
|
|
11
|
+
* Authenticated API key user (set by HMAC or SERVICE_HMAC auth)
|
|
12
|
+
*/
|
|
13
|
+
apiUser?: {
|
|
14
|
+
id: string;
|
|
15
|
+
isService: boolean;
|
|
16
|
+
service?: LevrService | null;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Ethereum address associated with the authenticated user
|
|
20
|
+
*/
|
|
21
|
+
ethAddress?: Address | null;
|
|
22
|
+
/**
|
|
23
|
+
* The authentication method used for this request
|
|
24
|
+
*/
|
|
25
|
+
authMethod?: string;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.types.js","sourceRoot":"","sources":["../../../src/server/types/express.types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./express.types";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/auth/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,OAAO,CAAA"}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import Redis from "ioredis";
|
|
2
|
-
/**
|
|
3
|
-
* Generates a new API key using AWS KMS
|
|
4
|
-
* @returns {Promise<Object>} Object containing:
|
|
5
|
-
* - plaintextKey: The decrypted API key to be provided to the user (shown only once)
|
|
6
|
-
* - encryptedKey: The encrypted version of the key to be stored in the database
|
|
7
|
-
* - keyId: The AWS KMS key ID used for encryption
|
|
8
|
-
*/
|
|
9
|
-
export declare const generateApiKey: (userId: string) => Promise<{
|
|
10
|
-
apiKey: string;
|
|
11
|
-
keyId: string;
|
|
12
|
-
}>;
|
|
13
|
-
/**
|
|
14
|
-
* Validates a provided API key by attempting to decrypt it using AWS KMS
|
|
15
|
-
* @param {string} providedKey - The API key to validate
|
|
16
|
-
* @returns {Promise<Object>} Object containing:
|
|
17
|
-
* - isValid: Boolean indicating whether the key is valid
|
|
18
|
-
*/
|
|
19
|
-
export declare const validateApiKey: (apiKey: string, redis: Redis) => Promise<{
|
|
20
|
-
isValid: boolean;
|
|
21
|
-
reason?: string;
|
|
22
|
-
keyId?: string;
|
|
23
|
-
}>;
|
|
24
|
-
export declare const revokeKey: (keyId: string, apiKey: string) => Promise<void>;
|
|
25
|
-
/**
|
|
26
|
-
* Admin function to revoke an API key by keyId.
|
|
27
|
-
* Marks the key as revoked in Redis.
|
|
28
|
-
*/
|
|
29
|
-
export declare const adminRevokeApiKey: (keyId: string, redis: Redis) => Promise<void>;
|
|
30
|
-
export declare const generateHmacSignature: (payload: string) => Promise<string>;
|
|
31
|
-
/**
|
|
32
|
-
* Verifies an HMAC signature
|
|
33
|
-
*/
|
|
34
|
-
export declare const verifyHmacSignature: (payload: string, signature: string) => Promise<boolean>;
|
package/dist/server/auth/kms.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AWS KMS service integration for API key management
|
|
3
|
-
* This module provides functionality for generating and validating API keys using AWS KMS
|
|
4
|
-
*/
|
|
5
|
-
import { GenerateMacCommand, VerifyCommand, VerifyMacCommand } from "@aws-sdk/client-kms";
|
|
6
|
-
import { randomBytes } from "crypto";
|
|
7
|
-
import { kmsClientManager, kmsEnvConfig } from "../config";
|
|
8
|
-
// import { getRevokedApiKey } from "../oracle/redis-cache-manager/cache.keys.utils"
|
|
9
|
-
/**
|
|
10
|
-
* Generates a new API key using AWS KMS
|
|
11
|
-
* @returns {Promise<Object>} Object containing:
|
|
12
|
-
* - plaintextKey: The decrypted API key to be provided to the user (shown only once)
|
|
13
|
-
* - encryptedKey: The encrypted version of the key to be stored in the database
|
|
14
|
-
* - keyId: The AWS KMS key ID used for encryption
|
|
15
|
-
*/
|
|
16
|
-
export const generateApiKey = async (userId) => {
|
|
17
|
-
const keyId = randomBytes(8).toString("hex"); // Unique key identifier
|
|
18
|
-
const payload = `${userId}:${keyId}`;
|
|
19
|
-
const client = kmsClientManager.client;
|
|
20
|
-
const { Mac } = await client.send(new GenerateMacCommand({
|
|
21
|
-
KeyId: kmsEnvConfig.HMAC_KEY_ALIAS,
|
|
22
|
-
Message: Buffer.from(payload),
|
|
23
|
-
MacAlgorithm: "HMAC_SHA_384",
|
|
24
|
-
}));
|
|
25
|
-
if (!Mac) {
|
|
26
|
-
throw new Error("Failed to generate HMAC");
|
|
27
|
-
}
|
|
28
|
-
return {
|
|
29
|
-
apiKey: `${payload}.${Buffer.from(Mac).toString("base64")}`, // Format: userId:keyId.signature
|
|
30
|
-
keyId,
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* Validates a provided API key by attempting to decrypt it using AWS KMS
|
|
35
|
-
* @param {string} providedKey - The API key to validate
|
|
36
|
-
* @returns {Promise<Object>} Object containing:
|
|
37
|
-
* - isValid: Boolean indicating whether the key is valid
|
|
38
|
-
*/
|
|
39
|
-
export const validateApiKey = async (apiKey, redis) => {
|
|
40
|
-
const [payload, signature] = apiKey.split(".");
|
|
41
|
-
const [, keyId] = payload.split(":");
|
|
42
|
-
// 1. Check if key is revoked in Redis
|
|
43
|
-
// const isRevoked = await redis.get(getRevokedApiKey(keyId as string)) // possibly save this to prisma too
|
|
44
|
-
let isRevoked = "1";
|
|
45
|
-
if (isRevoked == "1") {
|
|
46
|
-
return { isValid: false, reason: "Key revoked" };
|
|
47
|
-
}
|
|
48
|
-
const client = kmsClientManager.client;
|
|
49
|
-
const { MacValid } = await client.send(new VerifyMacCommand({
|
|
50
|
-
KeyId: kmsEnvConfig.HMAC_KEY_ALIAS,
|
|
51
|
-
Message: Buffer.from(payload),
|
|
52
|
-
Mac: Buffer.from(signature, "base64"),
|
|
53
|
-
MacAlgorithm: "HMAC_SHA_384",
|
|
54
|
-
}));
|
|
55
|
-
if (!MacValid) {
|
|
56
|
-
return { isValid: false, reason: "Invalid signature" };
|
|
57
|
-
}
|
|
58
|
-
return {
|
|
59
|
-
isValid: MacValid,
|
|
60
|
-
keyId,
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
export const revokeKey = async (keyId, apiKey) => {
|
|
64
|
-
const [payload, signature] = apiKey.split(".");
|
|
65
|
-
const [, providedKeyId] = payload.split(":");
|
|
66
|
-
if (providedKeyId !== keyId) {
|
|
67
|
-
throw new Error("Key ID mismatch during revocation");
|
|
68
|
-
}
|
|
69
|
-
const client = kmsClientManager.client;
|
|
70
|
-
// 2. Verify KMS signature before revocation
|
|
71
|
-
const { SignatureValid } = await client.send(new VerifyCommand({
|
|
72
|
-
KeyId: kmsEnvConfig.HMAC_KEY_ALIAS,
|
|
73
|
-
Message: Buffer.from(payload),
|
|
74
|
-
Signature: Buffer.from(signature, "base64"),
|
|
75
|
-
SigningAlgorithm: "ECDSA_SHA_256",
|
|
76
|
-
}));
|
|
77
|
-
if (!SignatureValid) {
|
|
78
|
-
throw new Error("Invalid signature during revocation");
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
/**
|
|
82
|
-
* Admin function to revoke an API key by keyId.
|
|
83
|
-
* Marks the key as revoked in Redis.
|
|
84
|
-
*/
|
|
85
|
-
//TODO guard under admin permissions
|
|
86
|
-
export const adminRevokeApiKey = async (keyId, redis) => {
|
|
87
|
-
await redis.set(`revoked:${keyId}`, "1");
|
|
88
|
-
};
|
|
89
|
-
export const generateHmacSignature = async (payload) => {
|
|
90
|
-
const command = new GenerateMacCommand({
|
|
91
|
-
KeyId: kmsEnvConfig.HMAC_KEY_ALIAS,
|
|
92
|
-
Message: Buffer.from(payload),
|
|
93
|
-
MacAlgorithm: "HMAC_SHA_384",
|
|
94
|
-
});
|
|
95
|
-
const client = kmsClientManager.client;
|
|
96
|
-
const response = await client.send(command);
|
|
97
|
-
if (!response.Mac) {
|
|
98
|
-
throw new Error("Failed to generate HMAC signature");
|
|
99
|
-
}
|
|
100
|
-
return Buffer.from(response.Mac).toString("base64");
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* Verifies an HMAC signature
|
|
104
|
-
*/
|
|
105
|
-
export const verifyHmacSignature = async (payload, signature) => {
|
|
106
|
-
try {
|
|
107
|
-
const command = new VerifyMacCommand({
|
|
108
|
-
KeyId: kmsEnvConfig.HMAC_KEY_ALIAS, //TODO: setup multiple signing keys, one for users, one for servers
|
|
109
|
-
Message: Buffer.from(payload),
|
|
110
|
-
Mac: Buffer.from(signature, "base64"),
|
|
111
|
-
MacAlgorithm: "HMAC_SHA_384",
|
|
112
|
-
});
|
|
113
|
-
const client = kmsClientManager.client;
|
|
114
|
-
const response = await client.send(command);
|
|
115
|
-
return response.MacValid || false;
|
|
116
|
-
}
|
|
117
|
-
catch (error) {
|
|
118
|
-
console.error("HMAC verification failed:", error);
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
//# sourceMappingURL=kms.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"kms.js","sourceRoot":"","sources":["../../../src/server/auth/kms.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AACzF,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AAEpC,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAC1D,oFAAoF;AAEpF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,EAC/B,MAAc,EAIf,EAAE;IACD,MAAM,KAAK,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA,CAAC,wBAAwB;IACrE,MAAM,OAAO,GAAG,GAAG,MAAM,IAAI,KAAK,EAAE,CAAA;IAEpC,MAAM,MAAM,GAAG,gBAAgB,CAAC,MAAM,CAAA;IAEtC,MAAM,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C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAA;QACjD,OAAO,KAAK,CAAA;IAChB,CAAC;AACL,CAAC,CAAA"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { User as PrivyUser } from "@privy-io/node";
|
|
2
|
-
import type { NextFunction, Request, Response } from "express";
|
|
3
|
-
import Redis from "ioredis";
|
|
4
|
-
import type { Address } from "viem";
|
|
5
|
-
import { LevrService } from "../../../core";
|
|
6
|
-
declare module "express-serve-static-core" {
|
|
7
|
-
interface Request {
|
|
8
|
-
privyUser?: PrivyUser;
|
|
9
|
-
ethAddress?: Address | null;
|
|
10
|
-
apiUser?: {
|
|
11
|
-
id: string;
|
|
12
|
-
isService: boolean;
|
|
13
|
-
service?: LevrService | null;
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export declare const authMiddleWare: (redis: Redis) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
|