Skip to content

Commit 7f700e6

Browse files
Upgraded oauth login from 1 to 2
1 parent f6212e9 commit 7f700e6

File tree

6 files changed

+54
-46
lines changed

6 files changed

+54
-46
lines changed

core/settings/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@
123123
)
124124

125125
AUTHENTICATION_BACKENDS = (
126-
"social_core.backends.openstreetmap.OpenStreetMapOAuth",
126+
# "social_core.backends.openstreetmap.OpenStreetMapOAuth",
127+
"social_core.backends.openstreetmap_oauth2.OpenStreetMapOAuth2",
127128
"oauth2_provider.backends.OAuth2Backend",
128129
"social_core.backends.email.EmailAuth",
129130
"social_core.backends.username.UsernameAuth",

core/settings/contrib.py

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,63 @@
88
# Extra installed apps
99
INSTALLED_APPS += (
1010
# any 3rd party apps
11-
'rest_framework',
12-
'rest_framework_gis',
13-
'rest_framework.authtoken',
14-
'social_django',
11+
"rest_framework",
12+
"rest_framework_gis",
13+
"rest_framework.authtoken",
14+
"social_django",
1515
)
1616

1717
# 3rd party specific app settings
1818
OAUTH2_PROVIDER = {
19-
'ACCESS_TOKEN_EXPIRE_SECONDS': 10 * 365 * 24 * 60 * 60,
19+
"ACCESS_TOKEN_EXPIRE_SECONDS": 10 * 365 * 24 * 60 * 60,
2020
}
2121

2222
REST_FRAMEWORK = {
23-
'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.SearchFilter',
24-
'rest_framework.filters.OrderingFilter'),
25-
'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework.authentication.TokenAuthentication',
26-
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
27-
'rest_framework.authentication.SessionAuthentication',),
28-
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
29-
'DEFAULT_RENDERER_CLASSES': (
30-
'rest_framework.renderers.JSONRenderer',
31-
'api.renderers.HOTExportApiRenderer',
23+
"DEFAULT_FILTER_BACKENDS": (
24+
"rest_framework.filters.SearchFilter",
25+
"rest_framework.filters.OrderingFilter",
3226
),
33-
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning',
34-
'DEFAULT_VERSION': '1.0',
35-
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
36-
'PAGE_SIZE': 20
27+
"DEFAULT_AUTHENTICATION_CLASSES": (
28+
"rest_framework.authentication.TokenAuthentication",
29+
"oauth2_provider.contrib.rest_framework.OAuth2Authentication",
30+
"rest_framework.authentication.SessionAuthentication",
31+
),
32+
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
33+
"DEFAULT_RENDERER_CLASSES": (
34+
"rest_framework.renderers.JSONRenderer",
35+
"api.renderers.HOTExportApiRenderer",
36+
),
37+
"DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.AcceptHeaderVersioning",
38+
"DEFAULT_VERSION": "1.0",
39+
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
40+
"PAGE_SIZE": 20,
3741
}
3842

43+
3944
# OAuth login settings
40-
SOCIAL_AUTH_OPENSTREETMAP_LOGIN_URL = '/osm/login/'
41-
SOCIAL_AUTH_OPENSTREETMAP_KEY = os.getenv('OSM_API_KEY')
42-
SOCIAL_AUTH_OPENSTREETMAP_SECRET = os.getenv('OSM_API_SECRET')
43-
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
44-
SOCIAL_AUTH_LOGIN_ERROR_URL = '/osm/error'
45-
SOCIAL_AUTH_URL_NAMESPACE = 'osm'
46-
SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ['username', 'first_name', 'email']
45+
SOCIAL_AUTH_OPENSTREETMAP_LOGIN_URL = "/osm/login/"
46+
SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY = os.getenv("OSM_API_KEY")
47+
SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET = os.getenv("OSM_API_SECRET")
48+
SOCIAL_AUTH_LOGIN_REDIRECT_URL = "/"
49+
SOCIAL_AUTH_LOGIN_ERROR_URL = "/osm/error"
50+
SOCIAL_AUTH_URL_NAMESPACE = "osm"
51+
SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ["username", "first_name", "email"]
4752
SOCIAL_AUTH_FORCE_EMAIL_VALIDATION = True
48-
SOCIAL_AUTH_EMAIL_VALIDATION_FUNCTION = 'ui.pipeline.email_validation'
49-
SOCIAL_AUTH_EMAIL_VALIDATION_URL = '/osm/email_verify_sent/'
53+
SOCIAL_AUTH_EMAIL_VALIDATION_FUNCTION = "ui.pipeline.email_validation"
54+
SOCIAL_AUTH_EMAIL_VALIDATION_URL = "/osm/email_verify_sent/"
5055

