external_credentials_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class ExternalCredentialsController < ApplicationController
  3. prepend_before_action { authentication_check && authorize! }
  4. def index
  5. model_index_render(ExternalCredential, params)
  6. end
  7. def show
  8. model_show_render(ExternalCredential, params)
  9. end
  10. def create
  11. model_create_render(ExternalCredential, params)
  12. end
  13. def update
  14. model_update_render(ExternalCredential, params)
  15. end
  16. def destroy
  17. model_destroy_render(ExternalCredential, params)
  18. end
  19. def app_verify
  20. render json: { attributes: ExternalCredential.app_verify(params.permit!.to_h) }, status: :ok
  21. rescue => e
  22. logger.error e
  23. render json: { error: e.message }, status: :ok
  24. end
  25. def link_account
  26. provider = params[:provider].downcase
  27. attributes = ExternalCredential.request_account_to_link(provider, params)
  28. session[:request_token] = attributes[:request_token]
  29. session[:channel_id] = params[:channel_id]
  30. redirect_to attributes[:authorize_url]
  31. end
  32. def callback
  33. provider = params[:provider].downcase
  34. channel = ExternalCredential.link_account(provider, session[:request_token], link_params)
  35. return redirect_to(channel) if channel.instance_of?(String)
  36. session[:request_token] = nil
  37. session[:channel_id] = nil
  38. redirect_to app_url(provider, channel.id)
  39. end
  40. private
  41. def link_params
  42. params.permit!.to_h.merge(channel_id: session[:channel_id])
  43. end
  44. def callback_url(provider)
  45. ExternalCredential.callback_url(provider)
  46. end
  47. def app_url(provider, channel_id)
  48. ExternalCredential.app_url(provider, channel_id)
  49. end
  50. end