merge
[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')\
14                                 .select('user_id','platform_id','auth_type','config')\
15                                 .filter_by('platform_id', '==', platform_id)\
16                                 .filter_by('user_id', '==', user_id)
17  
18         account_details  = execute_query(self.request, account_query)
19         self.user_id     = account_details['user_id']
20         self.platform_id = account_details['platform_id']
21         self.auth_type   = account_details['auth_type']
22         self.config      = account_details['config']
23         account_config   = json.loads(account_details['config'])
24
25         self.usr_hrn = account_config.get('user_hrn',None)
26         self.pub_key = account_config.get('user_public_key',None)
27         self.reference = account_config.get ('reference_platform',None)
28
29         self.user_cred = account_config.get('delegated_user_credential',None)
30         self.user_cred_expiration = get_expiration(self.user_cred)
31         
32         slice_creds = account_config.get('delegated_slice_credentials',None)
33         for slice_name, slice_cred in slice_creds.iteritems():
34             self.slice_cred_expiration = get_expiration(self.slice_cred)
35
36         self.auth_cred = account_config.get('delegated_authority_credentials',None)
37         self.auth_cred_expiration = get_expiration(self.auth_cred)
38
39 def get_expiration (credential, format = 'UTC'):
40     exp_date = re.search('<expires>(.*)</expires>', credential)
41     if exp_date:
42         exp_date = exp_date.group(1)
43         if format == 'timestamp':
44             exp_date = calendar.timegm(dateutil.parser.parse(exp_date).utctimetuple())
45     else:
46         exp_date = None
47     return exp_date
48