def rw_protected_resource(scopes=None, validator_cls=OAuth2Validator, server_cls=Server):
 _scopes = (scopes or [])
 def decorator(view_func):
  @wraps(view_func)
  def _validate(request, *args, **kwargs):
   provided_scopes = oauth2_settings._SCOPES
   read_write_scopes = [oauth2_settings.READ_SCOPE, oauth2_settings.WRITE_SCOPE]
   if (not set(read_write_scopes).issubset(set(provided_scopes))):
    raise ImproperlyConfigured("rw_protected_resource decorator requires following scopes {0} to be in OAUTH2_PROVIDER['SCOPES'] list in settings".format(read_write_scopes))
   if (request.method.upper() in ['GET', 'HEAD', 'OPTIONS']):
    _scopes.append(oauth2_settings.READ_SCOPE)
   else:
    _scopes.append(oauth2_settings.WRITE_SCOPE)
   validator = validator_cls()
   core = OAuthLibCore(server_cls(validator))
   (valid, oauthlib_req) = core.verify_request(request, scopes=_scopes)
   if valid:
    request.resource_owner = oauthlib_req.user
    return view_func(request, *args, **kwargs)
   return HttpResponseForbidden()
  return _validate
 return decorator

def rw_protected_resource ( scopes = None , validator_cls = OAuth2Validator , server_cls = Server ) : _scopes = ( scopes or [ ] ) def decorator ( view_func ) : @ wraps ( view_func ) def _validate ( request , * args , ** kwargs ) : provided_scopes = oauth2_settings . _SCOPES read_write_scopes = [ oauth2_settings . READ_SCOPE , oauth2_settings . WRITE_SCOPE ] if ( not set ( read_write_scopes ) . issubset ( set ( provided_scopes ) ) ) : raise ImproperlyConfigured ( "rw_protected_resource decorator requires following scopes {0} to be in OAUTH2_PROVIDER['SCOPES'] list in settings" . format ( read_write_scopes ) ) if ( request . method . upper ( ) in [ 'GET' , 'HEAD' , 'OPTIONS' ] ) : _scopes . append ( oauth2_settings . READ_SCOPE ) else : _scopes . append ( oauth2_settings . WRITE_SCOPE ) validator = validator_cls ( ) core = OAuthLibCore ( server_cls ( validator ) ) ( valid , oauthlib_req ) = core . verify_request ( request , scopes = _scopes ) if valid : request . resource_owner = oauthlib_req . user return view_func ( request , * args , ** kwargs ) return HttpResponseForbidden ( ) return _validate return decorator
decorator to protect views by providing oauth2 authentication and read/write scopes out of the box .