This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
from allauth.account.models import EmailAddress
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class NaverAccount(ProviderAccount):
def get_avatar_url(self):
return self.account.extra_data.get("profile_image")
def to_str(self):
return self.account.extra_data.get("nickname", self.account.uid)
class NaverProvider(OAuth2Provider):
id = "naver"
name = "Naver"
account_class = NaverAccount
def extract_uid(self, data):
return str(data["id"])
def extract_common_fields(self, data):
email = data.get("email")
return dict(email=email)
def extract_email_addresses(self, data):
ret = []
email = data.get("email")
if email:
ret.append(EmailAddress(email=email, verified=True, primary=True))
return ret
provider_classes = [NaverProvider]

View File

@@ -0,0 +1,32 @@
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import NaverProvider
class NaverTests(OAuth2TestsMixin, TestCase):
provider_id = NaverProvider.id
def get_mocked_response(self):
return MockedResponse(
200,
"""
{
"response":
{
"enc_id": "46111c25f969116de4e545f13a415bb5383db2a79782da8851db72b2cced3180",
"nickname": "\ubc31\ud638",
"profile_image":
"https://ssl.pstatic.net/static/pwe/address/nodata_33x33.gif",
"gender": "M",
"id": "7163491",
"age": "20-29",
"birthday": "03-22",
"email": "shlee940322@example.com",
"name": "\uc774\uc0c1\ud601"
},
"message": "success",
"resultcode": "00"
}
""",
)

View File

@@ -0,0 +1,6 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from .provider import NaverProvider
urlpatterns = default_urlpatterns(NaverProvider)

View File

@@ -0,0 +1,27 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import NaverProvider
class NaverOAuth2Adapter(OAuth2Adapter):
provider_id = NaverProvider.id
access_token_url = "https://nid.naver.com/oauth2.0/token"
authorize_url = "https://nid.naver.com/oauth2.0/authorize"
profile_url = "https://openapi.naver.com/v1/nid/me"
def complete_login(self, request, app, token, **kwargs):
headers = {"Authorization": "Bearer {0}".format(token.token)}
resp = requests.get(self.profile_url, headers=headers)
resp.raise_for_status()
extra_data = resp.json().get("response")
return self.get_provider().sociallogin_from_response(request, extra_data)
oauth2_login = OAuth2LoginView.adapter_view(NaverOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(NaverOAuth2Adapter)