Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit 950b231

Browse files
author
Brian Caffey
committed
wip fix google oauth2 signing
1 parent 85a6266 commit 950b231

File tree

10 files changed

+74
-25
lines changed

10 files changed

+74
-25
lines changed

.env.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ RDS_PORT=5432
1010
GITHUB_KEY=yourkey
1111
GITHUB_SECRET=yoursecret
1212

13-
GOOGLE_KEY=12345-abcde.apps.googleusercontent.com
14-
GOOGLE_SECRET=abcde-fghij
13+
GOOGLE_OAUTH2_KEY=12345-abcde.apps.googleusercontent.com
14+
GOOGLE_OAUTH2_SECRET=abcde-fghij
1515

1616
CELERY_BROKER_URL=redis://redis:6379
1717
CELERY_RESULT_BACKEND=redis://redis:6379

backend/accounts/utils/social/oauth.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,46 @@
66
import requests
77

88

9-
def get_access_token_from_code(backend, code):
10-
"""Get access token for any OAuth backend from code"""
9+
def get_payload(backend, code):
10+
1111

1212
key = f"{backend.upper()}_KEY"
1313
secret = f"{backend.upper()}_SECRET"
1414

1515
client_id = os.environ.get(key, "nokey")
1616
client_secret = os.environ.get(secret, "nosecret")
1717

18-
url = c.OAUTH[backend]['url']
1918

20-
payload = {
19+
if backend == 'github':
20+
payload = {
21+
'code': code,
22+
'client_id': client_id,
23+
'client_secret': client_secret,
24+
}
25+
26+
elif backend == 'google_oauth2':
27+
payload = {
2128
'code': code,
2229
'client_id': client_id,
2330
'client_secret': client_secret,
31+
'redirect_uri': "http://localhost/auth/google/callback",
32+
'grant_type': "authorization_code"
2433
}
2534

35+
return payload
36+
37+
38+
def get_access_token_from_code(backend, code):
39+
"""Get access token for any OAuth backend from code"""
40+
41+
url = c.OAUTH[backend]['url']
42+
43+
payload = get_payload(backend, code)
44+
2645
r = requests.post(url, data=payload)
2746

2847
# TODO: cleanup logic
2948
url = "http://example.com?" + str(r.content)
3049
params = dict(parse.parse_qsl(parse.urlsplit(url).query))
50+
3151
return params["b'access_token"]

backend/backend/settings/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@
6767

6868
AUTHENTICATION_BACKENDS = (
6969
'social_core.backends.github.GithubOAuth2',
70+
'social_core.backends.google.GoogleOAuth2',
7071
'django.contrib.auth.backends.ModelBackend',
7172
)
7273

7374
for key in ['GITHUB_KEY',
7475
'GITHUB_SECRET',
75-
# 'FACEBOOK_KEY',
76-
# 'FACEBOOK_SECRET'
76+
'GOOGLE_OAUTH2_KEY',
77+
'GOOGLE_OAUTH2_SECRET'
7778
]:
7879
# Use exec instead of eval here because we're not
7980
# just trying to evaluate a dynamic value here;

backend/core/constants.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
# https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#web-application-flow
12
# https://developers.google.com/identity/protocols/OpenIDConnect
23

34
OAUTH = {
45
'github': {
6+
'name': 'github',
57
'url':'https://github.com/login/oauth/access_token'
8+
},
9+
'google': {
10+
'name': 'google_oauth2',
11+
'url':'https://oauth2.googleapis.com/token'
612
}
7-
}
13+
}

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ services:
7070
environment:
7171
- CHOKIDAR_USEPOLLING=true
7272
- GITHUB_KEY=${GITHUB_KEY}
73-
- GOOGLE_KEY=${GOOGLE_KEY}
73+
- GOOGLE_OAUTH2_KEY=${GOOGLE_OAUTH2_KEY}
7474
- HTTP_PROTOCOL=http
7575
- WS_PROTOCOL=ws
7676
- DOMAIN_NAME=${DOMAIN_NAME}
@@ -103,8 +103,8 @@ services:
103103
- DJANGO_SETTINGS_MODULE=backend.settings.development
104104
- GITHUB_KEY=${GITHUB_KEY}
105105
- GITHUB_SECRET=${GITHUB_SECRET}
106-
- GOOGLE_KEY=${GOOGLE_KEY}
107-
- GOOGLE_SECRET=${GOOGLE_SECRET}
106+
- GOOGLE_OAUTH2_KEY=${GOOGLE_OAUTH2_KEY}
107+
- GOOGLE_OAUTH2_SECRET=${GOOGLE_OAUTH2_SECRET}
108108
depends_on:
109109
- db
110110

