|
| 1 | +''' |
| 2 | +Created on Aug 1, 2016 |
| 3 | +
|
| 4 | +A very basic KBase auth client for the Python server. |
| 5 | +
|
| 6 | +@author: gaprice@lbl.gov |
| 7 | +''' |
| 8 | +import time as _time |
| 9 | +import requests as _requests |
| 10 | +import threading as _threading |
| 11 | +import hashlib |
| 12 | + |
| 13 | + |
| 14 | +class TokenCache(object): |
| 15 | + ''' A basic cache for tokens. ''' |
| 16 | + |
| 17 | + _MAX_TIME_SEC = 5 * 60 # 5 min |
| 18 | + |
| 19 | + _lock = _threading.RLock() |
| 20 | + |
| 21 | + def __init__(self, maxsize=2000): |
| 22 | + self._cache = {} |
| 23 | + self._maxsize = maxsize |
| 24 | + self._halfmax = maxsize / 2 # int division to round down |
| 25 | + |
| 26 | + def get_user(self, token): |
| 27 | + token = hashlib.sha256(token.encode('utf-8')).hexdigest() |
| 28 | + with self._lock: |
| 29 | + usertime = self._cache.get(token) |
| 30 | + if not usertime: |
| 31 | + return None |
| 32 | + |
| 33 | + user, intime = usertime |
| 34 | + if _time.time() - intime > self._MAX_TIME_SEC: |
| 35 | + return None |
| 36 | + return user |
| 37 | + |
| 38 | + def add_valid_token(self, token, user): |
| 39 | + if not token: |
| 40 | + raise ValueError('Must supply token') |
| 41 | + if not user: |
| 42 | + raise ValueError('Must supply user') |
| 43 | + token = hashlib.sha256(token.encode('utf-8')).hexdigest() |
| 44 | + with self._lock: |
| 45 | + self._cache[token] = [user, _time.time()] |
| 46 | + if len(self._cache) > self._maxsize: |
| 47 | + sorted_items = sorted( |
| 48 | + list(self._cache.items()), |
| 49 | + key=(lambda v: v[1][1]) |
| 50 | + ) |
| 51 | + for i, (t, _) in enumerate(sorted_items): |
| 52 | + if i <= self._halfmax: |
| 53 | + del self._cache[t] |
| 54 | + else: |
| 55 | + break |
| 56 | + |
| 57 | + |
| 58 | +class KBaseAuth(object): |
| 59 | + ''' |
| 60 | + A very basic KBase auth client for the Python server. |
| 61 | + ''' |
| 62 | + |
| 63 | + _LOGIN_URL = 'https://kbase.us/services/auth/api/legacy/KBase/Sessions/Login' |
| 64 | + |
| 65 | + def __init__(self, auth_url=None): |
| 66 | + ''' |
| 67 | + Constructor |
| 68 | + ''' |
| 69 | + self._authurl = auth_url |
| 70 | + if not self._authurl: |
| 71 | + self._authurl = self._LOGIN_URL |
| 72 | + self._cache = TokenCache() |
| 73 | + |
| 74 | + def get_user(self, token): |
| 75 | + if not token: |
| 76 | + raise ValueError('Must supply token') |
| 77 | + user = self._cache.get_user(token) |
| 78 | + if user: |
| 79 | + return user |
| 80 | + |
| 81 | + d = {'token': token, 'fields': 'user_id'} |
| 82 | + ret = _requests.post(self._authurl, data=d) |
| 83 | + if not ret.ok: |
| 84 | + try: |
| 85 | + err = ret.json() |
| 86 | + except Exception as e: |
| 87 | + ret.raise_for_status() |
| 88 | + raise ValueError('Error connecting to auth service: {} {}\n{}' |
| 89 | + .format(ret.status_code, ret.reason, |
| 90 | + err['error']['message'])) |
| 91 | + |
| 92 | + user = ret.json()['user_id'] |
| 93 | + self._cache.add_valid_token(token, user) |
| 94 | + return user |
0 commit comments