-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.html
More file actions
417 lines (357 loc) · 15 KB
/
authentication.html
File metadata and controls
417 lines (357 loc) · 15 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authentication - Circuit Auction API</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<style>
body { padding-top: 70px; background-color: #f8f9fa; }
.navbar-brand { font-weight: bold; }
.main-content { padding: 2rem; }
pre { background: #f4f4f4; padding: 1rem; border-radius: 0.375rem; position: relative; }
.copy-btn { position: absolute; top: 0.5rem; right: 0.5rem; font-size: 0.8rem; }
.endpoint { font-family: 'Courier New', monospace; background: #e9ecef; padding: 0.2rem 0.4rem; border-radius: 0.25rem; }
.badge-method { font-size: 0.7em; padding: 0.3em 0.6em; }
.param-table th { background-color: #f8f9fa; }
</style>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="index.html">Circuit Auction API v1.0</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="index.html">Home</a></li>
<li class="nav-item"><a class="nav-link active" href="authentication.html">Authentication</a></li>
</ul>
</div>
</div>
</nav>
<div class="container main-content">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.html">API</a></li>
<li class="breadcrumb-item active">Authentication</li>
</ol>
</nav>
<h1>Authentication</h1>
<p class="lead">Obtain and use access tokens to authenticate API requests.</p>
<div class="card mb-4">
<div class="card-header">
<span class="badge bg-success badge-method">GET</span>
<code class="endpoint">/api/login-token</code>
</div>
<div class="card-body">
<h5>Overview</h5>
<p>The login-token endpoint uses HTTP Basic Authentication to generate or retrieve an access token for the authenticated user.</p>
<div class="alert alert-info">
<h6 class="alert-heading"><i class="fas fa-info-circle"></i> Token Reuse</h6>
<p class="mb-0">This endpoint intelligently manages tokens:</p>
<ul class="mb-0">
<li>If you already have a valid unexpired token, it returns the existing token</li>
<li>If no token exists or your token has expired, it creates a new one</li>
<li>This prevents token proliferation and ensures consistent authentication</li>
</ul>
</div>
<h5 class="mt-4">Request</h5>
<h6>HTTP Method: GET</h6>
<h6 class="mt-3">Headers</h6>
<div class="table-responsive">
<table class="table table-sm param-table">
<thead>
<tr>
<th>Header</th>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Authorization</code></td>
<td><code>Basic {credentials}</code></td>
<td>Base64-encoded <code>email:password</code></td>
</tr>
</tbody>
</table>
</div>
<h5 class="mt-4">Creating Basic Auth Credentials</h5>
<h6>JavaScript / Browser</h6>
<pre><code class="language-javascript">const email = 'user@example.com';
const password = 'securePassword123';
// Using btoa() for base64 encoding
const credentials = btoa(`${email}:${password}`);
// Result: dXNlckBleGFtcGxlLmNvbTpzZWN1cmVQYXNzd29yZDEyMw==</code></pre>
<h6 class="mt-3">Node.js</h6>
<pre><code class="language-javascript">const email = 'user@example.com';
const password = 'securePassword123';
// Using Buffer for base64 encoding
const credentials = Buffer.from(`${email}:${password}`).toString('base64');
// Result: dXNlckBleGFtcGxlLmNvbTpzZWN1cmVQYXNzd29yZDEyMw==</code></pre>
<h6 class="mt-3">Python</h6>
<pre><code class="language-python">import base64
email = 'user@example.com'
password = 'securePassword123'
# Using base64 encoding
credentials = base64.b64encode(f'{email}:{password}'.encode()).decode()
# Result: dXNlckBleGFtcGxlLmNvbTpzZWN1cmVQYXNzd29yZDEyMw==</code></pre>
<h6 class="mt-3">Command Line (bash)</h6>
<pre><code class="language-bash">echo -n 'user@example.com:securePassword123' | base64
# Result: dXNlckBleGFtcGxlLmNvbTpzZWN1cmVQYXNzd29yZDEyMw==</code></pre>
<h5 class="mt-4">Complete Request Examples</h5>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#js-fetch">JavaScript (Fetch)</a></li>
<li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#js-axios">JavaScript (Axios)</a></li>
<li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#curl">cURL</a></li>
<li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#python">Python</a></li>
</ul>
<div class="tab-content mt-2">
<div class="tab-pane fade show active" id="js-fetch">
<pre><code class="language-javascript">const email = 'user@example.com';
const password = 'securePassword123';
const credentials = btoa(`${email}:${password}`);
const response = await fetch('https://backoffice.ddev.site/api/login-token', {
method: 'GET',
headers: {
'Authorization': `Basic ${credentials}`
}
});
const data = await response.json();
console.log('Access Token:', data.data[0].token);</code></pre>
</div>
<div class="tab-pane fade" id="js-axios">
<pre><code class="language-javascript">import axios from 'axios';
const email = 'user@example.com';
const password = 'securePassword123';
const credentials = btoa(`${email}:${password}`);
const response = await axios.get('https://backoffice.ddev.site/api/login-token', {
headers: {
'Authorization': `Basic ${credentials}`
}
});
const token = response.data.data[0].token;
console.log('Access Token:', token);</code></pre>
</div>
<div class="tab-pane fade" id="curl">
<pre><code class="language-bash">curl -X GET "https://backoffice.ddev.site/api/login-token" \
-H "Authorization: Basic $(echo -n 'user@example.com:securePassword123' | base64)"
# Or with username:password directly (curl handles encoding)
curl -X GET "https://backoffice.ddev.site/api/login-token" \
-u "user@example.com:securePassword123"</code></pre>
</div>
<div class="tab-pane fade" id="python">
<pre><code class="language-python">import requests
import base64
email = 'user@example.com'
password = 'securePassword123'
credentials = base64.b64encode(f'{email}:{password}'.encode()).decode()
response = requests.get(
'https://backoffice.ddev.site/api/login-token',
headers={'Authorization': f'Basic {credentials}'}
)
token = response.json()['data'][0]['token']
print(f'Access Token: {token}')
# Or use requests basic auth helper
from requests.auth import HTTPBasicAuth
response = requests.get(
'https://backoffice.ddev.site/api/login-token',
auth=HTTPBasicAuth(email, password)
)</code></pre>
</div>
</div>
<h5 class="mt-4">Response</h5>
<pre><code class="language-json">{
"data": [{
"id": "123",
"token": "abc123def456ghi789jkl012mno345pqr678stu901vwx234yz",
"type": "access_token",
"uid": "456",
"expire": 1735689600
}]
}</code></pre>
<h5 class="mt-4">Response Fields</h5>
<div class="table-responsive">
<table class="table table-sm param-table">
<thead>
<tr>
<th>Field</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>id</code></td>
<td>string</td>
<td>Token entity ID</td>
</tr>
<tr>
<td><code>token</code></td>
<td>string</td>
<td>The access token to use for API requests</td>
</tr>
<tr>
<td><code>type</code></td>
<td>string</td>
<td>Token type (always "access_token")</td>
</tr>
<tr>
<td><code>uid</code></td>
<td>string</td>
<td>User ID associated with this token</td>
</tr>
<tr>
<td><code>expire</code></td>
<td>integer</td>
<td>Unix timestamp when the token expires</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="card mb-4">
<div class="card-header bg-primary text-white">
<h5 class="mb-0">Using the Access Token</h5>
</div>
<div class="card-body">
<p>Once you have obtained an access token, include it in the <code>Authorization</code> header of all subsequent API requests using the Bearer scheme.</p>
<h5>Request Format</h5>
<pre><code class="language-http">GET /api/v1.0/datatable-clients HTTP/1.1
Host: backoffice.ddev.site
Authorization: Bearer abc123def456ghi789jkl012mno345pqr678stu901vwx234yz</code></pre>
<h5 class="mt-4">Complete Example: Login and Make API Call</h5>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#complete-js">JavaScript</a></li>
<li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#complete-curl">cURL</a></li>
<li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#complete-python">Python</a></li>
</ul>
<div class="tab-content mt-2">
<div class="tab-pane fade show active" id="complete-js">
<pre><code class="language-javascript">// Step 1: Login and get token
const email = 'user@example.com';
const password = 'securePassword123';
const credentials = btoa(`${email}:${password}`);
const loginResponse = await fetch('https://backoffice.ddev.site/api/login-token', {
method: 'GET',
headers: {
'Authorization': `Basic ${credentials}`
}
});
const loginData = await loginResponse.json();
const accessToken = loginData.data[0].token;
// Step 2: Use token for API requests
const clientsResponse = await fetch('https://backoffice.ddev.site/api/v1.0/datatable-clients', {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const clients = await clientsResponse.json();
console.log('Clients:', clients);</code></pre>
</div>
<div class="tab-pane fade" id="complete-curl">
<pre><code class="language-bash"># Step 1: Login and get token
TOKEN=$(curl -s -X GET "https://backoffice.ddev.site/api/login-token" \
-u "user@example.com:securePassword123" \
| jq -r '.data[0].token')
echo "Token: $TOKEN"
# Step 2: Use token for API requests
curl -X GET "https://backoffice.ddev.site/api/v1.0/datatable-clients" \
-H "Authorization: Bearer $TOKEN"</code></pre>
</div>
<div class="tab-pane fade" id="complete-python">
<pre><code class="language-python">import requests
from requests.auth import HTTPBasicAuth
# Step 1: Login and get token
email = 'user@example.com'
password = 'securePassword123'
login_response = requests.get(
'https://backoffice.ddev.site/api/login-token',
auth=HTTPBasicAuth(email, password)
)
access_token = login_response.json()['data'][0]['token']
print(f'Token: {access_token}')
# Step 2: Use token for API requests
clients_response = requests.get(
'https://backoffice.ddev.site/api/v1.0/datatable-clients',
headers={'Authorization': f'Bearer {access_token}'}
)
clients = clients_response.json()
print(f'Clients: {clients}')</code></pre>
</div>
</div>
</div>
</div>
<div class="card mb-4">
<div class="card-header bg-warning">
<h5 class="mb-0">Error Handling</h5>
</div>
<div class="card-body">
<h5>401 Unauthorized - Invalid Credentials</h5>
<pre><code class="language-json">{
"type": "https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2",
"title": "Unauthorized",
"status": 401,
"detail": "Wrong credentials."
}</code></pre>
<h5 class="mt-3">401 Unauthorized - Missing Authorization Header</h5>
<pre><code class="language-json">{
"type": "https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2",
"title": "Unauthorized",
"status": 401,
"detail": "No authentication credentials provided."
}</code></pre>
<h5 class="mt-3">403 Forbidden - Token Expired</h5>
<p>When using an expired token for API requests:</p>
<pre><code class="language-json">{
"type": "https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4",
"title": "Forbidden",
"status": 403,
"detail": "Access token expired."
}</code></pre>
<p>When this occurs, request a new token using the <code>/api/login-token</code> endpoint.</p>
</div>
</div>
<div class="card mb-4">
<div class="card-header bg-info text-white">
<h5 class="mb-0">Best Practices</h5>
</div>
<div class="card-body">
<ul>
<li><strong>Store tokens securely:</strong> Never expose tokens in client-side code, logs, or version control</li>
<li><strong>Check expiration:</strong> Monitor the <code>expire</code> field and refresh tokens before they expire</li>
<li><strong>Handle 403 errors:</strong> Implement automatic re-authentication when receiving token expired errors</li>
<li><strong>Use HTTPS:</strong> Always use HTTPS to prevent token interception</li>
<li><strong>Token reuse:</strong> The endpoint returns existing valid tokens, so it's safe to call repeatedly</li>
<li><strong>Never log credentials:</strong> Avoid logging Basic Auth credentials or Bearer tokens</li>
<li><strong>Implement token caching:</strong> Cache tokens client-side to reduce authentication requests</li>
</ul>
</div>
</div>
<a href="index.html" class="btn btn-outline-secondary">Back to API Home</a>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<script>
document.querySelectorAll('pre code').forEach(block => {
const button = document.createElement('button');
button.className = 'btn btn-sm btn-outline-secondary copy-btn';
button.textContent = 'Copy';
button.onclick = () => {
navigator.clipboard.writeText(block.textContent);
button.textContent = 'Copied!';
setTimeout(() => button.textContent = 'Copy', 2000);
};
block.parentElement.appendChild(button);
});
</script>
</body>
</html>