-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
83 lines (63 loc) · 2.49 KB
/
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
import os
import flask
from flask import Flask
from flask_restplus import Api, Resource, reqparse
import requests
from google.oauth2 import id_token
from google.auth.transport import requests
import sqlalchemy
from dotenv import load_dotenv
load_dotenv()
from services import *
CLIENT_ID = os.getenv('G_API_CLIENT_ID')
app = Flask(__name__)
app.secret_key = os.getenv('G_API_SECRET')
api = Api(app, doc=False)
@api.route('/')
class RootView(Resource):
def get():
return {'msg': 'You are at root'}
@api.route('/token-signup')
class TokenSignup(Resource):
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('idtoken', required=True, help='IDToken must be specified.')
args = parser.parse_args()
token = args['idtoken']
try:
idinfo = id_token.verify_oauth2_token(token, requests.Request(), CLIENT_ID)
if not user_exists(idinfo['email']):
app_token = create_user(idinfo['name'], idinfo['email'], idinfo['jti'])
return {'msg': 'User created successfully.', 'token': app_token}
else:
return {'error': 'User exists already'}
except ValueError:
return {'error': 'Could not verify token.'}, 400
except sqlalchemy.exc.IntegrityError:
return {'error': 'User exists already'}
@api.route('/token-signin')
class TokenSignin(Resource):
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('idtoken', required=True, help='IDToken must be specified.')
args = parser.parse_args()
token = args['idtoken']
try:
idinfo = id_token.verify_oauth2_token(token, requests.Request(), CLIENT_ID)
app_token = token_signin_get_token(idinfo['email'])
return {'msg': 'User authenticated successfully.', 'token': app_token}
except ValueError:
return {'error': 'Could not verify token.'}, 400
@api.route('/profile')
class Profile(Resource):
"""Profile Endpoint fetches profile info related to the signed in account.
"""
def post(self):
auth_header = flask.request.headers.get('Authorization')
user = authenticate_user(auth_header)
flask.session['user'] = user.email
return {'name': user.name, 'email': user.email}
if __name__ == "__main__":
debug = True if os.getenv('DEBUG_APP') == 'True' else False
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
app.run('localhost', 8081, debug=debug)