|
| 1 | +import workos |
| 2 | +from workos.exceptions import ConfigurationException |
| 3 | +from workos.utils.request import RequestHelper, REQUEST_METHOD_POST |
| 4 | +from workos.utils.validation import validate_api_key_and_project_id |
| 5 | + |
| 6 | +EVENTS_PATH = "events" |
| 7 | +METADATA_LIMIT = 50 |
| 8 | + |
| 9 | + |
| 10 | +class AuditLog(object): |
| 11 | + """Offers methods through the WorkOS Audit Log service.""" |
| 12 | + |
| 13 | + @validate_api_key_and_project_id("Audit Log") |
| 14 | + def __init__(self): |
| 15 | + pass |
| 16 | + |
| 17 | + @property |
| 18 | + def request_helper(self): |
| 19 | + if not getattr(self, "_request_helper", None): |
| 20 | + self._request_helper = RequestHelper() |
| 21 | + return self._request_helper |
| 22 | + |
| 23 | + def create_event(self, event, idempotency_key=None): |
| 24 | + """Create an Audit Log event. |
| 25 | +
|
| 26 | + Args: |
| 27 | + event (dict) - An event object |
| 28 | + event[action] (str) - Specific activity performed by the actor. |
| 29 | + event[action_type] (str) - Corresponding CRUD category of the |
| 30 | + event. Can be one of C, R, U, or D. |
| 31 | + event[actor_name] (str) - Display name of the entity performing the action |
| 32 | + event[actor_id] (str) - Unique identifier of the entity performing the action |
| 33 | + event[group] (str) - A single organization containing related . |
| 34 | + members. This will normally be the customer of a vendor's application |
| 35 | + event[location] (str) - Identifier for where the event |
| 36 | + originated. This will be an IP address (IPv4 or IPv6), |
| 37 | + hostname, or device ID. |
| 38 | + event[occurred_at] (str) - ISO-8601 datetime at which the event |
| 39 | + happened, with millisecond precision. |
| 40 | + event[metadata] (str) - Arbitrary key-value data containing |
| 41 | + information associated with the event. Note: There is a limit of 50 |
| 42 | + keys. Key names can be up to 40 characters long, and values can be up |
| 43 | + to 500 characters long. |
| 44 | + event[target_id] (str) - Unique identifier of the object or |
| 45 | + resource being acted upon. |
| 46 | + event[target_name] (str) - Display name of the object or |
| 47 | + resource that is being acted upon. |
| 48 | + idempotency_key (str) - An idempotency key |
| 49 | +
|
| 50 | + Returns: |
| 51 | + dict: Response from WorkOS |
| 52 | + """ |
| 53 | + if len(event.get("metadata", {})) > METADATA_LIMIT: |
| 54 | + raise Exception("Number of metadata keys exceeds %d." % METADATA_LIMIT) |
| 55 | + |
| 56 | + headers = { |
| 57 | + "Authorization": "Bearer %s" % workos.api_key, |
| 58 | + "idempotency_key": idempotency_key, |
| 59 | + } |
| 60 | + |
| 61 | + return self.request_helper.request( |
| 62 | + EVENTS_PATH, method=REQUEST_METHOD_POST, params=event, headers=headers |
| 63 | + ) |
0 commit comments