@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.
@@ -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,CA8Ef;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,YAAY,GACjB,IAAI,CAWN"}
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"}
@@ -25,44 +25,49 @@ export async function authenticate(req, res, next) {
25
25
  });
26
26
  return;
27
27
  }
28
- const apiKey = authHeader.substring(7); // Remove "Bearer "
29
- if (!apiKey || !apiKey.startsWith('layer_')) {
28
+ const token = authHeader.substring(7); // Remove "Bearer "
29
+ if (!token) {
30
30
  res.status(401).json({
31
31
  error: 'unauthorized',
32
- message: 'Invalid API key format. Must start with "layer_"',
32
+ message: 'Missing token',
33
33
  });
34
34
  return;
35
35
  }
36
- const keyHash = crypto
36
+ const tokenHash = crypto
37
37
  .createHash('sha256')
38
- .update(apiKey)
38
+ .update(token)
39
39
  .digest('hex');
40
- const apiKeyRecord = await db.getApiKeyByHash(keyHash);
41
- if (!apiKeyRecord) {
42
- // Not an API key (it's potentially a session key)
43
- const sessionKey = await db.getSessionKeyByHash(keyHash);
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
- req.userId = sessionKey.userId;
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 (!apiKeyRecord.isActive) {
53
- res.status(401).json({
54
- error: 'unauthorized',
55
- message: 'API key has been revoked',
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
- // Attach userId to request for downstream handlers
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@layer-ai/core",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Core API routes and services for Layer AI",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",