From cc6b6ce702103572d4bd3436fc5083b7695aa670 Mon Sep 17 00:00:00 2001 From: mmd-osm Date: Sat, 27 Jan 2024 13:18:16 +0100 Subject: [PATCH 1/2] [feat] Add OAuth2 support for OpenStreetMap Fixes #758 --- social_core/backends/openstreetmap_oauth2.py | 56 +++++++++++++++++++ .../backends/test_openstreetmap_oauth2.py | 30 ++++++++++ 2 files changed, 86 insertions(+) create mode 100644 social_core/backends/openstreetmap_oauth2.py create mode 100644 social_core/tests/backends/test_openstreetmap_oauth2.py diff --git a/social_core/backends/openstreetmap_oauth2.py b/social_core/backends/openstreetmap_oauth2.py new file mode 100644 index 000000000..06c4660f5 --- /dev/null +++ b/social_core/backends/openstreetmap_oauth2.py @@ -0,0 +1,56 @@ +""" +OpenStreetMap OAuth 2.0 support. + +This adds support for OpenStreetMap OAuth service. An application must be +registered first on OpenStreetMap and the settings +SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_KEY and SOCIAL_AUTH_OPENSTREETMAP_OAUTH2_SECRET +must be defined with the corresponding values. + +More info: https://wiki.openstreetmap.org/wiki/OAuth +""" + +from .oauth import BaseOAuth2PKCE + +class OpenStreetMapOAuth2(BaseOAuth2PKCE): + """OpenStreetMap OAuth2 authentication backend""" + + name = "openstreetmap-oauth2" + AUTHORIZATION_URL = "https://www.openstreetmap.org/oauth2/authorize" + ACCESS_TOKEN_URL = "https://www.openstreetmap.org/oauth2/token" + ACCESS_TOKEN_METHOD = "POST" + SCOPE_SEPARATOR = " " + STATE_PARAMETER = True + DEFAULT_SCOPE = ["read_prefs"] + EXTRA_DATA = [ + ("id", "id"), + ("avatar", "avatar"), + ("account_created", "account_created"), + ] + PKCE_DEFAULT_CODE_CHALLENGE_METHOD = "S256" + DEFAULT_USE_PKCE = True + + def get_user_details(self, response): + """Return user details from OpenStreetMap account""" + return { + "username": response["username"], + "email": "", + "fullname": "", + "first_name": "", + "last_name": "", + } + + def user_data(self, access_token, *args, **kwargs): + """Return user data provided""" + + headers = {"Authorization": f"Bearer {access_token}"} + response = self.get_json( + url="https://api.openstreetmap.org/api/0.6/user/details.json", + headers=headers + ) + + return { + "id": response["user"]["id"], + "username": response["user"]["display_name"], + "account_created": response["user"]["account_created"], + "avatar": response["user"].get("img", {}).get("href") + } diff --git a/social_core/tests/backends/test_openstreetmap_oauth2.py b/social_core/tests/backends/test_openstreetmap_oauth2.py new file mode 100644 index 000000000..9c1a6b01f --- /dev/null +++ b/social_core/tests/backends/test_openstreetmap_oauth2.py @@ -0,0 +1,30 @@ +import json + +from .oauth import OAuth2Test + + +class OpenStreetMapOAuth2Test(OAuth2Test): + backend_path = "social_core.backends.openstreetmap_oauth2.OpenStreetMapOAuth2" + user_data_url = "https://api.openstreetmap.org/api/0.6/user/details.json" + expected_username = "Steve" + access_token_body = json.dumps({"access_token": "foobar", "token_type": "bearer"}) + user_data_body = json.dumps( + { + "version": "0.6", + "generator": "OpenStreetMap server", + "copyright": "OpenStreetMap and contributors", + "attribution": "http://www.openstreetmap.org/copyright", + "license": "http://opendatacommons.org/licenses/odbl/1-0/", + "user": { + "id": 1, + "display_name": "Steve", + "account_created": "2005-09-13T15:32:57Z" + } + } + ) + + def test_login(self): + self.do_login() + + def test_partial_pipeline(self): + self.do_partial_pipeline() From 85da2dfed4645b2d0f78d87c500705da4a4ddfba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 28 Jan 2024 19:59:56 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- social_core/backends/openstreetmap_oauth2.py | 5 +++-- social_core/tests/backends/test_openstreetmap_oauth2.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/social_core/backends/openstreetmap_oauth2.py b/social_core/backends/openstreetmap_oauth2.py index 06c4660f5..6df409273 100644 --- a/social_core/backends/openstreetmap_oauth2.py +++ b/social_core/backends/openstreetmap_oauth2.py @@ -11,6 +11,7 @@ from .oauth import BaseOAuth2PKCE + class OpenStreetMapOAuth2(BaseOAuth2PKCE): """OpenStreetMap OAuth2 authentication backend""" @@ -45,12 +46,12 @@ def user_data(self, access_token, *args, **kwargs): headers = {"Authorization": f"Bearer {access_token}"} response = self.get_json( url="https://api.openstreetmap.org/api/0.6/user/details.json", - headers=headers + headers=headers, ) return { "id": response["user"]["id"], "username": response["user"]["display_name"], "account_created": response["user"]["account_created"], - "avatar": response["user"].get("img", {}).get("href") + "avatar": response["user"].get("img", {}).get("href"), } diff --git a/social_core/tests/backends/test_openstreetmap_oauth2.py b/social_core/tests/backends/test_openstreetmap_oauth2.py index 9c1a6b01f..7ff69ad6c 100644 --- a/social_core/tests/backends/test_openstreetmap_oauth2.py +++ b/social_core/tests/backends/test_openstreetmap_oauth2.py @@ -18,8 +18,8 @@ class OpenStreetMapOAuth2Test(OAuth2Test): "user": { "id": 1, "display_name": "Steve", - "account_created": "2005-09-13T15:32:57Z" - } + "account_created": "2005-09-13T15:32:57Z", + }, } )