forked from Talenttrust/Talenttrust-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiKeys.routes.ts
More file actions
167 lines (159 loc) · 4.37 KB
/
Copy pathapiKeys.routes.ts
File metadata and controls
167 lines (159 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* @module apiKeys.routes
* @description Express routes for API key management.
*
* These routes are protected by JWT authentication (not API key auth)
* since they are used to manage API keys themselves.
*/
import { Router } from 'express';
import { authenticateMiddleware } from '../auth/authenticate';
import { requirePermission } from '../auth/middleware';
import {
createApiKeyController,
listApiKeysController,
getApiKeyController,
rotateApiKeyController,
deactivateApiKeyController
} from '../controllers/apiKeyController';
const router = Router();
/**
* @route POST /api/v1/api-keys
* @desc Create a new API key
* @access Private (requires JWT authentication)
* @example
* // Request
* POST /api/v1/api-keys
* {
* "name": "Internal Service Key",
* "scope": ["contracts:read", "contracts:create"],
* "expiresAt": "2024-12-31T23:59:59Z"
* }
*
* // Response
* {
* "message": "API key created successfully",
* "apiKey": "abc123...", // Only returned once
* "info": {
* "id": "key-id",
* "name": "Internal Service Key",
* "scope": ["contracts:read", "contracts:create"],
* "createdBy": "user-id",
* "createdAt": "2024-01-01T00:00:00Z",
* "expiresAt": "2024-12-31T23:59:59Z",
* "isActive": true
* }
* }
*/
router.post(
'/api-keys',
authenticateMiddleware,
requirePermission('api-keys', 'create'),
createApiKeyController
);
/**
* @route GET /api/v1/api-keys
* @desc List active API keys for the authenticated user, newest first.
* Cursor-paginated via opaque `nextCursor` tokens so results stay
* stable across pages even as new keys are created concurrently.
* @access Private (requires JWT authentication)
* @query limit - Page size, 1-100 (default 20). Out-of-range values are
* clamped rather than rejected.
* @query cursor - Opaque cursor from the previous page's `nextCursor`.
* Omit for the first page. A malformed cursor returns 400.
* @example
* // Request
* GET /api/v1/api-keys?limit=20
*
* // Response
* {
* "apiKeys": [
* {
* "id": "key-id",
* "name": "Internal Service Key",
* "scope": ["contracts:read", "contracts:create"],
* "createdAt": "2024-01-01T00:00:00Z",
* "updatedAt": "2024-01-01T00:00:00Z",
* "expiresAt": "2024-12-31T23:59:59Z",
* "lastUsedAt": "2024-01-15T10:30:00Z",
* "isActive": true
* }
* ],
* "total": 1,
* "nextCursor": null,
* "hasNextPage": false,
* "limit": 20
* }
*/
router.get(
'/api-keys',
authenticateMiddleware,
requirePermission('api-keys', 'read'),
listApiKeysController
);
/**
* @route GET /api/v1/api-keys/:id
* @desc Get details of a specific API key
* @access Private (requires JWT authentication)
* @example
* // Response
* {
* "id": "key-id",
* "name": "Internal Service Key",
* "scope": ["contracts:read", "contracts:create"],
* "createdAt": "2024-01-01T00:00:00Z",
* "updatedAt": "2024-01-01T00:00:00Z",
* "expiresAt": "2024-12-31T23:59:59Z",
* "lastUsedAt": "2024-01-15T10:30:00Z",
* "isActive": true
* }
*/
router.get(
'/api-keys/:id',
authenticateMiddleware,
requirePermission('api-keys', 'read'),
getApiKeyController
);
/**
* @route POST /api/v1/api-keys/:id/rotate
* @desc Rotate an existing API key (generate new key, keep same ID)
* @access Private (requires JWT authentication)
* @example
* // Response
* {
* "message": "API key rotated successfully",
* "apiKey": "def456...", // New key - only returned once
* "info": {
* "id": "key-id",
* "name": "Internal Service Key",
* "scope": ["contracts:read", "contracts:create"],
* "createdBy": "user-id",
* "createdAt": "2024-01-01T00:00:00Z",
* "updatedAt": "2024-01-15T10:30:00Z",
* "expiresAt": "2024-12-31T23:59:59Z",
* "isActive": true
* }
* }
*/
router.post(
'/api-keys/:id/rotate',
authenticateMiddleware,
requirePermission('api-keys', 'update'),
rotateApiKeyController
);
/**
* @route DELETE /api/v1/api-keys/:id
* @desc Deactivate an API key
* @access Private (requires JWT authentication)
* @example
* // Response
* {
* "message": "API key deactivated successfully"
* }
*/
router.delete(
'/api-keys/:id',
authenticateMiddleware,
requirePermission('api-keys', 'delete'),
deactivateApiKeyController
);
export default router;