Skip to content

Commit

Permalink
coderabbit result apply
Browse files Browse the repository at this point in the history
  • Loading branch information
search5 committed Mar 10, 2024
2 parents 301aa08 + d0a118c commit b79c985
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
2 changes: 1 addition & 1 deletion social_cherrypy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.0'
__version__ = "1.0.0"
42 changes: 24 additions & 18 deletions social_cherrypy/models.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
"""Flask SQLAlchemy ORM models for Social Auth"""
import cherrypy

from sqlalchemy import String, ForeignKey
from sqlalchemy.orm import relationship, DeclarativeBase, Mapped, mapped_column

from social_core.utils import setting_name, module_member
from social_sqlalchemy.storage import SQLAlchemyUserMixin, \
SQLAlchemyAssociationMixin, \
SQLAlchemyNonceMixin, \
SQLAlchemyPartialMixin, \
BaseSQLAlchemyStorage
import cherrypy
from social_core.utils import module_member, setting_name
from social_sqlalchemy.storage import (
BaseSQLAlchemyStorage,
SQLAlchemyAssociationMixin,
SQLAlchemyNonceMixin,
SQLAlchemyPartialMixin,
SQLAlchemyUserMixin,
)
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship


class SocialBase(DeclarativeBase):
pass


DB_SESSION_ATTR = cherrypy.config.get(setting_name('DB_SESSION_ATTR'), 'db')
UID_LENGTH = cherrypy.config.get(setting_name('UID_LENGTH'), 255)
User = module_member(cherrypy.config[setting_name('USER_MODEL')])
DB_SESSION_ATTR = cherrypy.config.get(setting_name("DB_SESSION_ATTR"), "db")
UID_LENGTH = cherrypy.config.get(setting_name("UID_LENGTH"), 255)
User = module_member(cherrypy.config[setting_name("USER_MODEL")])


class CherryPySocialBase(object):
class CherryPySocialBase:
@classmethod
def _session(cls):
return getattr(cherrypy.request, DB_SESSION_ATTR)


class UserSocialAuth(CherryPySocialBase, SQLAlchemyUserMixin, SocialBase):
"""Social Auth association model"""

uid: Mapped[str] = mapped_column(String(UID_LENGTH))
user_id: Mapped[int] = mapped_column(ForeignKey(User.id),
nullable=False, index=True)
user: Mapped["User"] = relationship(User, backref='social_auth')
user_id: Mapped[int] = mapped_column(
ForeignKey(User.id), nullable=False, index=True
)
user: Mapped["User"] = relationship(User, backref="social_auth")

@classmethod
def username_max_length(cls):
return User.__table__.columns.get('username').type.length
return User.__table__.columns.get("username").type.length

@classmethod
def user_model(cls):
Expand All @@ -45,16 +48,19 @@ def user_model(cls):

class Nonce(CherryPySocialBase, SQLAlchemyNonceMixin, SocialBase):
"""One use numbers"""

pass


class Association(CherryPySocialBase, SQLAlchemyAssociationMixin, SocialBase):
"""OpenId account association"""

pass


class Partial(CherryPySocialBase, SQLAlchemyPartialMixin, SocialBase):
"""Partial pipeline storage"""

pass


Expand Down
16 changes: 10 additions & 6 deletions social_cherrypy/strategy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cherrypy

import six
from social_core.strategy import BaseStrategy, BaseTemplateStrategy


Expand All @@ -24,7 +24,7 @@ def get_setting(self, name):
def request_data(self, merge=True):
if merge:
data = cherrypy.request.params
elif cherrypy.request.method == 'POST':
elif cherrypy.request.method == "POST":
data = cherrypy.body.params
else:
data = cherrypy.request.params
Expand All @@ -40,9 +40,9 @@ def html(self, content):
return content

def authenticate(self, backend, *args, **kwargs):
kwargs['strategy'] = self
kwargs['storage'] = self.storage
kwargs['backend'] = backend
kwargs["strategy"] = self
kwargs["storage"] = self.storage
kwargs["backend"] = backend
return backend.authenticate(*args, **kwargs)

def session_get(self, name, default=None):
Expand All @@ -58,8 +58,12 @@ def session_setdefault(self, name, value):
return cherrypy.session.setdefault(name, value)

def build_absolute_uri(self, path=None):
return cherrypy.url(path or '')
return cherrypy.url(path or "")

