Skip to content

Commit

Permalink
Merge pull request #2 from mstrcnvs/master
Browse files Browse the repository at this point in the history
Various improvements and new WordPress auth cookie hashing algorithm support
  • Loading branch information
dellis23 committed Jan 15, 2015
2 parents fabbec9 + ed6c0e5 commit 9e9f367
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 140 deletions.
73 changes: 73 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Created by https://www.gitignore.io

### Linux ###
*~

# KDE directory preferences
.directory


### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/


### Django ###
*.log
*.pot
*.pyc
__pycache__/
local_settings.py

37 changes: 18 additions & 19 deletions readme.rst → README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Django Wordpress Auth
Introduction
============

Allows for access in Django to a Wordpress installation for checking for
Allows for access in Django to a WordPress installation for checking for
things like login status and roles / capabilities.

Requirements
Expand All @@ -27,14 +27,14 @@ Wordpress Dependencies :
Installation
============

Add your wordpress's auth keys and salts (found in wp-config.php).
Add your WordPress's auth keys and salts (found in wp-config.php) to your settings.py.

.. sourcecode:: python

LOGGED_IN_KEY = "rs&^D%jPdu=vk|VVDsdfsdgsdgsdg9sd87f98s7h[Xm$3gT/@1xdasd"
LOGGED_IN_SALT = "3]x^n{d8=su23902iu09jdc09asjd09asjd09jasdV-Lv-OydAQ%?~"
WORDPRESS_LOGGED_IN_KEY = "rs&^D%jPdu=vk|VVDsdfsdgsdgsdg9sd87f98s7h[Xm$3gT/@1xdasd"
WORDPRESS_LOGGED_IN_SALT = "3]x^n{d8=su23902iu09jdc09asjd09asjd09jasdV-Lv-OydAQ%?~"

Add your wordpress database.
Add your WordPress database to DATABASES in settings.py.

.. sourcecode:: python

Expand All @@ -45,31 +45,31 @@ Add your wordpress database.
'wordpress': { # must be named 'wordpress'
'ENGINE': 'django.db.backends.mysql',
'NAME': 'wordpress',
'USER': 'XXX',
'PASSWORD': 'XXX',
'HOST': '',
'PORT': '',
'USER': '...',
'PASSWORD': '...',
'HOST': '...',
'PORT': 3306,
}
}

Add the middleware. Make sure it's placed somewhere after the session
middleware.
Add the middleware to MIDDLEWARE_CLASSES in settings.py.
Make sure it's placed somewhere after the session middleware.

.. sourcecode:: python

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
# ...
'django_wordpress_auth.middleware.WordpressAuthMiddleware',
'wordpress_auth.middleware.WordpressAuthMiddleware',
)

Finally, add to installed apps.
Finally, add `wordpress_auth` to INSTALLED_APPS.

.. sourcecode:: python

INSTALLED_APPS = (
# ...
'django_wordpress_auth',
'wordpress_auth',
)

Usage
Expand All @@ -80,7 +80,7 @@ To restrict a view to a certain role, simply wrap the view in the

.. sourcecode:: python

from django_wordpress_auth.decorators import wordpress_requires_role
from wordpress_auth.decorators import wordpress_requires_role

@wordpress_requires_role('my_role')
def my_view():
Expand All @@ -90,14 +90,13 @@ You can restrict a view to a capability as well.

.. sourcecode:: python

from django_wordpress_auth.decorators import wordpress_requires_capability
from wordpress_auth.decorators import wordpress_requires_capability

@wordpress_requires_capability('my_capability')
def my_view():
pass

Finally, the middleware provides access to the wordpress user via
``request.wordpress_user``.
Finally, the middleware provides access to the WordPress user via ``request.wordpress_user``.

See ``models.py`` for full reference. Some of the redundant naming conventions
in the wordpress database have been made simpler as well.
in the WordPress database have been made simpler as well.
41 changes: 0 additions & 41 deletions django_wordpress_auth/__init__.py

This file was deleted.

10 changes: 0 additions & 10 deletions django_wordpress_auth/middleware.py

This file was deleted.

29 changes: 0 additions & 29 deletions django_wordpress_auth/router.py

This file was deleted.

28 changes: 0 additions & 28 deletions django_wordpress_auth/routers.py

This file was deleted.

31 changes: 31 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python

try:
from setuptools import setup
except ImportError:
from distutils.core import setup

setup(
name='django-wordpress-auth',
version='0.1.0',
description='Django integration with WordPress authentication and roles / capabilities system.',
long_description=open('README.rst').read(),
include_package_data=True,
packages=[
'wordpress_auth',
],
install_requires=[
'Django',
'phpserialize==1.3'
],
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Intended Audience :: Developers',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Utilities'
]
)
7 changes: 7 additions & 0 deletions wordpress_auth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__version__ = '0.1.0'

from django.conf import settings

WORDPRESS_TABLE_PREFIX = getattr(settings, 'WORDPRESS_TABLE_PREFIX', 'wp_')
WORDPRESS_LOGGED_IN_KEY = getattr(settings, 'WORDPRESS_LOGGED_IN_KEY')
WORDPRESS_LOGGED_IN_SALT = getattr(settings, 'WORDPRESS_LOGGED_IN_SALT')
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from django.core.exceptions import PermissionDenied
from django.shortcuts import redirect

from . import get_wordpress_user, LOGIN_URL
from wordpress_auth.utils import get_login_url


def wordpress_login_required(fn, *args, **kwargs):
def wrapped(request, *args, **kwargs):
if not request.wordpress_user:
redirect_to = request.build_absolute_uri(request.path)
return redirect(LOGIN_URL + "?redirect_to=" + redirect_to)
return redirect(get_login_url() + "?redirect_to=" + redirect_to)
else:
return fn(request, *args, **kwargs)
return wrapped
Expand Down
8 changes: 8 additions & 0 deletions wordpress_auth/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.utils.functional import SimpleLazyObject

from wordpress_auth.utils import get_wordpress_user


class WordPressAuthMiddleware(object):
def process_request(self, request):
request.wordpress_user = SimpleLazyObject(lambda: get_wordpress_user(request))
Loading

0 comments on commit 9e9f367

Please sign in to comment.