5156
SOCIAL_AUTH_PIPELINE = (
52-
'social_core.pipeline.social_auth.social_details',
53-
'social_core.pipeline.social_auth.social_uid',
54-
'social_core.pipeline.social_auth.auth_allowed',
55-
'social_core.pipeline.social_auth.social_user',
56-
'social_core.pipeline.user.get_username',
57-
'ui.pipeline.require_email',
58-
'social_core.pipeline.mail.mail_validation',
59-
'social_core.pipeline.social_auth.associate_by_email',
60-
'social_core.pipeline.user.create_user',
61-
'social_core.pipeline.social_auth.associate_user',
62-
'social_core.pipeline.debug.debug',
63-
'social_core.pipeline.social_auth.load_extra_data',
64-
'social_core.pipeline.user.user_details'
57+
"social_core.pipeline.social_auth.social_details",
58+
"social_core.pipeline.social_auth.social_uid",
59+
"social_core.pipeline.social_auth.auth_allowed",
60+
"social_core.pipeline.social_auth.social_user",
61+
"social_core.pipeline.user.get_username",
62+
"ui.pipeline.require_email",
63+
"social_core.pipeline.mail.mail_validation",
64+
"social_core.pipeline.social_auth.associate_by_email",
65+
"social_core.pipeline.user.create_user",
66+
"social_core.pipeline.social_auth.associate_user",
67+
"social_core.pipeline.debug.debug",
68+
"social_core.pipeline.social_auth.load_extra_data",
69+
"social_core.pipeline.user.user_details",
6570
)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mercantile~=0.10.0
1111
psycopg2
1212
python3-openid==3.2.0
1313
social-auth-app-django==5.4.0
14-
social-auth-core==4.4.2
14+
social-auth-core==4.4.2 ### Upgrade this to include oauth2
1515
pytz
1616
pyyaml>=5.3
1717
raven

ui/app/actions/meta.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ if (window.OAUTH_CLIENT_ID == null) {
2727
}
2828

2929
const oauthConfig = {
30+
// url: window.EXPORTS_API_URL + "/o/openstreetmap_oauth2",
3031
url: window.EXPORTS_API_URL + "/o/authorize?approval_prompt=auto",
3132
client: window.OAUTH_CLIENT_ID,
3233
redirect: `${window.location.protocol}//${hostname}/authorized`

ui/templates/osm/email.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{% blocktrans %}Please provide a valid email address. This email address will be used to notifiy you when your exports are ready.
1515
A verification link will be sent to the email address you provide.{% endblocktrans %}
1616
<br/><br/>
17-
<form method="post" action="{% url 'osm:complete' 'openstreetmap' %}" id="registerEmail">
17+
<form method="post" action="{% url 'osm:complete' 'openstreetmap-oauth2' %}" id="registerEmail">
1818
{% csrf_token %}
1919
<div class="form-group row" id="form-group-email">
2020
<div class="col-md-12">

ui/views.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def login(request):
3030
if not request.user.is_authenticated:
3131
# preserve redirects ("next" in request.GET)
3232
return redirect(
33-
reverse("osm:begin", args=["openstreetmap"]) + "?" + request.GET.urlencode()
33+
reverse("osm:begin", args=["openstreetmap-oauth2"])
34+
+ "?"
35+
+ request.GET.urlencode()
3436
)
3537
else:
3638
return redirect("/v3/")
@@ -48,7 +50,7 @@ def v3(request, *args, **kwargs):
4850
except Application.DoesNotExist:
4951
ui_app = Application.objects.create(
5052
name="OSM Export Tool UI",
51-
redirect_uris="http://localhost/authorized http://localhost:8080/authorized http://localhost:8000/authorized",
53+
redirect_uris="http://localhost/authorized http://127.0.0.1:8000/authorized http://localhost:8080/authorized http://localhost:8000/authorized",
5254
client_type=Application.CLIENT_PUBLIC,
5355
authorization_grant_type=Application.GRANT_IMPLICIT,
5456
skip_authorization=True,
@@ -70,7 +72,6 @@ def redirect_to_v3(request):
7072
def worker_dashboard(request):
7173
if not request.user.is_superuser:
7274
return HttpResponseForbidden()
73-
# return HttpResponse('test')
7475
return HttpResponseRedirect(f"/{settings.WORKER_SECRET_KEY}/")
7576

7677

0 commit comments

Comments
 (0)