debian-mirror-gitlab/app/services/personal_access_tokens/revoke_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.7 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
module PersonalAccessTokens
2022-11-25 23:54:43 +05:30
class RevokeService < BaseService
2021-01-03 14:25:43 +05:30
attr_reader :token, :current_user, :group
2020-10-24 23:57:45 +05:30
2023-03-17 16:20:25 +05:30
VALID_SOURCES = %i[self secret_detection].freeze
2023-03-04 22:38:38 +05:30
def initialize(current_user = nil, token: nil, group: nil, source: nil)
2020-10-24 23:57:45 +05:30
@current_user = current_user
2021-01-29 00:20:46 +05:30
@token = token
@group = group
2023-03-04 22:38:38 +05:30
@source = source
2023-03-17 16:20:25 +05:30
@source = :self if @current_user && !@source
raise ArgumentError unless VALID_SOURCES.include?(@source)
2020-10-24 23:57:45 +05:30
end
def execute
return ServiceResponse.error(message: 'Not permitted to revoke') unless revocation_permitted?
if token.revoke!
2021-01-29 00:20:46 +05:30
log_event
2023-03-04 22:38:38 +05:30
notification_service.access_token_revoked(token.user, token.name, @source)
2020-10-24 23:57:45 +05:30
ServiceResponse.success(message: success_message)
else
ServiceResponse.error(message: error_message)
end
end
private
def error_message
_("Could not revoke personal access token %{personal_access_token_name}.") % { personal_access_token_name: token.name }
end
def success_message
_("Revoked personal access token %{personal_access_token_name}!") % { personal_access_token_name: token.name }
end
def revocation_permitted?
2023-03-17 16:20:25 +05:30
case @source
when :self
2023-03-04 22:38:38 +05:30
Ability.allowed?(current_user, :revoke_token, token)
2023-03-17 16:20:25 +05:30
when :secret_detection
true
2023-03-04 22:38:38 +05:30
else
2023-03-17 16:20:25 +05:30
false
2023-03-04 22:38:38 +05:30
end
end
2021-01-29 00:20:46 +05:30
def log_event
2023-03-04 22:38:38 +05:30
Gitlab::AppLogger.info(
class: self.class.name,
message: "PAT Revoked",
2023-03-17 16:20:25 +05:30
revoked_by: current_user&.username || @source,
2023-03-04 22:38:38 +05:30
revoked_for: token.user.username,
token_id: token.id)
2021-01-29 00:20:46 +05:30
end
2020-10-24 23:57:45 +05:30
end
end
2021-01-03 14:25:43 +05:30
2021-06-08 01:23:25 +05:30
PersonalAccessTokens::RevokeService.prepend_mod_with('PersonalAccessTokens::RevokeService')