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,32 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class BitlyAccount(ProviderAccount):
def get_profile_url(self):
return self.account.extra_data.get("profile_url")
def get_avatar_url(self):
return self.account.extra_data.get("profile_image")
def to_str(self):
dflt = super(BitlyAccount, self).to_str()
return "%s (%s)" % (
self.account.extra_data.get("full_name", ""),
dflt,
)
class BitlyProvider(OAuth2Provider):
id = "bitly"
name = "Bitly"
account_class = BitlyAccount
def extract_uid(self, data):
return str(data["login"])
def extract_common_fields(self, data):
return dict(username=data["login"], name=data.get("full_name"))
provider_classes = [BitlyProvider]

View File

@@ -0,0 +1,30 @@
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import BitlyProvider
class BitlyTests(OAuth2TestsMixin, TestCase):
provider_id = BitlyProvider.id
def get_mocked_response(self):
return MockedResponse(
200,
"""{
"data": {
"apiKey": "R_f6397a37e765574f2e198dba5bb59522",
"custom_short_domain": null,
"display_name": null,
"full_name": "Bitly API Oauth Demo Account",
"is_enterprise": false,
"login": "bitlyapioauthdemo",
"member_since": 1331567982,
"profile_image": "http://bitly.com/u/bitlyapioauthdemo.png",
"profile_url": "http://bitly.com/u/bitlyapioauthdemo",
"share_accounts": [],
"tracking_domains": []
},
"status_code": 200,
"status_txt": "OK"
}""",
)

View File

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

View File

@@ -0,0 +1,26 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import BitlyProvider
class BitlyOAuth2Adapter(OAuth2Adapter):
provider_id = BitlyProvider.id
access_token_url = "https://api-ssl.bitly.com/oauth/access_token"
authorize_url = "https://bitly.com/oauth/authorize"
profile_url = "https://api-ssl.bitly.com/v3/user/info"
supports_state = False
def complete_login(self, request, app, token, **kwargs):
resp = requests.get(self.profile_url, params={"access_token": token.token})
extra_data = resp.json()["data"]
return self.get_provider().sociallogin_from_response(request, extra_data)
oauth2_login = OAuth2LoginView.adapter_view(BitlyOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(BitlyOAuth2Adapter)