Module: Rack::Firebase::TestHelpers

Defined in:
lib/rack/firebase/test_helpers.rb

Class Method Summary collapse

Class Method Details

.auth_headers(headers, uid, aud: nil, options: {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rack/firebase/test_helpers.rb', line 4

def self.auth_headers(headers, uid, aud: nil, options: {})
  cached_current_time = Time.now.to_i
  aud ||= Rack::Firebase.configuration.project_ids.first

  payload = {
    iss: "https://securetoken.google.com/#{aud}",
    aud: aud,
    auth_time: options[:auth_time] || options[:iat] || cached_current_time,
    user_id: uid,
    sub: uid,
    iat: options[:iat] || cached_current_time,
    exp: options[:exp] || cached_current_time + 5000,
    email: options[:email] || "[email protected]",
    email_verified: !!options[:verified],
    firebase: {
      identities: {
        email: [
          options[:email],
          "[email protected]"
        ]
      },
      sign_in_provider: "password"
    }
  }
  token = JWT.encode(payload, secret_key, Rack::Firebase::ALG, kid: "1234567890")
  headers = headers.dup
  headers["Authorization"] = "Bearer #{token}"
  headers
end

.mock_endObject



45
46
47
48
# File 'lib/rack/firebase/test_helpers.rb', line 45

def self.mock_end
  Rack::Firebase.instance_variable_set(:@cached_keys, nil)
  Rack::Firebase.instance_variable_set(:@refresh_cache_by, nil)
end

.mock_signature_verificationObject



39
40
41
42
43
# File 'lib/rack/firebase/test_helpers.rb', line 39

def self.mock_signature_verification
  mock_start
  yield
  mock_end
end

.mock_start(expires_in = 5000) ⇒ Object



34
35
36
37
# File 'lib/rack/firebase/test_helpers.rb', line 34

def self.mock_start(expires_in = 5000)
  Rack::Firebase.instance_variable_set(:@cached_keys, [JWT::JWK::RSA.new(secret_key.public_key, kid: "1234567890")])
  Rack::Firebase.instance_variable_set(:@refresh_cache_by, Time.now.to_i + expires_in)
end

.secret_keyObject



52
53
54
# File 'lib/rack/firebase/test_helpers.rb', line 52

def self.secret_key
  @secret_key ||= OpenSSL::PKey::RSA.generate(2048)
end