Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add OAuth2 support for OpenStreetMap #877

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions social_core/backends/openstreetmap_oauth2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
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"),
}
30 changes: 30 additions & 0 deletions social_core/tests/backends/test_openstreetmap_oauth2.py
Original file line number Diff line number Diff line change
@@ -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()