4 # Client is authenticated with an API key and a secret
5 # The API key is a 64 chars string (digits and letters) that is passed to the request
6 # The secret is a 64 chars string that is used to sign the request
7 # The generated signature is a SHA256 hes digest
16 from myslice.configengine import ConfigEngine
17 from myslice.settings import logger
20 config = ConfigEngine()
21 if config.activity and config.activity.apikey :
22 apikey = config.activity.apikey
24 # apikey will be necessary
27 if config.activity and config.activity.secret :
28 secret = config.activity.secret
30 # secret will be necessary
33 if config.activity and config.activity.server :
34 server = config.activity.server
37 server = "http://athos.ipv6.lip6.fr/activity/push/log"
39 def logWrite(request, action, message, objects = None):
42 logger.info("===============>> activity: no apikey")
45 logger.info("===============>> activity: no secret")
48 timestamp = time.mktime(datetime.datetime.today().timetuple())
49 ip = getClientIp(request)
51 "timestamp" : timestamp,
53 "host" : request.get_host(),
54 "referrer" : request.META.get('HTTP_REFERER'),
55 "user" : request.user,
59 "signature" : sign(secret, "%s%s%s%s" % (timestamp, ip, request.user, action)),
62 "resource_type" : None,
67 if objects is not None:
73 result = urllib2.urlopen(server, urllib.urlencode(log))
74 logger.info("===============>> activity: {} <{}> {}".format(action, request.user,message))
75 content = result.read()
76 except urllib2.URLError as e:
77 logger.error("===============>> activity: connection to {} impossible, could not log action".format(server))
78 logger.error(e.strerror)
80 def log(request, action, message, objects = None):
81 # Create a new thread in Daemon mode to send the log entry
82 t = threading.Thread(target=logWrite, args=(request, action, message, objects))
86 def getClientIp(request):
88 x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
89 ip = x_forwarded_for.split(',')[0]
91 ip = request.META.get('REMOTE_ADDR')
95 # sign the request with the secret key
96 def sign(secret, message):
97 return hmac.new(secret, msg=message, digestmod=hashlib.sha256).hexdigest()