1 from flask import Flask, url_for, session, request, jsonify, render_template, redirect |
1 from flask import Flask, url_for, session, request, jsonify, render_template, redirect |
2 from flask_oauthlib.client import OAuth |
2 from flask_oauthlib.client import OAuth |
3 from settings.client_settings import ClientSettings |
3 from settings.client_settings import ClientSettings |
4 |
4 import base64 |
5 |
5 import requests |
|
6 import json |
6 |
7 |
7 app = Flask(__name__) |
8 app = Flask(__name__) |
8 app.debug = True |
9 app.debug = True |
9 app.secret_key = 'secret' |
10 app.secret_key = 'secret' |
10 app.config.from_object(ClientSettings) |
11 app.config.from_object(ClientSettings) |
22 ) |
23 ) |
23 |
24 |
24 |
25 |
25 @app.route('/') |
26 @app.route('/') |
26 def index(): |
27 def index(): |
27 if 'remote_oauth' in session: |
28 if 'remote_oauth_authorizationcode' not in session: |
28 resp = remote.get('me') |
29 next_url = request.args.get('next') or request.referrer or None |
29 username = resp.data.get("username", "") |
30 return remote.authorize( |
30 return render_template('client/index.html', username=username) |
31 callback=url_for('authorized', next=next_url, _external=True) |
31 next_url = request.args.get('next') or request.referrer or None |
32 ) |
32 return remote.authorize( |
33 if 'me' not in session: |
33 callback=url_for('authorized', next=next_url, _external=True) |
34 resp = remote.get('user/InfoComplete') |
34 ) |
35 print("authcode resp data: "+str(resp.data)) |
|
36 me = resp.data.get("username", "") |
|
37 session["me"] = me |
|
38 if 'remote_oauth_clientcredentials' not in session: |
|
39 auth_string = bytes(app.config["CLIENT_ID"]+':'+app.config['CLIENT_SECRET'], "utf-8") |
|
40 auth_code = base64.b64encode(auth_string).decode("utf-8") |
|
41 resp = requests.post(app.config["ACCESS_TOKEN_URL"]+"?grant_type=client_credentials&scope=basic", data={}, headers={ |
|
42 'Authorization': 'Basic %s' % auth_code, |
|
43 }) |
|
44 if resp is None: |
|
45 return 'Access denied: reason=%s error=%s' % ( |
|
46 request.args['error_reason'], |
|
47 request.args['error_description'] |
|
48 ) |
|
49 session['remote_oauth_clientcredentials'] = (json.loads(resp.text)['access_token'], '') |
|
50 resp = remote.get('user/InfoComplete') |
|
51 print("clientcredentials resp data: "+str(resp.data)) |
|
52 server = resp.data.get("username", "") |
|
53 session["server"] = server |
|
54 return render_template('client/index.html', current_username=session["me"], oauth_username=session["server"]) |
35 |
55 |
|
56 @app.route('/renkan-request') |
|
57 def renkan_request(): |
|
58 if 'remote_oauth_clientcredentials' in session: |
|
59 resp = requests.post( |
|
60 app.config["CREATE_RENKAN_ENDPOINT"]+"?act_as="+session.get("me", "anonymous"), |
|
61 {"title": "RENKAN_FROM_GED"}, |
|
62 headers={ |
|
63 'Authorization': 'Bearer %s' % session['remote_oauth_clientcredentials'][0], |
|
64 'renkan-act-as': session.get("me", "anonymous") |
|
65 } |
|
66 ) |
|
67 print(resp.text) |
|
68 return redirect('/') |
36 |
69 |
37 @app.route('/authorized') |
70 @app.route('/authorized') |
38 def authorized(): |
71 def authorized(): |
39 resp = remote.authorized_response() |
72 resp = remote.authorized_response() |
40 if resp is None: |
73 if resp is None: |
41 return 'Access denied: reason=%s error=%s' % ( |
74 return 'Access denied: reason=%s error=%s' % ( |
42 request.args['error_reason'], |
75 request.args['error_reason'], |
43 request.args['error_description'] |
76 request.args['error_description'] |
44 ) |
77 ) |
45 print resp |
78 session['remote_oauth_authorizationcode'] = (resp['access_token'], '') |
46 session['remote_oauth'] = (resp['access_token'], '') |
79 |
47 return redirect('/') |
80 return redirect('/') |
48 |
81 |
49 |
82 |
50 @remote.tokengetter |
83 @remote.tokengetter |
51 def get_oauth_token(): |
84 def get_oauth_token(): |
52 return session.get('remote_oauth') |
85 print("referrer : "+request.referrer) |
|
86 if 'remote_oauth_clientcredentials' in session and 'server' not in session: |
|
87 return session['remote_oauth_clientcredentials'] |
|
88 else: |
|
89 return session.get('remote_oauth_authorizationcode', '') |
|
90 |
53 |
91 |
54 |
92 |
55 if __name__ == '__main__': |
93 if __name__ == '__main__': |
56 import os |
94 import os |
57 os.environ['DEBUG'] = 'true' |
95 os.environ['DEBUG'] = 'true' |