Class: Stripe::RequestSigningAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/stripe/request_signing_authenticator.rb

Constant Summary collapse

AUTHORIZATION_HEADER_NAME =
"Authorization"
CONTENT_TYPE_HEADER_NAME =
"Content-Type"
STRIPE_CONTEXT_HEADER_NAME =
"Stripe-Context"
STRIPE_ACCOUNT_HEADER_NAME =
"Stripe-Account"
CONTENT_DIGEST_HEADER_NAME =
"Content-Digest"
SIGNATURE_INPUT_HEADER_NAME =
"Signature-Input"
SIGNATURE_HEADER_NAME =
"Signature"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_token, sign_lambda) ⇒ RequestSigningAuthenticator

Returns a new instance of RequestSigningAuthenticator.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
# File 'lib/stripe/request_signing_authenticator.rb', line 15

def initialize(auth_token, sign_lambda)
  raise ArgumentError, "auth_token must be a string" unless auth_token.is_a?(String)
  raise ArgumentError, "sign_lambda must be a lambda" unless sign_lambda.is_a?(Proc)

  @auth_token = auth_token
  @sign_lambda = sign_lambda
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



13
14
15
# File 'lib/stripe/request_signing_authenticator.rb', line 13

def auth_token
  @auth_token
end

#sign_lambdaObject (readonly)

Returns the value of attribute sign_lambda.



13
14
15
# File 'lib/stripe/request_signing_authenticator.rb', line 13

def sign_lambda
  @sign_lambda
end

Instance Method Details

#authenticate(method, headers, body) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/stripe/request_signing_authenticator.rb', line 23

def authenticate(method, headers, body)
  covered_headers = [CONTENT_TYPE_HEADER_NAME,
                     CONTENT_DIGEST_HEADER_NAME,
                     STRIPE_CONTEXT_HEADER_NAME,
                     STRIPE_ACCOUNT_HEADER_NAME,
                     AUTHORIZATION_HEADER_NAME,]

  headers[AUTHORIZATION_HEADER_NAME] = "STRIPE-V2-SIG #{auth_token}"

  if method == :get
    covered_headers -= [CONTENT_TYPE_HEADER_NAME,
                        CONTENT_DIGEST_HEADER_NAME,]
  else
    content = body || ""
    headers[CONTENT_DIGEST_HEADER_NAME] =
      %(sha-256=:#{content_digest(content)}:)
  end

  covered_headers_formatted = covered_headers
                              .map { |string| %("#{string.downcase}") }
                              .join(" ")

  signature_input = "(#{covered_headers_formatted});created=#{created_time}"

  inputs = covered_headers
           .map { |header| %("#{header.downcase}": #{headers[header]}) }
           .join("\n")

  signature_base = %(#{inputs}\n"@signature-params": #{signature_input})
                   .encode(Encoding::UTF_8)

  headers[SIGNATURE_INPUT_HEADER_NAME] = "sig1=#{signature_input}"

  headers[SIGNATURE_HEADER_NAME] =
    "sig1=:#{encoded_signature(signature_base)}:"
end