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

Commit 19a7526

Browse files
committed
finished google oauth
1 parent 950b231 commit 19a7526

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

backend/accounts/utils/social/oauth.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
from urllib import parse
34

@@ -8,9 +9,9 @@
89

910
def get_payload(backend, code):
1011

11-
12-
key = f"{backend.upper()}_KEY"
13-
secret = f"{backend.upper()}_SECRET"
12+
# this might need a better solution
13+
key = f"{backend.upper()}_KEY".replace("-", "_")
14+
secret = f"{backend.upper()}_SECRET".replace("-", "_")
1415

1516
client_id = os.environ.get(key, "nokey")
1617
client_secret = os.environ.get(secret, "nosecret")
@@ -23,12 +24,12 @@ def get_payload(backend, code):
2324
'client_secret': client_secret,
2425
}
2526

26-
elif backend == 'google_oauth2':
27+
elif backend == 'google-oauth2':
2728
payload = {
2829
'code': code,
2930
'client_id': client_id,
3031
'client_secret': client_secret,
31-
'redirect_uri': "http://localhost/auth/google/callback",
32+
'redirect_uri': "http://localhost/auth/google-oauth2/callback",
3233
'grant_type': "authorization_code"
3334
}
3435

@@ -44,8 +45,31 @@ def get_access_token_from_code(backend, code):
4445

4546
r = requests.post(url, data=payload)
4647

47-
# TODO: cleanup logic
48-
url = "http://example.com?" + str(r.content)
49-
params = dict(parse.parse_qsl(parse.urlsplit(url).query))
48+
# different providers have different responses to their oauth endpoints
49+
# for example:
50+
51+
# github returns this:
52+
#
53+
# b'access_token=76e9d25ed009fb7feroifjf0f58jf9rneb0b6b&scope=user&token_type=bearer'
54+
if backend == "github":
55+
56+
# TODO: cleanup logic
57+
url = "http://example.com?" + str(r.content)
58+
params = dict(parse.parse_qsl(parse.urlsplit(url).query))
59+
60+
return params["b'access_token"]
61+
62+
63+
# google returns this:
64+
# {
65+
# 'access_token': 'ya29.frejf8erf.erferfeg.erfeogOS9tzAPQlNlUXitkMbmSt',
66+
# 'expires_in': 3596,
67+
# 'scope': 'openid https://www.googleapis.com/auth/userinfo.email',
68+
# 'token_type': 'Bearer',
69+
# 'id_token': 'oierfoie940j.ferferfoprek/refpekf9efoeik.long token'
70+
# }
71+
elif backend == "google-oauth2":
72+
73+
token = r.json()['access_token']
5074

51-
return params["b'access_token"]
75+
return token

backend/core/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
'name': 'github',
77
'url':'https://github.com/login/oauth/access_token'
88
},
9-
'google': {
10-
'name': 'google_oauth2',
9+
'google-oauth2': {
10+
'name': 'google-oauth2',
1111
'url':'https://oauth2.googleapis.com/token'
1212
}
1313
}

quasar/src/components/AuthModal.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
label="Sign Up with GitHub"
5757
/>
5858
<q-btn
59-
:href="$store.getters.oauthUrl('google')"
59+
:href="$store.getters.oauthUrl('google-oauth2')"
6060
type="a"
6161
class="full-width q-mt-md"
6262
label="Sign Up with Google"

quasar/src/utils/oauth.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ const oauth = {
1111
state: "eworifjeovivoiej"
1212
}
1313
},
14-
google: {
14+
"google-oauth2": {
1515
url: "https://accounts.google.com/o/oauth2/v2/auth",
1616
params: {
1717
client_id: process.env.GOOGLE_OAUTH2_KEY,
1818
response_type: "code",
1919
scope: "openid email",
20-
redirect_uri: `${url}/auth/google/callback`,
21-
state: "eworifjeovivoiej",
20+
redirect_uri: `${url}/auth/google-oauth2/callback`,
21+
state: "eworifjeovivoiej", // TODO: change these
2222
nonce: "forewijf43oirjoifj",
2323
login_hint: ""
2424
}

0 commit comments

Comments
 (0)