quasar/quasar.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module.exports = function(ctx) {
6565
}/ws/ping-pong/`
6666
),
6767
GITHUB_KEY: JSON.stringify(process.env.GITHUB_KEY),
68-
GOOGLE_KEY: JSON.stringify(process.env.GOOGLE_KEY)
68+
GOOGLE_OAUTH2_KEY: JSON.stringify(process.env.GOOGLE_OAUTH2_KEY)
6969
}
7070
: {
7171
API_URL: JSON.stringify(
@@ -77,7 +77,7 @@ module.exports = function(ctx) {
7777
}/ws/ping-pong/`
7878
),
7979
GITHUB_KEY: JSON.stringify(process.env.GITHUB_KEY),
80-
GOOGLE_KEY: JSON.stringify(process.env.GOOGLE_KEY)
80+
GOOGLE_OAUTH2_KEY: JSON.stringify(process.env.GOOGLE_OAUTH2_KEY)
8181
},
8282
scopeHoisting: true,
8383
useNotifier: false,

quasar/src/components/AuthModal.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@
5050
<div class="text-h6">TODO</div>
5151
Implement Sign up with Email + Email Verification
5252
<q-btn
53-
:href="githuboath2link"
53+
:href="$store.getters.oauthUrl('github')"
5454
type="a"
5555
class="full-width q-mt-md"
5656
label="Sign Up with GitHub"
5757
/>
58+
<q-btn
59+
:href="$store.getters.oauthUrl('google')"
60+
type="a"
61+
class="full-width q-mt-md"
62+
label="Sign Up with Google"
63+
/>
5864
</q-tab-panel>
5965
</q-tab-panels>
6066
</q-card>

quasar/src/store/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Vuex from "vuex";
33
import ui from "./ui";
44
import auth from "./auth.js";
55
import user from "./user.js";
6+
import social from "./social";
67

78
// import example from './module-example'
89

@@ -17,7 +18,8 @@ export default new Vuex.Store({
1718
modules: {
1819
ui,
1920
auth,
20-
user
21+
user,
22+
social
2123
},
2224

2325
// enable strict mode (adds overhead!)

quasar/src/store/social.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import buildURL from "axios/lib/helpers/buildURL";
12
import oauth from "../utils/oauth";
23

34
const state = {
@@ -6,10 +7,13 @@ const state = {
67

78
const getters = {
89
oauthUrl: () => {
9-
return provider => state.oauth[provider].sender;
10-
},
11-
getProfile: s => s.status,
12-
getUrl: s => s.status
10+
return provider => {
11+
const url = state.oauth[provider].url;
12+
const params = state.oauth[provider].params;
13+
14+
return buildURL(url, params);
15+
};
16+
}
1317
};
1418

1519
const actions = {};

quasar/src/utils/oauth.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
const protocol = process.env.NODE_ENV === "production" ? "https://" : "http://";
2-
const url = process.env.SITE_URL;
3-
const baseUrl = protocol + url;
1+
const url = process.env.API_URL;
42

53
const oauth = {
64
github: {
7-
url: "https://github.com/oauth",
5+
url: "https://github.com/login/oauth/authorize",
86
params: {
97
client_id: process.env.GITHUB_KEY,
10-
redirect_uri: `${baseUrl}/auth/github/callback`,
8+
redirect_uri: `${url}/auth/github/callback`,
119
login: "",
1210
scope: "user",
1311
state: "eworifjeovivoiej"
1412
}
13+
},
14+
google: {
15+
url: "https://accounts.google.com/o/oauth2/v2/auth",
16+
params: {
17+
client_id: process.env.GOOGLE_OAUTH2_KEY,
18+
response_type: "code",
19+
scope: "openid email",
20+
redirect_uri: `${url}/auth/google/callback`,
21+
state: "eworifjeovivoiej",
22+
nonce: "forewijf43oirjoifj",
23+
login_hint: ""
24+
}
1525
}
1626
};
1727

0 commit comments

Comments
 (0)