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 AuthAction, ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class Scope(object):
ACCESS = "read-only"
class YNABAccount(ProviderAccount):
pass
class YNABProvider(OAuth2Provider):
id = "ynab"
name = "YNAB"
account_class = YNABAccount
def get_default_scope(self):
scope = [Scope.ACCESS]
return scope
def get_auth_params(self, request, action):
ret = super(YNABProvider, self).get_auth_params(request, action)
if action == AuthAction.REAUTHENTICATE:
ret["prompt"] = "select_account consent"
return ret
def extract_uid(self, data):
return str(data["data"]["user"]["id"])
provider_classes = [YNABProvider]

View File

@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from requests.exceptions import HTTPError
from django.test.client import RequestFactory
from django.test.utils import override_settings
from django.urls import reverse
from allauth.socialaccount.models import SocialToken
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase, patch
from .provider import YNABProvider
@override_settings(
SOCIALACCOUNT_AUTO_SIGNUP=True,
ACCOUNT_SIGNUP_FORM_CLASS=None,
)
# ACCOUNT_EMAIL_VERIFICATION=account_settings
# .EmailVerificationMethod.MANDATORY)
class YNABTests(OAuth2TestsMixin, TestCase):
provider_id = YNABProvider.id
def get_mocked_response(self):
return MockedResponse(
200,
"""
{"data": {
"user":{
"id": "abcd1234xyz5678"
}
}
}
""",
)
def test_ynab_compelete_login_401(self):
from allauth.socialaccount.providers.ynab.views import (
YNABOAuth2Adapter,
)
class LessMockedResponse(MockedResponse):
def raise_for_status(self):
if self.status_code != 200:
raise HTTPError(None)
request = RequestFactory().get(
reverse(self.provider.id + "_login"), dict(process="login")
)
adapter = YNABOAuth2Adapter(request)
app = adapter.get_provider().app
token = SocialToken(token="some_token")
response_with_401 = LessMockedResponse(
401,
"""
{"error": {
"errors": [{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization" } ],
"code": 401,
"message": "Invalid Credentials" }
}""",
)
with patch(
"allauth.socialaccount.providers.ynab.views.requests"
) as patched_requests:
patched_requests.get.return_value = response_with_401
with self.assertRaises(HTTPError):
adapter.complete_login(request, app, token)

View File

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

View File

@@ -0,0 +1,30 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import YNABProvider
class YNABOAuth2Adapter(OAuth2Adapter):
provider_id = YNABProvider.id
access_token_url = "https://app.youneedabudget.com/oauth/token"
authorize_url = "https://app.youneedabudget.com/oauth/authorize"
profile_url = "https://api.youneedabudget.com/v1/user"
def complete_login(self, request, app, token, **kwargs):
resp = requests.get(
self.profile_url,
headers={"Authorization": "Bearer {}".format(token.token)},
)
resp.raise_for_status()
extra_data = resp.json()
login = self.get_provider().sociallogin_from_response(request, extra_data)
return login
oauth2_login = OAuth2LoginView.adapter_view(YNABOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(YNABOAuth2Adapter)