factored out authentication/token part

This commit is contained in:
2021-05-27 15:36:59 +02:00
parent d1f44d04c1
commit 9a4e9c359d
3 changed files with 52 additions and 76 deletions

View File

@ -1,24 +1,9 @@
import getpass
from .config import Config
from .utils import post_request, get_request
from .authclient import AuthClient, AUTH_HEADERS
from .autherror import AuthError
from .utils import post_request, get_request
AUTH_HEADERS = {
"Content-type": "application/json",
"Accept": "application/json"
}
class SciCat:
def __init__(self, address):
self.address = address.rstrip("/")
self._token = None
self.config = Config(".scicat-tokens")
def __repr__(self):
return f"SciCat @ {self.address}"
class SciCat(AuthClient):
@property
def proposals(self):
@ -26,26 +11,6 @@ class SciCat:
headers = self.auth_headers
return get_request(url, headers=headers)
@property
def auth_headers(self):
headers = AUTH_HEADERS.copy()
headers["Authorization"] = self.token
return headers
@property
def token(self):
username = getpass.getuser()
password = getpass.getpass(prompt=f"SciCat password for {username}: ")
token = self._token
if token is None:
try:
token = self.config[username]
except KeyError:
token = self.authenticate(username, password)
self.config[username] = self._token = token
return token
def authenticate(self, username, password):
url = self.address + "/users/login"
auth_payload = {