diff --git a/kort_client/api.py b/kort_client/api.py index 6cdda86e9146cd60363fa4c18131fb475296a593..f6b3cb6b55f5e780e99119ad93b2bc18c55c18e7 100644 --- a/kort_client/api.py +++ b/kort_client/api.py @@ -3,7 +3,7 @@ from urllib.parse import urljoin import click import requests as requests -from requests_oauthlib import OAuth2Session +from requests import request from .config import settings @@ -29,7 +29,7 @@ class KortAPI: def __init__(self): self.settings = settings - self.oauth = self._get_oauth_session() + self._validate_settings() def get_printer(self) -> dict[str, Any]: r = self._do_request("GET", "printer") @@ -79,25 +79,6 @@ class KortAPI: if not self.settings.access_token: raise ConfigurationException("No access_token set.") - def _get_oauth_session(self) -> OAuth2Session: - self._validate_settings() - - def token_saver(token): - self.settings.access_token = token - self.settings.save_settings() - - oauth = OAuth2Session( - self.settings.client_id, - token=self.settings.access_token, - auto_refresh_url=urljoin(self.settings.base_url, "oauth/token/"), - auto_refresh_kwargs=dict( - client_id=self.settings.client_id, - client_secret=self.settings.client_secret, - ), - token_updater=token_saver, - ) - return oauth - def _do_request( self, method: str, url: str, attrs: Optional[Sequence] = None, **kwargs ) -> dict[str, Any]: @@ -107,7 +88,12 @@ class KortAPI: url = url.format(*attrs) url = urljoin(settings.base_url, url) try: - r = self.oauth.request(method, url, **kwargs) + r = request( + method, + url, + auth=(self.settings.client_id, self.settings.client_secret), + **kwargs + ) if not r.ok: click.secho("Error {}: {}".format(r.status_code, r.text), fg="red") raise KortAPIException("Error {}: {}".format(r.status_code, r.text)) diff --git a/kort_client/cli.py b/kort_client/cli.py index aa54d661f29986aebeb645e21b46f0eae5c059a2..442d5a6d3771745ede2467701b915b461d1b71ce 100644 --- a/kort_client/cli.py +++ b/kort_client/cli.py @@ -1,8 +1,6 @@ import json -from urllib.parse import urljoin import click -from requests_oauthlib import OAuth2Session from kort_client.api import KortAPI from kort_client.run import PrintClient @@ -41,28 +39,6 @@ def setup(config_file): settings.client_secret = config["client_secret"] settings.base_url = config["base_url"] - scope = ["card_printer"] - - oauth = OAuth2Session( - settings.client_id, scope=scope, redirect_uri="urn:ietf:wg:oauth:2.0:oob" - ) - authorization_url, state = oauth.authorization_url( - urljoin(settings.base_url, "oauth/authorize") - ) - - click.echo("Please go to {} and authorize access".format(authorization_url)) - code = click.prompt("Paste the authorization code here", type=str) - - token = oauth.fetch_token( - urljoin(settings.base_url, "oauth/token/"), - code=code, - include_client_id=True, - client_secret=settings.client_secret, - ) - - click.echo(token) - settings.access_token = token - settings.save_settings() api = KortAPI() @@ -71,6 +47,8 @@ def setup(config_file): api.set_printer_status(printer_id, "offline", "Newly registered printer") + click.echo("Successfully registered printer") + @cli.command() def config(): diff --git a/kort_client/config.py b/kort_client/config.py index 1e02c8e473368c1a2eab3a8d1609c6fc9ea3bcf8..84de541be2a7350faddb32c29d86fb68df1f8576 100644 --- a/kort_client/config.py +++ b/kort_client/config.py @@ -4,5 +4,4 @@ settings = usersettings.Settings("org.aleksis.apps.kort.client") settings.add_setting("client_id", str, default="") settings.add_setting("client_secret", str, default="") settings.add_setting("base_url", str, default="") -settings.add_setting("access_token", dict, default={}) settings.load_settings() diff --git a/pyproject.toml b/pyproject.toml index ddf75d054d599563e371396eadf12c5efc3fd9b8..11263aedb09e8f23cf9ef9b934cacedddfae922a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,6 @@ python = "^3.9" click = "^8.0.4" usersettings = "^1.1.5" requests = "^2.27.1" -requests-oauthlib = "^1.3.1" pycups = "^2.0.1" usb-barcode-scanner-julz = "^0.2"