Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing an issue with UUID to JSON serialization #244

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions opentaxii/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def create_account(argv=None):
account = app.taxii_server.auth.api.create_account(
username=args.username,
password=args.password,
is_admin=args.admin,
)
token = app.taxii_server.auth.authenticate(
username=account.username,
Expand Down
4 changes: 2 additions & 2 deletions opentaxii/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def __init__(
def can_read(self, collection_name):
return (
self.is_admin or
self.permissions.get(collection_name) in ('read', 'modify'))
self.permissions.get(str(collection_name)) in ('read', 'modify'))

def can_modify(self, collection_name):
return (
self.is_admin or
self.permissions.get(collection_name) == 'modify')
self.permissions.get(str(collection_name)) == 'modify')

def __repr__(self):
return (
Expand Down
4 changes: 2 additions & 2 deletions opentaxii/taxii2/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def can_read(self, account: Optional[Account]):
return self.is_public or (
account
and (
account.is_admin or "read" in set(account.permissions.get(self.id, []))
account.is_admin or "read" in set(account.permissions.get(str(self.id), []))
)
)

Expand All @@ -75,7 +75,7 @@ def can_write(self, account: Optional[Account]):
return self.is_public_write or (
account
and (
account.is_admin or "write" in set(account.permissions.get(self.id, []))
account.is_admin or "write" in set(account.permissions.get(str(self.id), []))
)
)

Expand Down
15 changes: 14 additions & 1 deletion opentaxii/taxii2/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
from typing import Dict, Optional

from flask import Response, make_response
from uuid import UUID


class Taxii2JSONEncoder(json.JSONEncoder):
"""
Extended JSONEncoder class with additional data types support.
"""
def default(self, o):
"""Implements UUID serialization"""
if isinstance(o, UUID):
return str(o)

return super().default(o)


def make_taxii2_response(data, status: Optional[int] = 200, extra_headers: Optional[Dict] = None) -> Response:
"""Turn input data into valid taxii2 response."""
if not isinstance(data, str):
data = json.dumps(data)
data = json.dumps(data, cls=Taxii2JSONEncoder)
response = make_response((data, status))
response.content_type = "application/taxii+json;version=2.1"
response.headers.update(extra_headers or {})
Expand Down