Skip to content

Commit be929d8

Browse files
committed
Cleanup and depend on social modules
1 parent 7811737 commit be929d8

File tree

208 files changed

+277
-12988
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

208 files changed

+277
-12988
lines changed

Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM n42org/tox
2+
MAINTAINER Matías Aguirre <[email protected]>
3+
RUN apt-get update
4+
RUN apt-get install -y make libssl-dev libxml2-dev libxmlsec1-dev mongodb-server mongodb-dev swig openssl
5+
RUN ln -s /usr/include/x86_64-linux-gnu/openssl/opensslconf.h /usr/include/openssl/opensslconf.h

Makefile

+22-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,28 @@ publish:
1414
python setup.py bdist_wheel --python-tag py2 upload
1515
BUILD_VERSION=3 python setup.py bdist_wheel --python-tag py3 upload
1616

17+
run-tox:
18+
@ tox
19+
20+
docker-tox-build:
21+
@ docker build -t omab/psa-legacy .
22+
23+
docker-tox: docker-tox-build
24+
@ docker run -it --rm \
25+
--name psa-legacy-test \
26+
-v "`pwd`:/code" \
27+
-w /code omab/psa-legacy tox
28+
29+
docker-shell: docker-tox-build
30+
@ docker run -it --rm \
31+
--name psa-legacy-test \
32+
-v "`pwd`:/code" \
33+
-w /code omab/psa-legacy bash
34+
1735
clean:
18-
find . -name '*.py[co]' -delete
19-
find . -name '__pycache__' -delete
20-
rm -rf python_social_auth.egg-info dist build
36+
@ find . -name '*.py[co]' -delete
37+
@ find . -name '__pycache__' -delete
38+
@ rm -rf *.egg-info dist build
39+
2140

2241
.PHONY: site docs publish

requirements-python3.txt

+14
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,17 @@ oauthlib>=1.0.3
44
requests-oauthlib>=0.6.1
55
six>=1.10.0
66
PyJWT>=1.4.0
7+
social-auth-core
8+
social-auth-storage-mongoengine
9+
social-auth-storage-peewee
10+
social-auth-storage-sqlalchemy
11+
social-auth-app-cherrypy
12+
social-auth-app-django
13+
social-auth-app-django-mongoengine
14+
social-auth-app-flask
15+
social-auth-app-flask-mongoengine
16+
social-auth-app-flask-peewee
17+
social-auth-app-flask-sqlalchemy
18+
social-auth-app-pyramid
19+
social-auth-app-tornado
20+
social-auth-app-webpy

requirements.txt

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
python-openid>=2.2.5
2-
requests>=2.9.1
3-
oauthlib>=1.0.3
4-
requests-oauthlib>=0.6.1
5-
six>=1.10.0
6-
PyJWT>=1.4.0
1+
social-auth-core

social/actions.py

