-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_web_App.py
125 lines (91 loc) · 4.09 KB
/
user_web_App.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
##################### Library Imports #####################
import requests
from flask import Flask, request, jsonify, abort,render_template,render_template_string, redirect, url_for,session
# from werkzeug.utils import redirect
from requests_oauthlib import OAuth2Session
from flask.json import jsonify
import os
# -- ADINT Intermidiate Project
# -- Made by: Diogo Ferreira and Rafael Cordeiro
# ----------------------------------------
# --------------User APP------------------
# ----------------------------------------
#Flask
app = Flask(__name__)
# This information is obtained upon registration of a new GitHub OAuth
# application here: https://github.com/settings/applications/new
client_id = "570015174623402"
client_secret = "uKU28VqDtiTGm1j51+FkjFwxOiBqMjU4DEVBeWyrzHZ+7VhdWcfQc+A1oaYmow2QMKDa/bsoQb6Gvf+/MD0eHw=="
authorization_base_url = 'https://fenix.tecnico.ulisboa.pt/oauth/userdialog'
token_url = 'https://fenix.tecnico.ulisboa.pt/oauth/access_token'
Authentifed = False
# Check the Secret Number// IF NOT REDIRECT TO LOGIN
# User authen leva o secret e confirmar com o sistema
# Se sim, alterar authentified com global authentified = True e redirect para user
# Condição para Authentified = FALSE?
@app.route("/user",methods = ['POST', 'GET'])
def user():
if Authentifed:
return app.send_static_file("qrgen.html")
else:
return redirect(url_for('.demo'))
@app.route("/user/authentified/<path:secret>")
def userAuth(secret):
# print(secret)
# print(str(app.secret_key))
if str(secret) == str(app.secret_key):
# if True:
global Authentifed
Authentifed = True
return redirect(url_for('.user'))
else:
abort(404)
@app.route("/login")
def demo():
"""Step 1: User Authorization.
Redirect the user/resource owner to the OAuth provider (i.e. Github)
using an URL with a few key OAuth parameters.
"""
github = OAuth2Session(client_id, redirect_uri="http://localhost:5000/callback")
authorization_url, state = github.authorization_url(authorization_base_url)
# State is used to prevent CSRF, keep this for later.
session['oauth_state'] = state
return redirect(authorization_url)
# Step 2: User authorization, this happens on the provider.
@app.route("/callback", methods=["GET"])
def callback():
""" Step 3: Retrieving an access token.
The user has been redirected back from the provider to your registered
callback URL. With this redirection comes an authorization code included
in the redirect URL. We will use that to obtain an access token.
"""
print("CALLABACK")
print(request.url)
github = OAuth2Session(client_id, redirect_uri="http://localhost:5000/callback")
print(github.authorized)
token = github.fetch_token(token_url, client_secret=client_secret,
authorization_response=request.url)
# At this point you can fetch protected resources but lets save
# the token and show how this is done from a persisted token
# in /profile.
session['oauth_token'] = token
return redirect(url_for('.profile'))
@app.route("/profile", methods=["GET"])
def profile():
"""Fetching a protected resource using an OAuth 2 token.
"""
github = OAuth2Session(client_id, token=session['oauth_token'])
# try:
Info = jsonify(github.get('https://fenix.tecnico.ulisboa.pt/api/fenix/v1/person').json())
# messages = {'Authentified':True,'Secret Key':app.secret_key}
# return redirect(url_for('.user', message = messages))
return redirect(url_for('.userAuth',secret = str(app.secret_key)))
# except:
# # messages = {'Authentified':True,'Secret Key':app.secret_key}
# # return redirect(url_for('.user', message = messages))
# return jsonify(github.get('https://fenix.tecnico.ulisboa.pt/api/fenix/v1/person').json())
#Start server
if __name__ == "__main__":
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = "1"
app.secret_key = os.urandom(24)
app.run(debug=True)