@layer-ai/core 0.1.4 → 0.1.5
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/middleware/auth.d.ts.map +1 -1
- package/dist/middleware/auth.js +28 -23
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAK1D,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,OAAO,CAAC;QAChB,UAAU,OAAO;YACf,MAAM,CAAC,EAAE,MAAM,CAAC;YAChB,UAAU,CAAC,EAAE,MAAM,CAAC;SACrB;KACF;CACF;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAChC,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAK1D,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,OAAO,CAAC;QAChB,UAAU,OAAO;YACf,MAAM,CAAC,EAAE,MAAM,CAAC;YAChB,UAAU,CAAC,EAAE,MAAM,CAAC;SACrB;KACF;CACF;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAChC,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC,IAAI,CAAC,CAoFf;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,YAAY,GACjB,IAAI,CAWN"}
|
package/dist/middleware/auth.js
CHANGED
|
@@ -25,44 +25,49 @@ export async function authenticate(req, res, next) {
|
|
|
25
25
|
});
|
|
26
26
|
return;
|
|
27
27
|
}
|
|
28
|
-
const
|
|
29
|
-
if (!
|
|
28
|
+
const token = authHeader.substring(7); // Remove "Bearer "
|
|
29
|
+
if (!token) {
|
|
30
30
|
res.status(401).json({
|
|
31
31
|
error: 'unauthorized',
|
|
32
|
-
message: '
|
|
32
|
+
message: 'Missing token',
|
|
33
33
|
});
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
|
-
const
|
|
36
|
+
const tokenHash = crypto
|
|
37
37
|
.createHash('sha256')
|
|
38
|
-
.update(
|
|
38
|
+
.update(token)
|
|
39
39
|
.digest('hex');
|
|
40
|
-
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (!sessionKey) {
|
|
40
|
+
// First check if it's an API key (starts with layer_)
|
|
41
|
+
if (token.startsWith('layer_')) {
|
|
42
|
+
const apiKeyRecord = await db.getApiKeyByHash(tokenHash);
|
|
43
|
+
if (!apiKeyRecord) {
|
|
45
44
|
res.status(401).json({ error: 'unauthorized', message: 'Invalid API key' });
|
|
46
45
|
return;
|
|
47
46
|
}
|
|
48
|
-
|
|
47
|
+
if (!apiKeyRecord.isActive) {
|
|
48
|
+
res.status(401).json({
|
|
49
|
+
error: 'unauthorized',
|
|
50
|
+
message: 'API key has been revoked',
|
|
51
|
+
});
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// Attach userId to request for downstream handlers
|
|
55
|
+
req.userId = apiKeyRecord.userId;
|
|
56
|
+
req.apiKeyHash = tokenHash;
|
|
57
|
+
// Update last_used_at timestamp (async, dont await)
|
|
58
|
+
db.updateApiKeyLastUsed(tokenHash).catch((err) => {
|
|
59
|
+
console.error('Failed to update API key last_used_at:', err);
|
|
60
|
+
});
|
|
49
61
|
next();
|
|
50
62
|
return;
|
|
51
63
|
}
|
|
52
|
-
if
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
});
|
|
64
|
+
// Otherwise, check if it's a session key
|
|
65
|
+
const sessionKey = await db.getSessionKeyByHash(tokenHash);
|
|
66
|
+
if (!sessionKey) {
|
|
67
|
+
res.status(401).json({ error: 'unauthorized', message: 'Invalid token' });
|
|
57
68
|
return;
|
|
58
69
|
}
|
|
59
|
-
|
|
60
|
-
req.userId = apiKeyRecord.userId;
|
|
61
|
-
req.apiKeyHash = keyHash;
|
|
62
|
-
// Update last_used_at timestamp (async, dont await)
|
|
63
|
-
db.updateApiKeyLastUsed(keyHash).catch((err) => {
|
|
64
|
-
console.error('Failed to update API key last_used_at:', err);
|
|
65
|
-
});
|
|
70
|
+
req.userId = sessionKey.userId;
|
|
66
71
|
next();
|
|
67
72
|
}
|
|
68
73
|
catch (error) {
|