tools: make auth optional for public routes (#22386)

* tools: make auth optional for public routes

* handle 403

* show warning from c++ replay
pull/22391/head
Willem Melching 2021-09-30 20:13:46 +02:00 committed by GitHub
parent 06944165d0
commit a31c6ce3f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 7 deletions

View File

@ -92,7 +92,10 @@ void HttpRequest::sendRequest(const QString &requestURL, const HttpRequest::Meth
QNetworkRequest request;
request.setUrl(QUrl(requestURL));
request.setRawHeader(QByteArray("Authorization"), ("JWT " + token).toUtf8());
if (!token.isEmpty()) {
request.setRawHeader(QByteArray("Authorization"), ("JWT " + token).toUtf8());
}
if (method == HttpRequest::Method::GET) {
reply = networkAccessManager->get(request);
@ -119,8 +122,11 @@ void HttpRequest::requestFinished() {
success = true;
emit receivedResponse(response);
} else {
qDebug() << reply->errorString();
emit failedResponse(reply->errorString());
if (reply->error() == QNetworkReply::ContentAccessDenied || reply->error() == QNetworkReply::AuthenticationRequiredError) {
qWarning() << ">> Unauthorized. Authenticate with tools/lib/auth.py <<";
}
}
} else {
networkAccessManager->clearAccessCache();

View File

@ -1,6 +1,5 @@
import os
import requests
from tools.lib.auth_config import clear_token
API_HOST = os.getenv('API_HOST', 'https://api.commadotai.com')
class CommaApi():
@ -14,8 +13,7 @@ class CommaApi():
resp = self.session.request(method, API_HOST + '/' + endpoint, **kwargs)
resp_json = resp.json()
if isinstance(resp_json, dict) and resp_json.get('error'):
if resp.status_code == 401:
clear_token()
if resp.status_code in [401, 403]:
raise UnauthorizedError('Unauthorized. Authenticate with tools/lib/auth.py')
e = APIError(str(resp.status_code) + ":" + resp_json.get('description', str(resp_json['error'])))

View File

@ -21,8 +21,8 @@ def get_token():
with open(os.path.join(CONFIG_DIR, 'auth.json')) as f:
auth = json.load(f)
return auth['access_token']
except Exception as e:
raise MissingAuthConfigError('Authenticate with tools/lib/auth.py') from e
except Exception:
return None
def set_token(token):