Skip to content

Commit 6f97c00

Browse files
author
Maria Korlotian
authored
Impersonate start/end (#92)
* Add start and end impersonation commands * Unify the method name - rename build to call
1 parent 52b04f4 commit 6f97c00

28 files changed

Lines changed: 229 additions & 116 deletions

castle/api/approve_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
class APIApproveDevice(object):
66
@staticmethod
77
def call(device_token):
8-
return APIRequest().call(CommandsApproveDevice.build(device_token))
8+
return APIRequest().call(CommandsApproveDevice.call(device_token))

castle/api/get_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
class APIGetDevice(object):
66
@staticmethod
77
def call(device_token):
8-
return APIRequest().call(CommandsGetDevice.build(device_token))
8+
return APIRequest().call(CommandsGetDevice.call(device_token))

castle/api/get_devices_for_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
class APIGetDevicesForUser(object):
66
@staticmethod
77
def call(user_id):
8-
return APIRequest().call(CommandsGetDevicesForUser.build(user_id))
8+
return APIRequest().call(CommandsGetDevicesForUser.call(user_id))

castle/api/report_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
class APIReportDevice(object):
66
@staticmethod
77
def call(device_token):
8-
return APIRequest().call(CommandsReportDevice.build(device_token))
8+
return APIRequest().call(CommandsReportDevice.call(device_token))

castle/api/review.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
class APIReview(object):
66
@staticmethod
77
def call(review_id):
8-
return APIRequest().call(CommandsReview.build(review_id))
8+
return APIRequest().call(CommandsReview.call(review_id))

castle/client.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from castle.api_request import APIRequest
22
from castle.commands.authenticate import CommandsAuthenticate
33
from castle.commands.identify import CommandsIdentify
4-
from castle.commands.impersonate import CommandsImpersonate
4+
from castle.commands.start_impersonation import CommandsStartImpersonation
5+
from castle.commands.end_impersonation import CommandsEndImpersonation
56
from castle.commands.track import CommandsTrack
67
from castle.configuration import configuration
78
from castle.context.prepare import ContextPrepare
@@ -43,7 +44,7 @@ def _add_timestamp_if_necessary(self, options):
4344
def authenticate(self, options):
4445
if self.tracked():
4546
self._add_timestamp_if_necessary(options)
46-
command = CommandsAuthenticate(self.context).build(options)
47+
command = CommandsAuthenticate(self.context).call(options)
4748
try:
4849
response = self.api.call(command)
4950
response.update(failover=False, failover_reason=None)
@@ -61,11 +62,18 @@ def identify(self, options):
6162
if not self.tracked():
6263
return None
6364
self._add_timestamp_if_necessary(options)
64-
return self.api.call(CommandsIdentify(self.context).build(options))
65+
return self.api.call(CommandsIdentify(self.context).call(options))
6566

66-
def impersonate(self, options):
67+
def start_impersonation(self, options):
6768
self._add_timestamp_if_necessary(options)
68-
response = self.api.call(CommandsImpersonate(self.context).build(options))
69+
response = self.api.call(CommandsStartImpersonation(self.context).call(options))
70+
if not response.get('success'):
71+
raise ImpersonationFailed
72+
return response
73+
74+
def end_impersonation(self, options):
75+
self._add_timestamp_if_necessary(options)
76+
response = self.api.call(CommandsEndImpersonation(self.context).call(options))
6977
if not response.get('success'):
7078
raise ImpersonationFailed
7179
return response
@@ -74,7 +82,7 @@ def track(self, options):
7482
if not self.tracked():
7583
return None
7684
self._add_timestamp_if_necessary(options)
77-
return self.api.call(CommandsTrack(self.context).build(options))
85+
return self.api.call(CommandsTrack(self.context).call(options))
7886

7987
def disable_tracking(self):
8088
self.do_not_track = True

castle/commands/approve_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class CommandsApproveDevice(object):
66

77
@staticmethod
8-
def build(device_token):
8+
def call(device_token):
99
ValidatorsPresent.call({'device_token': device_token}, 'device_token')
1010

1111
return Command(

castle/commands/authenticate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CommandsAuthenticate(object):
99
def __init__(self, context):
1010
self.context = context
1111

12-
def build(self, options):
12+
def call(self, options):
1313
ValidatorsPresent.call(options, 'event')
1414
context = ContextMerge.call(self.context, options.get('context'))
1515
context = ContextSanitize.call(context)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from castle.command import Command
2+
from castle.utils.timestamp import UtilsTimestamp as generate_timestamp
3+
from castle.context.merge import ContextMerge
4+
from castle.context.sanitize import ContextSanitize
5+
from castle.validators.present import ValidatorsPresent
6+
7+
8+
class CommandsEndImpersonation(object):
9+
def __init__(self, context):
10+
self.context = context
11+
12+
def call(self, options):
13+
ValidatorsPresent.call(options, 'user_id')
14+
15+
context = ContextMerge.call(self.context, options.get('context'))
16+
context = ContextSanitize.call(context)
17+
ValidatorsPresent.call(context, 'user_agent', 'ip')
18+
19+
if context:
20+
options.update({'context': context})
21+
options.update({'sent_at': generate_timestamp.call()})
22+
23+
return Command(method='delete', path='impersonate', data=options)

castle/commands/get_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class CommandsGetDevice(object):
66

77
@staticmethod
8-
def build(device_token):
8+
def call(device_token):
99
ValidatorsPresent.call({'device_token': device_token}, 'device_token')
1010

1111
return Command(

0 commit comments

Comments
 (0)