firebase-admin 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9693687c6c5f815855662c55897a1f9fc1b44f9e21b2887fa3f843f630613959
4
- data.tar.gz: 1ea223a2eb707350eee44de4b46d115537d961986bc6b7bc513cbe4eba56d6ab
3
+ metadata.gz: e17ee3117fba2303b9994810707260adc620a0df6856fafcab5f2d91ae73d198
4
+ data.tar.gz: 1b8cb745fdcb281555ba1210ae46de28d88adbe32fd0a3eb4f28b4dffa0bf579
5
5
  SHA512:
6
- metadata.gz: c57e26695001a4be8cdec4a8850bfa6b0e49e5b33215c7163612b31805d28cc936e771f65333af1b0d219a706fe2ccb8fe65d952ed55a10a1d9929d868486154
7
- data.tar.gz: 2d35835dccdddb45c97604f11bf79c24f03c714e96b1d3ec969bac8320499ed126f210b54e1ae02e966d93d85ec59f301da3bbb05b84e9de8eb6a07450f87fcf
6
+ metadata.gz: 3b477554cecc66c68eca389b9a6b1b1b406e3279c73fab7be20c1435f53e688338c8df6093e7ea2196a27ef21b82335ff7345cbb15670b2bc33b187fd76d94f6
7
+ data.tar.gz: eeb3c9e88e13fb973c051d3c8d389f0870b48fb27f372ab36733a0f51247291499afaee4a971c2a18ef2068aa5fd4fc619983a985550d5b6ab96e4443ad4b091
data/.gitignore CHANGED
@@ -1,3 +1,6 @@
1
1
  firebase-admin.iml
2
2
  out
3
3
  coverage
4
+ vendor/bundle
5
+ .idea
6
+ .run
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- firebase-admin (0.1.6)
4
+ firebase-admin (0.2.0)
5
5
  addressable (~> 2.7)
6
6
  faraday (~> 1.3)
7
7
  faraday_middleware (~> 1.0)
@@ -85,6 +85,7 @@ GEM
85
85
  hashdiff (>= 0.4.0, < 2.0.0)
86
86
 
87
87
  PLATFORMS
88
+ arm64-darwin-21
88
89
  x86_64-darwin-20
89
90
 
90
91
  DEPENDENCIES
@@ -95,4 +96,4 @@ DEPENDENCIES
95
96
  webmock (~> 3.11)
96
97
 
97
98
  BUNDLED WITH
98
- 2.2.22
99
+ 2.3.17
@@ -117,6 +117,16 @@ module FirebaseAdmin
117
117
  # @example
118
118
  # FirebaseAdmin.create_custom_token('...')
119
119
  def create_custom_token(uid)
120
+ if service_account_email.nil? || service_account_email == ''
121
+ raise InvalidCredentials,
122
+ "No client email provided via options, 'GOOGLE_APPLICATION_CREDENTIALS' or 'GOOGLE_CLIENT_EMAIL'"
123
+ end
124
+
125
+ if service_account_private_key.nil? || service_account_private_key == ''
126
+ raise InvalidCredentials,
127
+ "No private key provided via options, 'GOOGLE_APPLICATION_CREDENTIALS' or 'GOOGLE_PRIVATE_KEY'"
128
+ end
129
+
120
130
  now_seconds = Time.now.to_i
121
131
  payload = {
122
132
  iss: service_account_email,
@@ -126,7 +136,7 @@ module FirebaseAdmin
126
136
  exp: now_seconds + (60 * 60), # Maximum expiration time is one hour
127
137
  uid: uid
128
138
  }
129
- JWT.encode(payload, private_key, 'RS256')
139
+ JWT.encode(payload, OpenSSL::PKey::RSA.new(unescape(service_account_private_key)), 'RS256')
130
140
  end
131
141
 
132
142
  # Get user by email/phone/uid
@@ -163,42 +173,6 @@ module FirebaseAdmin
163
173
  str = str[1..-2] if str.start_with?('"') && str.end_with?('"')
164
174
  str
165
175
  end
166
-
167
- def service_account_email
168
- email = default_credentials.fetch('client_email') { ENV['GOOGLE_CLIENT_EMAIL'] }
169
- if email.nil? || email == ''
170
- raise InvalidCredentials,
171
- "No client email provided via 'GOOGLE_APPLICATION_CREDENTIALS' or 'GOOGLE_CLIENT_EMAIL'"
172
- end
173
-
174
- email
175
- end
176
-
177
- def private_key
178
- key = default_credentials.fetch('private_key') { unescape(ENV['GOOGLE_PRIVATE_KEY']) }
179
- if key.nil? || key == ''
180
- raise InvalidCredentials,
181
- "No private key provided via 'GOOGLE_APPLICATION_CREDENTIALS' or 'GOOGLE_PRIVATE_KEY'"
182
- end
183
-
184
- OpenSSL::PKey::RSA.new(key)
185
- end
186
-
187
- def default_credentials
188
- @default_credentials ||= read_default_credentials
189
- end
190
-
191
- def read_default_credentials
192
- credentials_path = ENV['GOOGLE_APPLICATION_CREDENTIALS']
193
- if credentials_path && File.exist?(credentials_path)
194
- JSON.parse(File.read(credentials_path))
195
- else
196
- {}
197
- end
198
- rescue StandardError => e
199
- raise InvalidCredentials,
200
- "Failed reading credentials from '#{ENV['GOOGLE_APPLICATION_CREDENTIALS']}'. Error: #{e.message}"
201
- end
202
176
  end
203
177
  end
204
178
  end
@@ -13,6 +13,8 @@ module FirebaseAdmin
13
13
  user_agent
14
14
  project_id
15
15
  loud_logger
16
+ service_account_email
17
+ service_account_private_key
16
18
  ].freeze
17
19
 
18
20
  # By default, don't set a user access token
@@ -47,6 +49,18 @@ module FirebaseAdmin
47
49
  # @private
48
50
  attr_accessor(*VALID_OPTIONS_KEYS)
49
51
 
52
+ def service_account_email
53
+ @service_account_email ||= service_account_credentials.fetch('client_email') do
54
+ ENV['GOOGLE_CLIENT_EMAIL']
55
+ end
56
+ end
57
+
58
+ def service_account_private_key
59
+ @service_account_private_key ||= service_account_credentials.fetch('private_key') do
60
+ ENV['GOOGLE_PRIVATE_KEY']
61
+ end
62
+ end
63
+
50
64
  # When this module is extended, set all configuration options to their default values
51
65
  def self.extended(base)
52
66
  base.reset
@@ -64,6 +78,23 @@ module FirebaseAdmin
64
78
  end
65
79
  end
66
80
 
81
+ def service_account_credentials
82
+ return {} unless ENV['GOOGLE_APPLICATION_CREDENTIALS']
83
+
84
+ @service_account_credentials ||= read_service_account_credentials(ENV['GOOGLE_APPLICATION_CREDENTIALS'])
85
+ end
86
+
87
+ def read_service_account_credentials(credentials_path)
88
+ if credentials_path && File.exist?(credentials_path)
89
+ JSON.parse(File.read(credentials_path))
90
+ else
91
+ {}
92
+ end
93
+ rescue StandardError => e
94
+ raise InvalidCredentials,
95
+ "Failed reading credentials from '#{ENV['GOOGLE_APPLICATION_CREDENTIALS']}'. Error: #{e.message}"
96
+ end
97
+
67
98
  # Reset all configuration options to defaults
68
99
  def reset
69
100
  self.access_token = DEFAULT_ACCESS_TOKEN
@@ -73,6 +104,9 @@ module FirebaseAdmin
73
104
  self.user_agent = DEFAULT_USER_AGENT
74
105
  self.project_id = DEFAULT_PROJECT_ID
75
106
  self.loud_logger = DEFAULT_LOUD_LOGGER
107
+ @service_account_credentials = nil
108
+ self.service_account_email = nil
109
+ self.service_account_private_key = nil
76
110
  end
77
111
  end
78
112
  end
@@ -1,3 +1,3 @@
1
1
  module FirebaseAdmin
2
- VERSION = '0.1.6'.freeze unless defined?(::FirebaseAdmin::VERSION)
2
+ VERSION = '0.2.0'.freeze unless defined?(::FirebaseAdmin::VERSION)
3
3
  end
@@ -34,7 +34,9 @@ describe FirebaseAdmin::API do
34
34
  endpoint: 'http://tumblr.com/',
35
35
  user_agent: 'Custom User Agent',
36
36
  project_id: 'test-project',
37
- loud_logger: true
37
+ loud_logger: true,
38
+ service_account_email: 'example@gmail.com',
39
+ service_account_private_key: '===private key==='
38
40
  }
39
41
  end
40
42
 
@@ -1,9 +1,7 @@
1
1
  require File.expand_path('../../spec_helper', __dir__)
2
2
 
3
3
  describe FirebaseAdmin::Client do
4
- before do
5
- @client = FirebaseAdmin::Client.new(project_id: 'test-project')
6
- end
4
+ let(:client) { FirebaseAdmin::Client.new(project_id: 'test-project') }
7
5
 
