allow user to reset api key

This commit is contained in:
spitkov 2024-09-16 14:41:26 +02:00
parent de1b826928
commit c3a8994769
3 changed files with 152 additions and 5 deletions

14
app.py
View File

@ -1222,6 +1222,20 @@ def is_valid_password(password):
banned_passwords = ['info', 'download'] banned_passwords = ['info', 'download']
return password not in banned_passwords return password not in banned_passwords
@app.route('/reset_api_key', methods=['POST'])
@login_required
def reset_api_key():
new_api_key = secrets.token_urlsafe(32)
db = get_db()
cursor = db.cursor()
cursor.execute("UPDATE users SET api_key = ? WHERE id = ?", (new_api_key, current_user.id))
db.commit()
return jsonify({'success': True, 'new_api_key': new_api_key})
@app.route('/api/docs')
def api_docs():
return render_template('api_docs.html')
if __name__ == '__main__': if __name__ == '__main__':
# Start the cleanup thread # Start the cleanup thread
cleanup_thread = threading.Thread(target=delete_old_files) cleanup_thread = threading.Thread(target=delete_old_files)

62
templates/api_docs.html Normal file
View File

@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API Documentation - sxbin</title>
<style>
/* Add your styles here */
</style>
</head>
<body>
<h1>sxbin API Documentation</h1>
<h2>Authentication</h2>
<p>All API requests require an API key to be sent in the X-API-Key header.</p>
<h2>Endpoints</h2>
<h3>Upload File</h3>
<pre>
POST /api/upload/file
Headers:
X-API-Key: your_api_key
Body: multipart/form-data
file: (binary)
</pre>
<p>Returns: JSON with file URL and deletion URL</p>
<h3>Upload Pastebin</h3>
<pre>
POST /api/upload/pastebin
Headers:
X-API-Key: your_api_key
Content-Type: application/json
Body:
{
"content": "Your pastebin content here"
}
</pre>
<p>Returns: JSON with pastebin URL and deletion URL</p>
<h3>Shorten URL</h3>
<pre>
POST /api/shorten
Headers:
X-API-Key: your_api_key
Content-Type: application/json
Body:
{
"url": "https://example.com/your-long-url-here"
}
</pre>
<p>Returns: JSON with shortened URL and deletion URL</p>
<h2>Error Handling</h2>
<p>All errors are returned as JSON with an "error" field describing the issue.</p>
<footer>
<p>For more information or support, please contact our team.</p>
</footer>
</body>
</html>

View File

@ -881,7 +881,7 @@
<div class="tabs"> <div class="tabs">
<button class="tab active" onclick="openTab(event, 'filesAndFolders')">Files and Folders</button> <button class="tab active" onclick="openTab(event, 'filesAndFolders')">Files and Folders</button>
<button class="tab" onclick="openTab(event, 'myUploads')">My Uploads</button> <button class="tab" onclick="openTab(event, 'myUploads')">My Uploads</button>
<button class="tab" onclick="openTab(event, 'shareXConfig')">ShareX Config</button> <button class="tab" onclick="openTab(event, 'apiKeyShareX')">API Key & ShareX</button>
</div> </div>
<div id="filesAndFolders" class="tab-content active"> <div id="filesAndFolders" class="tab-content active">
@ -985,10 +985,33 @@
</div> </div>
</div> </div>
<div id="shareXConfig" class="tab-content"> <div id="apiKeyShareX" class="tab-content">
<h3>ShareX Configuration</h3> <h3>API Key & ShareX</h3>
<p>Click the button below to download your ShareX configuration file:</p>
<a href="{{ url_for('generate_sharex_config') }}" class="btn" download="aCloud_ShareX.sxcu">Download ShareX Config</a> <div class="sharex-config-section">
<h4>ShareX Configuration</h4>
<p>Download the ShareX configuration file to easily integrate with our service:</p>
<a href="{{ url_for('generate_sharex_config') }}" class="btn">Download ShareX Config</a>
</div>
<div class="api-key-section">
<h4>Your API Key</h4>
<p>API Key: <span id="apiKey" class="blurred">{{ current_user.api_key }}</span></p>
<button onclick="showResetApiKeyModal()" class="btn">Reset API Key</button>
</div>
</div>
<!-- Reset API Key Modal -->
<div id="resetApiKeyModal" class="modal">
<div class="modal-content">
<h2>Reset API Key</h2>
<p>Are you sure you want to reset your API key? This action cannot be undone.</p>
<p>Your old ShareX configuration will no longer work. You'll need to generate a new one after resetting.</p>
<div class="modal-buttons">
<button onclick="resetApiKey()" class="btn btn-danger">Reset API Key</button>
<button onclick="closeResetApiKeyModal()" class="btn">Cancel</button>
</div>
</div>
</div> </div>
</div> </div>
@ -1589,6 +1612,22 @@
.file-group-link a:hover { .file-group-link a:hover {
text-decoration: underline; text-decoration: underline;
} }
.blurred {
filter: blur(5px);
transition: filter 0.3s ease;
}
.blurred:hover {
filter: blur(0);
}
.api-key-section, .sharex-config-section {
margin-bottom: 20px;
padding: 15px;
background-color: var(--highlight-bg);
border-radius: 5px;
}
</style> </style>
<script> <script>
@ -2188,6 +2227,38 @@
}); });
}); });
}); });
function showResetApiKeyModal() {
document.getElementById('resetApiKeyModal').style.display = 'block';
}
function closeResetApiKeyModal() {
document.getElementById('resetApiKeyModal').style.display = 'none';
}
function resetApiKey() {
fetch('/reset_api_key', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})
.then(response => response.json())
.then(data => {
if (data.success) {
document.getElementById('apiKey').textContent = data.new_api_key;
alert('API Key has been reset. Please update your applications and download a new ShareX configuration.');
} else {
alert('Error resetting API Key: ' + data.error);
}
closeResetApiKeyModal();
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while resetting the API Key');
closeResetApiKeyModal();
});
}
</script> </script>
</body> </body>
</html> </html>