Skip to content

Commit c1b9154

Browse files
Merge pull request #525 from hotosm/feature/oauth-2
Upgrade : Oauth2 Login for OSM
2 parents 6468955 + f0aba75 commit c1b9154

File tree

9 files changed

+59
-51
lines changed

9 files changed

+59
-51
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
)

ops/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Examples of how to backup and restore the database:
6262
psql exports < export_tool_2023-03-06.pgdump
6363
```
6464

65-
8. Modify the OAuth1 application with your hostname's `redirect_uris`
65+
8. Modify the OAuth2 application with your hostname's `redirect_uris`
6666

6767
### Storage and Environment Variables
6868

ops/systemd/export_workers.env

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ EMAIL_HOST_USER=<INSECURE_SMTP_USERNAME>
88
EMAIL_HOST_PASSWORD=<SECURE_SMTP_PASSWORD>
99
REPLY_TO_EMAIL=<INSECURE_REPLY_EMAIL>
1010

11-
##OAUTH 1 Settings
12-
OSM_API_KEY=<SECURE_OSM_OAUTH1_APP_CONSUMER_KEY>
13-
OSM_API_SECRET=<SECURE_OSM_OAUTH1_APP_CONSUMER_SECRET>
11+
##OAUTH 2 Settings
12+
OSM_API_KEY=<SECURE_OSM_OAUTH2_APP_CONSUMER_KEY>
13+
OSM_API_SECRET=<SECURE_OSM_OAUTH2_APP_CONSUMER_SECRET>
1414

1515
## Workers
1616
WORKER_SECRET_KEY=<SECURE_WORKER_KEY>

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/app/components/help/API.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default () =>
3333
<p>User authentication and authorization is a two-step process.</p>
3434
<p>
3535
The Export Tool requires that users log into OpenStreetMap using{" "}
36-
<a href="https://oauth.net/1/">OAuth 1.0a</a> (you don't need to know
36+
<a href="https://oauth.net/2/">OAuth 2.0a</a> (you don't need to know
3737
this). This provides user identity, specifically an OSM username to
3838
associate with exports. You generally don't need to care about this,
3939
except to know that usernames are the same as on OSM.

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)