Skip to content
Snippets Groups Projects
Verified Commit 77233a2b authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Change authentication method to client credentials

parent 61401a1a
No related branches found
No related tags found
No related merge requests found
......@@ -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))
......
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():
......
......@@ -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()
......@@ -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"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment