2020-10-31 02:09:06 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
class PersonalAccessTokens < ::API::Base
|
|
|
|
include ::API::PaginationParams
|
|
|
|
|
2020-11-02 10:08:52 -05:00
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2020-10-31 02:09:06 -04:00
|
|
|
desc 'Get all Personal Access Tokens' do
|
|
|
|
detail 'This feature was added in GitLab 13.3'
|
|
|
|
success Entities::PersonalAccessToken
|
|
|
|
end
|
|
|
|
params do
|
2022-09-20 05:10:52 -04:00
|
|
|
optional :user_id, type: Integer, desc: 'Filter PATs by User ID'
|
|
|
|
optional :revoked, type: Boolean, desc: 'Filter PATs where revoked state matches parameter'
|
|
|
|
optional :state, type: String, desc: 'Filter PATs which are either active or not',
|
|
|
|
values: %w[active inactive]
|
|
|
|
optional :created_before, type: DateTime, desc: 'Filter PATs which were created before given datetime'
|
|
|
|
optional :created_after, type: DateTime, desc: 'Filter PATs which were created after given datetime'
|
|
|
|
optional :last_used_before, type: DateTime, desc: 'Filter PATs which were used before given datetime'
|
|
|
|
optional :last_used_after, type: DateTime, desc: 'Filter PATs which were used after given datetime'
|
|
|
|
optional :search, type: String, desc: 'Filters PATs by its name'
|
2020-10-31 02:09:06 -04:00
|
|
|
|
|
|
|
use :pagination
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
authenticate!
|
2022-09-05 08:13:20 -04:00
|
|
|
restrict_non_admins! unless current_user.can_admin_all_resources?
|
2020-10-31 02:09:06 -04:00
|
|
|
end
|
|
|
|
|
2022-09-05 08:13:20 -04:00
|
|
|
helpers ::API::Helpers::PersonalAccessTokensHelpers
|
2020-10-31 02:09:06 -04:00
|
|
|
|
|
|
|
resources :personal_access_tokens do
|
|
|
|
get do
|
|
|
|
tokens = PersonalAccessTokensFinder.new(finder_params(current_user), current_user).execute
|
|
|
|
|
|
|
|
present paginate(tokens), with: Entities::PersonalAccessToken
|
|
|
|
end
|
|
|
|
|
2022-05-26 11:08:29 -04:00
|
|
|
get ':id' do
|
|
|
|
token = PersonalAccessToken.find_by_id(params[:id])
|
|
|
|
|
2022-08-04 08:11:22 -04:00
|
|
|
allowed = Ability.allowed?(current_user, :read_user_personal_access_tokens, token&.user)
|
|
|
|
|
|
|
|
if allowed
|
|
|
|
present token, with: Entities::PersonalAccessToken
|
|
|
|
else
|
|
|
|
# Only admins should be informed if the token doesn't exist
|
2022-09-05 08:13:20 -04:00
|
|
|
current_user.can_admin_all_resources? ? not_found! : unauthorized!
|
2022-08-04 08:11:22 -04:00
|
|
|
end
|
2022-05-26 11:08:29 -04:00
|
|
|
end
|
|
|
|
|
2020-10-31 02:09:06 -04:00
|
|
|
delete ':id' do
|
2022-05-13 08:08:49 -04:00
|
|
|
token = find_token(params[:id])
|
2020-10-31 02:09:06 -04:00
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
revoke_token(token)
|
2020-10-31 02:09:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|