Class: Spaceship::Slack2fa::MonkeyPatch Private

Inherits:
Object
  • Object
show all
Defined in:
lib/spaceship/slack2fa/monkey_patch.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

REQUIRED_SLACK_SCOPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[channels.history chat.write].freeze

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ MonkeyPatch

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MonkeyPatch.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :slack_api_token (String)

    Required. A bot token for your Slack app.

  • :channel_id (String)

    Required. The ID of the channel where the message will be posted.

  • :user_id (String)

    Required. The ID of the user posting the message.

  • :referrer (String)

    Required. A mrkdwn text to identify which service consumes 6-digit code, typically the name of your app.

  • :allow_any_users (Boolean) — default: false

    Optional. If true, spaceship-slack2fa recognizes only messages from the bot user specified in slack_api_token, otherwise it treats all messages with 6-digits numbers as 2FA code.

  • :retry_count (Integer) — default: 3

    Optional. The number of retries to try if a message is not found.

  • :retry_interval (Float) — default: 20

    Optional. The interval between retries in seconds.

See Also:



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spaceship/slack2fa/monkey_patch.rb', line 32

def initialize(**options)
  slack_api_token = options.fetch(:slack_api_token)
  @slack = Slack::Web::Client.new(token: slack_api_token)
  @channel_id = options.fetch(:channel_id)
  @user_id = options.fetch(:user_id)
  @referrer = options.fetch(:referrer)
  @allow_any_users = options.fetch(:allow_any_users, false)
  @retry_count = options.fetch(:retry_count, 3)
  @retry_interval = options.fetch(:retry_interval, 20.0)
  @logger = Logger.new($stderr)
  @logger.level = (options.fetch(:verbose, false) ? Logger::DEBUG : Logger::WARN)
end

Instance Method Details

#disableObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
# File 'lib/spaceship/slack2fa/monkey_patch.rb', line 50

def disable
  Spaceship::Client.alias_method(:ask_for_2fa_code, :original_ask_for_2fa_code)
  Spaceship::Client.remove_method(:original_ask_for_2fa_code)
end

#enableObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
48
# File 'lib/spaceship/slack2fa/monkey_patch.rb', line 45

def enable
  Spaceship::Client.alias_method(:original_ask_for_2fa_code, :ask_for_2fa_code)
  Spaceship::Client.define_method(:ask_for_2fa_code, &public_method(:retrieve_2fa_code))
end

#retrieve_2fa_code(*_args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/spaceship/slack2fa/monkey_patch.rb', line 55

def retrieve_2fa_code(*_args)
  timestamp = Time.now.to_i
  with_retrying do |i|
    @logger.debug("Attempt ##{i}.")
    response = @slack.conversations_history(channel: @channel_id, oldest: timestamp)
    @logger.debug("Found #{response.messages.size} messages.")
    message = response.messages.select { |msg| unused_2fa_code?(msg) }.max_by(&:ts)
    @logger.debug("Possible message: #{message}.")
    code = message&.text
    if code
      @logger.debug("Found 2FA code: #{code}.")
      comment_on_thread_of(message)
      return code
    end
  end
  raise VerificationCodeNotFound
end