Skip to content

Instantly share code, notes, and snippets.

@ichord
Created July 3, 2012 02:01
Show Gist options
  • Save ichord/3037054 to your computer and use it in GitHub Desktop.
Save ichord/3037054 to your computer and use it in GitHub Desktop.
decorator for django oauth2app authencation
#-*- coding: utf-8 -*-
from oauth2app.models import AccessRange
from oauth2app.authenticate import JSONAuthenticator, AuthenticationException
import functools
def decorator(decorator):
def func_wrapper(func):
def functor(*args, **kw):
functools.wraps(func)(decorator)
return decorator(func, *args, **kw)
return functor
return func_wrapper
@decorator
def api(func, *args, **kw):
authenticator = JSONAuthenticator()
request = args[0]
try:
authenticator.validate(request)
except AuthenticationException:
return authenticator.error_response()
args[0] = authenticator
data = func(*args, **kw)
return authenticator.response(data)
@decorator
def api_without_auth(func, *args, **kw):
authenticator = JSONAuthenticator()
data = func(*args, **kw)
return authenticator.response(data)
def api_with_scope(key):
def decorator(func):
def func_wrapper(*args, **kw):
scope = AccessRange.objects.get(key=key)
authenticator = JSONAuthenticator(scope=scope)
request = args[0]
try:
authenticator.validate(request)
except AuthenticationException:
return authenticator.error_response()
args[0] = authenticator
data = func(*args, **kw)
return authenticator.response(data)
return func_wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment