- Add API Key Registration to tenant
This commit is contained in:
@@ -54,6 +54,8 @@ class Tenant(db.Model):
|
|||||||
license_end_date = db.Column(db.Date, nullable=True)
|
license_end_date = db.Column(db.Date, nullable=True)
|
||||||
allowed_monthly_interactions = db.Column(db.Integer, nullable=True)
|
allowed_monthly_interactions = db.Column(db.Integer, nullable=True)
|
||||||
encrypted_chat_api_key = db.Column(db.String(500), nullable=True)
|
encrypted_chat_api_key = db.Column(db.String(500), nullable=True)
|
||||||
|
encrypted_api_key = db.Column(db.String(500), nullable=True)
|
||||||
|
|
||||||
|
|
||||||
# Tuning enablers
|
# Tuning enablers
|
||||||
embed_tuning = db.Column(db.Boolean, nullable=True, default=False)
|
embed_tuning = db.Column(db.Boolean, nullable=True, default=False)
|
||||||
|
|||||||
@@ -62,12 +62,19 @@
|
|||||||
{{ render_included_field(field, disabled_fields=license_fields, include_fields=license_fields) }}
|
{{ render_included_field(field, disabled_fields=license_fields, include_fields=license_fields) }}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<!-- Register API Key Button -->
|
<!-- Register API Key Button -->
|
||||||
|
<button type="button" class="btn btn-primary" onclick="generateNewChatApiKey()">Register Chat API Key</button>
|
||||||
<button type="button" class="btn btn-primary" onclick="generateNewApiKey()">Register API Key</button>
|
<button type="button" class="btn btn-primary" onclick="generateNewApiKey()">Register API Key</button>
|
||||||
<!-- API Key Display Field -->
|
<!-- API Key Display Field -->
|
||||||
|
<div id="chat-api-key-field" style="display:none;">
|
||||||
|
<label for="chat-api-key">Chat API Key:</label>
|
||||||
|
<input type="text" id="chat-api-key" class="form-control" readonly>
|
||||||
|
<button type="button" id="copy-chat-button" class="btn btn-primary">Copy to Clipboard</button>
|
||||||
|
<p id="copy-chat-message" style="display:none;color:green;">Chat API key copied to clipboard</p>
|
||||||
|
</div>
|
||||||
<div id="api-key-field" style="display:none;">
|
<div id="api-key-field" style="display:none;">
|
||||||
<label for="api-key">API Key:</label>
|
<label for="api-key">API Key:</label>
|
||||||
<input type="text" id="api-key" class="form-control" readonly>
|
<input type="text" id="api-key" class="form-control" readonly>
|
||||||
<button type="button" id="copy-button" class="btn btn-primary">Copy to Clipboard</button>
|
<button type="button" id="copy-api-button" class="btn btn-primary">Copy to Clipboard</button>
|
||||||
<p id="copy-message" style="display:none;color:green;">API key copied to clipboard</p>
|
<p id="copy-message" style="display:none;color:green;">API key copied to clipboard</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -105,14 +112,25 @@
|
|||||||
|
|
||||||
{% block scripts %}
|
{% block scripts %}
|
||||||
<script>
|
<script>
|
||||||
|
// Function to generate a new Chat API Key
|
||||||
|
function generateNewChatApiKey() {
|
||||||
|
generateApiKey('/admin/user/generate_chat_api_key', '#chat-api-key', '#chat-api-key-field');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to generate a new general API Key
|
||||||
function generateNewApiKey() {
|
function generateNewApiKey() {
|
||||||
|
generateApiKey('/admin/user/generate_api_api_key', '#api-key', '#api-key-field');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reusable function to handle API key generation
|
||||||
|
function generateApiKey(url, inputSelector, fieldSelector) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/user/generate_chat_api_key',
|
url: url,
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
contentType: 'application/json',
|
contentType: 'application/json',
|
||||||
success: function(response) {
|
success: function(response) {
|
||||||
$('#api-key').val(response.api_key);
|
$(inputSelector).val(response.api_key);
|
||||||
$('#api-key-field').show();
|
$(fieldSelector).show();
|
||||||
},
|
},
|
||||||
error: function(error) {
|
error: function(error) {
|
||||||
alert('Error generating new API key: ' + error.responseText);
|
alert('Error generating new API key: ' + error.responseText);
|
||||||
@@ -120,25 +138,27 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function copyToClipboard(selector) {
|
// Function to copy text to clipboard
|
||||||
|
function copyToClipboard(selector, messageSelector) {
|
||||||
const element = document.querySelector(selector);
|
const element = document.querySelector(selector);
|
||||||
if (element) {
|
if (element) {
|
||||||
const text = element.value;
|
const text = element.value;
|
||||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||||
navigator.clipboard.writeText(text).then(function() {
|
navigator.clipboard.writeText(text).then(function() {
|
||||||
showCopyMessage();
|
showCopyMessage(messageSelector);
|
||||||
}).catch(function(error) {
|
}).catch(function(error) {
|
||||||
alert('Failed to copy text: ' + error);
|
alert('Failed to copy text: ' + error);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
fallbackCopyToClipboard(text);
|
fallbackCopyToClipboard(text, messageSelector);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.error('Element not found for selector:', selector);
|
console.error('Element not found for selector:', selector);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fallbackCopyToClipboard(text) {
|
// Fallback method for copying text to clipboard
|
||||||
|
function fallbackCopyToClipboard(text, messageSelector) {
|
||||||
const textArea = document.createElement('textarea');
|
const textArea = document.createElement('textarea');
|
||||||
textArea.value = text;
|
textArea.value = text;
|
||||||
document.body.appendChild(textArea);
|
document.body.appendChild(textArea);
|
||||||
@@ -146,15 +166,16 @@
|
|||||||
textArea.select();
|
textArea.select();
|
||||||
try {
|
try {
|
||||||
document.execCommand('copy');
|
document.execCommand('copy');
|
||||||
showCopyMessage();
|
showCopyMessage(messageSelector);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert('Fallback: Oops, unable to copy', err);
|
alert('Fallback: Oops, unable to copy', err);
|
||||||
}
|
}
|
||||||
document.body.removeChild(textArea);
|
document.body.removeChild(textArea);
|
||||||
}
|
}
|
||||||
|
|
||||||
function showCopyMessage() {
|
// Function to show copy confirmation message
|
||||||
const message = document.getElementById('copy-message');
|
function showCopyMessage(messageSelector) {
|
||||||
|
const message = document.querySelector(messageSelector);
|
||||||
if (message) {
|
if (message) {
|
||||||
message.style.display = 'block';
|
message.style.display = 'block';
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
@@ -163,8 +184,13 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('copy-button').addEventListener('click', function() {
|
// Event listeners for copy buttons
|
||||||
copyToClipboard('#api-key');
|
document.getElementById('copy-chat-button').addEventListener('click', function() {
|
||||||
|
copyToClipboard('#chat-api-key', '#copy-chat-message');
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('copy-api-button').addEventListener('click', function() {
|
||||||
|
copyToClipboard('#api-key', '#copy-message');
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
@@ -435,6 +435,36 @@ def generate_chat_api_key():
|
|||||||
tenant.encrypted_chat_api_key = simple_encryption.encrypt_api_key(new_api_key)
|
tenant.encrypted_chat_api_key = simple_encryption.encrypt_api_key(new_api_key)
|
||||||
update_logging_information(tenant, dt.now(tz.utc))
|
update_logging_information(tenant, dt.now(tz.utc))
|
||||||
|
|
||||||
|
try:
|
||||||
|
db.session.add(tenant)
|
||||||
|
db.session.commit()
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
db.session.rollback()
|
||||||
|
current_app.logger.error(f'Unable to store chat api key for tenant {tenant.id}. Error: {str(e)}')
|
||||||
|
|
||||||
|
return jsonify({'api_key': new_api_key}), 200
|
||||||
|
|
||||||
|
|
||||||
|
@user_bp.route('/check_api_api_key', methods=['POST'])
|
||||||
|
@roles_accepted('Super User', 'Tenant Admin')
|
||||||
|
def check_api_api_key():
|
||||||
|
tenant_id = session['tenant']['id']
|
||||||
|
tenant = Tenant.query.get_or_404(tenant_id)
|
||||||
|
|
||||||
|
if tenant.encrypted_api_key:
|
||||||
|
return jsonify({'api_key_exists': True})
|
||||||
|
return jsonify({'api_key_exists': False})
|
||||||
|
|
||||||
|
|
||||||
|
@user_bp.route('/generate_api_api_key', methods=['POST'])
|
||||||
|
@roles_accepted('Super User', 'Tenant Admin')
|
||||||
|
def generate_api_api_key():
|
||||||
|
tenant = Tenant.query.get_or_404(session['tenant']['id'])
|
||||||
|
|
||||||
|
new_api_key = generate_api_key(prefix="EveAI-API")
|
||||||
|
tenant.encrypted_api_key = simple_encryption.encrypt_api_key(new_api_key)
|
||||||
|
update_logging_information(tenant, dt.now(tz.utc))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
db.session.add(tenant)
|
db.session.add(tenant)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user