-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Boris
committed
Apr 24, 2024
1 parent
df6b793
commit bafa704
Showing
48 changed files
with
1,366 additions
and
1,732 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,4 +104,4 @@ data/ | |
.DS_Store | ||
.virtualenvs/ | ||
test.env | ||
run_tests_single.sh | ||
run_tests_single.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,4 +75,4 @@ | |
"host": "localhost" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
"files.autoSave": "afterDelay", | ||
"files.autoSaveDelay": 500, | ||
"python.pythonPath": "python3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1126,4 +1126,4 @@ | |
] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
''' | ||
""" | ||
Created on Aug 1, 2016 | ||
A very basic KBase auth client for the Python server. | ||
@author: [email protected] | ||
''' | ||
""" | ||
import hashlib | ||
import threading as _threading | ||
import time as _time | ||
|
||
import requests as _requests | ||
import threading as _threading | ||
import hashlib | ||
|
||
|
||
class TokenCache(object): | ||
''' A basic cache for tokens. ''' | ||
"""A basic cache for tokens.""" | ||
|
||
_MAX_TIME_SEC = 5 * 60 # 5 min | ||
|
||
|
@@ -24,7 +25,7 @@ def __init__(self, maxsize=2000): | |
self._halfmax = maxsize / 2 # int division to round down | ||
|
||
def get_user(self, token): | ||
token = hashlib.sha256(token.encode('utf-8')).hexdigest() | ||
token = hashlib.sha256(token.encode("utf-8")).hexdigest() | ||
with self._lock: | ||
usertime = self._cache.get(token) | ||
if not usertime: | ||
|
@@ -37,17 +38,14 @@ def get_user(self, token): | |
|
||
def add_valid_token(self, token, user): | ||
if not token: | ||
raise ValueError('Must supply token') | ||
raise ValueError("Must supply token") | ||
if not user: | ||
raise ValueError('Must supply user') | ||
token = hashlib.sha256(token.encode('utf-8')).hexdigest() | ||
raise ValueError("Must supply user") | ||
token = hashlib.sha256(token.encode("utf-8")).hexdigest() | ||
with self._lock: | ||
self._cache[token] = [user, _time.time()] | ||
if len(self._cache) > self._maxsize: | ||
sorted_items = sorted( | ||
list(self._cache.items()), | ||
key=(lambda v: v[1][1]) | ||
) | ||
sorted_items = sorted(list(self._cache.items()), key=(lambda v: v[1][1])) | ||
for i, (t, _) in enumerate(sorted_items): | ||
if i <= self._halfmax: | ||
del self._cache[t] | ||
|
@@ -56,39 +54,37 @@ def add_valid_token(self, token, user): | |
|
||
|
||
class KBaseAuth(object): | ||
''' | ||
""" | ||
A very basic KBase auth client for the Python server. | ||
''' | ||
""" | ||
|
||
_LOGIN_URL = 'https://kbase.us/services/auth/api/legacy/KBase/Sessions/Login' | ||
_LOGIN_URL = "https://kbase.us/services/auth/api/legacy/KBase/Sessions/Login" | ||
|
||
def __init__(self, auth_url=None): | ||
''' | ||
""" | ||
Constructor | ||
''' | ||
""" | ||
self._authurl = auth_url | ||
if not self._authurl: | ||
self._authurl = self._LOGIN_URL | ||
self._cache = TokenCache() | ||
|
||
def get_user(self, token): | ||
if not token: | ||
raise ValueError('Must supply token') | ||
raise ValueError("Must supply token") | ||
user = self._cache.get_user(token) | ||
if user: | ||
return user | ||
|
||
d = {'token': token, 'fields': 'user_id'} | ||
d = {"token": token, "fields": "user_id"} | ||
ret = _requests.post(self._authurl, data=d) | ||
if not ret.ok: | ||
try: | ||
err = ret.json() | ||
except Exception as e: | ||
except Exception: | ||
ret.raise_for_status() | ||
raise ValueError('Error connecting to auth service: {} {}\n{}' | ||
.format(ret.status_code, ret.reason, | ||
err['error']['message'])) | ||
raise ValueError("Error connecting to auth service: {} {}\n{}".format(ret.status_code, ret.reason, err["error"]["message"])) | ||
|
||
user = ret.json()['user_id'] | ||
user = ret.json()["user_id"] | ||
self._cache.add_valid_token(token, user) | ||
return user |
Oops, something went wrong.