Login somewhat works, token file doesn't
This commit is contained in:
parent
94e203d1ba
commit
818333de54
|
@ -2,3 +2,4 @@ python34/
|
|||
junk/
|
||||
__pycache__/
|
||||
config.toml
|
||||
.qcstoken
|
|
@ -43,11 +43,11 @@ class ApiContext:
|
|||
if response.status_code == 200:
|
||||
return response.json()
|
||||
elif response.status_code == 400:
|
||||
raise BadRequestError("Bad request: %s" % response.content)
|
||||
raise BadRequestError("Bad request: %s" % response.text)
|
||||
elif response.status_code == 401:
|
||||
raise AuthenticationError("Your token is bad!")
|
||||
elif response.status_code == 404:
|
||||
raise NotFoundError("Could not find content!")
|
||||
raise NotFoundError("Not found: %s" % response.text)
|
||||
else:
|
||||
raise Exception("Unknown error (%s) - %s" % (response.status_code, response.content))
|
||||
|
||||
|
@ -65,10 +65,13 @@ class ApiContext:
|
|||
response = requests.post(url, headers = self.gen_header(), json = data)
|
||||
return self.parse_response(response)
|
||||
|
||||
# Connect to the API to determine if your token is still valid.
|
||||
def is_token_valid(self):
|
||||
# Connect to the API to determine if your token is still valid. Or, if you pass a token,
|
||||
# check if only the given token is valid
|
||||
def is_token_valid(self, token = False):
|
||||
if not token:
|
||||
token = self.token
|
||||
try:
|
||||
return self.token and self.get("user/me")
|
||||
return token and self.get("user/me")
|
||||
except Exception as ex:
|
||||
self.logger.debug("Error from endpoint: %s" % ex)
|
||||
return False
|
||||
|
|
11
main.py
11
main.py
|
@ -56,15 +56,18 @@ def authenticate(config, context: contentapi.ApiContext):
|
|||
if os.path.isfile(config["tokenfile"]):
|
||||
with open(config["tokenfile"], 'r') as f:
|
||||
token = f.read()
|
||||
if context.is_token_valid():
|
||||
logging.debug("Token from file: " + token)
|
||||
if context.is_token_valid(token):
|
||||
context.token = token
|
||||
logging.info("Logged in using token file " + config["tokenfile"])
|
||||
return
|
||||
else:
|
||||
message = "Token file expired"
|
||||
|
||||
message += ", Please enter login for " + config["api"]
|
||||
|
||||
while True:
|
||||
print(message + ", Please enter login for " + config["api"])
|
||||
print(message)
|
||||
username = input("Username: ")
|
||||
password = getpass.getpass("Password: ")
|
||||
try:
|
||||
|
@ -73,8 +76,10 @@ def authenticate(config, context: contentapi.ApiContext):
|
|||
f.write(token)
|
||||
logging.info("Token accepted, written to " + config["tokenfile"])
|
||||
context.token = token
|
||||
return
|
||||
except Exception as ex:
|
||||
print("ERROR: Could not login: %s" % ex)
|
||||
print("ERROR: %s" % ex)
|
||||
message = "Please try logging in again:"
|
||||
|
||||
|
||||
# Because python reasons
|
||||
|
|
Loading…
Reference in New Issue