Class: TxghServer::TransifexRequestAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/txgh-server/transifex_request_auth.rb

Constant Summary collapse

HMAC_DIGEST =
OpenSSL::Digest.new('sha256')
RACK_HEADER =
'HTTP_X_TX_SIGNATURE_V2'
TRANSIFEX_HEADER =
'X-TX-Signature-V2'

Class Method Summary collapse

Class Method Details

.authentic_request?(request, secret) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/txgh-server/transifex_request_auth.rb', line 12

def authentic_request?(request, secret)
  request.body.rewind

  expected_signature = compute_signature(
    http_verb: request.request_method,
    date_str: request.env['HTTP_DATE'],
    url: request.env['HTTP_X_TX_URL'],
    content: request.body.read,
    secret: secret
  )

  actual_signature = signature_from(request)
  actual_signature == expected_signature
end

.compute_signature(http_verb: 'POST', url:, date_str:, content:, secret:) ⇒ Object



27
28
29
30
# File 'lib/txgh-server/transifex_request_auth.rb', line 27

def compute_signature(http_verb: 'POST', url:, date_str:, content:, secret:)
  data = [http_verb, url, date_str, Digest::MD5.hexdigest(content)]
  digest(data.join("\n"), secret)
end

.signature_from(request) ⇒ Object



32
33
34
# File 'lib/txgh-server/transifex_request_auth.rb', line 32

def signature_from(request)
  request.env[RACK_HEADER]
end