Remove legacy login
parent
45e4f94741
commit
228535e042
|
@ -33,8 +33,6 @@ class Extension(mopidy.ext.Extension):
|
|||
schema["client_secret"] = mopidy.config.String(optional=True)
|
||||
schema["client_id"] = mopidy.config.String(optional=True)
|
||||
|
||||
schema["username"] = mopidy.config.String(optional=True)
|
||||
schema["password"] = mopidy.config.Secret(optional=True)
|
||||
schema["cache_duration"] = mopidy.config.Integer(optional=True)
|
||||
schema["verify_cert"] = mopidy.config.Boolean(optional=True)
|
||||
|
||||
|
@ -42,16 +40,6 @@ class Extension(mopidy.ext.Extension):
|
|||
|
||||
return schema
|
||||
|
||||
def validate_config(self, config):
|
||||
if not config.getboolean("funkwhale", "enabled"):
|
||||
return
|
||||
username = config.getstring("funkwhale", "username")
|
||||
password = config.getstring("funkwhale", "password")
|
||||
if any([username, password]) and not all([username, password]):
|
||||
raise mopidy.ext.ExtensionError(
|
||||
"You need to provide username and password to authenticate with the funkwhale backend"
|
||||
)
|
||||
|
||||
def setup(self, registry):
|
||||
from . import actor
|
||||
|
||||
|
|
|
@ -26,13 +26,6 @@ class FunkwhaleBackend(pykka.ThreadingActor, backend.Backend):
|
|||
def on_start(self):
|
||||
if self.config["funkwhale"]["client_id"]:
|
||||
logger.info('Using OAuth2 connection"')
|
||||
elif self.client.username is not None:
|
||||
self.client.login()
|
||||
logger.info(
|
||||
'Logged in to Funkwhale as "%s" on "%s"',
|
||||
self.client.username,
|
||||
self.config["funkwhale"]["url"],
|
||||
)
|
||||
else:
|
||||
logger.info('Using "%s" anonymously', self.config["funkwhale"]["url"])
|
||||
|
||||
|
|
|
@ -52,49 +52,27 @@ def get_requests_session(url, proxy_config, user_agent, base_cls, **kwargs):
|
|||
return session
|
||||
|
||||
|
||||
def login_legacy(session, username, password):
|
||||
if not username:
|
||||
return
|
||||
response = session.post("token/", {"username": username, "password": password})
|
||||
try:
|
||||
response.raise_for_status()
|
||||
except requests.exceptions.HTTPError:
|
||||
raise exceptions.BackendError("Authentication failed for user %s" % (username,))
|
||||
token = response.json()["token"]
|
||||
session.headers.update({"Authorization": "JWT %s" % (token,)})
|
||||
return token
|
||||
|
||||
|
||||
class APIClient(object):
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.jwt_token = None
|
||||
self.oauth_token = get_token(config)
|
||||
if self.use_oauth:
|
||||
self.session = get_requests_session(
|
||||
config["funkwhale"]["url"],
|
||||
proxy_config=config["proxy"],
|
||||
user_agent="%s/%s" % (Extension.dist_name, __version__),
|
||||
base_cls=OAuth2Session,
|
||||
client_id=self.config["funkwhale"]["client_id"],
|
||||
token=self.oauth_token,
|
||||
auto_refresh_url=config["funkwhale"]["url"]
|
||||
+ config["funkwhale"].get("token_endpoint")
|
||||
or "/api/v1/oauth/token/",
|
||||
auto_refresh_kwargs={
|
||||
"client_id": self.config["funkwhale"]["client_id"],
|
||||
"client_secret": self.config["funkwhale"]["client_secret"],
|
||||
},
|
||||
token_updater=self.refresh_token,
|
||||
)
|
||||
else:
|
||||
self.session = get_requests_session(
|
||||
config["funkwhale"]["url"],
|
||||
proxy_config=config["proxy"],
|
||||
user_agent="%s/%s" % (Extension.dist_name, __version__),
|
||||
base_cls=SessionWithUrlBase,
|
||||
)
|
||||
self.username = self.config["funkwhale"]["username"]
|
||||
|
||||
self.session = get_requests_session(
|
||||
config["funkwhale"]["url"],
|
||||
proxy_config=config["proxy"],
|
||||
user_agent="%s/%s" % (Extension.dist_name, __version__),
|
||||
base_cls=OAuth2Session,
|
||||
client_id=self.config["funkwhale"]["client_id"],
|
||||
token=self.oauth_token,
|
||||
auto_refresh_url=config["funkwhale"]["url"]
|
||||
+ config["funkwhale"].get("token_endpoint")
|
||||
or "/api/v1/oauth/token/",
|
||||
auto_refresh_kwargs={
|
||||
"client_id": self.config["funkwhale"]["client_id"],
|
||||
"client_secret": self.config["funkwhale"]["client_secret"],
|
||||
},
|
||||
token_updater=self.refresh_token,
|
||||
)
|
||||
self.session.verify = config["funkwhale"].get("verify_cert", True)
|
||||
|
||||
self.exclude_compilation_artists = self.config["funkwhale"].get("exclude_compilation_artists", True)
|
||||
|
@ -107,17 +85,6 @@ class APIClient(object):
|
|||
self.oauth_token = token
|
||||
set_token(token, self.config)
|
||||
|
||||
def login(self):
|
||||
self.username = self.config["funkwhale"]["username"]
|
||||
if self.username:
|
||||
self.jwt_token = login_legacy(
|
||||
self.session,
|
||||
self.config["funkwhale"]["username"],
|
||||
self.config["funkwhale"]["password"],
|
||||
)
|
||||
else:
|
||||
self.jwt_token = None
|
||||
|
||||
def search(self, query):
|
||||
response = self.session.get("search", params={"query": query})
|
||||
response.raise_for_status()
|
||||
|
@ -186,6 +153,9 @@ def get_token(config):
|
|||
|
||||
|
||||
def set_token(token_data, config):
|
||||
|
||||
|
||||
|
||||
import mopidy_funkwhale
|
||||
|
||||
data_dir = mopidy_funkwhale.Extension.get_data_dir(config)
|
||||
|
|
|
@ -14,13 +14,6 @@ client_secret =
|
|||
authorization_endpoint = /authorize
|
||||
token_endpoint = /api/v1/oauth/token/
|
||||
|
||||
## Settings below are for legacy password-based auth, you should use OAuth instead
|
||||
|
||||
# Username to use when authenticating (leave empty fo anonymous access)
|
||||
username =
|
||||
# Password to use when authenticating (leave empty fo anonymous access)
|
||||
password =
|
||||
|
||||
# duration of cache entries before they are removed, in seconds
|
||||
# 0 to cache forever, empty to disable cache
|
||||
cache_duration = 600
|
||||
|
@ -30,4 +23,4 @@ verify_cert = true
|
|||
|
||||
# Hide artists that only have track credits when browsing artists.
|
||||
# Set this to false to show all artists.
|
||||
exclude_compilation_artists = true
|
||||
exclude_compilation_artists = true
|
||||
|
|
Loading…
Reference in New Issue