2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-29 05:52:21 -04:00
|
|
|
module API
|
|
|
|
# Keys API
|
|
|
|
class Keys < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
resource :keys do
|
2016-09-13 14:55:00 -04:00
|
|
|
desc 'Get single ssh key by id. Only available to admin users' do
|
|
|
|
success Entities::SSHKeyWithUser
|
|
|
|
end
|
2015-08-29 05:52:21 -04:00
|
|
|
get ":id" do
|
|
|
|
authenticated_as_admin!
|
|
|
|
|
|
|
|
key = Key.find(params[:id])
|
|
|
|
|
2018-07-24 08:46:19 -04:00
|
|
|
present key, with: Entities::SSHKeyWithUser, current_user: current_user
|
2015-08-29 05:52:21 -04:00
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
|
|
|
|
desc 'Get SSH Key information' do
|
|
|
|
success Entities::UserWithAdmin
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :fingerprint, type: String, desc: 'Search for a SSH fingerprint'
|
|
|
|
end
|
|
|
|
get do
|
2019-12-17 04:07:48 -05:00
|
|
|
authenticated_with_can_read_all_resources!
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2020-01-13 19:08:14 -05:00
|
|
|
key = KeysFinder.new(current_user, params).execute
|
2019-12-11 13:08:10 -05:00
|
|
|
|
|
|
|
not_found!('Key') unless key
|
2020-01-13 19:08:14 -05:00
|
|
|
|
|
|
|
if key.type == "DeployKey"
|
|
|
|
present key, with: Entities::DeployKeyWithUser, current_user: current_user
|
|
|
|
else
|
|
|
|
present key, with: Entities::SSHKeyWithUser, current_user: current_user
|
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
rescue KeysFinder::InvalidFingerprint
|
|
|
|
render_api_error!('Failed to return the key', 400)
|
|
|
|
end
|
2015-08-29 05:52:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|