Skip to content

Commit

Permalink
fix: API error when using SuperAdmin token (#8739)
Browse files Browse the repository at this point in the history
- Fixes the issue in release 3.5.0, which causes SuperAdmin tokens to throw error during API calls

Fixes: #8719
  • Loading branch information
sojan-official committed Jan 18, 2024
1 parent aacf326 commit ce8190d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/controllers/concerns/access_token_auth_helper.rb
Expand Up @@ -14,7 +14,14 @@ def authenticate_access_token!
render_unauthorized('Invalid Access Token') && return if @access_token.blank?

@resource = @access_token.owner
Current.user = @resource if [User, AgentBot].include?(@resource.class)
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!
Expand Down
19 changes: 19 additions & 0 deletions spec/controllers/api/base_controller_spec.rb
Expand Up @@ -29,6 +29,25 @@
end
end

describe 'request with api_access_token for a super admin' do
before do
user.update!(type: 'SuperAdmin')
end

context 'when its a valid api_access_token' do
it 'returns current user information' do
get '/api/v1/profile',
headers: { api_access_token: user.access_token.token },
as: :json

expect(response).to have_http_status(:success)
json_response = response.parsed_body
expect(json_response['id']).to eq(user.id)
expect(json_response['email']).to eq(user.email)
end
end
end

describe 'request with api_access_token for bot' do
let!(:agent_bot) { create(:agent_bot) }
let!(:inbox) { create(:inbox, account: account) }
Expand Down

0 comments on commit ce8190d

Please sign in to comment.