-
Notifications
You must be signed in to change notification settings - Fork 33
/
brlogin.py
182 lines (154 loc) · 6.43 KB
/
brlogin.py
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
from flask import Blueprint, request, redirect, url_for, render_template, session, jsonify, make_response
from flask import current_app as app
from limiter import limiter # Import the limiter instance
from utils.config import get_broker_api_key, get_broker_api_secret, get_login_rate_limit_min, get_login_rate_limit_hour
from utils.auth_utils import handle_auth_success, handle_auth_failure
import http.client
import json
import jwt
BROKER_API_KEY = get_broker_api_key()
LOGIN_RATE_LIMIT_MIN = get_login_rate_limit_min()
LOGIN_RATE_LIMIT_HOUR = get_login_rate_limit_hour()
brlogin_bp = Blueprint('brlogin', __name__, url_prefix='/')
@brlogin_bp.errorhandler(429)
def ratelimit_handler(e):
return jsonify(error="Rate limit exceeded"), 429
@brlogin_bp.route('/<broker>/callback', methods=['POST','GET'])
@limiter.limit(LOGIN_RATE_LIMIT_MIN)
@limiter.limit(LOGIN_RATE_LIMIT_HOUR)
def broker_callback(broker,para=None):
print(f'Broker is {broker}')
if session.get('logged_in'):
# Store broker in session and g
session['broker'] = broker
return redirect(url_for('dashboard_bp.dashboard'))
broker_auth_functions = app.broker_auth_functions
auth_function = broker_auth_functions.get(f'{broker}_auth')
if not auth_function:
return jsonify(error="Broker authentication function not found."), 404
if broker == 'fivepaisa':
if request.method == 'GET':
if 'user' not in session:
return redirect(url_for('auth.login'))
return render_template('5paisa.html')
elif request.method == 'POST':
clientcode = request.form.get('clientid')
broker_pin = request.form.get('pin')
totp_code = request.form.get('totp')
auth_token, error_message = auth_function(clientcode, broker_pin, totp_code)
forward_url = '5paisa.html'
elif broker == 'angel':
if request.method == 'GET':
if 'user' not in session:
return redirect(url_for('auth.login'))
return render_template('angel.html')
elif request.method == 'POST':
clientcode = request.form.get('clientid')
broker_pin = request.form.get('pin')
totp_code = request.form.get('totp')
auth_token, error_message = auth_function(clientcode, broker_pin, totp_code)
forward_url = 'angel.html'
elif broker=='fyers':
code = request.args.get('auth_code')
print(f'The code is {code}')
auth_token, error_message = auth_function(code)
forward_url = 'broker.html'
elif broker=='icici':
full_url = request.full_path
print(f'Full URL: {full_url}')
code = request.args.get('apisession')
print(f'The code is {code}')
auth_token, error_message = auth_function(code)
forward_url = 'broker.html'
elif broker=='dhan':
code = 'dhan'
print(f'The code is {code}')
auth_token, error_message = auth_function(code)
forward_url = 'broker.html'
elif broker=='kotak':
print(f"The Broker is {broker}")
if request.method == 'GET':
if 'user' not in session:
return redirect(url_for('auth.login'))
return render_template('kotak.html')
elif request.method == 'POST':
otp = request.form.get('otp')
token = request.form.get('token')
sid = request.form.get('sid')
userid = request.form.get('userid')
api_secret = get_broker_api_secret()
auth_token, error_message = auth_function(otp,token,sid,userid,api_secret)
forward_url = 'kotak.html'
else:
code = request.args.get('code') or request.args.get('request_token')
print(f'The code is {code}')
auth_token, error_message = auth_function(code)
forward_url = 'broker.html'
if auth_token:
# Store broker in session
session['broker'] = broker
print(f'Connected broker: {broker}')
if broker == 'zerodha':
#token = request.args.get('request_token')
auth_token = f'{BROKER_API_KEY}:{auth_token}'
if broker == 'dhan':
#token = request.args.get('request_token')
auth_token = f'{auth_token}'
return handle_auth_success(auth_token, session['user'],broker)
else:
return handle_auth_failure(error_message, forward_url=forward_url)
@brlogin_bp.route('/<broker>/loginflow', methods=['POST','GET'])
@limiter.limit(LOGIN_RATE_LIMIT_MIN)
@limiter.limit(LOGIN_RATE_LIMIT_HOUR)
def broker_loginflow(broker):
if broker == 'kotak':
mobilenumber = request.form.get('mobilenumber')
password = request.form.get('password')
conn = http.client.HTTPSConnection("gw-napi.kotaksecurities.com")
payload = json.dumps({
"mobileNumber": mobilenumber,
"password": password
})
api_secret = get_broker_api_secret()
headers = {
'accept': '*/*',
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_secret}'
}
conn.request("POST", "/login/1.0/login/v2/validate", payload, headers)
res = conn.getresponse()
data = res.read().decode("utf-8")
data_dict = json.loads(data)
print(data_dict)
if 'data' in data_dict:
token = data_dict['data']['token']
sid = data_dict['data']['sid']
decode_jwt = jwt.decode(token, options={"verify_signature": False})
userid = decode_jwt.get("sub")
para = {
"token": token,
"sid": sid,
"userid": userid
}
getKotakOTP(userid, api_secret)
return render_template('kotakotp.html', para=para)
else:
error_message = data_dict.get('message', 'Unknown error occurred')
return render_template('kotak.html', error_message=error_message)
return
def getKotakOTP(userid,token):
conn = http.client.HTTPSConnection("gw-napi.kotaksecurities.com")
payload = json.dumps({
"userId": userid,
"sendEmail": True,
"isWhitelisted": True
})
headers = {
'accept': '*/*',
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}
conn.request("POST", "/login/1.0/login/otp/generate", payload, headers)
res = conn.getresponse()
data = res.read()
return 'success'