Commit bfb76788 by David Robinson

Merge pull request #120 from justinwp/master

with MasterKey('key'):
parents 928d4ee9 9741b0d4
...@@ -558,4 +558,16 @@ with SessionToken(token): ...@@ -558,4 +558,16 @@ with SessionToken(token):
Assuming the CollectedItem 'Sword' is read-protected from the public by an ACL and is readable only by the user, SessionToken allows the user to bypass the ACL and get the 'Sword' item. Assuming the CollectedItem 'Sword' is read-protected from the public by an ACL and is readable only by the user, SessionToken allows the user to bypass the ACL and get the 'Sword' item.
Elevating Access to Master
--------------------------
Sometimes it is useful to only allow privileged use of the master key for specific uses.
~~~~~ {python}
from parse_rest.connection import MasterKey
with MasterKey('master key'):
# do privileged calls
~~~~~
That's it! This is a first try at a Python library for Parse, and is probably not bug-free. If you run into any issues, please get in touch -- dgrtwo@princeton.edu. Thanks! That's it! This is a first try at a Python library for Parse, and is probably not bug-free. If you run into any issues, please get in touch -- dgrtwo@princeton.edu. Thanks!
...@@ -45,7 +45,19 @@ class SessionToken: ...@@ -45,7 +45,19 @@ class SessionToken:
ACCESS_KEYS.update({'session_token': self.token}) ACCESS_KEYS.update({'session_token': self.token})
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
ACCESS_KEYS['session_token'] del ACCESS_KEYS['session_token']
class MasterKey:
def __init__(self, master_key):
global ACCESS_KEYS
self.master_key = master_key
def __enter__(self):
return ACCESS_KEYS.update({'master_key': self.master_key})
def __exit__(self, type, value, traceback):
del ACCESS_KEYS['master_key']
def master_key_required(func): def master_key_required(func):
......
...@@ -15,7 +15,7 @@ import six ...@@ -15,7 +15,7 @@ import six
from itertools import chain from itertools import chain
from parse_rest.core import ResourceRequestNotFound from parse_rest.core import ResourceRequestNotFound
from parse_rest.connection import register, ParseBatcher from parse_rest.connection import register, ParseBatcher, SessionToken, MasterKey
from parse_rest.datatypes import GeoPoint, Object, Function, Pointer from parse_rest.datatypes import GeoPoint, Object, Function, Pointer
from parse_rest.user import User from parse_rest.user import User
from parse_rest import query from parse_rest import query
...@@ -579,6 +579,34 @@ class TestPush(unittest.TestCase): ...@@ -579,6 +579,34 @@ class TestPush(unittest.TestCase):
channels=["Mets"], where={"scores": True}) channels=["Mets"], where={"scores": True})
class TestSessionToken(unittest.TestCase):
"""
Test SessionToken class enter and exit.
"""
def get_access_keys(self):
from parse_rest.connection import ACCESS_KEYS
return ACCESS_KEYS
def testWithSessionToken(self):
with SessionToken(token='asdf'):
self.assertIn('session_token', self.get_access_keys())
self.assertNotIn('session_token', self.get_access_keys())
class TestMasterKey(unittest.TestCase):
"""
Test MasterKey class enter and exit.
"""
def get_access_keys(self):
from parse_rest.connection import ACCESS_KEYS
return ACCESS_KEYS
def testWithMasterKey(self):
with MasterKey(master_key='asdf'):
self.assertIn('master_key', self.get_access_keys() )
self.assertNotIn('master_key', self.get_access_keys() )
def run_tests(): def run_tests():
"""Run all tests in the parse_rest package""" """Run all tests in the parse_rest package"""
tests = unittest.TestLoader().loadTestsFromNames(['parse_rest.tests']) tests = unittest.TestLoader().loadTestsFromNames(['parse_rest.tests'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment