Skip to content

Commit c67fd39

Browse files
author
f.fallah
committed
wip(docs): update upload, webhook, auth, intro
1 parent a3ee495 commit c67fd39

File tree

4 files changed

+217
-407
lines changed

4 files changed

+217
-407
lines changed

docs/en/auth.md

Lines changed: 56 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,73 @@
11
# Authentication Guide
22

3-
The Basalam Python SDK supports multiple authentication methods to suit different use cases. This guide covers all available authentication options with practical examples.
3+
The SDK supports three main authentication methods, all managed via authentication objects implementing the `BaseAuth` interface:
44

5-
All authentication is handled through authentication objects that implement the `BaseAuth` interface. The SDK supports three main authentication methods:
5+
- **Personal Access Token** – for personal or dedicated applications
6+
- **Authorization Code Flow** – for user authentication scenarios on third-party platforms
7+
- **Client Credentials** – for applications with legal/organizational access
68

7-
1. **Personal Token** - For applications with existing tokens
8-
2. **Authorization Code Flow** - For user authentication scenarios
9-
3. **Client Credentials** - For server-to-server applications
9+
For more information about these methods, refer to the [Authentication in Basalam API](../../quick-start#auth) document.
1010

1111
## Table of Contents
1212

13-
- [Personal Token](#personal-token-for-existing-tokens)
13+
- [Personal Access Token](#personal-access-token)
1414
- [Authorization Code Flow](#authorization-code-flow-for-user-authentication)
15-
- [Client Credentials](#client-credentials-recommended-for-server-to-server)
15+
- [Client Credentials](#client-credentials)
1616
- [Token Management](#token-management)
1717
- [Scopes](#scopes)
1818

19-
## Personal Token (For Existing Tokens)
19+
## Personal Access Token
2020

21-
Use this method when you already have valid access and refresh tokens.
22-
23-
### Basic Usage
21+
For personal applications for a booth or user, after [getting your token from the developer panel](https://developers.basalam.com/panel/tokens), you can manage it using `PersonalToken`.
2422

2523
```python
2624
from basalam_sdk import BasalamClient, PersonalToken
2725

28-
# Initialize with existing tokens
29-
auth = PersonalToken(
30-
token="your_existing_access_token",
31-
refresh_token="your_existing_refresh_token"
32-
)
33-
34-
# Create authenticated client
35-
client = BasalamClient(auth=auth)
36-
```
37-
38-
### Advanced Configuration
39-
40-
```python
41-
from basalam_sdk import PersonalToken, BasalamConfig
42-
43-
config = BasalamConfig(
44-
base_url="https://api.basalam.com",
45-
timeout=30
46-
)
47-
48-
auth = PersonalToken(
49-
token="your_access_token",
50-
refresh_token="your_refresh_token",
51-
token_type="Bearer",
52-
expires_in=7200, # 2 hours
53-
scope="customer.wallet.read vendor.product.write",
54-
config=config
55-
)
56-
57-
client = BasalamClient(auth=auth)
58-
```
59-
60-
### Usage Example
61-
62-
```python
63-
async def personal_token_example():
26+
def personal_token_example():
27+
# Initialize with existing tokens
6428
auth = PersonalToken(
6529
token="your_access_token",
6630
refresh_token="your_refresh_token",
67-
expires_in=3600
6831
)
69-
32+
33+
# Create an authenticated client
7034
client = BasalamClient(auth=auth)
71-
72-
# Use the client normally
73-
balance = await client.get_balance(user_id=123)
74-
return balance
75-
```
7635

77-
## Authorization Code Flow (For User Authentication)
36+
# Get current user info
37+
user = client.get_current_user()
38+
return user
39+
```
7840

79-
Use this method when you need to authenticate on behalf of a user.
41+
## Authorization Code Flow (for user authentication)
8042

81-
### Step-by-Step Process
43+
When building a third-party app requiring access from users, after [creating a client in the developer panel](https://developers.basalam.com/panel/clients), use the `AuthorizationCode` class to manage the flow.
8244

8345
```python
8446
from basalam_sdk import BasalamClient, AuthorizationCode, Scope
8547

86-
# Step 1: Create auth object
48+
# Step 1: Create the auth object
8749
auth = AuthorizationCode(
8850
client_id="your-client-id",
8951
client_secret="your-client-secret",
9052
redirect_uri="https://your-app.com/callback",
91-
scopes=[Scope.CUSTOMER_WALLET_READ, Scope.CUSTOMER_ORDER_READ]
53+
scopes=[
54+
Scope.CUSTOMER_WALLET_READ,
55+
Scope.CUSTOMER_ORDER_READ
56+
]
9257
)
9358

9459
# Step 2: Get authorization URL
9560
auth_url = auth.get_authorization_url(state="optional_state_parameter")
9661
print(f"Visit: {auth_url}")
9762

98-
# Step 3: Exchange code for tokens (after user authorization)
99-
token_info = await auth.get_token(code="authorization_code_from_callback")
63+
# Step 3: Exchange code for tokens (after receiving the code from the registered redirect URI)
64+
token_info = auth.get_token(code="authorization_code_from_callback")
10065

101-
# Step 4: Create authenticated client
66+
# Step 4: Create an authenticated client
10267
client = BasalamClient(auth=auth)
10368
```
10469

105-
### Simplified Flow
106-
107-
```python
108-
async def auth_code_example():
109-
# One-step token retrieval
110-
auth = AuthorizationCode(
111-
client_id="your-client-id",
112-
client_secret="your-client-secret",
113-
redirect_uri="https://your-app.com/callback"
114-
)
115-
116-
# Directly get token with authorization code
117-
token_info = await auth.get_token(code="your_authorization_code")
118-
119-
# The auth object now has the token and can be used with BasalamClient
120-
client = BasalamClient(auth=auth)
121-
122-
# Use the client
123-
balance = await client.get_balance(user_id=123)
124-
return balance
125-
```
126-
127-
### Web Application Example
70+
### Usage Example
12871

12972
```python
13073
from flask import Flask, request, redirect
@@ -138,134 +81,93 @@ def login():
13881
client_secret="your-client-secret",
13982
redirect_uri="https://your-app.com/callback"
14083
)
141-
84+
14285
auth_url = auth.get_authorization_url(state="user_session_id")
14386
return redirect(auth_url)
14487

14588
@app.route('/callback')
14689
async def callback():
14790
code = request.args.get('code')
14891
state = request.args.get('state')
149-
92+
15093
auth = AuthorizationCode(
15194
client_id="your-client-id",
15295
client_secret="your-client-secret",
15396
redirect_uri="https://your-app.com/callback"
15497
)
155-
98+
15699
# Exchange code for tokens
157100
token_info = await auth.get_token(code=code)
158-
159-
# Store tokens securely
160-
# ... store token_info.access_token, token_info.refresh_token
161-
101+
102+
# Securely store tokens
103+
# ... save token_info.access_token, token_info.refresh_token
104+
162105
return "Authentication successful!"
163106
```
164107

165-
## Client Credentials (Recommended for Server-to-Server)
108+
## Client Credentials
166109

167-
Use this method for server-to-server applications where you have a client ID and secret.
110+
To use APIs requiring organizational identity (e.g., Wallet), after [client authentication](../../quick-start#client_credentials) using `grant_type="client_credentials"`, use `ClientCredentials`.
168111

169-
### Basic Setup
112+
### Initial Configuration
170113

171114
```python
172115
from basalam_sdk import BasalamClient, ClientCredentials
173116

174117
# Basic authentication
175-
auth = ClientCredentials(
176-
client_id="your-client-id",
177-
client_secret="your-client-secret"
178-
)
179-
180-
# Create client
181-
client = BasalamClient(auth=auth)
182-
```
183-
184-
### With Specific Scopes
185-
186-
```python
187-
from basalam_sdk import ClientCredentials, Scope
188-
189-
# With specific scopes
190118
auth = ClientCredentials(
191119
client_id="your-client-id",
192120
client_secret="your-client-secret",
193-
scopes=[Scope.CUSTOMER_WALLET_READ, Scope.VENDOR_PRODUCT_WRITE]
121+
scopes=[
122+
Scope.CUSTOMER_WALLET_READ,
123+
Scope.VENDOR_PRODUCT_WRITE
124+
]
194125
)
195126

127+
# Create client
196128
client = BasalamClient(auth=auth)
197129
```
198130

199131
### Usage Example
200132

201133
```python
134+
from basalam_sdk import BasalamClient, ClientCredentials
135+
202136
async def client_credentials_example():
203137
auth = ClientCredentials(
204138
client_id="your-client-id",
205139
client_secret="your-client-secret"
206140
)
207141
client = BasalamClient(auth=auth)
208-
142+
209143
# Get user balance
210144
balance = await client.get_balance(user_id=123)
211-
212-
# Create webhook
213-
webhook = await client.create_webhook({
214-
"url": "https://your-app.com/webhook",
215-
"events": ["order.created"]
216-
})
217-
218-
return balance, webhook
145+
146+
return balance
219147
```
220148

221149
## Token Management
222150

223-
All authentication methods support intelligent token management.
224-
225-
### Get Token Information
151+
### Get Token Info
226152

227153
```python
154+
from basalam_sdk import BasalamClient, ClientCredentials
155+
228156
async def token_management_example():
229157
auth = ClientCredentials(
230158
client_id="your-client-id",
231159
client_secret="your-client-secret"
232160
)
233-
234-
# Get token - will reuse existing token if not expired
235-
token_info = await auth.get_token()
236-
237-
# Check token status
238-
if auth.token_info:
239-
print(f"Token expires at: {auth.token_info.expires_at}")
240-
print(f"Is expired: {auth.token_info.is_expired}")
241-
print(f"Should refresh: {auth.token_info.should_refresh}")
242-
print(f"Granted scopes: {list(auth.token_info.granted_scopes)}")
243-
244-
return token_info
245-
```
246161

247-
### Token Refresh
162+
# Get token – will reuse existing one if not expired
163+
token_info = await auth.get_token()
248164

249-
```python
250-
async def refresh_token_example():
251-
auth = AuthorizationCode(
252-
client_id="your-client-id",
253-
client_secret="your-client-secret",
254-
redirect_uri="https://your-app.com/callback"
255-
)
256-
257-
# Get initial token
258-
await auth.get_token(code="authorization_code")
259-
260-
# Later, refresh the token
261-
refreshed_token = await auth.get_token()
262-
263-
return refreshed_token
165+
return token_info
264166
```
265167

266168
## Scopes
267169

268-
Scopes define what permissions your application has. Available scopes include:
170+
Scopes define the permissions granted to your app. In addition to the [Scopes doc](https://developers.basalam.com/scopes), the SDK provides scopes via the `Scope` class. Available scopes include:
269171

270172
```python
271173
from basalam_sdk import Scope
@@ -293,9 +195,4 @@ auth = ClientCredentials(
293195
Scope.CUSTOMER_ORDER_READ
294196
]
295197
)
296-
```
297-
298-
## Next Steps
299-
300-
- [Wallet Service](./services/wallet.md) - Manage user balances
301-
- [Webhook Service](./services/webhook.md) - Handle webhook subscriptions
198+
```

0 commit comments

Comments
 (0)