139 lines
3.5 KiB
Python
139 lines
3.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Optional
|
|
|
|
from qgis.core import QgsApplication, QgsAuthMethodConfig, QgsSettings
|
|
|
|
from . import config
|
|
from .i18n import tr
|
|
|
|
CONFIG_NAME = "GeoDonkey API"
|
|
|
|
CONFIG_METHOD = "APIHeader"
|
|
|
|
SETTINGS_KEY = config.SETTINGS_GROUP + "/authcfg"
|
|
|
|
_session_token = {"value": ""}
|
|
|
|
|
|
class AuthUnavailable(Exception):
|
|
pass
|
|
|
|
|
|
def _manager():
|
|
return QgsApplication.authManager()
|
|
|
|
|
|
def available() -> bool:
|
|
manager = _manager()
|
|
if manager is None:
|
|
return False
|
|
try:
|
|
if manager.isDisabled():
|
|
return False
|
|
except AttributeError:
|
|
return False
|
|
return True
|
|
|
|
|
|
def unlock() -> bool:
|
|
if not available():
|
|
return False
|
|
manager = _manager()
|
|
if manager.masterPasswordIsSet():
|
|
return True
|
|
return bool(manager.setMasterPassword(True))
|
|
|
|
|
|
def stored_id() -> str:
|
|
return str(QgsSettings().value(SETTINGS_KEY, "") or "")
|
|
|
|
|
|
def save_token(token: str) -> str:
|
|
token = (token or "").strip()
|
|
if not token:
|
|
raise ValueError("empty token")
|
|
if not unlock():
|
|
raise AuthUnavailable(
|
|
tr("The QGIS authentication database is not available. "
|
|
"The key will only be kept for this session."))
|
|
|
|
manager = _manager()
|
|
cfg = QgsAuthMethodConfig()
|
|
existing = stored_id()
|
|
if existing and existing in manager.configIds():
|
|
manager.loadAuthenticationConfig(existing, cfg, True)
|
|
cfg.setName(CONFIG_NAME)
|
|
cfg.setMethod(CONFIG_METHOD)
|
|
cfg.setConfig("Authorization", "Bearer " + token)
|
|
cfg.setConfig("token", token)
|
|
|
|
if existing and existing in manager.configIds():
|
|
cfg.setId(existing)
|
|
if not manager.updateAuthenticationConfig(cfg):
|
|
raise AuthUnavailable(tr("The key could not be stored."))
|
|
config_id = existing
|
|
else:
|
|
if not manager.storeAuthenticationConfig(cfg):
|
|
raise AuthUnavailable(tr("The key could not be stored."))
|
|
config_id = cfg.id()
|
|
|
|
QgsSettings().setValue(SETTINGS_KEY, config_id)
|
|
_session_token["value"] = token
|
|
return config_id
|
|
|
|
|
|
def load_token() -> str:
|
|
if _session_token["value"]:
|
|
return _session_token["value"]
|
|
config_id = stored_id()
|
|
if not config_id or not available():
|
|
return ""
|
|
manager = _manager()
|
|
if config_id not in manager.configIds():
|
|
return ""
|
|
if not unlock():
|
|
return ""
|
|
cfg = QgsAuthMethodConfig()
|
|
if not manager.loadAuthenticationConfig(config_id, cfg, True):
|
|
return ""
|
|
token = cfg.config("token") or ""
|
|
if not token:
|
|
header = cfg.config("Authorization") or ""
|
|
token = header[7:] if header.startswith("Bearer ") else header
|
|
_session_token["value"] = token
|
|
return token
|
|
|
|
|
|
def remember_for_session(token: str) -> None:
|
|
_session_token["value"] = (token or "").strip()
|
|
|
|
|
|
def forget() -> None:
|
|
_session_token["value"] = ""
|
|
config_id = stored_id()
|
|
QgsSettings().remove(SETTINGS_KEY)
|
|
if config_id and available():
|
|
manager = _manager()
|
|
if config_id in manager.configIds() and unlock():
|
|
manager.removeAuthenticationConfig(config_id)
|
|
|
|
|
|
def looks_like_token(token: str) -> bool:
|
|
token = (token or "").strip()
|
|
return token.startswith(config.TOKEN_PREFIXES) and len(token) > 16
|
|
|
|
|
|
def masked(token: str) -> str:
|
|
token = (token or "").strip()
|
|
if not token:
|
|
return ""
|
|
if len(token) <= 14:
|
|
return "…"
|
|
return token[:11] + "…" + token[-4:]
|
|
|
|
|
|
def optional_token(token: str) -> Optional[str]:
|
|
token = (token or "").strip()
|
|
return token or None
|