Skip to content

Commit 523770e

Browse files
committed
[authentication] fix examples
1 parent 7f1e4a2 commit 523770e

File tree

2 files changed

+19
-87
lines changed

2 files changed

+19
-87
lines changed

docs/faq/authentication.md

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,7 @@ JWT (JSON Web Token) authentication is the primary authentication mechanism for
88

99
Migrating from Basic Authentication to JWT provides enhanced security, better performance, and fine-grained access control. Here's how to migrate:
1010

11-
### Step 1: Generate a JWT Token
12-
13-
```bash
14-
curl -X POST "https://api.sms-gate.app/3rdparty/v1/auth/token" \
15-
-u "username:password" \
16-
-H "Content-Type: application/json" \
17-
-d '{
18-
"ttl": 3600,
19-
"scopes": ["messages:send", "messages:read"]
20-
}'
21-
```
22-
23-
### Step 2: Update Your Code
11+
### Step 1: Update Your Code
2412

2513
Replace Basic Auth with JWT Bearer tokens:
2614

@@ -29,7 +17,7 @@ Replace Basic Auth with JWT Bearer tokens:
2917
response = requests.post(
3018
"https://api.sms-gate.app/3rdparty/v1/messages",
3119
auth=("username", "password"),
32-
json={"recipient": "+1234567890", "message": "Hello world!"}
20+
json={"phoneNumbers": ["+1234567890"], "textMessage": {"text": "Hello world!"}}
3321
)
3422
```
3523

@@ -53,11 +41,11 @@ Replace Basic Auth with JWT Bearer tokens:
5341
"Authorization": f"Bearer {access_token}",
5442
"Content-Type": "application/json"
5543
},
56-
json={"recipient": "+1234567890", "message": "Hello world!"}
44+
json={"phoneNumbers": ["+1234567890"], "textMessage": {"text": "Hello world!"}}
5745
)
5846
```
5947

60-
### Step 3: Implement Token Management
48+
### Step 2: Implement Token Management
6149

6250
- **Token Refresh**: Implement automatic token refresh before expiration
6351
- **Error Handling**: Handle 401/403 errors gracefully
@@ -153,7 +141,6 @@ class SMSGatewayClient:
153141
- Refresh tokens 5-10 minutes before expiration
154142
- Implement exponential backoff for failed refresh attempts
155143
- Store tokens securely (not in client-side code)
156-
- Monitor token usage patterns
157144

158145
## 🛡️ How do I revoke a JWT token?
159146

@@ -168,35 +155,6 @@ curl -X DELETE "https://api.sms-gate.app/3rdparty/v1/auth/token/{jti}" \
168155

169156
Where `{jti}` is the token ID from the token response.
170157

171-
## 🔒 Is JWT authentication more secure than Basic Auth?
172-
173-
Yes, JWT authentication provides several security advantages over Basic Authentication:
174-
175-
### Security Benefits
176-
177-
1. **No Credential Transmission**: JWT tokens don't transmit username/password with each request
178-
2. **Fine-grained Access Control**: Scopes limit what each token can do
179-
3. **Short-lived Tokens**: Tokens expire automatically, reducing the impact of compromise
180-
4. **Digital Signatures**: Tokens are cryptographically signed to prevent tampering
181-
5. **No Session Storage**: Stateless authentication reduces server-side attack surface
182-
183-
### Security Considerations
184-
185-
While JWT is more secure than Basic Auth, proper implementation is crucial:
186-
187-
- **Strong Secrets**: Use at least 32-character random secrets
188-
- **HTTPS Only**: Always transmit tokens over encrypted connections
189-
- **Short TTLs**: Use the shortest practical expiration time
190-
- **Scope Limitation**: Request only necessary scopes
191-
- **Secure Storage**: Store tokens securely on the server side
192-
193-
!!! tip "Security Best Practices"
194-
- Implement token revocation for compromised tokens
195-
- Monitor token generation and usage patterns
196-
- Use HTTPS for all API communications
197-
- Regularly rotate JWT secrets
198-
- Implement proper error handling for authentication failures
199-
200158
## 🔐 "Invalid token" JWT Error
201159

202160
The "invalid token" error occurs when the JWT token is malformed, has an incorrect signature, or cannot be validated by the server.
@@ -349,10 +307,9 @@ When migrating from Basic Authentication to JWT, you may encounter various issue
349307

350308
### Common Issues
351309