+1-122
Original file line numberDiff line numberDiff line change
@@ -1,122 +1 @@
1-
from social.p3 import quote
2-
from social.utils import sanitize_redirect, user_is_authenticated, \
3-
user_is_active, partial_pipeline_data, setting_url
4-
5-
6-
def do_auth(backend, redirect_name='next'):
7-
# Clean any partial pipeline data
8-
backend.strategy.clean_partial_pipeline()
9-
10-
# Save any defined next value into session
11-
data = backend.strategy.request_data(merge=False)
12-
13-
# Save extra data into session.
14-
for field_name in backend.setting('FIELDS_STORED_IN_SESSION', []):
15-
if field_name in data:
16-
backend.strategy.session_set(field_name, data[field_name])
17-
18-
if redirect_name in data:
19-
# Check and sanitize a user-defined GET/POST next field value
20-
redirect_uri = data[redirect_name]
21-
if backend.setting('SANITIZE_REDIRECTS', True):
22-
allowed_hosts = backend.setting('ALLOWED_REDIRECT_HOSTS', []) + \
23-
[backend.strategy.request_host()]
24-
redirect_uri = sanitize_redirect(allowed_hosts, redirect_uri)
25-
backend.strategy.session_set(
26-
redirect_name,
27-
redirect_uri or backend.setting('LOGIN_REDIRECT_URL')
28-
)
29-
return backend.start()
30-
31-
32-
def do_complete(backend, login, user=None, redirect_name='next',
33-
*args, **kwargs):
34-
data = backend.strategy.request_data()
35-
36-
is_authenticated = user_is_authenticated(user)
37-
user = is_authenticated and user or None
38-
39-
partial = partial_pipeline_data(backend, user, *args, **kwargs)
40-
if partial:
41-
xargs, xkwargs = partial
42-
user = backend.continue_pipeline(*xargs, **xkwargs)
43-
else:
44-
user = backend.complete(user=user, *args, **kwargs)
45-
46-
# pop redirect value before the session is trashed on login(), but after
47-
# the pipeline so that the pipeline can change the redirect if needed
48-
redirect_value = backend.strategy.session_get(redirect_name, '') or \
49-
data.get(redirect_name, '')
50-
51-
user_model = backend.strategy.storage.user.user_model()
52-
if user and not isinstance(user, user_model):
53-
return user
54-
55-
if is_authenticated:
56-
if not user:
57-
url = setting_url(backend, redirect_value, 'LOGIN_REDIRECT_URL')
58-
else:
59-
url = setting_url(backend, redirect_value,
60-
'NEW_ASSOCIATION_REDIRECT_URL',
61-
'LOGIN_REDIRECT_URL')
62-
elif user:
63-
if user_is_active(user):
64-
# catch is_new/social_user in case login() resets the instance
65-
is_new = getattr(user, 'is_new', False)
66-
social_user = user.social_user
67-
login(backend, user, social_user)
68-
# store last login backend name in session
69-
backend.strategy.session_set('social_auth_last_login_backend',
70-
social_user.provider)
71-
72-
if is_new:
73-
url = setting_url(backend,
74-
'NEW_USER_REDIRECT_URL',
75-
redirect_value,
76-
'LOGIN_REDIRECT_URL')
77-
else:
78-
url = setting_url(backend, redirect_value,
79-
'LOGIN_REDIRECT_URL')
80-
else:
81-
if backend.setting('INACTIVE_USER_LOGIN', False):
82-
social_user = user.social_user
83-
login(backend, user, social_user)
84-
url = setting_url(backend, 'INACTIVE_USER_URL', 'LOGIN_ERROR_URL',
85-
'LOGIN_URL')
86-
else:
87-
url = setting_url(backend, 'LOGIN_ERROR_URL', 'LOGIN_URL')
88-
89-
if redirect_value and redirect_value != url:
90-
redirect_value = quote(redirect_value)
91-
url += ('?' in url and '&' or '?') + \
92-
'{0}={1}'.format(redirect_name, redirect_value)
93-
94-
if backend.setting('SANITIZE_REDIRECTS', True):
95-
allowed_hosts = backend.setting('ALLOWED_REDIRECT_HOSTS', []) + \
96-
[backend.strategy.request_host()]
97-
url = sanitize_redirect(allowed_hosts, url) or \
98-
backend.setting('LOGIN_REDIRECT_URL')
99-
return backend.strategy.redirect(url)
100-
101-
102-
def do_disconnect(backend, user, association_id=None, redirect_name='next',
103-
*args, **kwargs):
104-
partial = partial_pipeline_data(backend, user, *args, **kwargs)
105-
if partial:
106-
xargs, xkwargs = partial
107-
if association_id and not xkwargs.get('association_id'):
108-
xkwargs['association_id'] = association_id
109-
response = backend.disconnect(*xargs, **xkwargs)
110-
else:
111-
response = backend.disconnect(user=user, association_id=association_id,
112-
*args, **kwargs)
113-
114-
if isinstance(response, dict):
115-
response = backend.strategy.redirect(
116-
backend.strategy.absolute_uri(
117-
backend.strategy.request_data().get(redirect_name, '') or
118-
backend.setting('DISCONNECT_REDIRECT_URL') or
119-
backend.setting('LOGIN_REDIRECT_URL')
120-
)
121-
)
122-
return response
1+
from social_core.actions import do_auth, do_complete, do_disconnect

social/apps/cherrypy_app/models.py

+1-57
Original file line numberDiff line numberDiff line change
@@ -1,57 +1 @@
1-
"""Flask SQLAlchemy ORM models for Social Auth"""
2-
import cherrypy
3-
4-
from sqlalchemy import Column, Integer, String, ForeignKey
5-
from sqlalchemy.orm import relationship
6-
from sqlalchemy.ext.declarative import declarative_base
7-
8-
from social.utils import setting_name, module_member
9-
from social.storage.sqlalchemy_orm import SQLAlchemyUserMixin, \
10-
SQLAlchemyAssociationMixin, \
11-
SQLAlchemyNonceMixin, \
12-
BaseSQLAlchemyStorage
13-
14-
15-
SocialBase = declarative_base()
16-
17-
DB_SESSION_ATTR = cherrypy.config.get(setting_name('DB_SESSION_ATTR'), 'db')
18-
UID_LENGTH = cherrypy.config.get(setting_name('UID_LENGTH'), 255)
19-
User = module_member(cherrypy.config[setting_name('USER_MODEL')])
20-
21-
22-
class CherryPySocialBase(object):
23-
@classmethod
24-
def _session(cls):
25-
return getattr(cherrypy.request, DB_SESSION_ATTR)
26-
27-
28-
class UserSocialAuth(CherryPySocialBase, SQLAlchemyUserMixin, SocialBase):
29-
"""Social Auth association model"""
30-
uid = Column(String(UID_LENGTH))
31-
user_id = Column(Integer, ForeignKey(User.id),
32-
nullable=False, index=True)
33-
user = relationship(User, backref='social_auth')
34-
35-
@classmethod
36-
def username_max_length(cls):
37-
return User.__table__.columns.get('username').type.length
38-
39-
@classmethod
40-
def user_model(cls):
41-
return User
42-
43-
44-
class Nonce(CherryPySocialBase, SQLAlchemyNonceMixin, SocialBase):
45-
"""One use numbers"""
46-
pass
47-
48-
49-
class Association(CherryPySocialBase, SQLAlchemyAssociationMixin, SocialBase):
50-
"""OpenId account association"""
51-
pass
52-
53-
54-
class CherryPyStorage(BaseSQLAlchemyStorage):
55-
user = UserSocialAuth
56-
nonce = Nonce
57-
association = Association
1+
from social_cherrypy.models import CherryPySocialBase, UserSocialAuth, Nonce, Association, CherryPyStorage

