Class: EventSubEvents::SignatureVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/event_sub_events/signature_verifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ SignatureVerifier

Returns a new instance of SignatureVerifier.



7
8
9
# File 'lib/event_sub_events/signature_verifier.rb', line 7

def initialize(request)
  @request = request
end

Instance Method Details

#reconstruct_eventObject



34
35
36
37
# File 'lib/event_sub_events/signature_verifier.rb', line 34

def reconstruct_event
  body = @request.body.read
  JSON.parse(body, object_class: OpenStruct)
end

#verifyObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/event_sub_events/signature_verifier.rb', line 11

def verify
  request = @request

  message_id = request.headers["Twitch-Eventsub-Message-Id"]
  timestamp  = request.headers["Twitch-Eventsub-Message-Timestamp"]
  signature  = request.headers["Twitch-Eventsub-Message-Signature"].gsub("sha256=", "")
  body       = request.body.read

  hmac_message = message_id + timestamp + body

  secrets = EventSubEvents.signing_secrets if EventSubEvents.signing_secret

  secrets.each_with_index do |secret, i|
    begin
      hex = OpenSSL::HMAC.hexdigest('sha256', secret, hmac_message)
      return validate(hex, signature)
    rescue EventSubEvents::SignatureVerificationError
      raise if i == secrets.length - 1
      next
    end
  end
end