forked from python-social-auth/social-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add OAuth2 support for OpenStreetMap
Fixes python-social-auth#758
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
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 | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |