Reputation: 113
I need to create a service that will authorize through python requests in the personal account of a third-party resource, and then redirect to this personal account from the browser.
I decided to use the Flask + Nginx bundle.
First, I do authorization, take cookies and headers and try to add them to my browser, and then make a proxy redirect to this closed personal account through Nginx.
The Flask is like this:
@app.route("/protected_area")
def protected_area():
session = requests.Session()
user = fake_useragent.UserAgent().random
header = CaseInsensitiveDict()
header['user-agent'] = user
header['X-UA-Compatible'] = 'IE=Edge,chrome=1'
header['Cache-Control'] = 'public, max-age=0'
payload = {
'mode': MODE,
'username': LOGIN,
'password': PASS
}
url_main = BASIC_SITE_URL + 'login'
url_login = BASIC_SITE_URL + 'login/form'
url_cabinet = BASIC_SITE_URL + '?personal_cabinet'
response = session.get(url_main, headers=header, allow_redirects=False)
cookies = get_cookies(response.cookies, URL)
cookies = cookies.split(';')
cookies = [(c.split('=', 1)) for c in cookies]
session.cookies.update(dict(cookies))
response = session.post(url_login, data=payload, cookies=response.cookies, headers=header, allow_redirects=False)
cookies = get_cookies(response.cookies, URL)
cookies = cookies.split(';')
cookies = [(c.split('=', 1)) for c in cookies]
session.cookies.update(dict(cookies))
response = session.get(url_cabinet, headers=header, cookies=dict(cookies), allow_redirects=False)
if response.status_code == 200:
print('Sucessful!')
expire_date = datetime.datetime.now()
expire_date = expire_date + datetime.timedelta(days=1)
response = make_response()
response.set_cookie('ba-sso-csrf', dict(cookies).get('ba-sso-csrf'), expires=expire_date)
response.set_cookie('paSession', dict(cookies).get('paSession'), expires=expire_date)
response.header = CaseInsensitiveDict()
response.header['user-agent'] = user
response.header['X-UA-Compatible'] = 'IE=Edge,chrome=1'
response.header['Cache-Control'] = f'public, max-age={expire_date}'
return redirect("/redirect")
elif response.status_code == 404:
print('Unsuccessful!')
return redirect("/redirect")
And the config in Nginx is as follows:
server {
listen 80;
server_name flask;
location / {
proxy_pass http://flask/;
}
location /redirect {
proxy_pass http://VNESHNIY_URL/?personal_cabinet;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
But when I go to 127.0.0.1/protected_area in my browser, nothing happens. He tries to do 127.0.0.1/redirect, then Nginx leaves it to protected_area.
Please tell me what is the correct Nginx config for my task and whether I correctly transfer cookies and headers to Flask in order to use them to enter the closed page of an external resource.
Thank you.
Upvotes: 0
Views: 599
Reputation: 764
First of all you do not need to use server_name
in local Nginx configuration. Cause you try to connect to domain name without upstream
specification.
Try to use something like that:
server {
listen 80 default_server;
location / {
proxy_pass http://127.0.0.1:5000; # your flask app
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /redirect {
proxy_pass http://VNESHNIY_URL/?personal_cabinet;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
Upvotes: 1