aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tools/pluginapi.py
diff options
context:
space:
mode:
authorJoar Wandborg <git@wandborg.com>2012-09-12 22:41:04 +0200
committerJoar Wandborg <git@wandborg.com>2012-09-13 20:38:59 +0200
commitf46e2a4db9e70aba473bec537300103c9102ef1a (patch)
tree8e77ab8e34a2d276a9fee3e8f30087dde05ea78b /mediagoblin/tools/pluginapi.py
parentbc875dc7cc7c3f62647dc37a7441fcf252379de5 (diff)
downloadmediagoblin-f46e2a4db9e70aba473bec537300103c9102ef1a.tar.lz
mediagoblin-f46e2a4db9e70aba473bec537300103c9102ef1a.tar.xz
mediagoblin-f46e2a4db9e70aba473bec537300103c9102ef1a.zip
Add OAuth models, plugin DB migrations, api_auth
Diffstat (limited to 'mediagoblin/tools/pluginapi.py')
-rw-r--r--mediagoblin/tools/pluginapi.py35
1 files changed, 34 insertions, 1 deletions
diff --git a/mediagoblin/tools/pluginapi.py b/mediagoblin/tools/pluginapi.py
index bf3775d5..f0c8bbc8 100644
--- a/mediagoblin/tools/pluginapi.py
+++ b/mediagoblin/tools/pluginapi.py
@@ -29,7 +29,7 @@ How do plugins work?
====================
Plugins are structured like any Python project. You create a Python package.
-In that package, you define a high-level ``__init__.py`` module that has a
+In that package, you define a high-level ``__init__.py`` module that has a
``hooks`` dict that maps hooks to callables that implement those hooks.
Additionally, you want a LICENSE file that specifies the license and a
@@ -58,6 +58,8 @@ Lifecycle
import logging
+from functools import wraps
+
from mediagoblin import mg_globals
@@ -205,3 +207,34 @@ def get_config(key):
global_config = mg_globals.global_config
plugin_section = global_config.get('plugins', {})
return plugin_section.get(key, {})
+
+
+def api_auth(controller):
+ @wraps(controller)
+ def wrapper(request, *args, **kw):
+ auth_candidates = []
+
+ for auth in PluginManager().get_hook_callables('auth'):
+ _log.debug('Plugin auth: {0}'.format(auth))
+ if auth.trigger(request):
+ auth_candidates.append(auth)
+
+ # If we can't find any authentication methods, we should not let them
+ # pass.
+ if not auth_candidates:
+ from webob import exc
+ return exc.HTTPForbidden()
+
+ # For now, just select the first one in the list
+ auth = auth_candidates[0]
+
+ _log.debug('Using {0} to authorize request {1}'.format(
+ auth, request.url))
+
+ if not auth(request, *args, **kw):
+ from webob import exc
+ return exc.HTTPForbidden()
+
+ return controller(request, *args, **kw)
+
+ return wrapper