You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

data_source_bearer_auth.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from flask_login import current_user
  2. from flask_restx import Resource, reqparse
  3. from werkzeug.exceptions import Forbidden
  4. from controllers.console import console_ns
  5. from controllers.console.auth.error import ApiKeyAuthFailedError
  6. from libs.login import login_required
  7. from services.auth.api_key_auth_service import ApiKeyAuthService
  8. from ..wraps import account_initialization_required, setup_required
  9. @console_ns.route("/api-key-auth/data-source")
  10. class ApiKeyAuthDataSource(Resource):
  11. @setup_required
  12. @login_required
  13. @account_initialization_required
  14. def get(self):
  15. data_source_api_key_bindings = ApiKeyAuthService.get_provider_auth_list(current_user.current_tenant_id)
  16. if data_source_api_key_bindings:
  17. return {
  18. "sources": [
  19. {
  20. "id": data_source_api_key_binding.id,
  21. "category": data_source_api_key_binding.category,
  22. "provider": data_source_api_key_binding.provider,
  23. "disabled": data_source_api_key_binding.disabled,
  24. "created_at": int(data_source_api_key_binding.created_at.timestamp()),
  25. "updated_at": int(data_source_api_key_binding.updated_at.timestamp()),
  26. }
  27. for data_source_api_key_binding in data_source_api_key_bindings
  28. ]
  29. }
  30. return {"sources": []}
  31. @console_ns.route("/api-key-auth/data-source/binding")
  32. class ApiKeyAuthDataSourceBinding(Resource):
  33. @setup_required
  34. @login_required
  35. @account_initialization_required
  36. def post(self):
  37. # The role of the current user in the table must be admin or owner
  38. if not current_user.is_admin_or_owner:
  39. raise Forbidden()
  40. parser = reqparse.RequestParser()
  41. parser.add_argument("category", type=str, required=True, nullable=False, location="json")
  42. parser.add_argument("provider", type=str, required=True, nullable=False, location="json")
  43. parser.add_argument("credentials", type=dict, required=True, nullable=False, location="json")
  44. args = parser.parse_args()
  45. ApiKeyAuthService.validate_api_key_auth_args(args)
  46. try:
  47. ApiKeyAuthService.create_provider_auth(current_user.current_tenant_id, args)
  48. except Exception as e:
  49. raise ApiKeyAuthFailedError(str(e))
  50. return {"result": "success"}, 200
  51. @console_ns.route("/api-key-auth/data-source/<uuid:binding_id>")
  52. class ApiKeyAuthDataSourceBindingDelete(Resource):
  53. @setup_required
  54. @login_required
  55. @account_initialization_required
  56. def delete(self, binding_id):
  57. # The role of the current user in the table must be admin or owner
  58. if not current_user.is_admin_or_owner:
  59. raise Forbidden()
  60. ApiKeyAuthService.delete_provider_auth(current_user.current_tenant_id, binding_id)
  61. return {"result": "success"}, 204