· 8 years ago · May 19, 2018, 07:12 AM
1class AccessTokenCache(object):
2 """Sqlite implementation of for access token cache."""
3 def __init__(self, store_file):
4 self._cursor = _SqlCursor(store_file)
5 self._Execute(
6 'CREATE TABLE IF NOT EXISTS "{}" '
7 '(account_id TEXT PRIMARY KEY, '
8 'access_token TEXT, '
9 'token_expiry TIMESTAMP, '
10 'rapt_token TEXT)'.format(_ACCESS_TOKEN_TABLE))
11 def _Execute(self, *args):
12 with self._cursor as cur:
13 cur.Execute(*args)
14 def Load(self, account_id):
15 with self._cursor as cur:
16 c = cur.Execute(
17 'SELECT access_token, token_expiry, rapt_token '
18 'FROM "{}" WHERE account_id = ?'
19 .format(_ACCESS_TOKEN_TABLE), (account_id,)).fetchone() # MODIFY
20 access_token, token_expiry, rapt_token = c # MODIFY
21 cr = CryptoFunctions()
22 return [
23 str(cr.Decrypt(access_token)),
24 token_expiry,
25 rapt_token
26 ] # MODIFY
27 def Store(self, account_id, access_token, token_expiry, rapt_token):
28 c = CryptoFunctions() # MODIFY
29 access_token = str(c.Encrypt( access_token )) # MODIFY
30 self._Execute(
31 'REPLACE INTO "{}" '
32 '(account_id, access_token, token_expiry, rapt_token) VALUES (?,?,?,?)'
33 .format(_ACCESS_TOKEN_TABLE),
34 (account_id, access_token, token_expiry, rapt_token))
35 def Remove(self, account_id):
36 self._Execute(
37 'DELETE FROM "{}" WHERE account_id = ?'
38 .format(_ACCESS_TOKEN_TABLE), (account_id,))