Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #94: Support for authlib 1.0.0 #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion django_example/website/logins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.http import HttpResponse, JsonResponse
from django.urls import include, path
from authlib.django.client import OAuth
from authlib.integrations.django_client import OAuth
from loginpass import create_django_urlpatterns
from loginpass import Twitter, GitHub, Google

Expand Down
24 changes: 24 additions & 0 deletions loginpass/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,27 @@ def map_profile_fields(data, fields):
profile[dst] = value

return profile


def oauth_register_remote_app(oauth, backend, **kwargs):
"""Registers & returns an instance of a remote application for
the given ``backend`` service.

:param oauth: Authlib OAuth instance
:param backend: Backend class to register, e.g GitHub, Twitter etc
:param kwargs: Optional, additional, parameters for Authlib OAuth.register()
:return: Authlib OAuth remote app
"""
client_cls = oauth.oauth2_client_cls
if backend.OAUTH_CONFIG.get('request_token_url'):
client_cls = oauth.oauth1_client_cls

class RemoteApp(backend, client_cls):
OAUTH_APP_CONFIG = backend.OAUTH_CONFIG

return oauth.register(
RemoteApp.NAME,
overwrite=True,
client_cls=RemoteApp,
**kwargs
)
12 changes: 4 additions & 8 deletions loginpass/_django.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
from ._core import oauth_register_remote_app

def create_django_urlpatterns(backend, oauth, handle_authorize):
from authlib.integrations.django_client import DjangoRemoteApp
from django.urls import path

class RemoteApp(backend, DjangoRemoteApp):
OAUTH_APP_CONFIG = backend.OAUTH_CONFIG

token_name = '_loginpass_{}_token'.format(backend.NAME)
auth_route_name = 'loginpass_{}_auth'.format(backend.NAME)
login_route_name = 'loginpass_{}_login'.format(backend.NAME)

remote = oauth.register(
backend.NAME,
overwrite=True,
remote = oauth_register_remote_app(
oauth,
backend,
fetch_token=lambda request: getattr(request, token_name, None),
client_cls=RemoteApp,
)

auth = create_auth_endpoint(remote, handle_authorize)
Expand Down
14 changes: 3 additions & 11 deletions loginpass/_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ._core import oauth_register_remote_app

def create_fastapi_routes(backends, oauth, handle_authorize):
"""Create a Fastapi routes that you can register it directly to fastapi
Expand Down Expand Up @@ -38,7 +39,7 @@ async def handle_authorize(remote, token, user_info, request):
router = APIRouter()

for b in backends:
register_to(oauth, b)
oauth_register_remote_app(oauth, b)

@router.get("/auth/{backend}")
async def auth(
Expand Down Expand Up @@ -82,13 +83,4 @@ async def login(backend: str, request: Request):
params = oauth.config.get(conf_key, default={})
return await remote.authorize_redirect(request, redirect_uri, **params)

return router


def register_to(oauth, backend_cls):
from authlib.integrations.starlette_client import StarletteRemoteApp

class RemoteApp(backend_cls, StarletteRemoteApp):
OAUTH_APP_CONFIG = backend_cls.OAUTH_CONFIG

oauth.register(RemoteApp.NAME, overwrite=True, client_cls=RemoteApp)
return router
14 changes: 3 additions & 11 deletions loginpass/_flask.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ._core import oauth_register_remote_app

def create_flask_blueprint(backends, oauth, handle_authorize):
"""Create a Flask blueprint that you can register it directly to Flask
Expand Down Expand Up @@ -35,7 +36,7 @@ def handle_authorize(remote, token, user_info):
from flask import Blueprint, request, url_for, current_app, abort

for b in backends:
register_to(oauth, b)
oauth_register_remote_app(oauth, b)

bp = Blueprint('loginpass', __name__)

Expand Down Expand Up @@ -76,13 +77,4 @@ def login(name):
params = current_app.config.get(conf_key, {})
return remote.authorize_redirect(redirect_uri, **params)

return bp


def register_to(oauth, backend_cls):
from authlib.integrations.flask_client import FlaskRemoteApp

class RemoteApp(backend_cls, FlaskRemoteApp):
OAUTH_APP_CONFIG = backend_cls.OAUTH_CONFIG

oauth.register(RemoteApp.NAME, overwrite=True, client_cls=RemoteApp)
return bp