factored out authentication/token part
This commit is contained in:
46
scilog/authclient.py
Normal file
46
scilog/authclient.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import getpass
|
||||||
|
from .config import Config
|
||||||
|
from .utils import typename
|
||||||
|
|
||||||
|
|
||||||
|
AUTH_HEADERS = {
|
||||||
|
"Content-type": "application/json",
|
||||||
|
"Accept": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class AuthClient:
|
||||||
|
|
||||||
|
def __init__(self, address):
|
||||||
|
self.address = address.rstrip("/")
|
||||||
|
self._token = None
|
||||||
|
tn = typename(self).lower()
|
||||||
|
self.config = Config(f".{tn}-tokens")
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
tn = typename(self)
|
||||||
|
return f"{tn} @ {self.address}"
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def auth_headers(self):
|
||||||
|
headers = AUTH_HEADERS.copy()
|
||||||
|
headers["Authorization"] = self.token
|
||||||
|
return headers
|
||||||
|
|
||||||
|
@property
|
||||||
|
def token(self):
|
||||||
|
username = getpass.getuser()
|
||||||
|
token = self._token
|
||||||
|
if token is None:
|
||||||
|
try:
|
||||||
|
token = self.config[username]
|
||||||
|
except KeyError:
|
||||||
|
tn = typename(self)
|
||||||
|
password = getpass.getpass(prompt=f"{tn} password for {username}: ")
|
||||||
|
token = self.authenticate(username, password)
|
||||||
|
self.config[username] = self._token = token
|
||||||
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,24 +1,9 @@
|
|||||||
import getpass
|
from .authclient import AuthClient, AUTH_HEADERS
|
||||||
from .config import Config
|
|
||||||
from .utils import post_request, get_request
|
|
||||||
from .autherror import AuthError
|
from .autherror import AuthError
|
||||||
|
from .utils import post_request, get_request
|
||||||
|
|
||||||
|
|
||||||
AUTH_HEADERS = {
|
class SciCat(AuthClient):
|
||||||
"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}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def proposals(self):
|
def proposals(self):
|
||||||
@ -26,26 +11,6 @@ class SciCat:
|
|||||||
headers = self.auth_headers
|
headers = self.auth_headers
|
||||||
return get_request(url, headers=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):
|
def authenticate(self, username, password):
|
||||||
url = self.address + "/users/login"
|
url = self.address + "/users/login"
|
||||||
auth_payload = {
|
auth_payload = {
|
||||||
|
@ -1,26 +1,10 @@
|
|||||||
import getpass
|
from .authclient import AuthClient, AUTH_HEADERS
|
||||||
from .config import Config
|
|
||||||
from .utils import post_request, get_request
|
|
||||||
from .autherror import AuthError
|
from .autherror import AuthError
|
||||||
|
from .utils import post_request, get_request
|
||||||
from .mkfilt import make_filter
|
from .mkfilt import make_filter
|
||||||
|
|
||||||
|
|
||||||
AUTH_HEADERS = {
|
class SciLog(AuthClient):
|
||||||
"Content-type": "application/json",
|
|
||||||
"Accept": "application/json"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class SciLog:
|
|
||||||
|
|
||||||
def __init__(self, address):
|
|
||||||
self.address = address.rstrip("/")
|
|
||||||
self._token = None
|
|
||||||
self.config = Config(".scilog-tokens")
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return f"SciLog @ {self.address}"
|
|
||||||
|
|
||||||
|
|
||||||
def get_snippets(self, **kwargs):
|
def get_snippets(self, **kwargs):
|
||||||
url = self.address + "/basesnippets"
|
url = self.address + "/basesnippets"
|
||||||
@ -35,25 +19,6 @@ class SciLog:
|
|||||||
return post_request(url, payload=payload, headers=headers)
|
return post_request(url, payload=payload, headers=headers)
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def auth_headers(self):
|
|
||||||
headers = AUTH_HEADERS.copy()
|
|
||||||
headers["Authorization"] = self.token
|
|
||||||
return headers
|
|
||||||
|
|
||||||
@property
|
|
||||||
def token(self):
|
|
||||||
username = getpass.getuser()
|
|
||||||
token = self._token
|
|
||||||
if token is None:
|
|
||||||
try:
|
|
||||||
token = self.config[username]
|
|
||||||
except KeyError:
|
|
||||||
password = getpass.getpass(prompt=f"SciLog password for {username}: ")
|
|
||||||
token = self.authenticate(username, password)
|
|
||||||
self.config[username] = self._token = token
|
|
||||||
return token
|
|
||||||
|
|
||||||
def authenticate(self, username, password):
|
def authenticate(self, username, password):
|
||||||
url = self.address + "/users/login"
|
url = self.address + "/users/login"
|
||||||
auth_payload = {
|
auth_payload = {
|
||||||
|
Reference in New Issue
Block a user