| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
|
| 5 |
from allauth.socialaccount.providers.oauth.client import OAuth |
| 6 |
from allauth.socialaccount.providers.oauth.views import ( |
| 7 |
OAuthAdapter, |
| 8 |
OAuthCallbackView, |
| 9 |
OAuthLoginView, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
class OpenStreetMapAPI(OAuth): |
| 14 |
url = "https://api.openstreetmap.org/api/0.6/user/details.json" |
| 15 |
|
| 16 |
def get_user_info(self): |
| 17 |
data = self.query(self.url).json() |
| 18 |
return data["user"] |
| 19 |
|
| 20 |
|
| 21 |
class OpenStreetMapOAuthAdapter(OAuthAdapter): |
| 22 |
provider_id = "openstreetmap" |
| 23 |
request_token_url = "https://www.openstreetmap.org/oauth/request_token" # nosec |
| 24 |
access_token_url = "https://www.openstreetmap.org/oauth/access_token" # nosec |
| 25 |
authorize_url = "https://www.openstreetmap.org/oauth/authorize" |
| 26 |
|
| 27 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 28 |
client = OpenStreetMapAPI( |
| 29 |
request, app.client_id, app.secret, self.request_token_url |
| 30 |
) |
| 31 |
extra_data = client.get_user_info() |
| 32 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 33 |
|
| 34 |
|
| 35 |
oauth_login = OAuthLoginView.adapter_view(OpenStreetMapOAuthAdapter) |
| 36 |
oauth_callback = OAuthCallbackView.adapter_view(OpenStreetMapOAuthAdapter) |