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
18 config = ConfigEngine()
19 if config.activity and config.activity.apikey :
20 apikey = config.activity.apikey
22 # apikey will be necessary
25 if config.activity and config.activity.secret :
26 secret = config.activity.secret
28 # secret will be necessary
31 if config.activity and config.activity.server :
32 server = config.activity.server
35 server = "http://athos.ipv6.lip6.fr/activity/push/log"
37 def logWrite(request, action, message, objects = None):
40 print "===============>> activity: no apikey"
43 print "===============>> activity: no secret"
46 timestamp = time.mktime(datetime.datetime.today().timetuple())
47 ip = getClientIp(request)
49 "timestamp" : timestamp,
51 "host" : request.get_host(),
52 "referrer" : request.META.get('HTTP_REFERER'),
53 "user" : request.user,
57 "signature" : sign(secret, "%s%s%s%s" % (timestamp, ip, request.user, action)),
60 "resource_type" : None,
65 if objects is not None:
71 result = urllib2.urlopen(server, urllib.urlencode(log))
72 print "===============>> activity: %s <%s> %s" % (action, request.user,message)
73 content = result.read()
74 except urllib2.URLError as e:
75 print "===============>> activity: connection to " + server + " impossible, could not log action"
79 def log(request, action, message, objects = None):
80 # Create a new thread in Daemon mode to send the log entry
81 t = threading.Thread(target=logWrite, args=(request, action, message, objects))
85 def getClientIp(request):
86 x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
88 ip = x_forwarded_for.split(',')[0]
90 ip = request.META.get('REMOTE_ADDR')
94 # sign the request with the secret key
95 def sign(secret, message):
96 return hmac.new(secret, msg=message, digestmod=hashlib.sha256).hexdigest()