Skip to content

Commit

Permalink
Python 3 friendly-ish.
Browse files Browse the repository at this point in the history
  • Loading branch information
rescrv committed Mar 4, 2016
1 parent 42f4a88 commit 9cc2f68
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ AM_CPPFLAGS = $(SODIUM_CFLAGS)
AM_CFLAGS = -fvisibility=hidden $(SODIUM_CFLAGS) $(WANAL_CFLAGS)
AM_CXXFLAGS = -fvisibility=hidden -fvisibility-inlines-hidden $(SODIUM_CFLAGS) $(WANAL_CXXFLAGS)

AM_DISTCHECK_CONFIGURE_FLAGS = --enable-python-bindings
AM_DISTCHECK_CONFIGURE_FLAGS = --enable-python-bindings PYTHON=python2
TESTS_ENVIRONMENT = . $(abs_top_srcdir)/test/env.sh "${abs_top_srcdir}" "${abs_top_builddir}" "${VERSION}";

pyx_verbose = $(pyx_verbose_$(V))
Expand Down
29 changes: 23 additions & 6 deletions bindings/python/macaroons.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


cdef extern from "stdlib.h":

void* malloc(size_t size)
Expand Down Expand Up @@ -84,6 +83,15 @@ class MacaroonError(Exception): pass
class Unauthorized(Exception): pass


cdef bytes tobytes(s):
if type(s) in (unicode, str):
return s.encode('utf8')
elif type(s) is bytes:
return s
else:
raise TypeError("a bytes/str-like object is required, not '%s'" % s.__class__.__name__)


cdef raise_error(macaroon_returncode err):
if err == MACAROON_OUT_OF_MEMORY:
raise MemoryError
Expand Down Expand Up @@ -225,8 +233,9 @@ cdef class Macaroon:
raise_error(err)
return DP

def add_first_party_caveat(self, bytes predicate):
def add_first_party_caveat(self, predicate):
self.assert_not_null()
predicate = tobytes(predicate)
cdef macarr
cdef macaroon_returncode err
cdef Macaroon M = Macaroon()
Expand All @@ -236,7 +245,10 @@ cdef class Macaroon:
raise_error(err)
return M

def add_third_party_caveat(self, bytes _location, bytes _key, bytes _key_id):
def add_third_party_caveat(self, _location, _key, _key_id):
_location = tobytes(_location)
_key = tobytes(_key)
_key_id = tobytes(_key_id)
cdef unsigned char* location = _location
cdef size_t location_sz = len(_location)
cdef unsigned char* key = _key
Expand Down Expand Up @@ -291,13 +303,15 @@ cdef class Verifier:
raise_error(err)
self._funcs.append(func)

def verify(self, Macaroon M, bytes key, MS=None):
def verify(self, Macaroon M, key, MS=None):
key = tobytes(key)
if self.verify_unsafe(M, key, MS):
return True
else:
raise Unauthorized("macaroon not authorized")

def verify_unsafe(self, Macaroon M, bytes key, MS=None):
def verify_unsafe(self, Macaroon M, key, MS=None):
key = tobytes(key)
cdef macaroon_returncode err
cdef macaroon** discharges = NULL
cdef Macaroon tmp
Expand All @@ -323,7 +337,10 @@ cdef class Verifier:
free(discharges)


def create(bytes _location, bytes _key, bytes _key_id):
def create(_location, _key, _key_id):
_location = tobytes(_location)
_key = tobytes(_key)
_key_id = tobytes(_key_id)
cdef unsigned char* location = _location
cdef size_t location_sz = len(_location)
cdef unsigned char* key = _key
Expand Down

0 comments on commit 9cc2f68

Please sign in to comment.