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

Commit 11b2204

Browse files
author
Brian Caffey
committed
refactored oauth signin
1 parent 855108c commit 11b2204

File tree

12 files changed

+119
-35
lines changed

12 files changed

+119
-35
lines changed

backend/accounts/utils/social/github.py

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
from urllib import parse
3+
4+
from core import constants as c
5+
6+
import requests
7+
8+
9+
def get_access_token_from_code(backend, code):
10+
"""Get access token for any OAuth backend from code"""
11+
12+
key = f"{backend.upper()}_KEY"
13+
secret = f"{backend.upper()}_SECRET"
14+
15+
client_id = os.environ.get(key, "nokey")
16+
client_secret = os.environ.get(secret, "nosecret")
17+
18+
url = c.OAUTH[backend]['url']
19+
20+
payload = {
21+
'code': code,
22+
'client_id': client_id,
23+
'client_secret': client_secret,
24+
}
25+
26+
r = requests.post(url, data=payload)
27+
28+
# TODO: cleanup logic
29+
url = "http://example.com?" + str(r.content)
30+
params = dict(parse.parse_qsl(parse.urlsplit(url).query))
31+
return params["b'access_token"]

backend/accounts/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from social_django.utils import psa
1111

1212
from .serializers import UserSerializer
13-
from .utils.social.github import get_github_access_token_from_code
13+
from .utils.social.oauth import get_access_token_from_code
1414

1515
User = get_user_model()
1616

@@ -26,7 +26,7 @@ def get_tokens_for_user(user):
2626

2727
class SocialSerializer(serializers.Serializer):
2828
"""
29-
Serializer which accepts an OAuth2 access token.
29+
Serializer which accepts an OAuth2 code.
3030
"""
3131
code = serializers.CharField(
3232
allow_blank=False,
@@ -63,7 +63,7 @@ def exchange_token(request, backend):
6363
if serializer.is_valid(raise_exception=True):
6464

6565
code = serializer.validated_data['code']
66-
access_token = get_github_access_token_from_code(code)
66+
access_token = get_access_token_from_code(backend, code)
6767
# set up non-field errors key
6868
# http://www.django-rest-framework.org/api-guide/exceptions/
6969
# #exception-handling-in-rest-framework-views

backend/core/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# https://developers.google.com/identity/protocols/OpenIDConnect
2+
3+
OAUTH = {
4+
'github': {
5+
'url':'https://github.com/login/oauth/access_token'
6+
}
7+
}

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ services:
6161
networks:
6262
- main
6363
ports:
64-
- "8089:8080"
64+
- "8080:8080"
6565
volumes:
6666
- ./quasar:/app/:rw
6767
depends_on:
@@ -99,6 +99,8 @@ services:
9999
- DJANGO_SETTINGS_MODULE=backend.settings.development
100100
- GITHUB_KEY=${GITHUB_KEY}
101101
- GITHUB_SECRET=${GITHUB_SECRET}
102+
- GOOGLE_KEY=${GOOGLE_KEY}
103+
- GOOGLE_SECRET=${GOOGLE_SECRET}
102104
depends_on:
103105
- db
104106

quasar/quasar.conf.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module.exports = function(ctx) {
6464
WS_PING_PONG: `"ws://nginx/ws/ping-pong/"`
6565
},
6666
scopeHoisting: true,
67+
useNotifier: false,
6768
vueRouterMode: "history",
6869
// vueCompiler: true,
6970
// gzip: true,
@@ -83,8 +84,13 @@ module.exports = function(ctx) {
8384
},
8485

8586
devServer: {
87+
headers: {
88+
"Access-Control-Allow-Origin": "*",
89+
"Access-Control-Allow-Headers":
90+
"Origin, X-Requested-With, Content-Type, Accept"
91+
},
8692
// https: true,
87-
// port: 8080,
93+
port: 8080,
8894
open: true // opens browser window automatically
8995
},
9096

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div>
3-
<q-dialog @hide="hideLoginMenu" v-model="visible">
3+
<q-dialog @hide="hideAuthMenu" v-model="visible">
44
<q-card style=" max-width: 95%; min-width: 320px;">
55
<q-tabs
66
v-model="tab"
@@ -76,7 +76,6 @@ export default {
7676
scope: "user",
7777
state: "eworifjeovivoiej"
7878
},
79-
githuboath2link: "",
8079
githuboauth2: "https://github.com/login/oauth/authorize",
8180
email: process.env.NODE_ENV === "production" ? "" : "admin@company.com",
8281
password: process.env.NODE_ENV === "production" ? "" : "password"
@@ -88,13 +87,13 @@ export default {
8887
computed: {
8988
visible: {
9089
get() {
91-
return this.$store.getters.loginModalVisible;
90+
return this.$store.getters.authModalVisible;
9291
},
9392
set() {}
9493
}
9594
},
9695
methods: {
97-
hideLoginMenu() {
96+
hideAuthMenu() {
9897
this.$store.commit("toggleLoginMenu");
9998
},
10099
login() {

quasar/src/layouts/primary/MainHeader.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@
3333
no-caps
3434
@click="logout"
3535
/>
36-
<login-modal />
36+
<auth-modal />
3737
</q-toolbar>
3838
</q-header>
3939
</template>
4040

4141
<script>
42-
import LoginModal from "components/LoginModal.vue";
42+
import AuthModal from "components/AuthModal.vue";
4343
4444
export default {
45-
components: { LoginModal },
45+
components: { AuthModal },
4646
methods: {
4747
logout() {
4848
this.$store.dispatch("AUTH_LOGOUT").then(() => this.$router.push("/"));

quasar/src/pages/Auth/Callback.vue

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<template>
2+
<base-page>Logging in with GitHub...</base-page>
3+
</template>
4+
5+
<script>
6+
import * as Cookies from "js-cookie";
7+
import axios from "axios";
8+
export default {
9+
methods: {
10+
githubAuth() {
11+
const provider = this.$route.params.provider;
12+
axios
13+
.post(`/api/social/${provider}/`, { code: this.$route.query.code })
14+
.then(resp => {
15+
Cookies.set("refresh-token", resp.data.refresh);
16+
Cookies.set("user-token", resp.data.access);
17+
window.location.href = "/";
18+
});
19+
}
20+
},
21+
created() {
22+
this.githubAuth();
23+
}
24+
};
25+
</script>
26+
27+
<style lang="scss" scoped></style>

quasar/src/pages/Auth/Google.vue

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<template>
2+
<base-page>
3+
Loging in with Google...
4+
</base-page>
5+
</template>
6+
7+
<script>
8+
import * as Cookies from "js-cookie";
9+
import axios from "axios";
10+
export default {
11+
methods: {
12+
googleAuth() {
13+
axios
14+
.post("/api/social/google/", {
15+
code: this.$route.query.code
16+
})
17+
.then(resp => {
18+
Cookies.set("refresh-token", resp.data.refresh);
19+
Cookies.set("user-token", resp.data.access);
20+
window.location.href = "/";
21+
});
22+
}
23+
},
24+
created() {
25+
this.googleAuth();
26+
}
27+
};
28+
</script>
29+
30+
<style lang="scss" scoped></style>

0 commit comments

Comments
 (0)