| author | durandn |
| Wed, 05 Oct 2016 14:24:32 +0200 | |
| changeset 124 | 983e72b4bc45 |
| parent 63 | 6bfac7c633a0 |
| permissions | -rw-r--r-- |
| 3 | 1 |
from flask import Flask, url_for, session, request, jsonify, render_template, redirect |
| 1 | 2 |
from flask_oauthlib.client import OAuth |
3 |
from settings.client_settings import ClientSettings |
|
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
4 |
import base64 |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
5 |
import requests |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
6 |
import json |
| 1 | 7 |
|
8 |
app = Flask(__name__) |
|
9 |
app.debug = True |
|
10 |
app.secret_key = 'secret' |
|
11 |
app.config.from_object(ClientSettings) |
|
12 |
oauth = OAuth(app) |
|
13 |
||
14 |
remote = oauth.remote_app( |
|
15 |
'remote', |
|
16 |
consumer_key=app.config.get("CLIENT_ID", ""), |
|
17 |
consumer_secret=app.config.get("CLIENT_SECRET", ""), |
|
18 |
request_token_params=app.config.get("REQUEST_TOKEN_PARAMS", ""), |
|
19 |
base_url=app.config.get("BASE_URL", ""), |
|
20 |
request_token_url=app.config.get("REQUEST_TOKEN_URL", ""), |
|
21 |
access_token_url=app.config.get("ACCESS_TOKEN_URL", ""), |
|
22 |
authorize_url=app.config.get("AUTHORIZE_URL", "") |
|
23 |
) |
|
24 |
||
25 |
||
26 |
@app.route('/') |
|
27 |
def index(): |
|
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
28 |
if 'remote_oauth_authorizationcode' not in session: |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
29 |
next_url = request.args.get('next') or request.referrer or None |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
30 |
return remote.authorize( |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
31 |
callback=url_for('authorized', next=next_url, _external=True) |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
32 |
) |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
33 |
if 'me' not in session: |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
34 |
resp = remote.get('user/InfoComplete') |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
35 |
print("authcode resp data: "+str(resp.data)) |
|
10
50b532f5e6cb
Adjusted oauth test server and client to better reflect GED interface
durandn
parents:
5
diff
changeset
|
36 |
me = resp.data.get("displayName", "") |
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
37 |
session["me"] = me |
|
10
50b532f5e6cb
Adjusted oauth test server and client to better reflect GED interface
durandn
parents:
5
diff
changeset
|
38 |
id = resp.data.get("id", "") |
|
50b532f5e6cb
Adjusted oauth test server and client to better reflect GED interface
durandn
parents:
5
diff
changeset
|
39 |
session["me_id"] = id |
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
40 |
if 'remote_oauth_clientcredentials' not in session: |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
41 |
auth_string = bytes(app.config["CLIENT_ID"]+':'+app.config['CLIENT_SECRET'], "utf-8") |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
42 |
auth_code = base64.b64encode(auth_string).decode("utf-8") |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
43 |
resp = requests.post(app.config["ACCESS_TOKEN_URL"]+"?grant_type=client_credentials&scope=basic", data={}, headers={ |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
44 |
'Authorization': 'Basic %s' % auth_code, |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
45 |
}) |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
46 |
if resp is None: |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
47 |
return 'Access denied: reason=%s error=%s' % ( |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
48 |
request.args['error_reason'], |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
49 |
request.args['error_description'] |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
50 |
) |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
51 |
session['remote_oauth_clientcredentials'] = (json.loads(resp.text)['access_token'], '') |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
52 |
resp = remote.get('user/InfoComplete') |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
53 |
print("clientcredentials resp data: "+str(resp.data)) |
| 32 | 54 |
server = resp.data.get("displayName", "") |
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
55 |
session["server"] = server |
|
10
50b532f5e6cb
Adjusted oauth test server and client to better reflect GED interface
durandn
parents:
5
diff
changeset
|
56 |
return render_template('client/index.html', current_user_id=session["me_id"], current_username=session["me"], oauth_username=session["server"]) |
| 1 | 57 |
|
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
58 |
@app.route('/renkan-request') |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
59 |
def renkan_request(): |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
60 |
if 'remote_oauth_clientcredentials' in session: |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
61 |
resp = requests.post( |
|
10
50b532f5e6cb
Adjusted oauth test server and client to better reflect GED interface
durandn
parents:
5
diff
changeset
|
62 |
app.config["CREATE_RENKAN_ENDPOINT"]+"?act_as="+str(session.get("me_id", "anonymous")), |
|
63
6bfac7c633a0
Added permission management into metaeducation (instead of it being in renkanmanager) + more logging around auth code + corrected client.py for oauth server so it sends the correct content-type when using client credentials
durandn
parents:
32
diff
changeset
|
63 |
json.dumps({"title": "RENKAN_FROM_GED"}), |
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
64 |
headers={ |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
65 |
'Authorization': 'Bearer %s' % session['remote_oauth_clientcredentials'][0], |
|
63
6bfac7c633a0
Added permission management into metaeducation (instead of it being in renkanmanager) + more logging around auth code + corrected client.py for oauth server so it sends the correct content-type when using client credentials
durandn
parents:
32
diff
changeset
|
66 |
'renkan-act-as': session.get("me_id", "anonymous"), |
|
6bfac7c633a0
Added permission management into metaeducation (instead of it being in renkanmanager) + more logging around auth code + corrected client.py for oauth server so it sends the correct content-type when using client credentials
durandn
parents:
32
diff
changeset
|
67 |
'content-type': "application/json" |
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
68 |
} |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
69 |
) |
|
63
6bfac7c633a0
Added permission management into metaeducation (instead of it being in renkanmanager) + more logging around auth code + corrected client.py for oauth server so it sends the correct content-type when using client credentials
durandn
parents:
32
diff
changeset
|
70 |
print("%r : %r" %(resp.status_code, json.loads(resp.text))) |
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
71 |
return redirect('/') |
| 1 | 72 |
|
73 |
@app.route('/authorized') |
|
74 |
def authorized(): |
|
75 |
resp = remote.authorized_response() |
|
76 |
if resp is None: |
|
77 |
return 'Access denied: reason=%s error=%s' % ( |
|
78 |
request.args['error_reason'], |
|
79 |
request.args['error_description'] |
|
80 |
) |
|
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
81 |
session['remote_oauth_authorizationcode'] = (resp['access_token'], '') |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
82 |
|
| 3 | 83 |
return redirect('/') |
| 1 | 84 |
|
85 |
||
86 |
@remote.tokengetter |
|
87 |
def get_oauth_token(): |
|
|
5
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
88 |
print("referrer : "+request.referrer) |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
89 |
if 'remote_oauth_clientcredentials' in session and 'server' not in session: |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
90 |
return session['remote_oauth_clientcredentials'] |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
91 |
else: |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
92 |
return session.get('remote_oauth_authorizationcode', '') |
|
4407b131a70e
adjustments on Oauth server and mock ged client + Readme
durandn
parents:
3
diff
changeset
|
93 |
|
| 1 | 94 |
|
95 |
||
96 |
if __name__ == '__main__': |
|
97 |
import os |
|
98 |
os.environ['DEBUG'] = 'true' |
|
99 |
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'true' |
|
100 |
app.run(host='localhost', port=8000) |