|
11 | 11 | from conduit.profile.models import UserProfile |
12 | 12 | from .models import User |
13 | 13 | from .serializers import user_schema |
| 14 | +from conduit.config import GITHUB_CLIENT, GITHUB_SECRET, ACCESS_TOKEN_URL, GITHUB_API, STATE |
14 | 15 | import requests |
| 16 | +import os |
15 | 17 |
|
16 | 18 | blueprint = Blueprint('user', __name__) |
17 | 19 |
|
@@ -65,62 +67,39 @@ def update_user(**kwargs): |
65 | 67 | user.update(**kwargs) |
66 | 68 | return user |
67 | 69 |
|
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',)) |
| 70 | +@blueprint.route('/api/user/callback/<github_code>/<state>', methods = ('GET',)) |
78 | 71 | @use_kwargs(user_schema) |
79 | 72 | @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 | | - |
| 73 | +def github_oauth(github_code, state): |
| 74 | + try: |
| 75 | + if (state.strip() != STATE): |
| 76 | + raise InvalidUsage.user_not_found() |
| 77 | + |
| 78 | + payload = { 'client_id': GITHUB_CLIENT, |
| 79 | + 'client_secret': GITHUB_SECRET, |
| 80 | + 'code': github_code, |
| 81 | + } |
| 82 | + header = { |
| 83 | + 'Accept': 'application/json', |
| 84 | + } |
| 85 | + |
| 86 | + auth_response = requests.post(ACCESS_TOKEN_URL, params=payload, headers=header).json() |
| 87 | + access_token = auth_response["access_token"] |
| 88 | + |
| 89 | + auth_header = {"Authorization": "Bearer " + access_token} |
| 90 | + data_response = requests.get(GITHUB_API + 'user', headers=auth_header).json() |
| 91 | + email_response = requests.get(GITHUB_API + 'user/emails', headers=auth_header).json() |
| 92 | + |
| 93 | + username = data_response["login"] |
| 94 | + email = email_response[0]["email"] |
| 95 | + github_id = data_response["id"] |
| 96 | + |
| 97 | + user = User.query.filter_by(email=email).first() |
| 98 | + if user is None: |
| 99 | + userprofile = UserProfile(User(username, email, github_access_token = access_token).save()).save() |
| 100 | + user = userprofile.user |
126 | 101 |
|
| 102 | + user.token = create_access_token(identity=user, fresh=True) |
| 103 | + return user |
| 104 | + except: |
| 105 | + raise InvalidUsage.user_not_found() |
0 commit comments