11# -*- coding: utf-8 -*-
22"""User views."""
3- from flask import Blueprint , request
3+ from flask import Blueprint , request , jsonify , g
44from flask_apispec import use_kwargs , marshal_with
55from flask_jwt_extended import jwt_required , jwt_optional , create_access_token , current_user
66from sqlalchemy .exc import IntegrityError
77
88from conduit .database import db
9+ from conduit .extensions import github
910from conduit .exceptions import InvalidUsage
1011from conduit .profile .models import UserProfile
1112from .models import User
1213from .serializers import user_schema
14+ import requests
1315
1416blueprint = Blueprint ('user' , __name__ )
1517
16-
1718@blueprint .route ('/api/users' , methods = ('POST' ,))
1819@use_kwargs (user_schema )
1920@marshal_with (user_schema )
@@ -26,7 +27,6 @@ def register_user(username, password, email, **kwargs):
2627 raise InvalidUsage .user_already_registered ()
2728 return userprofile .user
2829
29-
3030@blueprint .route ('/api/users/login' , methods = ('POST' ,))
3131@jwt_optional
3232@use_kwargs (user_schema )
@@ -64,3 +64,63 @@ def update_user(**kwargs):
6464 kwargs ['updated_at' ] = user .created_at .replace (tzinfo = None )
6565 user .update (** kwargs )
6666 return user
67+
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 )
79+ @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+
126+
0 commit comments