Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[unfold.git] / portal / account.py
1 from manifold.core.query                import Query
2 from manifoldapi.manifoldapi            import execute_query
3
4 from datetime import datetime
5 import time
6 import dateutil.parser
7 import calendar
8
9 import json, os, re, itertools
10
11 class Account:
12     def __init__ (self, platform_id, user_id):
13         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config').filter_by('platform_id', '==', platform_id).filter_by('user_id', '==', user_id)
14  
15         account_details = execute_query(self.request, account_query)
16         self.user_id     = account_details['user_id']
17         self.platform_id = account_details['platform_id']
18         self.auth_type   = account_details['auth_type']
19         self.config      = account_details['config']
20         account_config = json.loads(account_details['config'])
21
22         self.usr_hrn = account_config.get('user_hrn',None)
23         self.pub_key = account_config.get('user_public_key',None)
24         self.reference = account_config.get ('reference_platform',None)
25
26         self.user_cred = account_config.get('delegated_user_credential',None)
27         self.user_cred_expiration = get_expiration(self.user_cred)
28         
29         slice_creds = account_config.get('delegated_slice_credentials',None)
30         for slice_name, slice_cred in slice_creds.iteritems():
31             self.slice_cred_expiration = get_expiration(self.slice_cred)
32
33         self.auth_cred = account_config.get('delegated_authority_credentials',None)
34         self.auth_cred_expiration = get_expiration(self.auth_cred)
35
36 def get_expiration (credential, format = 'UTC'):
37     exp_date = re.search('<expires>(.*)</expires>', credential)
38     if exp_date:
39         exp_date = exp_date.group(1)
40         if format == 'timestamp':
41             exp_date = calendar.timegm(dateutil.parser.parse(exp_date).utctimetuple())
42     else:
43         exp_date = None
44     return exp_date
45