Class: Banacle::SlackValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/banacle/slack_validator.rb

Constant Summary collapse

SLACK_SIGNING_SECRET_VERSION =
'v0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signing_secret) ⇒ SlackValidator

Returns a new instance of SlackValidator.



8
9
10
# File 'lib/banacle/slack_validator.rb', line 8

def initialize(signing_secret)
  @signing_secret = signing_secret
end

Instance Attribute Details

#signing_secretObject (readonly)

Returns the value of attribute signing_secret.



12
13
14
# File 'lib/banacle/slack_validator.rb', line 12

def signing_secret
  @signing_secret
end

Instance Method Details

#valid_signature?(request) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/banacle/slack_validator.rb', line 14

def valid_signature?(request)
  body = request.env["rack.request.form_vars"]
  slack_signature = request.env["HTTP_X_SLACK_SIGNATURE"]
  slack_timestamp = request.env["HTTP_X_SLACK_REQUEST_TIMESTAMP"]

  # https://api.slack.com/docs/verifying-requests-from-slack#verification_token_deprecation
  if (slack_timestamp.to_i - Time.now.to_i).abs > 60 * 5
    return false
  end

  sig_basestring = "#{SLACK_SIGNING_SECRET_VERSION}:#{slack_timestamp}:#{body}"
  digest = OpenSSL::HMAC.hexdigest("SHA256", signing_secret, sig_basestring)

  slack_signature == "#{SLACK_SIGNING_SECRET_VERSION}=#{digest}"
end