Module: SlackBot::GrapeHelpers

Defined in:
lib/slack_bot/grape_extension.rb

Instance Method Summary collapse

Instance Method Details

#events_callback(params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/slack_bot/grape_extension.rb', line 71

def events_callback(params)
  verify_slack_team!

  SlackBot::DevConsole.log_input "SlackApi::Events#events_callback: #{params.inspect}"
  handler = config.find_event_handler(params[:event][:type].to_sym)
  return if handler.blank?

  event = handler.new(params: params, current_user: current_user)
  event.call
end

#fetch_team_idObject



6
7
8
# File 'lib/slack_bot/grape_extension.rb', line 6

def fetch_team_id
  params.dig("team_id") || params.dig("team", "id")
end

#fetch_user_idObject



10
11
12
# File 'lib/slack_bot/grape_extension.rb', line 10

def fetch_user_id
  params.dig("user_id") || params.dig("user", "id") || params.dig("event", "user")
end

#handle_block_actions_view(view:, user:, params:) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/slack_bot/grape_extension.rb', line 87

def handle_block_actions_view(view:, user:, params:)
  callback_id = view&.dig("callback_id")

  callback = SlackBot::Callback.find(callback_id, user: user, config: config)
  raise SlackBot::Errors::CallbackNotFound.new if callback.blank?

  SlackBot::DevConsole.log_check "SlackApi::Interactions##{__method__}: #{callback.id} #{callback.payload} #{callback.user_id} #{user&.id}"

  if callback.user_id != user.id
    raise "Callback user is not equal to action user"
  end

  interaction_klass = callback.handler_class&.interaction_klass
  return if interaction_klass.blank?

  interaction_klass.new(current_user: user, params: params, callback: callback, config: config).call
end

#url_verification(params) ⇒ Object



82
83
84
85
# File 'lib/slack_bot/grape_extension.rb', line 82

def url_verification(params)
  SlackBot::DevConsole.log_input "SlackApi::Events#url_verification: #{params.inspect}"
  {challenge: params[:challenge]}
end

#verify_current_user!Object



63
64
65
66
67
68
69
# File 'lib/slack_bot/grape_extension.rb', line 63

def verify_current_user!
  if current_user
    true
  else
    raise SlackBot::Errors::UserAuthenticationError.new("User is not authorized")
  end
end

#verify_direct_message_channel!Object



53
54
55
56
57
58
59
60
61
# File 'lib/slack_bot/grape_extension.rb', line 53

def verify_direct_message_channel!
  if params[:channel_name] == "directmessage"
    true
  else
    raise SlackBot::Errors::ChannelAuthenticationError.new(
      "This command is only available in direct messages"
    )
  end
end

#verify_slack_signature!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/slack_bot/grape_extension.rb', line 14

def verify_slack_signature!
  slack_signing_secret = ENV["SLACK_SIGNING_SECRET"]
  timestamp = request.headers["x-slack-request-timestamp"] || request.headers["X-Slack-Request-Timestamp"]
  slack_signature = request.headers["x-slack-signature"] || request.headers["X-Slack-Signature"]
  if slack_signing_secret.blank? || timestamp.blank? || slack_signature.blank?
    raise SlackBot::Errors::SignatureAuthenticationError.new("Missing signature headers")
  end

  request.body.rewind
  request_body = request.body.read
  request.body.rewind

  sig_basestring = "v0:#{timestamp}:#{request_body}"
  my_signature =
    "v0=" +
    OpenSSL::HMAC.hexdigest(
      OpenSSL::Digest.new("sha256"),
      slack_signing_secret,
      sig_basestring
    )
  if ActiveSupport::SecurityUtils.secure_compare(
    my_signature,
    slack_signature
  )
    true
  else
    raise SlackBot::Errors::SignatureAuthenticationError.new("Signature mismatch")
  end
end

#verify_slack_team!Object



44
45
46
47
48
49
50
51
# File 'lib/slack_bot/grape_extension.rb', line 44

def verify_slack_team!
  slack_team_id = ENV.fetch("SLACK_TEAM_ID")
  if slack_team_id == fetch_team_id
    true
  else
    raise SlackBot::Errors::TeamAuthenticationError.new("Team is not authorized")
  end
end