Skip to content

Commit 77a1c60

Browse files
authored
Merge pull request #359 from HackSoftware/google-oauth
Blog example for Google OAuth2 with Django
2 parents 11796f9 + 65072a1 commit 77a1c60

File tree

20 files changed

+668
-5
lines changed

20 files changed

+668
-5
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ AWS_S3_SECRET_ACCESS_KEY=""
99
AWS_STORAGE_BUCKET_NAME=""
1010
AWS_S3_REGION_NAME=""
1111
AWS_S3_CUSTOM_DOMAIN=""
12+
13+
DJANGO_GOOGLE_OAUTH2_CLIENT_ID=""
14+
DJANGO_GOOGLE_OAUTH2_CLIENT_SECRET=""
15+
DJANGO_GOOGLE_OAUTH2_PROJECT_ID=""

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,5 @@ dmypy.json
134134

135135
node_modules/
136136
package*.json
137+
138+
google_client_secret.json

config/django/base.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
ALLOWED_HOSTS = ["*"]
2929

30-
3130
# Application definition
3231

3332
LOCAL_APPS = [
@@ -99,7 +98,6 @@
9998

10099
WSGI_APPLICATION = "config.wsgi.application"
101100

102-
103101
# Database
104102
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
105103

@@ -120,7 +118,6 @@
120118
}
121119
}
122120

123-
124121
# Password validation
125122
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
126123

@@ -141,7 +138,6 @@
141138

142139
AUTH_USER_MODEL = "users.BaseUser"
143140

144-
145141
# Internationalization
146142
# https://docs.djangoproject.com/en/3.0/topics/i18n/
147143

@@ -155,7 +151,6 @@
155151

156152
USE_TZ = True
157153

158-
159154
# Static files (CSS, JavaScript, Images)
160155
# https://docs.djangoproject.com/en/3.0/howto/static-files/
161156

@@ -178,6 +173,7 @@
178173
from config.settings.cors import * # noqa
179174
from config.settings.email_sending import * # noqa
180175
from config.settings.files_and_storages import * # noqa
176+
from config.settings.google_oauth2 import * # noqa
181177
from config.settings.jwt import * # noqa
182178
from config.settings.sentry import * # noqa
183179
from config.settings.sessions import * # noqa

config/settings/cors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1+
from config.env import env
2+
13
CORS_ALLOW_CREDENTIALS = True
24
CORS_ALLOW_ALL_ORIGINS = True
5+
6+
BASE_BACKEND_URL = env.str("DJANGO_BASE_BACKEND_URL", default="http://localhost:8000")
7+
BASE_FRONTEND_URL = env.str("DJANGO_BASE_FRONTEND_URL", default="http://localhost:3000")
8+
CORS_ORIGIN_WHITELIST = env.list("DJANGO_CORS_ORIGIN_WHITELIST", default=[BASE_FRONTEND_URL])

config/settings/google_oauth2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from config.env import env
2+
3+
GOOGLE_OAUTH2_CLIENT_ID = env.str("DJANGO_GOOGLE_OAUTH2_CLIENT_ID", default="")
4+
GOOGLE_OAUTH2_CLIENT_SECRET = env.str("DJANGO_GOOGLE_OAUTH2_CLIENT_SECRET", default="")
5+
GOOGLE_OAUTH2_PROJECT_ID = env.str("DJANGO_GOOGLE_OAUTH2_PROJECT_ID", default="")

mypy.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ ignore_missing_imports = True
3333
[mypy-rest_framework_jwt.*]
3434
# Remove this when rest_framework_jwt stubs are present
3535
ignore_missing_imports = True
36+
37+
[mypy-google_auth_oauthlib.*]
38+
# Remove this when google_auth_oauthlib stubs are present
39+
ignore_missing_imports = True
40+
41+
[mypy-oauthlib.*]
42+
# Remove this when oauthlib stubs are present
43+
ignore_missing_imports = True

requirements/base.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ attrs==22.1.0
2121

2222
gunicorn==20.1.0
2323
sentry-sdk==1.14.0
24+
25+
requests==2.30.0
26+
27+
google-api-python-client==2.86.0
28+
google-auth==2.18.0
29+
google-auth-httplib2==0.1.0
30+
google-auth-oauthlib==1.0.0

styleguide_example/api/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
path("users/", include(("styleguide_example.users.urls", "users"))),
66
path("errors/", include(("styleguide_example.errors.urls", "errors"))),
77
path("files/", include(("styleguide_example.files.urls", "files"))),
8+
path(
9+
"google-oauth2/", include(("styleguide_example.blog_examples.google_login_server_flow.urls", "google-oauth2"))
10+
),
811
]

styleguide_example/blog_examples/google_login_server_flow/__init__.py

Whitespace-only changes.

styleguide_example/blog_examples/google_login_server_flow/raw/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)