Skip to content

Commit cd794f7

Browse files
committedJan 29, 2017
initial
0 parents  commit cd794f7

File tree

8 files changed

+94
-0
lines changed

8 files changed

+94
-0
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
env/
2+
.idea/
3+
*.pyc

‎Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn appcore:app

‎appcore.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import Flask
2+
from modules.neutron import neutron
3+
4+
app = Flask(__name__)
5+
6+
7+
app.register_blueprint(neutron, url_prefix='/neutron')
8+
9+
10+
if __name__ == '__main__':
11+
app.run()

‎modules/__init__.py

Whitespace-only changes.

‎modules/neutron/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from views import neutron

‎modules/neutron/adfslogin.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
4+
5+
class AdfsLoginFailed(Exception):
6+
pass
7+
8+
9+
def adfslogin(email, password):
10+
s = requests.Session()
11+
12+
# First, request SMS to be redirected to ADFS
13+
r = s.get('https://sms.eursc.eu/sso.php')
14+
15+
# Second, log in to ADFS
16+
# Find the URL to continue the flow
17+
soup = BeautifulSoup(r.text, "html.parser")
18+
form = soup.select('#loginForm')
19+
form_url = 'https://sts.eursc.eu' + form[0]['action']
20+
21+
payload = {'AuthMethod': 'FormsAuthentication', 'UserName': email, 'Password': password}
22+
23+
r2 = s.post(form_url, data=payload)
24+
25+
# Third, follow ADFS's form to be redirected back to SMS
26+
soup2 = BeautifulSoup(r2.text, "html.parser")
27+
form2 = soup2.select('form')
28+
form_url2 = form2[0]['action']
29+
30+
# ADFS's intermediary form has a bunch of inputs we need to finish the SAML flow
31+
payload2 = {}
32+
33+
try:
34+
35+
for input in soup2.select('input[type=\'hidden\']'):
36+
payload2[input['name']] = input['value']
37+
38+
r3 = s.post(form_url2, data=payload2)
39+
40+
# Epic success!
41+
return s.cookies['PHPSESSID']
42+
except KeyError as e:
43+
raise AdfsLoginFailed(e)

‎modules/neutron/views.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from flask import Blueprint, request
2+
import json
3+
from adfslogin import adfslogin, AdfsLoginFailed
4+
import traceback
5+
6+
neutron = Blueprint('neutron', __name__)
7+
8+
9+
@neutron.route('/login', methods=['POST'])
10+
def login():
11+
email = request.values['email']
12+
password = request.values['password']
13+
14+
try:
15+
result = adfslogin(email, password)
16+
return json.dumps({
17+
'error': False,
18+
'cookie': result
19+
})
20+
except AdfsLoginFailed as e:
21+
return json.dumps({
22+
'error': True,
23+
'exception': traceback.format_exc()
24+
})

‎requirements.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
beautifulsoup4==4.5.3
2+
bs4==0.0.1
3+
click==6.7
4+
Flask==0.12
5+
gunicorn==19.6.0
6+
itsdangerous==0.24
7+
Jinja2==2.9.5
8+
MarkupSafe==0.23
9+
requests==2.13.0
10+
Werkzeug==0.11.15
11+
wheel==0.24.0

0 commit comments

Comments
 (0)
Please sign in to comment.