8
6
  describe '.create_account' do
9
7
  before do
@@ -12,7 +10,7 @@ describe FirebaseAdmin::Client do
12
10
  end
13
11
 
14
12
  it 'should get the correct resource' do
15
- @client.create_account(email: 'john@smith.com', password: 'supersecret')
13
+ client.create_account(email: 'john@smith.com', password: 'supersecret')
16
14
  expect(
17
15
  a_post('v1/projects/test-project/accounts')
18
16
  .with(body: { email: 'john@smith.com', password: 'supersecret' }.to_json)
@@ -28,7 +26,7 @@ describe FirebaseAdmin::Client do
28
26
  end
29
27
 
30
28
  it 'should post to the update endpoint' do
31
- @client.update_account(email: 'john@smith.com', password: 'supersecret')
29
+ client.update_account(email: 'john@smith.com', password: 'supersecret')
32
30
  expect(
33
31
  a_post('v1/projects/test-project/accounts:update')
34
32
  .with(body: { email: 'john@smith.com', password: 'supersecret' }.to_json)
@@ -41,10 +39,11 @@ describe FirebaseAdmin::Client do
41
39
  context 'when credentials are set via GOOGLE_APPLICATION_CREDENTIALS' do
42
40
  before do
43
41
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = fixture('google_credentials.json').path
42
+ FirebaseAdmin.reset
44
43
  end
45
44
 
46
45
  it 'returns a valid JWT token' do
47
- token = @client.create_custom_token('user-123')
46
+ token = client.create_custom_token('user-123')
48
47
  token_data, _alg = JWT.decode(token, nil, false)
49
48
  expect(token_data['uid']).to eq('user-123')
50
49
  end
@@ -53,10 +52,13 @@ describe FirebaseAdmin::Client do
53
52
  context 'when GOOGLE_APPLICATION_CREDENTIALS points to an invalid file' do
54
53
  before do
55
54
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = fixture('google_credentials_invalid.json').path
55
+ FirebaseAdmin.reset
56
56
  end
57
57
 
58
58
  it 'raises an error' do
59
- expect { @client.create_custom_token('user-123') }.to raise_error FirebaseAdmin::InvalidCredentials
59
+ expect {
60
+ client.create_custom_token('user-123')
61
+ }.to raise_error FirebaseAdmin::InvalidCredentials
60
62
  end
61
63
  end
62
64
 
@@ -68,10 +70,11 @@ describe FirebaseAdmin::Client do
68
70
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = nil
69
71
  ENV['GOOGLE_CLIENT_EMAIL'] = email
70
72
  ENV['GOOGLE_PRIVATE_KEY'] = private_key
73
+ FirebaseAdmin.reset
71
74
  end
72
75
 
73
76
  it 'returns a valid JWT token' do
74
- token = @client.create_custom_token('user-123')
77
+ token = client.create_custom_token('user-123')
75
78
  token_data, _alg = JWT.decode(token, nil, false)
76
79
  expect(token_data['uid']).to eq('user-123')
77
80
  end
@@ -82,19 +85,22 @@ describe FirebaseAdmin::Client do
82
85
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = nil
83
86
  ENV['GOOGLE_CLIENT_EMAIL'] = nil
84
87
  ENV['GOOGLE_PRIVATE_KEY'] = nil
88
+ FirebaseAdmin.reset
85
89
  end
86
90
 
87
91
  it 'raises an error' do
88
- expect { @client.create_custom_token('user-123') }.to raise_error FirebaseAdmin::InvalidCredentials
92
+ expect {
93
+ client.create_custom_token('user-123')
94
+ }.to raise_error FirebaseAdmin::InvalidCredentials
89
95
  end
90
96
  end
91
97
  end
92
98
 
93
99
  describe '.sign_in_for_uid' do
94
100
  it 'should post to the update endpoint' do
95
- expect(@client).to receive(:create_custom_token).with('user-123').and_return('token')
96
- expect(@client).to receive(:sign_in_with_custom_token).with('token').and_return('result')
97
- result = @client.sign_in_for_uid('user-123')
101
+ expect(client).to receive(:create_custom_token).with('user-123').and_return('token')
102
+ expect(client).to receive(:sign_in_with_custom_token).with('token').and_return('result')
103
+ result = client.sign_in_for_uid('user-123')
98
104
  expect(result).to eq('result')
99
105
  end
100
106
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: firebase-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Harris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-07 00:00:00.000000000 Z
11
+ date: 2022-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake