1111from conduit .profile .models import UserProfile
1212from .models import User
1313from .serializers import user_schema
14+ from conduit .config import GITHUB_CLIENT , GITHUB_SECRET , ACCESS_TOKEN_URL , GITHUB_API , STATE
1415import requests
16+ import os
1517
1618blueprint = Blueprint ('user' , __name__ )
1719
@@ -65,62 +67,38 @@ def update_user(**kwargs):
6567 user .update (** kwargs )
6668 return user
6769
68- #TODO:
69- #1) we have to add the state to make sure no third party access when sending code
70- #2) change this away from username, only allows me to call the thing username cause of user_schema.
71- #if bit_token invalid and access_tok still valid, just reauthenticate with new code and stuff
72- #if access_token invalid but bit_token valid, ignore until bit_token gets invalid
73-
74- #Note: the parameter is username but it should be changed to github_code
75- #i just get errors thrown if
76-
77- @blueprint .route ('/api/user/callback' , methods = ('POST' ,))
78- @use_kwargs (user_schema )
70+ @blueprint .route ('/api/user/callback/<github_code>/<state>' , methods = ('GET' ,))
7971@marshal_with (user_schema )
80- def github_oauth (username , ** kwargs ):
81- #refactor and hide these
82-
83- #NOTE: use try catch block later
84- payload = { 'client_id' : "98574e099fa640413899" ,
85- 'client_secret' : "272ac3010797de4cc29c5c0caf0bbd9df4d79832" ,
86- 'code' : username ,
87- }
88- header = {
89- 'Accept' : 'application/json' ,
90- }
91-
92- auth_response = requests .post ('https://github.com/login/oauth/access_token' , params = payload , headers = header ).json ()
93-
94- #if it's an error response, the access_token will not work (like if code is invalid)
95- #it won't have access_token key-value pair
96- #build in try catch!
97- access_token = auth_response ["access_token" ]
98-
99- auth_header = {"Authorization" : "Bearer " + access_token }
100- data_response = requests .get ('https://api.github.com/user' , headers = auth_header ).json ()
101- email_response = requests .get ('https://api.github.com/user/emails' , headers = auth_header ).json ()
102-
103- username = data_response ["login" ]
104- email = email_response [0 ]["email" ]
105- github_id = data_response ["id" ]
106-
107- user = User .query .filter_by (email = email ).first ()
108- if user is None :
109- userprofile = UserProfile (User (username , email , github_access_token = access_token ).save ()).save ()
110- user = userprofile .user
111-
112- user .token = create_access_token (identity = user , fresh = True )
113- return user
114-
115- # Flask Migrate
116-
117- # write code
118- # run flaskdb migrate in the code
119- # flaskdb upgrade in the code
120- # Code isn't working because staging db uses staging code
121- # Code isn't working on local because we don't have db
122-
123- # When doing github auth, we need to use flask db migrate to be able to add our cols
124- # to our remote db
125-
72+ def github_oauth (github_code , state ):
73+ try :
74+ if (state .strip () != STATE ):
75+ raise InvalidUsage .user_not_found ()
76+
77+ payload = { 'client_id' : GITHUB_CLIENT ,
78+ 'client_secret' : GITHUB_SECRET ,
79+ 'code' : github_code ,
80+ }
81+ header = {
82+ 'Accept' : 'application/json' ,
83+ }
84+
85+ auth_response = requests .post (ACCESS_TOKEN_URL , params = payload , headers = header ).json ()
86+ access_token = auth_response ["access_token" ]
87+
88+ auth_header = {"Authorization" : "Bearer " + access_token }
89+ data_response = requests .get (GITHUB_API + 'user' , headers = auth_header ).json ()
90+ email_response = requests .get (GITHUB_API + 'user/emails' , headers = auth_header ).json ()
91+
92+ username = data_response ["login" ]
93+ email = email_response [0 ]["email" ]
94+ github_id = data_response ["id" ]
95+
96+ user = User .query .filter_by (email = email ).first ()
97+ if user is None :
98+ userprofile = UserProfile (User (username , email , github_access_token = access_token ).save ()).save ()
99+ user = userprofile .user
126100
101+ user .token = create_access_token (identity = user , fresh = True )
102+ return user
103+ except :
104+ raise InvalidUsage .user_not_found ()
0 commit comments