Agent bots assigned to a conversation can now fetch that conversation's details and manage its labels using their bot token. Previously these two operations returned `Access to this endpoint is not authorized for bots`, even though the same token worked for sending messages, updating the conversation, and setting custom attributes. Fixes https://linear.app/chatwoot/issue/PLA-174/agent-bot-tokens-cannot-fetch-conversation-details-or-manage-labels Co-authored-by: Muhsin <12408980+muhsin-k@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
41 lines
1.4 KiB
Ruby
41 lines
1.4 KiB
Ruby
module AccessTokenAuthHelper
|
|
BOT_ACCESSIBLE_ENDPOINTS = {
|
|
'api/v1/accounts/conversations' => %w[show toggle_status toggle_typing_status toggle_priority create update custom_attributes],
|
|
'api/v1/accounts/conversations/messages' => ['create'],
|
|
'api/v1/accounts/conversations/assignments' => ['create'],
|
|
'api/v1/accounts/conversations/labels' => %w[index create]
|
|
}.freeze
|
|
|
|
def ensure_access_token
|
|
token = request.headers[:api_access_token] || request.headers[:HTTP_API_ACCESS_TOKEN]
|
|
@access_token = AccessToken.find_by(token: token) if token.present?
|
|
end
|
|
|
|
def authenticate_access_token!
|
|
ensure_access_token
|
|
render_unauthorized('Invalid Access Token') && return if @access_token.blank?
|
|
|
|
# NOTE: This ensures that current_user is set and available for the rest of the controller actions
|
|
@resource = @access_token.owner
|
|
Current.user = @resource if allowed_current_user_type?(@resource)
|
|
end
|
|
|
|
def allowed_current_user_type?(resource)
|
|
return true if resource.is_a?(User)
|
|
return true if resource.is_a?(AgentBot)
|
|
|
|
false
|
|
end
|
|
|
|
def validate_bot_access_token!
|
|
return if Current.user.is_a?(User)
|
|
return if @resource.is_a?(AgentBot) && agent_bot_accessible?
|
|
|
|
render_unauthorized('Access to this endpoint is not authorized for bots')
|
|
end
|
|
|
|
def agent_bot_accessible?
|
|
BOT_ACCESSIBLE_ENDPOINTS.fetch(params[:controller], []).include?(params[:action])
|
|
end
|
|
end
|