352-
1. **Mixed Authentication**: Some requests use Basic Auth while others use JWT
353-
2. **Token Generation Errors**: Unable to generate JWT tokens
354-
3. **Permission Errors**: JWT tokens don't have the same permissions as Basic Auth
355-
4. **Code Compatibility**: Existing code doesn't work with JWT authentication
310+
1. **Token Generation Errors**: Unable to generate JWT tokens
311+
2. **Permission Errors**: JWT tokens don't have the same permissions as Basic Auth
312+
3. **Code Compatibility**: Existing code doesn't work with JWT authentication
356313

357314
### Troubleshooting Steps
358315

docs/integration/authentication.md

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ All scopes follow the pattern: `resource:action`
235235
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
236236
-H "Content-Type: application/json" \
237237
-d '{
238-
"recipient": "+1234567890",
238+
"phoneNumbers": ["+1234567890"],
239239
"textMessage": {
240240
"text": "Hello from JWT!"
241241
}
@@ -288,12 +288,12 @@ All scopes follow the pattern: `resource:action`
288288
"Content-Type": "application/json"
289289
},
290290
data=json.dumps({
291-
"recipient": "+1234567890",
292-
"message": "Hello from JWT!"
291+
"phoneNumbers": ["+1234567890"],
292+
"textMessage": {"text": "Hello from JWT!"}
293293
})
294294
)
295295

296-
if response.status_code == 201:
296+
if response.status_code == 202:
297297
print("Message sent successfully!")
298298
else:
299299
print(f"Error sending message: {response.status_code} - {response.text}")
@@ -343,8 +343,8 @@ All scopes follow the pattern: `resource:action`
343343
'Content-Type': 'application/json'
344344
},
345345
body: JSON.stringify({
346-
recipient: "+1234567890",
347-
message: "Hello from JWT!"
346+
phoneNumbers: ["+1234567890"],
347+
textMessage: {text: "Hello from JWT!"}
348348
})
349349
})
350350
.then(response => response.json())
@@ -382,8 +382,8 @@ All scopes follow the pattern: `resource:action`
382382
"https://api.sms-gate.app/3rdparty/v1/messages",
383383
auth=("username", "password"),
384384
json={
385-
"recipient": "+1234567890",
386-
"message": "Hello world!"
385+
"phoneNumbers": ["+1234567890"],
386+
"textMessage": {"text": "Hello world!"}
387387
}
388388
)
389389
```
@@ -414,8 +414,8 @@ All scopes follow the pattern: `resource:action`
414414
"Content-Type": "application/json"
415415
},
416416
json={
417-
"recipient": "+1234567890",
418-
"message": "Hello world!"
417+
"phoneNumbers": ["+1234567890"],
418+
"textMessage": {"text": "Hello world!"}
419419
}
420420
)
421421
```
@@ -498,7 +498,7 @@ class SMSGatewayClient:
498498
json={"recipient": recipient, "textMessage": {"text": message}}
499499
)
500500

501-
if response.status_code == 201:
501+
if response.status_code == 202:
502502
return response.json()
503503
else:
504504
raise Exception(f"Failed to send message: {response.status_code} - {response.text}")
@@ -531,34 +531,9 @@ print("Message sent:", result)
531531
- **Token Storage**: Store tokens securely (e.g., HttpOnly cookies, secure server-side storage)
532532
- **CSRF Protection**: Implement CSRF protection for web applications
533533

534-
## Troubleshooting 🔧
535-
536-
### Common Issues
537-
538-
#### Token Not Working
539-
540-
1. **Verify Expiration**: Check if the token has expired
541-
2. **Confirm Scopes**: Verify the token has the required scopes
542-
543-
#### Authentication Errors
544-
545-
1. **Verify Credentials**: Check username and password for token generation
546-
2. **Check Headers**: Ensure the Authorization header is correctly formatted
547-
3. **Confirm Endpoint**: Verify you're using the correct API endpoint
548-
4. **Check Network**: Ensure you can reach the API server
549-
550-
#### Permission Errors
551-
552-
1. **Check Scopes**: Verify your token has the required scopes
553-
2. **Check Resource**: Confirm you're accessing the correct resource
554-
555-
### Debugging Tips
556-
557-
1. **Test with cURL**: Use cURL to test authentication independently
558-
2. **Validate Tokens**: Use online JWT validators to check token structure
559-
560534
## See Also 🔗
561535

562536
- [API Reference](api.md) - Complete API endpoint documentation
563537
- [Integration Guide](index.md) - Overview of integration options
564538
- [Client Libraries](client-libraries.md) - Pre-built libraries for various languages
539+
- [Authentication FAQ](../faq/authentication.md) - Frequently Asked Questions about JWT authentication

0 commit comments

Comments
 (0)