social/apps/cherrypy_app/utils.py

+1-51
Original file line numberDiff line numberDiff line change
@@ -1,51 +1 @@
1-
import warnings
2-
from functools import wraps
3-
4-
import cherrypy
5-
6-
from social.utils import setting_name, module_member
7-
from social.strategies.utils import get_strategy
8-
from social.backends.utils import get_backend, user_backends_data
9-
10-
11-
DEFAULTS = {
12-
'STRATEGY': 'social.strategies.cherrypy_strategy.CherryPyStrategy',
13-
'STORAGE': 'social.apps.cherrypy_app.models.CherryPyStorage'
14-
}
15-
16-
17-
def get_helper(name):
18-
return cherrypy.config.get(setting_name(name), DEFAULTS.get(name, None))
19-
20-
21-
def load_backend(strategy, name, redirect_uri):
22-
backends = get_helper('AUTHENTICATION_BACKENDS')
23-
Backend = get_backend(backends, name)
24-
return Backend(strategy=strategy, redirect_uri=redirect_uri)
25-
26-
27-
def psa(redirect_uri=None):
28-
def decorator(func):
29-
@wraps(func)
30-
def wrapper(self, backend=None, *args, **kwargs):
31-
uri = redirect_uri
32-
if uri and backend and '%(backend)s' in uri:
33-
uri = uri % {'backend': backend}
34-
self.strategy = get_strategy(get_helper('STRATEGY'),
35-
get_helper('STORAGE'))
36-
self.backend = load_backend(self.strategy, backend, uri)
37-
return func(self, backend, *args, **kwargs)
38-
return wrapper
39-
return decorator
40-
41-
42-
def backends(user):
43-
"""Load Social Auth current user data to context under the key 'backends'.
44-
Will return the output of social.backends.utils.user_backends_data."""
45-
return user_backends_data(user, get_helper('AUTHENTICATION_BACKENDS'),
46-
module_member(get_helper('STORAGE')))
47-
48-
49-
def strategy(*args, **kwargs):
50-
warnings.warn('@strategy decorator is deprecated, use @psa instead')
51-
return psa(*args, **kwargs)
1+
from social_cherrypy.utils import get_helper, load_backend, psa, backends, strategy

social/apps/cherrypy_app/views.py

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,29 +1 @@
1-
import cherrypy
2-
3-
from social.utils import setting_name, module_member
4-
from social.actions import do_auth, do_complete, do_disconnect
5-
from social.apps.cherrypy_app.utils import psa
6-
7-
8-
class CherryPyPSAViews(object):
9-
@cherrypy.expose
10-
@psa('/complete/%(backend)s')
11-
def login(self, backend):
12-
return do_auth(self.backend)
13-
14-
@cherrypy.expose
15-
@psa('/complete/%(backend)s')
16-
def complete(self, backend, *args, **kwargs):
17-
login = cherrypy.config.get(setting_name('LOGIN_METHOD'))
18-
do_login = module_member(login) if login else self.do_login
19-
user = getattr(cherrypy.request, 'user', None)
20-
return do_complete(self.backend, do_login, user=user, *args, **kwargs)
21-
22-
@cherrypy.expose
23-
@psa()
24-
def disconnect(self, backend, association_id=None):
25-
user = getattr(cherrypy.request, 'user', None)
26-
return do_disconnect(self.backend, user, association_id)
27-
28-
def do_login(self, backend, user, social_user):
29-
backend.strategy.session_set('user_id', user.id)
1+
from social_cherrypy.views import CherryPyPSAViews

social/apps/django_app/__init__.py

-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +0,0 @@
1-
"""
2-
Django framework support.
3-
4-
To use this:
5-
* Add 'social.apps.django_app.default' if using default ORM,
6-
or 'social.apps.django_app.me' if using mongoengine
7-
* Add url('', include('social.apps.django_app.urls', namespace='social')) to
8-
urls.py
9-
* Define SOCIAL_AUTH_STORAGE and SOCIAL_AUTH_STRATEGY, default values:
10-
SOCIAL_AUTH_STRATEGY = 'social.strategies.django_strategy.DjangoStrategy'
11-
SOCIAL_AUTH_STORAGE = 'social.apps.django_app.default.models.DjangoStorage'
12-
"""
13-
import django
14-
15-
16-
if django.VERSION[0] == 1 and django.VERSION[1] < 7:
17-
from social.strategies.utils import set_current_strategy_getter
18-
from social.apps.django_app.utils import load_strategy
19-
# Set strategy loader method to workaround current strategy getter needed on
20-
# get_user() method on authentication backends when working with Django
21-
set_current_strategy_getter(load_strategy)

0 commit comments

Comments
 (0)