from flask_restful import Resource, reqparse from flask_jwt_extended import create_access_token from common.models.user import Tenant from common.extensions import simple_encryption from flask import current_app class TokenResource(Resource): def post(self): parser = reqparse.RequestParser() parser.add_argument('tenant_id', type=int, required=True) parser.add_argument('api_key', type=str, required=True) args = parser.parse_args() tenant = Tenant.query.get(args['tenant_id']) if not tenant: return {'message': 'Tenant not found'}, 404 decrypted_api_key = simple_encryption.decrypt_api_key(tenant.encrypted_api_key) if args['api_key'] != decrypted_api_key: return {'message': 'Invalid API key'}, 401 access_token = create_access_token(identity={'tenant_id': tenant.id}) return {'access_token': access_token}, 200