2023-06-08 19:12:28 +02:00
|
|
|
"""
|
2023-06-16 22:51:47 +02:00
|
|
|
=========================================================================================
|
|
|
|
|
2023-06-08 19:12:28 +02:00
|
|
|
Copyright © 2023 Alexandre Racine <https://alex-racine.ch>
|
|
|
|
|
|
|
|
This file is part of Inopy.
|
|
|
|
|
|
|
|
Inopy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
Inopy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with Inopy. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2023-06-16 22:51:47 +02:00
|
|
|
=========================================================================================
|
2023-06-08 19:12:28 +02:00
|
|
|
|
|
|
|
DISCLAIMER: parts of this code and comments blocks were created
|
|
|
|
with the help of ChatGPT developped by OpenAI <https://openai.com/>
|
|
|
|
Followed by human reviewing, refactoring and fine-tuning.
|
|
|
|
|
2023-06-16 22:51:47 +02:00
|
|
|
=========================================================================================
|
2023-06-08 19:12:28 +02:00
|
|
|
|
2023-06-16 22:51:47 +02:00
|
|
|
The aim of this module is to refresh both OAuth 2.0 access and refresh tokens and update the configuration file with the refreshed tokens.
|
2023-06-08 19:12:28 +02:00
|
|
|
|
2023-06-16 22:51:47 +02:00
|
|
|
=========================================================================================
|
2023-06-08 19:12:28 +02:00
|
|
|
"""
|
|
|
|
|
2023-06-16 22:51:47 +02:00
|
|
|
'''
|
|
|
|
=====================================================
|
|
|
|
Create a refresh function to refresh the bearer
|
|
|
|
and refresh token in the config data
|
|
|
|
|
|
|
|
* Set the headers and prepare the payload
|
|
|
|
data for the HTTP request
|
|
|
|
|
|
|
|
* Send a POST request to the specified
|
|
|
|
endpoint with the payload and headers
|
|
|
|
|
|
|
|
* Check the response status code to determine
|
|
|
|
if the request was successful
|
|
|
|
|
|
|
|
* Parse the response data as JSON and extract
|
|
|
|
the refreshed bearer token and new refresh
|
|
|
|
token from the response data
|
|
|
|
|
|
|
|
* Open the config file. Load the existing
|
|
|
|
config data from the file and update the
|
|
|
|
bearer token and refresh token in the config
|
|
|
|
data
|
|
|
|
|
|
|
|
* Move the file pointer to the beginning of
|
|
|
|
the file, write the updated config data back
|
|
|
|
to the file, overwriting the existing content
|
|
|
|
|
|
|
|
* Truncate the file to remove any remaining
|
|
|
|
content after the updated data
|
|
|
|
=====================================================
|
|
|
|
'''
|
|
|
|
|
2023-06-08 19:12:28 +02:00
|
|
|
import requests
|
|
|
|
import json
|
2023-06-30 19:46:35 +02:00
|
|
|
import logging
|
|
|
|
from logs import LogFile
|
|
|
|
|
|
|
|
# Set logs file
|
|
|
|
log_file = LogFile()
|
2023-06-08 19:12:28 +02:00
|
|
|
|
2023-06-10 17:55:50 +02:00
|
|
|
def refresh(config_path, endpoint, client_id, client_secret, refresh_token):
|
|
|
|
headers = {"Content-type": "application/x-www-form-urlencoded"}
|
2023-06-16 22:51:47 +02:00
|
|
|
|
2023-06-10 17:55:50 +02:00
|
|
|
payload = {
|
|
|
|
"client_id": client_id,
|
|
|
|
"client_secret": client_secret,
|
|
|
|
"grant_type": "refresh_token",
|
|
|
|
"refresh_token": refresh_token
|
|
|
|
}
|
2023-06-08 19:12:28 +02:00
|
|
|
|
2023-06-10 17:55:50 +02:00
|
|
|
response = requests.post(endpoint, data=payload, headers=headers)
|
2023-06-16 22:51:47 +02:00
|
|
|
|
2023-06-08 19:12:28 +02:00
|
|
|
if response.status_code == 200:
|
2023-06-30 19:46:35 +02:00
|
|
|
logging.info("Request was successful. Token was refreshed...")
|
2023-06-16 22:51:47 +02:00
|
|
|
|
2023-06-08 19:12:28 +02:00
|
|
|
else:
|
2023-06-30 19:46:35 +02:00
|
|
|
logging.debug(f'Request failed with status code: {response.status_code}')
|
2023-06-16 22:51:47 +02:00
|
|
|
|
2023-06-08 19:12:28 +02:00
|
|
|
data = json.loads(response.text)
|
|
|
|
|
|
|
|
refreshed_bearer = data['access_token']
|
|
|
|
new_refresh_token = data['refresh_token']
|
|
|
|
|
2023-06-10 17:55:50 +02:00
|
|
|
with open(config_path, 'r+') as config_file:
|
|
|
|
config = json.load(config_file)
|
|
|
|
|
|
|
|
config['oauth']['bearer'] = refreshed_bearer
|
|
|
|
config['oauth']['refresh_token'] = new_refresh_token
|
|
|
|
|
|
|
|
config_file.seek(0)
|
|
|
|
json.dump(config, config_file, indent=4)
|
|
|
|
config_file.truncate()
|