- Created a new eveai_chat plugin to support the new dynamic possibilities of the Specialists. Currently only supports standard Rag retrievers (i.e. no extra arguments).
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from datetime import timedelta
|
||||
from datetime import timedelta, datetime as dt, timezone as tz
|
||||
|
||||
from flask_restx import Namespace, Resource, fields
|
||||
from flask_jwt_extended import create_access_token
|
||||
from flask_jwt_extended import create_access_token, verify_jwt_in_request, get_jwt
|
||||
from common.models.user import Tenant, TenantProject
|
||||
from common.extensions import simple_encryption
|
||||
from flask import current_app, request
|
||||
@@ -18,6 +18,12 @@ token_response = auth_ns.model('TokenResponse', {
|
||||
'expires_in': fields.Integer(description='Token expiration time in seconds')
|
||||
})
|
||||
|
||||
token_verification = auth_ns.model('TokenVerification', {
|
||||
'is_valid': fields.Boolean(description='Token validity status'),
|
||||
'expires_in': fields.Integer(description='Seconds until token expiration'),
|
||||
'tenant_id': fields.Integer(description='Tenant ID from token')
|
||||
})
|
||||
|
||||
|
||||
@auth_ns.route('/token')
|
||||
class Token(Resource):
|
||||
@@ -82,3 +88,61 @@ class Token(Resource):
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error creating access token: {e}")
|
||||
return {'message': "Internal server error"}, 500
|
||||
|
||||
|
||||
@auth_ns.route('/verify')
|
||||
class TokenVerification(Resource):
|
||||
@auth_ns.doc('verify_token')
|
||||
@auth_ns.response(200, 'Token verification result', token_verification)
|
||||
@auth_ns.response(401, 'Invalid token')
|
||||
def get(self):
|
||||
"""Verify a token's validity and get expiration information"""
|
||||
try:
|
||||
verify_jwt_in_request()
|
||||
jwt_data = get_jwt()
|
||||
|
||||
# Get expiration timestamp from token
|
||||
exp_timestamp = jwt_data['exp']
|
||||
current_timestamp = dt.now().timestamp()
|
||||
|
||||
return {
|
||||
'is_valid': True,
|
||||
'expires_in': int(exp_timestamp - current_timestamp),
|
||||
'tenant_id': jwt_data['sub'] # tenant_id is stored in 'sub' claim
|
||||
}, 200
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Token verification failed: {str(e)}")
|
||||
return {
|
||||
'is_valid': False,
|
||||
'message': 'Invalid token'
|
||||
}, 401
|
||||
|
||||
|
||||
@auth_ns.route('/refresh')
|
||||
class TokenRefresh(Resource):
|
||||
@auth_ns.doc('refresh_token')
|
||||
@auth_ns.response(200, 'New token', token_response)
|
||||
@auth_ns.response(401, 'Invalid token')
|
||||
def post(self):
|
||||
"""Get a new token before the current one expires"""
|
||||
try:
|
||||
verify_jwt_in_request()
|
||||
jwt_data = get_jwt()
|
||||
tenant_id = jwt_data['sub']
|
||||
|
||||
# Optional: Add additional verification here if needed
|
||||
|
||||
# Create new token
|
||||
expires_delta = current_app.config.get('JWT_ACCESS_TOKEN_EXPIRES', timedelta(minutes=15))
|
||||
new_token = create_access_token(
|
||||
identity=tenant_id,
|
||||
expires_delta=expires_delta
|
||||
)
|
||||
|
||||
return {
|
||||
'access_token': new_token,
|
||||
'expires_in': int(expires_delta.total_seconds())
|
||||
}, 200
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Token refresh failed: {str(e)}")
|
||||
return {'message': 'Token refresh failed'}, 401
|
||||
Reference in New Issue
Block a user