def is_response(self, value):
<<<<<<< HEAD
return isinstance(value, str) or \
isinstance(value, cherrypy.CherryPyException)
=======
return isinstance(value, str) or isinstance(value, cherrypy.CherryPyException)
>>>>>>> d0a118c8c84b78bea4c7fa220d6c764e4405a095
35 changes: 19 additions & 16 deletions social_cherrypy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
from functools import wraps

import cherrypy

from social_core.utils import setting_name, module_member, get_strategy
from social_core.backends.utils import get_backend, user_backends_data

from social_core.utils import get_strategy, module_member, setting_name

DEFAULTS = {
'STRATEGY': 'social_cherrypy.strategy.CherryPyStrategy',
'STORAGE': 'social_cherrypy.models.CherryPyStorage'
"STRATEGY": "social_cherrypy.strategy.CherryPyStrategy",
"STORAGE": "social_cherrypy.models.CherryPyStorage",
}


Expand All @@ -18,12 +16,11 @@ def get_helper(name):


def load_strategy():
return get_strategy(get_helper('STRATEGY'),
get_helper('STORAGE'))
return get_strategy(get_helper("STRATEGY"), get_helper("STORAGE"))


def load_backend(strategy, name, redirect_uri):
backends = get_helper('AUTHENTICATION_BACKENDS')
backends = get_helper("AUTHENTICATION_BACKENDS")
Backend = get_backend(backends, name)
return Backend(strategy=strategy, redirect_uri=redirect_uri)

Expand All @@ -33,25 +30,31 @@ def decorator(func):
@wraps(func)
def wrapper(self, backend=None, *args, **kwargs):
uri = redirect_uri
if uri and backend and '%(backend)s' in uri:
uri = uri % {'backend': backend}
if uri[-1] != '/' and \
cherrypy.config.get(setting_name('TRAILING_SLASH'), False):
uri = uri + '/'
if uri and backend and "%(backend)s" in uri:
uri = uri % {"backend": backend}
if uri[-1] != "/" and cherrypy.config.get(
setting_name("TRAILING_SLASH"), False
):
uri = uri + "/"
self.strategy = load_strategy()
self.backend = load_backend(self.strategy, backend, uri)
return func(self, backend, *args, **kwargs)

return wrapper

return decorator


def backends(user):
"""Load Social Auth current user data to context under the key 'backends'.
Will return the output of social_core.backends.utils.user_backends_data."""
return user_backends_data(user, get_helper('AUTHENTICATION_BACKENDS'),
module_member(get_helper('STORAGE')))
return user_backends_data(
user,
get_helper("AUTHENTICATION_BACKENDS"),
module_member(get_helper("STORAGE")),
)


def strategy(*args, **kwargs):
warnings.warn('@strategy decorator is deprecated, use @psa instead')
warnings.warn("@strategy decorator is deprecated, use @psa instead")
return psa(*args, **kwargs)
17 changes: 8 additions & 9 deletions social_cherrypy/views.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import cherrypy

from social_core.utils import setting_name, module_member
from social_core.actions import do_auth, do_complete, do_disconnect
from social_core.utils import module_member, setting_name

from .utils import psa


class CherryPyPSAViews(object):
class CherryPyPSAViews:
@cherrypy.expose
@psa('/complete/%(backend)s')
@psa("/complete/%(backend)s")
def login(self, backend):
return do_auth(self.backend)

@cherrypy.expose
@psa('/complete/%(backend)s')
@psa("/complete/%(backend)s")
def complete(self, backend, *args, **kwargs):
login = cherrypy.config.get(setting_name('LOGIN_METHOD'))
login = cherrypy.config.get(setting_name("LOGIN_METHOD"))
do_login = module_member(login) if login else self.do_login
user = getattr(cherrypy.request, 'user', None)
user = getattr(cherrypy.request, "user", None)
return do_complete(self.backend, do_login, user=user, *args, **kwargs)

@cherrypy.expose
@psa()
def disconnect(self, backend, association_id=None, csrfmiddlewaretoken=None):
user = getattr(cherrypy.request, 'user', None)
user = getattr(cherrypy.request, "user", None)
return do_disconnect(self.backend, user, association_id)

def do_login(self, backend, user, social_user):
backend.strategy.session_set('user_id', user.id)
backend.strategy.session_set("user_id", user.id)

0 comments on commit b79c985

Please sign in to comment.