2020-04-21 15:21:10 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-05-05 21:09:42 +00:00
|
|
|
module ResourceAccessTokens
|
|
|
|
class CreateService < BaseService
|
|
|
|
def initialize(current_user, resource, params = {})
|
|
|
|
@resource_type = resource.class.name.downcase
|
2020-04-21 15:21:10 +00:00
|
|
|
@resource = resource
|
2020-05-05 21:09:42 +00:00
|
|
|
@current_user = current_user
|
2020-04-21 15:21:10 +00:00
|
|
|
@params = params.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-01-22 21:09:10 +00:00
|
|
|
return error("User does not have permission to create #{resource_type} access token") unless has_permission_to_create?
|
2020-04-21 15:21:10 +00:00
|
|
|
|
|
|
|
user = create_user
|
|
|
|
|
|
|
|
return error(user.errors.full_messages.to_sentence) unless user.persisted?
|
2020-11-02 21:09:10 +00:00
|
|
|
|
2021-08-31 21:10:43 +00:00
|
|
|
user.update!(external: true) if current_user.external?
|
|
|
|
|
2021-06-29 12:08:48 +00:00
|
|
|
access_level = params[:access_level] || Gitlab::Access::MAINTAINER
|
|
|
|
member = create_membership(resource, user, access_level)
|
2020-11-02 21:09:10 +00:00
|
|
|
|
|
|
|
unless member.persisted?
|
|
|
|
delete_failed_user(user)
|
2021-06-29 12:08:48 +00:00
|
|
|
return error("Could not provision #{Gitlab::Access.human_access(access_level).downcase} access to project access token")
|
2020-11-02 21:09:10 +00:00
|
|
|
end
|
2020-04-21 15:21:10 +00:00
|
|
|
|
|
|
|
token_response = create_personal_access_token(user)
|
|
|
|
|
|
|
|
if token_response.success?
|
2021-01-22 21:09:10 +00:00
|
|
|
log_event(token_response.payload[:personal_access_token])
|
2020-04-21 15:21:10 +00:00
|
|
|
success(token_response.payload[:personal_access_token])
|
|
|
|
else
|
2020-11-02 21:09:10 +00:00
|
|
|
delete_failed_user(user)
|
2020-04-21 15:21:10 +00:00
|
|
|
error(token_response.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-05 21:09:42 +00:00
|
|
|
attr_reader :resource_type, :resource
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def has_permission_to_create?
|
2021-03-26 21:09:22 +00:00
|
|
|
%w(project group).include?(resource_type) && can?(current_user, :create_resource_access_tokens, resource)
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_user
|
2020-06-30 12:08:57 +00:00
|
|
|
# Even project maintainers can create project access tokens, which in turn
|
|
|
|
# creates a bot user, and so it becomes necessary to have `skip_authorization: true`
|
|
|
|
# since someone like a project maintainer does not inherently have the ability
|
|
|
|
# to create a new user in the system.
|
|
|
|
|
2021-05-31 15:11:12 +00:00
|
|
|
::Users::AuthorizedCreateService.new(current_user, default_user_params).execute
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
|
2020-11-02 21:09:10 +00:00
|
|
|
def delete_failed_user(user)
|
|
|
|
DeleteUserWorker.perform_async(current_user.id, user.id, hard_delete: true, skip_authorization: true)
|
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def default_user_params
|
|
|
|
{
|
|
|
|
name: params[:name] || "#{resource.name.to_s.humanize} bot",
|
|
|
|
email: generate_email,
|
|
|
|
username: generate_username,
|
2022-01-08 00:14:32 +00:00
|
|
|
user_type: :project_bot,
|
2020-06-30 12:08:57 +00:00
|
|
|
skip_confirmation: true # Bot users should always have their emails confirmed.
|
2020-04-21 15:21:10 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_username
|
|
|
|
base_username = "#{resource_type}_#{resource.id}_bot"
|
|
|
|
|
|
|
|
uniquify.string(base_username) { |s| User.find_by_username(s) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_email
|
2022-01-19 09:14:10 +00:00
|
|
|
# Default emaildomain need to be reworked. See gitlab-org/gitlab#260305
|
|
|
|
email_pattern = "#{resource_type}#{resource.id}_bot%s@noreply.#{Gitlab.config.gitlab.host}"
|
2020-04-21 15:21:10 +00:00
|
|
|
|
|
|
|
uniquify.string(-> (n) { Kernel.sprintf(email_pattern, n) }) do |s|
|
|
|
|
User.find_by_email(s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uniquify
|
|
|
|
Uniquify.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_personal_access_token(user)
|
2020-11-09 12:09:24 +00:00
|
|
|
PersonalAccessTokens::CreateService.new(
|
|
|
|
current_user: user, target_user: user, params: personal_access_token_params
|
|
|
|
).execute
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def personal_access_token_params
|
|
|
|
{
|
2020-05-05 21:09:42 +00:00
|
|
|
name: params[:name] || "#{resource_type}_bot",
|
2020-04-21 15:21:10 +00:00
|
|
|
impersonation: false,
|
|
|
|
scopes: params[:scopes] || default_scopes,
|
|
|
|
expires_at: params[:expires_at] || nil
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_scopes
|
2020-05-05 21:09:42 +00:00
|
|
|
Gitlab::Auth.resource_bot_scopes
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
|
2021-06-29 12:08:48 +00:00
|
|
|
def create_membership(resource, user, access_level)
|
|
|
|
resource.add_user(user, access_level, expires_at: params[:expires_at])
|
2020-04-21 15:21:10 +00:00
|
|
|
end
|
|
|
|
|
2021-01-22 21:09:10 +00:00
|
|
|
def log_event(token)
|
|
|
|
::Gitlab::AppLogger.info "PROJECT ACCESS TOKEN CREATION: created_by: #{current_user.username}, project_id: #{resource.id}, token_user: #{token.user.name}, token_id: #{token.id}"
|
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def error(message)
|
|
|
|
ServiceResponse.error(message: message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def success(access_token)
|
|
|
|
ServiceResponse.success(payload: { access_token: access_token })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-22 21:09:10 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
ResourceAccessTokens::CreateService.prepend_mod_with('ResourceAccessTokens::CreateService')
|