Skip to content

Commit be8f71d

Browse files
committed
Moved into .env files and refactored code
1 parent 8821716 commit be8f71d

File tree

5 files changed

+66
-67
lines changed

5 files changed

+66
-67
lines changed

backend/conduit/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
from dotenv import load_dotenv
3+
from os.path import dirname, join
4+
5+
dotenv_path = join(dirname(__file__), '.env')
6+
load_dotenv(dotenv_path)
7+
8+
GITHUB_CLIENT = os.environ.get('GITHUB_ID')
9+
GITHUB_SECRET = os.environ.get('GITHUB_SECRET')
10+
ACCESS_TOKEN_URL = os.environ.get('ACCESS_TOKEN_URL')
11+
GITHUB_API = os.environ.get('GITHUB_API')
12+
STATE = os.environ.get('STATE')

backend/conduit/user/views.py

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from conduit.profile.models import UserProfile
1212
from .models import User
1313
from .serializers import user_schema
14+
from conduit.config import GITHUB_CLIENT, GITHUB_SECRET, ACCESS_TOKEN_URL, GITHUB_API, STATE
1415
import requests
16+
import os
1517

1618
blueprint = Blueprint('user', __name__)
1719

@@ -65,62 +67,39 @@ 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',))
70+
@blueprint.route('/api/user/callback/<github_code>/<state>', methods = ('GET',))
7871
@use_kwargs(user_schema)
7972
@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
126101

102+
user.token = create_access_token(identity=user, fresh=True)
103+
return user
104+
except:
105+
raise InvalidUsage.user_not_found()

components/profile/LoginForm.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import Router from "next/router";
2-
import React from "react";
2+
import React, { useEffect } from "react";
33
import { mutate } from "swr";
44

55
import ListErrors from "../common/ListErrors";
66
import UserAPI from "../../lib/api/user";
7+
import { CODE_URL, STATE, SCOPE, GITHUB_CLIENT } from "../../lib/utils/constant";
78

89
const LoginForm = () => {
910
const [isLoading, setLoading] = React.useState(false);
@@ -20,16 +21,20 @@ const LoginForm = () => {
2021
[]
2122
);
2223

24+
const authorize_url = CODE_URL + "?client_id=" + GITHUB_CLIENT + "&scope=" + SCOPE
25+
+ "&state=" + STATE;
26+
2327
let logging_in;
2428
if (typeof window !== "undefined"){
2529
const code = new URLSearchParams(window.location.search).get("code");
30+
const state = new URLSearchParams(window.location.search).get("state");
2631
if (code){
2732
logging_in = (<p>Redirecting to home page...</p>);
28-
React.useEffect(() => {
33+
useEffect(() => {
2934

3035
async function post_code(){
3136
try{
32-
const {data, status} = await UserAPI.post_code(code);
37+
const {data, status} = await UserAPI.post_code(code, state);
3338
console.log("begun await");
3439
if (data?.user){
3540
console.log(data.user)
@@ -105,10 +110,8 @@ const LoginForm = () => {
105110
</button>
106111
</fieldset>
107112
</form>
108-
<a href="https://github.com/login/oauth/authorize?client_id=98574e099fa640413899&scope=user+repo"
109-
className="btn btn-lg btn-primary pull-xs-left"
110-
>
111-
Sign in through GitHub REAL</a>
113+
<a href={authorize_url} className="btn btn-lg btn-primary pull-xs-right">
114+
Sign in through Github</a>
112115
{logging_in}
113116
</>
114117
);

lib/api/user.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ const UserAPI = {
138138
},
139139
get: async (username) => axios.get(`${SERVER_BASE_URL}/profiles/${username}`),
140140

141-
post_code: async (username) => {
141+
post_code: async (github_code, state) => {
142142
try{
143-
const response = await axios.post(
144-
`${SERVER_BASE_URL}/user/callback`,
145-
JSON.stringify({ user: { username } }),
143+
const response = await axios.get(
144+
`${SERVER_BASE_URL}/user/callback/${encodeURIComponent(github_code)}/${encodeURIComponent(state)}`,
146145
{
147146
headers: {
148147
"Content-Type": "application/json",

lib/utils/constant.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export const SERVER_BASE_URL = `http://127.0.0.1:5000/api`;
1414

1515
export const APP_NAME = `conduit`;
1616

17+
export const CODE_URL = 'https://github.com/login/oauth/authorize';
18+
export const GITHUB_CLIENT = '98574e099fa640413899';
19+
export const SCOPE = 'user+repo';
20+
//must conceal state later
21+
export const STATE = 'd3Asp0fK03M0Ldnwoi2Pnbh9knB2K335Ln';
22+
1723
export const ARTICLE_QUERY_MAP = {
1824
"tab=feed": `${SERVER_BASE_URL}/articles/feed`,
1925
"tab=tag": `${SERVER_BASE_URL}/articles/tag`

0 commit comments

Comments
 (0)