Class: Slackened::Authentication::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/slackened/authentication/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp:, signature:, body:) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
# File 'lib/slackened/authentication/request.rb', line 9

def initialize(timestamp:, signature:, body:)
	@timestamp = timestamp.to_i
	@signature = signature
	@body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/slackened/authentication/request.rb', line 7

def body
  @body
end

#signatureObject (readonly)

Returns the value of attribute signature.



7
8
9
# File 'lib/slackened/authentication/request.rb', line 7

def signature
  @signature
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



7
8
9
# File 'lib/slackened/authentication/request.rb', line 7

def timestamp
  @timestamp
end

Instance Method Details

#stale?Boolean

The signature depends on the timestamp to protect against replay attacks. While you’re extracting the timestamp, check to make sure that the request occurred recently. In this example, we verify that the timestamp does not differ from local time by more than five minutes. api.slack.com/authentication/verifying-requests-from-slack

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/slackened/authentication/request.rb', line 19

def stale?
	# is it less than 5 minutes old?
	five_minutes_ago = Time.now - 60 * 5

	Time.at(@timestamp) > five_minutes_ago
end

#valid?Boolean

Slack creates a unique string for your app and shares it with you. Verify requests from Slack with confidence by verifying signatures using your signing secret. api.slack.com/authentication/verifying-requests-from-slack

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/slackened/authentication/request.rb', line 29

def valid?
	return false if stale?

	sig_basestring = "v0:#{@timestamp}:#{@body}"

	secret = Slackened.configuration.signing_secret

	digest = OpenSSL::HMAC.hexdigest('SHA256', secret, sig_basestring)

	@signature == "v0=#{digest}"
end