-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauth.py
More file actions
411 lines (332 loc) · 11 KB
/
Copy pathauth.py
File metadata and controls
411 lines (332 loc) · 11 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
"""
DhanHQ Authentication Module (v2.2.0).
This module provides OAuth and TOTP authentication using DhanLogin.
Simply run: python auth.py
Usage:
# OAuth Flow (opens browser)
python auth.py
# PIN + TOTP Flow (programmatic)
python auth.py --totp
# In your code
from auth import get_access_token, get_dhan_context
context = get_dhan_context() # Returns DhanContext for API calls
"""
import json
import os
import sys
import webbrowser
from dataclasses import dataclass
from pathlib import Path
try:
from dhanhq import DhanContext, DhanLogin
except ImportError:
print("Error: dhanhq>=2.2.0 not installed. Run: pip install dhanhq>=2.2.0")
sys.exit(1)
# Token file location (in project root)
TOKEN_FILE = Path(__file__).parent / ".dhan_token.json"
@dataclass
class DhanCredentials:
"""Stored Dhan credentials and tokens."""
client_id: str
access_token: str
app_id: str = ""
def to_dict(self) -> dict:
"""Convert to dictionary for JSON storage."""
return {
"client_id": self.client_id,
"access_token": self.access_token,
"app_id": self.app_id,
}
@classmethod
def from_dict(cls, data: dict) -> "DhanCredentials":
"""Create from dictionary."""
return cls(
client_id=data["client_id"],
access_token=data["access_token"],
app_id=data.get("app_id", ""),
)
def save_credentials(credentials: DhanCredentials) -> None:
"""Save credentials to token file."""
with open(TOKEN_FILE, "w") as f:
json.dump(credentials.to_dict(), f, indent=2)
print(f"\n[OK] Token saved to {TOKEN_FILE}")
def load_credentials() -> DhanCredentials | None:
"""Load credentials from token file if exists."""
if not TOKEN_FILE.exists():
return None
try:
with open(TOKEN_FILE, "r") as f:
data = json.load(f)
return DhanCredentials.from_dict(data)
except (json.JSONDecodeError, KeyError):
return None
def get_access_token() -> str:
"""
Get access token from saved credentials or environment variable.
Returns:
Access token string
Raises:
ValueError: If no token is available
"""
credentials = load_credentials()
if credentials:
return credentials.access_token
env_token = os.getenv("DHAN_ACCESS_TOKEN")
if env_token and env_token != "YOUR_ACCESS_TOKEN":
return env_token
raise ValueError(
"No access token found. Run 'python auth.py' to authenticate, "
"or set DHAN_ACCESS_TOKEN environment variable."
)
def get_client_id() -> str:
"""
Get client ID from saved credentials or environment variable.
Returns:
Client ID string
Raises:
ValueError: If no client ID is available
"""
credentials = load_credentials()
if credentials:
return credentials.client_id
env_client_id = os.getenv("DHAN_CLIENT_ID")
if env_client_id and env_client_id != "YOUR_CLIENT_ID":
return env_client_id
raise ValueError(
"No client ID found. Run 'python auth.py' to authenticate, "
"or set DHAN_CLIENT_ID environment variable."
)
def get_dhan_context() -> DhanContext:
"""
Get DhanContext for API initialization.
Returns:
DhanContext object for use with dhanhq, MarketFeed, OrderUpdate
Raises:
ValueError: If credentials not available
"""
return DhanContext(get_client_id(), get_access_token())
def oauth_login() -> DhanCredentials:
"""
Perform OAuth browser-based login using DhanLogin.
Returns:
DhanCredentials with access token
"""
print("=" * 60)
print("DhanHQ OAuth Authentication")
print("=" * 60)
print()
print("Enter your Dhan API credentials (from https://developers.dhan.co/):")
print()
client_id = input("Client ID: ").strip()
if not client_id:
print("Error: Client ID is required")
sys.exit(1)
app_id = input("App ID: ").strip()
if not app_id:
print("Error: App ID is required")
sys.exit(1)
app_secret = input("App Secret: ").strip()
if not app_secret:
print("Error: App Secret is required")
sys.exit(1)
print()
print("-" * 60)
print("Starting OAuth flow...")
print()
# Initialize DhanLogin
dhan_login = DhanLogin(client_id)
# Step 1: Generate consent
print("Step 1: Generating login session...")
try:
consent_id = dhan_login.generate_login_session(app_id, app_secret)
print(f"[OK] Consent ID generated: {consent_id}")
except Exception as e:
print(f"Error generating login session: {e}")
sys.exit(1)
print()
print("A browser window will open for you to login to Dhan.")
input("Press Enter to open the browser...")
# Open browser for login
login_url = f"https://login.dhan.co/?consent_id={consent_id}"
webbrowser.open(login_url)
print()
print("-" * 60)
print("After logging in, copy the 'token_id' from the redirect URL.")
print("The URL will look like: https://yourapp.com/?token_id=XXXXX")
print("-" * 60)
print()
# Step 2: Get token ID from user
token_id = input("Enter the Token ID from the redirect URL: ").strip()
if not token_id:
print("Error: Token ID is required")
sys.exit(1)
# Step 3: Exchange token ID for access token
print()
print("Step 2: Exchanging Token ID for Access Token...")
try:
access_token = dhan_login.consume_token_id(token_id, app_id, app_secret)
print("[OK] Access token obtained successfully!")
except Exception as e:
print(f"Error obtaining access token: {e}")
sys.exit(1)
# Validate token with user profile
print()
print("Step 3: Validating token...")
try:
user_info = dhan_login.user_profile(access_token)
print(f"[OK] Logged in as: {user_info.get('name', 'Unknown')}")
except Exception as e:
print(f"Warning: Could not validate profile: {e}")
# Create and save credentials
credentials = DhanCredentials(
client_id=client_id,
access_token=access_token,
app_id=app_id,
)
save_credentials(credentials)
print()
print("=" * 60)
print("[OK] Authentication successful!")
print("=" * 60)
print()
print("You can now run the trading bot with: python main.py")
print()
return credentials
def totp_login() -> DhanCredentials:
"""
Perform PIN + TOTP programmatic login using DhanLogin.
Returns:
DhanCredentials with access token
"""
print("=" * 60)
print("DhanHQ PIN + TOTP Authentication")
print("=" * 60)
print()
client_id = input("Client ID: ").strip()
if not client_id:
print("Error: Client ID is required")
sys.exit(1)
pin = input("PIN: ").strip()
if not pin:
print("Error: PIN is required")
sys.exit(1)
totp = input("TOTP (from authenticator app): ").strip()
if not totp:
print("Error: TOTP is required")
sys.exit(1)
print()
print("Generating access token...")
# Initialize DhanLogin and generate token
dhan_login = DhanLogin(client_id)
try:
token_data = dhan_login.generate_token(pin, totp)
access_token = token_data.get("access_token", token_data)
if isinstance(access_token, dict):
access_token = access_token.get("accessToken", str(token_data))
print("[OK] Access token generated!")
except Exception as e:
print(f"Error generating token: {e}")
sys.exit(1)
# Create and save credentials
credentials = DhanCredentials(
client_id=client_id,
access_token=str(access_token),
)
save_credentials(credentials)
print()
print("=" * 60)
print("[OK] Authentication successful!")
print("=" * 60)
print()
return credentials
def renew_token() -> None:
"""Renew existing access token."""
credentials = load_credentials()
if not credentials:
print("No saved credentials. Run 'python auth.py' first.")
sys.exit(1)
print("Renewing access token...")
dhan_login = DhanLogin(credentials.client_id)
try:
dhan_login.renew_token(credentials.access_token)
print("[OK] Token renewed successfully!")
except Exception as e:
print(f"Error renewing token: {e}")
print("You may need to re-authenticate with 'python auth.py'")
sys.exit(1)
def clear_credentials() -> None:
"""Remove saved credentials."""
if TOKEN_FILE.exists():
TOKEN_FILE.unlink()
print(f"[OK] Removed {TOKEN_FILE}")
else:
print("No saved credentials found.")
def show_status() -> None:
"""Show current authentication status."""
print("=" * 60)
print("DhanHQ Authentication Status")
print("=" * 60)
print()
credentials = load_credentials()
if credentials:
print("[OK] Authenticated (saved token)")
print(f" Client ID: {credentials.client_id}")
print(f" Token: {credentials.access_token[:20]}...")
print(f" Token file: {TOKEN_FILE}")
# Try to validate profile
try:
dhan_login = DhanLogin(credentials.client_id)
user_info = dhan_login.user_profile(credentials.access_token)
print(f" User: {user_info.get('name', 'Unknown')}")
print(" Status: Valid")
except Exception:
print(" Status: Token may be expired")
else:
env_token = os.getenv("DHAN_ACCESS_TOKEN")
if env_token and env_token != "YOUR_ACCESS_TOKEN":
print("[OK] Using environment variable DHAN_ACCESS_TOKEN")
else:
print("[X] Not authenticated")
print()
print("Run 'python auth.py' for OAuth login")
print("Run 'python auth.py --totp' for PIN+TOTP login")
def main():
"""Main entry point for CLI."""
import argparse
parser = argparse.ArgumentParser(
description="DhanHQ Authentication (v2.2.0)",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python auth.py # OAuth login (opens browser)
python auth.py --totp # PIN + TOTP login
python auth.py --renew # Renew existing token
python auth.py --status # Check authentication status
python auth.py --logout # Remove saved credentials
""",
)
parser.add_argument(
"--totp", "-t", action="store_true", help="Use PIN + TOTP authentication"
)
parser.add_argument(
"--renew", "-r", action="store_true", help="Renew existing access token"
)
parser.add_argument(
"--status", "-s", action="store_true", help="Show authentication status"
)
parser.add_argument(
"--logout", "-l", action="store_true", help="Remove saved credentials"
)
args = parser.parse_args()
if args.status:
show_status()
elif args.logout:
clear_credentials()
elif args.renew:
renew_token()
elif args.totp:
totp_login()
else:
oauth_login()
if __name__ == "__main__":
main()