Skip to content

Commit

Permalink
Check for version compatibility on login
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorjerse committed Nov 22, 2023
1 parent ee27284 commit 2904240
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Changed
-------
- **BACKWARD INCOMPATIBLE:** Remove ``inherit_collection`` parameter in
``Data.duplicate()`` and ``Sample.duplicate()``
- Minimal supported version is checked when connection to the server and
warning is printed on mismatch


===================
Expand Down
23 changes: 23 additions & 0 deletions src/resdk/resolwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import time
import webbrowser
from contextlib import suppress
from importlib.metadata import version as package_version
from typing import Optional, TypedDict
from urllib.parse import urlencode, urljoin, urlparse

import requests
import slumber
from packaging import version

from resdk.uploader import Uploader

Expand Down Expand Up @@ -48,6 +50,7 @@
DEFAULT_URL = "http://localhost:8000"
AUTOMATIC_LOGIN_POSTFIX = "saml-auth/api-login/"
INTERACTIVE_LOGIN_POSTFIX = "saml-auth/remote-login/"
MINIMAL_SUPPORTED_VERSION_POSTFIX = "api/resdk_minimal_supported_version"


class ResolweResource(slumber.Resource):
Expand Down Expand Up @@ -520,6 +523,9 @@ def __init__(
self.automatic_login_url = urljoin(self.url, AUTOMATIC_LOGIN_POSTFIX)
self.interactive_login_url = urljoin(self.url, INTERACTIVE_LOGIN_POSTFIX)

# Check minimal supported version.
self.version_check()

if not interactive and (username is None or password is None):
# Anonymous authentication
return
Expand All @@ -531,6 +537,23 @@ def __init__(
self.cookies = self.interactive_login()
self.logger.info("Successfully logged in.")

def version_check(self):
"""Check that the server is compatible with the client."""
url = urljoin(self.url, MINIMAL_SUPPORTED_VERSION_POSTFIX)
minimal_version = version.parse(
requests.get(url).json["minimal_supported_version"]
)
my_version = version.parse(package_version("resdk"))
if my_version < minimal_version:
message = (
f"Warning: your version of ReSDK ('{my_version}') is not compatible with "
f"the server: minimal required version is '{minimal_version}'. "
"To update the package run\n\n"
"pip install --upgrade resdk\n\n"
"from the command line."
)
print(message)

def automatic_login(self, username: str, password: str) -> AuthCookie:
"""Attempt to perform automatic SAML login.
Expand Down

0 comments on commit 2904240

Please sign in to comment.