asyx 0.5.9 → 0.6.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 +73 -9
- package/SECURITY.md +5 -3
- package/package.json +13 -23
- package/src/commands/name.js +23 -0
- package/src/commands/reject-messages.js +54 -0
- package/src/commands/setup.js +98 -102
- package/src/commands/tunnel.js +279 -2425
- package/src/lib/naming.js +81 -0
- package/src/main.js +9 -1
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.d.ts +260 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.js +1088 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.d.ts +79 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.js +138 -0
- package/vendor/tunnel-core/dist/control-plane/session.d.ts +50 -0
- package/vendor/tunnel-core/dist/control-plane/session.js +115 -0
- package/vendor/tunnel-core/dist/errors.d.ts +49 -0
- package/vendor/tunnel-core/dist/errors.js +53 -0
- package/vendor/tunnel-core/dist/flows/http.d.ts +50 -0
- package/vendor/tunnel-core/dist/flows/http.js +585 -0
- package/vendor/tunnel-core/dist/flows/tcp.d.ts +60 -0
- package/vendor/tunnel-core/dist/flows/tcp.js +546 -0
- package/vendor/tunnel-core/dist/flows/udp.d.ts +41 -0
- package/vendor/tunnel-core/dist/flows/udp.js +376 -0
- package/vendor/tunnel-core/dist/lib/adapt-logger.d.ts +2 -0
- package/vendor/tunnel-core/dist/lib/adapt-logger.js +6 -0
- package/vendor/tunnel-core/dist/lib/fingerprint.d.ts +13 -0
- package/vendor/tunnel-core/dist/lib/fingerprint.js +95 -0
- package/vendor/tunnel-core/dist/lib/jwt-verifier.d.ts +27 -0
- package/vendor/tunnel-core/dist/lib/jwt-verifier.js +230 -0
- package/vendor/tunnel-core/dist/lib/logger.d.ts +9 -0
- package/vendor/tunnel-core/dist/lib/logger.js +58 -0
- package/vendor/tunnel-core/dist/lib/refresh-helpers.d.ts +16 -0
- package/vendor/tunnel-core/dist/lib/refresh-helpers.js +66 -0
- package/vendor/tunnel-core/dist/lifecycle.d.ts +24 -0
- package/vendor/tunnel-core/dist/lifecycle.js +29 -0
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.d.ts +56 -0
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.js +227 -0
- package/vendor/tunnel-core/dist/mqtt/events/handlers.d.ts +58 -0
- package/vendor/tunnel-core/dist/mqtt/events/handlers.js +250 -0
- package/vendor/tunnel-core/dist/mqtt/events/index.d.ts +1 -0
- package/vendor/tunnel-core/dist/mqtt/events/index.js +191 -0
- package/vendor/tunnel-core/dist/mqtt/events/schemas.d.ts +498 -0
- package/vendor/tunnel-core/dist/mqtt/events/schemas.js +51 -0
- package/vendor/tunnel-core/dist/mqtt/ingress.d.ts +63 -0
- package/vendor/tunnel-core/dist/mqtt/ingress.js +170 -0
- package/vendor/tunnel-core/dist/state/assignment.d.ts +122 -0
- package/vendor/tunnel-core/dist/state/assignment.js +322 -0
- package/vendor/tunnel-core/dist/state/instance-registry.d.ts +88 -0
- package/{src → vendor/tunnel-core/dist}/state/instance-registry.js +38 -53
- package/vendor/tunnel-core/dist/tcp-v1/framing.d.ts +43 -0
- package/vendor/tunnel-core/dist/tcp-v1/framing.js +157 -0
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.d.ts +137 -0
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.js +1591 -0
- package/vendor/tunnel-core/dist/tunnel/httpws-constants.d.ts +13 -0
- package/{src → vendor/tunnel-core/dist}/tunnel/httpws-constants.js +5 -7
- package/vendor/tunnel-core/dist/tunnel/reconnect.d.ts +76 -0
- package/vendor/tunnel-core/dist/tunnel/reconnect.js +183 -0
- package/vendor/tunnel-core/dist/tunnel/request-sink.d.ts +40 -0
- package/vendor/tunnel-core/dist/tunnel/request-sink.js +39 -0
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.d.ts +89 -0
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.js +838 -0
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.d.ts +90 -0
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.js +737 -0
- package/vendor/tunnel-core/dist/tunnel/ws-control.d.ts +22 -0
- package/vendor/tunnel-core/dist/tunnel/ws-control.js +124 -0
- package/vendor/tunnel-core/dist/types.d.ts +24 -0
- package/vendor/tunnel-core/dist/types.js +2 -0
- package/vendor/tunnel-core/dist/udp-v1/framing.d.ts +41 -0
- package/vendor/tunnel-core/dist/udp-v1/framing.js +158 -0
- package/vendor/tunnel-core/package.json +34 -0
- package/src/control-plane/MqttControlPlaneClient.js +0 -1196
- package/src/control-plane/MqttControlPlaneTransport.js +0 -150
- package/src/lib/fingerprint.js +0 -115
- package/src/lib/jwt-verifier.js +0 -260
- package/src/lib/logger.js +0 -62
- package/src/mqtt/awaiter-map.js +0 -247
- package/src/mqtt/events/handlers.js +0 -300
- package/src/mqtt/events/index.js +0 -222
- package/src/mqtt/events/schemas.js +0 -58
- package/src/mqtt/ingress.js +0 -182
- package/src/state/assignment.js +0 -333
- package/src/tcp-v1/framing.js +0 -203
- package/src/tunnel/http-tunnel.js +0 -1738
- package/src/tunnel/reconnect.js +0 -182
- package/src/tunnel/tcp-tunnel.js +0 -944
- package/src/tunnel/udp-tunnel.js +0 -813
- package/src/tunnel/ws-control.js +0 -135
- package/src/udp-v1/framing.js +0 -203
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MqttControlPlaneTransport
|
|
3
|
-
*
|
|
4
|
-
* Adapter that wraps MqttControlPlaneClient to implement the Transport interface
|
|
5
|
-
* required by MqttCore. This allows us to use MqttControlPlaneClient for connection
|
|
6
|
-
* management while still using MqttCore for the higher-level API.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { MqttControlPlaneClient } from './MqttControlPlaneClient.js';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Transport adapter that wraps MqttControlPlaneClient.
|
|
13
|
-
* Implements the Transport interface from @asyx/mqtt-core.
|
|
14
|
-
*/
|
|
15
|
-
export class MqttControlPlaneTransport {
|
|
16
|
-
/**
|
|
17
|
-
* @param {MqttControlPlaneClient} client
|
|
18
|
-
*/
|
|
19
|
-
constructor(client) {
|
|
20
|
-
this.client = client;
|
|
21
|
-
this._messageHandler = null;
|
|
22
|
-
this._connectHandler = null;
|
|
23
|
-
this._disconnectHandler = null;
|
|
24
|
-
|
|
25
|
-
// Store original callbacks to preserve them
|
|
26
|
-
const originalOnConnected = this.client.onConnected;
|
|
27
|
-
const originalOnOffline = this.client.onOffline;
|
|
28
|
-
|
|
29
|
-
// Wire up handlers when client connects (chain with existing callbacks)
|
|
30
|
-
this.client.onConnected = async (mqttClient) => {
|
|
31
|
-
// Wire up message handler (setMessageHandler ensures it's wired on reconnect)
|
|
32
|
-
if (this._messageHandler) {
|
|
33
|
-
this.client.setMessageHandler(this._messageHandler);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Call original callback (set by tunnel.js)
|
|
37
|
-
if (originalOnConnected) {
|
|
38
|
-
await originalOnConnected(mqttClient);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Call Transport connect handler (for MqttCore)
|
|
42
|
-
if (this._connectHandler) {
|
|
43
|
-
this._connectHandler();
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// Wire up disconnect handler (chain with existing callbacks)
|
|
48
|
-
this.client.onOffline = (reason) => {
|
|
49
|
-
// Call original callback (set by tunnel.js)
|
|
50
|
-
if (originalOnOffline) {
|
|
51
|
-
originalOnOffline(reason);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Call Transport disconnect handler (for MqttCore)
|
|
55
|
-
if (this._disconnectHandler) {
|
|
56
|
-
this._disconnectHandler(new Error(reason));
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Transport interface: connect
|
|
63
|
-
* @param {any} opts - ConnectOptions (ignored, client manages its own connection)
|
|
64
|
-
* @returns {Promise<void>}
|
|
65
|
-
*/
|
|
66
|
-
async connect(opts) {
|
|
67
|
-
// Client manages its own connection, just ensure it's connected
|
|
68
|
-
if (!this.client.isOnline()) {
|
|
69
|
-
await this.client.connect();
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Transport interface: end
|
|
75
|
-
* @returns {Promise<void>}
|
|
76
|
-
*/
|
|
77
|
-
async end() {
|
|
78
|
-
return this.client.disconnect();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Transport interface: publish
|
|
83
|
-
* @param {{topic: string, payload: string | Buffer | Uint8Array, qos?: 0 | 1, retain?: boolean}} p
|
|
84
|
-
* @returns {Promise<void>}
|
|
85
|
-
*/
|
|
86
|
-
async publish(p) {
|
|
87
|
-
const payload = p.payload instanceof Uint8Array
|
|
88
|
-
? Buffer.from(p.payload)
|
|
89
|
-
: (typeof p.payload === 'string' ? p.payload : p.payload);
|
|
90
|
-
return this.client.publish(p.topic, payload, { qos: p.qos ?? 0, retain: p.retain ?? false });
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Transport interface: subscribe
|
|
95
|
-
* @param {string} topic
|
|
96
|
-
* @param {0 | 1} [qos=0]
|
|
97
|
-
* @returns {Promise<{topic: string, qos: 0 | 1}>}
|
|
98
|
-
*/
|
|
99
|
-
async subscribe(topic, qos = 0) {
|
|
100
|
-
return this.client.subscribe(topic, qos);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Transport interface: unsubscribe
|
|
105
|
-
* @param {string} topic
|
|
106
|
-
* @returns {Promise<void>}
|
|
107
|
-
*/
|
|
108
|
-
async unsubscribe(topic) {
|
|
109
|
-
return this.client.unsubscribe(topic);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Transport interface: onMessage
|
|
114
|
-
* @param {(message: {topic: string, payload: Uint8Array}) => void} cb
|
|
115
|
-
*/
|
|
116
|
-
onMessage(cb) {
|
|
117
|
-
this._messageHandler = cb;
|
|
118
|
-
// Set message handler on client (will be wired on connect/reconnect)
|
|
119
|
-
this.client.setMessageHandler(cb);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Transport interface: onConnect
|
|
124
|
-
* @param {() => void} cb
|
|
125
|
-
*/
|
|
126
|
-
onConnect(cb) {
|
|
127
|
-
this._connectHandler = cb;
|
|
128
|
-
// If already connected, call immediately
|
|
129
|
-
if (this.client.isOnline()) {
|
|
130
|
-
cb();
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Transport interface: onDisconnect
|
|
136
|
-
* @param {(err?: unknown) => void} cb
|
|
137
|
-
*/
|
|
138
|
-
onDisconnect(cb) {
|
|
139
|
-
this._disconnectHandler = cb;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Expose client for helpers that need direct access
|
|
144
|
-
* @returns {MqttControlPlaneClient}
|
|
145
|
-
*/
|
|
146
|
-
getClient() {
|
|
147
|
-
return this.client;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
package/src/lib/fingerprint.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CP-3C: Safe Hardware Fingerprint Collection (macOS Privacy Compliant)
|
|
3
|
-
*
|
|
4
|
-
* Privacy-preserving hardware fingerprinting that avoids macOS privacy warnings.
|
|
5
|
-
* Uses only safe fields that don't trigger system permission requests.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { getFingerprintingInfo } from 'hw-fingerprint';
|
|
9
|
-
import { createHash } from 'node:crypto';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Safe hardware attributes that don't trigger macOS privacy warnings.
|
|
13
|
-
* Based on analysis of hw-fingerprint library behavior.
|
|
14
|
-
*/
|
|
15
|
-
const SAFE_FIELDS = [
|
|
16
|
-
'platform', // OS platform (darwin, linux, win32)
|
|
17
|
-
'arch', // CPU architecture (arm64, x64)
|
|
18
|
-
'cores', // Number of CPU cores
|
|
19
|
-
'manufacturer', // Hardware manufacturer
|
|
20
|
-
'vendor', // System vendor
|
|
21
|
-
'model' // Hardware model (Mac16,11, etc.)
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Collect safe hardware fingerprint using only non-sensitive fields.
|
|
26
|
-
* Returns canonical JSON string with stable ordering.
|
|
27
|
-
*/
|
|
28
|
-
async function collectSafeHardwareFingerprint() {
|
|
29
|
-
try {
|
|
30
|
-
const fingerprint = await getFingerprintingInfo();
|
|
31
|
-
|
|
32
|
-
// Extract only safe fields, dropping empties
|
|
33
|
-
const safeData = {};
|
|
34
|
-
|
|
35
|
-
for (const field of SAFE_FIELDS) {
|
|
36
|
-
const value = fingerprint[field];
|
|
37
|
-
if (value !== undefined && value !== null && value !== '') {
|
|
38
|
-
safeData[field] = value;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Create canonical JSON with stable ordering
|
|
43
|
-
const canonical = {
|
|
44
|
-
version: 'fpv1-safe',
|
|
45
|
-
fields: safeData
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
return JSON.stringify(canonical, Object.keys(canonical).sort());
|
|
49
|
-
} catch (error) {
|
|
50
|
-
// Return empty canonical structure on failure
|
|
51
|
-
return JSON.stringify({ version: 'fpv1-safe', fields: {} }, Object.keys({ version: 'fpv1-safe', fields: {} }).sort());
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Hash fingerprint using SHA-256 with public salt.
|
|
57
|
-
* Returns hex string.
|
|
58
|
-
*/
|
|
59
|
-
function hashFingerprint(canonical, clientId) {
|
|
60
|
-
const publicSalt = `asyx|${clientId}`;
|
|
61
|
-
const preimage = publicSalt + canonical;
|
|
62
|
-
|
|
63
|
-
return createHash('sha256')
|
|
64
|
-
.update(preimage, 'utf8')
|
|
65
|
-
.digest('hex');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Get safe hardware fingerprint hash with version metadata.
|
|
70
|
-
* Returns structured object for cmd/open payload.
|
|
71
|
-
*
|
|
72
|
-
* This version avoids macOS privacy warnings by using only safe fields.
|
|
73
|
-
*/
|
|
74
|
-
export async function getFpHash(clientId) {
|
|
75
|
-
try {
|
|
76
|
-
const canonical = await collectSafeHardwareFingerprint();
|
|
77
|
-
const clientHash = hashFingerprint(canonical, clientId);
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
fpv: '1-safe',
|
|
81
|
-
fph: clientHash,
|
|
82
|
-
alg: 'sha256',
|
|
83
|
-
lib: 'hw-fingerprint@3.2.0-safe'
|
|
84
|
-
};
|
|
85
|
-
} catch (error) {
|
|
86
|
-
// Return unavailable fingerprint on any failure
|
|
87
|
-
return {
|
|
88
|
-
fpv: '1-safe',
|
|
89
|
-
fph: 'unavailable',
|
|
90
|
-
alg: 'sha256',
|
|
91
|
-
lib: 'hw-fingerprint@3.2.0-safe'
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Test function to verify safe fingerprinting works without warnings
|
|
98
|
-
*/
|
|
99
|
-
export async function testSafeFingerprint() {
|
|
100
|
-
console.log('🧪 Testing safe hardware fingerprint...');
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
const result = await getFpHash('test-client');
|
|
104
|
-
console.log('✅ Safe fingerprint result:', JSON.stringify(result, null, 2));
|
|
105
|
-
|
|
106
|
-
// Test deterministic behavior
|
|
107
|
-
const result2 = await getFpHash('test-client');
|
|
108
|
-
console.log('✅ Deterministic test - same result:', result.fph === result2.fph);
|
|
109
|
-
|
|
110
|
-
return result;
|
|
111
|
-
} catch (error) {
|
|
112
|
-
console.error('❌ Safe fingerprint test failed:', error.message);
|
|
113
|
-
return null;
|
|
114
|
-
}
|
|
115
|
-
}
|
package/src/lib/jwt-verifier.js
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
// Shared JWT verifier using JWKS for assignment token validation
|
|
2
|
-
// Used by TCP/HTTP/UDP tunnels to verify assignment JWTs before opening WebSocket
|
|
3
|
-
// AC1.4/AC5.1: Enforces "no JWKS → no tunnel" policy
|
|
4
|
-
import { createRemoteJWKSet, jwtVerify } from 'jose';
|
|
5
|
-
import logger from './logger.js';
|
|
6
|
-
|
|
7
|
-
// JWKS URL configuration (environment-specific)
|
|
8
|
-
const JWKS_URL_PROD = 'https://auth.asyx.ai/.well-known/jwks.json';
|
|
9
|
-
const JWKS_URL_DEV = 'https://auth.dev.asyx.ai/.well-known/jwks.json';
|
|
10
|
-
|
|
11
|
-
// Expected issuer and audience
|
|
12
|
-
const EXPECTED_ISSUER = 'https://auth.asyx.ai';
|
|
13
|
-
const EXPECTED_AUDIENCE = 'tunnel-relay';
|
|
14
|
-
|
|
15
|
-
// Singleton JWKS cache (shared across all tunnel instances)
|
|
16
|
-
let jwksCache = null;
|
|
17
|
-
let jwksInitialized = false;
|
|
18
|
-
let jwksInitPromise = null;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Initialize JWKS cache (lazy, on first use)
|
|
22
|
-
* @returns {Promise<void>}
|
|
23
|
-
*/
|
|
24
|
-
async function initializeJWKS() {
|
|
25
|
-
if (jwksInitialized) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (jwksInitPromise) {
|
|
30
|
-
return jwksInitPromise;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
jwksInitPromise = (async () => {
|
|
34
|
-
try {
|
|
35
|
-
// Determine JWKS URL from environment
|
|
36
|
-
const env = process.env.ASYX_ENV || process.env.NODE_ENV || 'prod';
|
|
37
|
-
const jwksUrl = env === 'dev' || env === 'development' ? JWKS_URL_DEV : JWKS_URL_PROD;
|
|
38
|
-
|
|
39
|
-
// Allow override via environment variable
|
|
40
|
-
const jwksUrlOverride = process.env.ASYX_JWKS_URL;
|
|
41
|
-
const finalJwksUrl = jwksUrlOverride || jwksUrl;
|
|
42
|
-
|
|
43
|
-
logger.debug('jwt.jwks.initializing', { jwksUrl: finalJwksUrl, env });
|
|
44
|
-
|
|
45
|
-
// Create remote JWKS set (jose handles caching and refresh)
|
|
46
|
-
jwksCache = createRemoteJWKSet(new URL(finalJwksUrl));
|
|
47
|
-
|
|
48
|
-
// Test fetch to ensure JWKS is available (fail fast if not)
|
|
49
|
-
// jose's createRemoteJWKSet doesn't fetch immediately, so we need to trigger a fetch
|
|
50
|
-
// We'll do this on first verification attempt
|
|
51
|
-
|
|
52
|
-
jwksInitialized = true;
|
|
53
|
-
logger.info('jwt.jwks.initialized', { jwksUrl: finalJwksUrl });
|
|
54
|
-
} catch (error) {
|
|
55
|
-
jwksInitPromise = null;
|
|
56
|
-
logger.error('jwt.jwks.init_failed', { error: error.message });
|
|
57
|
-
throw error;
|
|
58
|
-
}
|
|
59
|
-
})();
|
|
60
|
-
|
|
61
|
-
return jwksInitPromise;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Verify assignment JWT with JWKS signature verification
|
|
66
|
-
* AC1.4/AC5.1: Validates signature, iss, aud, exp, relayId, protocol, tunnelMode, leaseId
|
|
67
|
-
* @param {string} token - JWT token to verify
|
|
68
|
-
* @param {Object} expectedClaims - Expected claim values
|
|
69
|
-
* @param {string} expectedClaims.relayId - Expected relayId
|
|
70
|
-
* @param {string} expectedClaims.protocol - Expected protocol ('udp' | 'tcp')
|
|
71
|
-
* @param {string} expectedClaims.tunnelMode - Expected tunnelMode ('udpWs-v1' | 'tcpWs-v1')
|
|
72
|
-
* @param {string} [expectedClaims.leaseId] - Expected leaseId (optional)
|
|
73
|
-
* @returns {Promise<Object>} Verified JWT payload
|
|
74
|
-
* @throws {Error} If JWT verification fails (signature, claims, or JWKS unavailable)
|
|
75
|
-
*/
|
|
76
|
-
export async function verifyAssignmentJWT(token, expectedClaims = {}) {
|
|
77
|
-
// Initialize JWKS if not already done
|
|
78
|
-
if (!jwksInitialized) {
|
|
79
|
-
await initializeJWKS();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// AC1.4/AC5.1: Fail fast if JWKS is not available
|
|
83
|
-
if (!jwksCache) {
|
|
84
|
-
const error = new Error('JWKS not available - cannot verify assignment JWT');
|
|
85
|
-
error.code = 'JWKS_UNAVAILABLE';
|
|
86
|
-
logger.error('jwt.verification.jwks_unavailable', {
|
|
87
|
-
tokenLength: token?.length || 0,
|
|
88
|
-
expectedRelayId: expectedClaims.relayId
|
|
89
|
-
});
|
|
90
|
-
throw error;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
try {
|
|
94
|
-
// Verify JWT signature and decode payload using JWKS
|
|
95
|
-
// jwtVerify automatically:
|
|
96
|
-
// - Fetches JWKS if not cached
|
|
97
|
-
// - Verifies signature using the key identified by 'kid' in JWT header
|
|
98
|
-
// - Validates exp, nbf, iat if present
|
|
99
|
-
const { payload, protectedHeader } = await jwtVerify(token, jwksCache, {
|
|
100
|
-
issuer: EXPECTED_ISSUER,
|
|
101
|
-
audience: EXPECTED_AUDIENCE,
|
|
102
|
-
// jose validates exp automatically with clock skew tolerance
|
|
103
|
-
// We'll get ERR_JWT_EXPIRED if expired, which we handle below
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
logger.debug('jwt.verification.signature_verified', {
|
|
107
|
-
kid: protectedHeader.kid,
|
|
108
|
-
alg: protectedHeader.alg,
|
|
109
|
-
relayId: payload.relayId,
|
|
110
|
-
protocol: payload.protocol,
|
|
111
|
-
tunnelMode: payload.tunnelMode
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
// AC5.1: Validate relayId matches expected
|
|
115
|
-
if (expectedClaims.relayId && payload.relayId !== expectedClaims.relayId) {
|
|
116
|
-
const error = new Error(`JWT relayId mismatch: ${payload.relayId} !== ${expectedClaims.relayId}`);
|
|
117
|
-
error.code = 'JWT_RELAYID_MISMATCH';
|
|
118
|
-
logger.error('jwt.verification.relayid_mismatch', {
|
|
119
|
-
expected: expectedClaims.relayId,
|
|
120
|
-
actual: payload.relayId,
|
|
121
|
-
leaseId: payload.leaseId
|
|
122
|
-
});
|
|
123
|
-
throw error;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// AC1.4: Validate protocol
|
|
127
|
-
if (expectedClaims.protocol && payload.protocol !== expectedClaims.protocol) {
|
|
128
|
-
const error = new Error(`JWT protocol mismatch: ${payload.protocol} !== ${expectedClaims.protocol}`);
|
|
129
|
-
error.code = 'JWT_PROTOCOL_MISMATCH';
|
|
130
|
-
logger.error('jwt.verification.protocol_mismatch', {
|
|
131
|
-
expected: expectedClaims.protocol,
|
|
132
|
-
actual: payload.protocol
|
|
133
|
-
});
|
|
134
|
-
throw error;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// AC1.4: Validate tunnelMode
|
|
138
|
-
if (expectedClaims.tunnelMode && payload.tunnelMode !== expectedClaims.tunnelMode) {
|
|
139
|
-
const error = new Error(`JWT tunnelMode mismatch: ${payload.tunnelMode} !== ${expectedClaims.tunnelMode}`);
|
|
140
|
-
error.code = 'JWT_TUNNELMODE_MISMATCH';
|
|
141
|
-
logger.error('jwt.verification.tunnelmode_mismatch', {
|
|
142
|
-
expected: expectedClaims.tunnelMode,
|
|
143
|
-
actual: payload.tunnelMode
|
|
144
|
-
});
|
|
145
|
-
throw error;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// AC5.1: Validate leaseId if provided
|
|
149
|
-
if (expectedClaims.leaseId && payload.leaseId && payload.leaseId !== expectedClaims.leaseId) {
|
|
150
|
-
const error = new Error(`JWT leaseId mismatch: ${payload.leaseId} !== ${expectedClaims.leaseId}`);
|
|
151
|
-
error.code = 'JWT_LEASEID_MISMATCH';
|
|
152
|
-
logger.error('jwt.verification.leaseid_mismatch', {
|
|
153
|
-
expected: expectedClaims.leaseId,
|
|
154
|
-
actual: payload.leaseId
|
|
155
|
-
});
|
|
156
|
-
throw error;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// AC1.4: Validate required claims are present
|
|
160
|
-
if (!payload.relayId) {
|
|
161
|
-
const error = new Error('Missing relayId in JWT payload');
|
|
162
|
-
error.code = 'JWT_MISSING_RELAYID';
|
|
163
|
-
throw error;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (!payload.protocol) {
|
|
167
|
-
const error = new Error('Missing protocol in JWT payload');
|
|
168
|
-
error.code = 'JWT_MISSING_PROTOCOL';
|
|
169
|
-
throw error;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (!payload.tunnelMode) {
|
|
173
|
-
const error = new Error('Missing tunnelMode in JWT payload');
|
|
174
|
-
error.code = 'JWT_MISSING_TUNNELMODE';
|
|
175
|
-
throw error;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
logger.debug('jwt.verification.success', {
|
|
179
|
-
relayId: payload.relayId,
|
|
180
|
-
protocol: payload.protocol,
|
|
181
|
-
tunnelMode: payload.tunnelMode,
|
|
182
|
-
leaseId: payload.leaseId
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
return payload;
|
|
186
|
-
} catch (error) {
|
|
187
|
-
// Map jose errors to our error codes
|
|
188
|
-
if (error.code === 'ERR_JWKS_TIMEOUT' || error.code === 'ERR_JWKS_MULTIPLE_MATCHING_KEYS') {
|
|
189
|
-
const jwksError = new Error(`JWKS error: ${error.message}`);
|
|
190
|
-
jwksError.code = 'JWKS_ERROR';
|
|
191
|
-
logger.error('jwt.verification.jwks_error', {
|
|
192
|
-
error: error.message,
|
|
193
|
-
code: error.code
|
|
194
|
-
});
|
|
195
|
-
throw jwksError;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
if (error.code === 'ERR_JWT_EXPIRED') {
|
|
199
|
-
const expiredError = new Error('JWT expired');
|
|
200
|
-
expiredError.code = 'JWT_EXPIRED';
|
|
201
|
-
logger.error('jwt.verification.expired', {
|
|
202
|
-
exp: error.claims?.exp,
|
|
203
|
-
now: Math.floor(Date.now() / 1000)
|
|
204
|
-
});
|
|
205
|
-
throw expiredError;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (error.code === 'ERR_JWT_CLAIM_VALIDATION_FAILED') {
|
|
209
|
-
// This could be issuer or audience mismatch
|
|
210
|
-
const claimError = new Error(`JWT claim validation failed: ${error.message}`);
|
|
211
|
-
claimError.code = 'JWT_CLAIM_INVALID';
|
|
212
|
-
logger.error('jwt.verification.claim_failed', {
|
|
213
|
-
error: error.message,
|
|
214
|
-
claim: error.claim
|
|
215
|
-
});
|
|
216
|
-
throw claimError;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (error.code === 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED') {
|
|
220
|
-
const sigError = new Error('JWT signature verification failed');
|
|
221
|
-
sigError.code = 'JWT_SIGNATURE_INVALID';
|
|
222
|
-
logger.error('jwt.verification.signature_failed', {
|
|
223
|
-
error: error.message
|
|
224
|
-
});
|
|
225
|
-
throw sigError;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Re-throw our custom errors
|
|
229
|
-
if (error.code && error.code.startsWith('JWT_') || error.code === 'JWKS_UNAVAILABLE' || error.code === 'JWKS_ERROR') {
|
|
230
|
-
throw error;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Unknown error - wrap it
|
|
234
|
-
const unknownError = new Error(`JWT verification failed: ${error.message}`);
|
|
235
|
-
unknownError.code = 'JWT_VERIFICATION_FAILED';
|
|
236
|
-
unknownError.originalError = error;
|
|
237
|
-
logger.error('jwt.verification.unknown_error', {
|
|
238
|
-
error: error.message,
|
|
239
|
-
code: error.code
|
|
240
|
-
});
|
|
241
|
-
throw unknownError;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Check if JWKS is available (for health checks)
|
|
247
|
-
* @returns {boolean} True if JWKS is initialized
|
|
248
|
-
*/
|
|
249
|
-
export function isJWKSAvailable() {
|
|
250
|
-
return jwksInitialized && jwksCache !== null;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Reset JWKS cache (for testing or re-initialization)
|
|
255
|
-
*/
|
|
256
|
-
export function resetJWKS() {
|
|
257
|
-
jwksCache = null;
|
|
258
|
-
jwksInitialized = false;
|
|
259
|
-
jwksInitPromise = null;
|
|
260
|
-
}
|
package/src/lib/logger.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
function isoNow() {
|
|
2
|
-
return new Date().toISOString();
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
// Parse env log level once at module load
|
|
6
|
-
const LEVEL_ORDER = { error: 40, warn: 30, info: 20, debug: 10 };
|
|
7
|
-
|
|
8
|
-
function normalizeLevel(value) {
|
|
9
|
-
const v = String(value || '').trim().toLowerCase();
|
|
10
|
-
if (v in LEVEL_ORDER) return v;
|
|
11
|
-
// QPA-CLI-10: Default to warn for quiet mode (suppress info/debug telemetry, show warn/error)
|
|
12
|
-
// Users can set LOG_LEVEL=info to see structured telemetry logs
|
|
13
|
-
return 'warn';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// QPA-CLI-10: Support both ASYX_LOG_LEVEL and LOG_LEVEL env vars
|
|
17
|
-
const ENV_LEVEL = normalizeLevel(process.env.ASYX_LOG_LEVEL || process.env.LOG_LEVEL);
|
|
18
|
-
|
|
19
|
-
function shouldEmit(level) {
|
|
20
|
-
// QPA-CLI-10: Default to warn level (quiet mode - suppress info/debug telemetry)
|
|
21
|
-
const desired = LEVEL_ORDER[ENV_LEVEL] ?? LEVEL_ORDER.warn;
|
|
22
|
-
const actual = LEVEL_ORDER[normalizeLevel(level)] ?? LEVEL_ORDER.error; // Default actual to error if invalid
|
|
23
|
-
return actual >= desired;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function emit(level, msg, fields = {}) {
|
|
27
|
-
const entry = {
|
|
28
|
-
ts: isoNow(),
|
|
29
|
-
lvl: level,
|
|
30
|
-
msg,
|
|
31
|
-
fields: fields && typeof fields === 'object' ? fields : {},
|
|
32
|
-
};
|
|
33
|
-
// Ensure single-line JSON output
|
|
34
|
-
const line = JSON.stringify(entry);
|
|
35
|
-
// Level gating: suppress messages below configured level
|
|
36
|
-
if (!shouldEmit(level)) return;
|
|
37
|
-
// Route by level to preserve severity semantics if needed
|
|
38
|
-
if (level === 'error') {
|
|
39
|
-
console.error(line);
|
|
40
|
-
} else if (level === 'warn') {
|
|
41
|
-
console.warn(line);
|
|
42
|
-
} else if (level === 'debug') {
|
|
43
|
-
console.debug(line);
|
|
44
|
-
} else {
|
|
45
|
-
console.log(line);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function log(level, msg, fields = {}) {
|
|
50
|
-
emit(level, msg, fields);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export const logger = {
|
|
54
|
-
debug: (msg, fields) => emit('debug', msg, fields),
|
|
55
|
-
info: (msg, fields) => emit('info', msg, fields),
|
|
56
|
-
warn: (msg, fields) => emit('warn', msg, fields),
|
|
57
|
-
error: (msg, fields) => emit('error', msg, fields),
|
|
58
|
-
log,
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export default logger;
|
|
62
|
-
|