firebase-admin 0.1.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9693687c6c5f815855662c55897a1f9fc1b44f9e21b2887fa3f843f630613959
4
- data.tar.gz: 1ea223a2eb707350eee44de4b46d115537d961986bc6b7bc513cbe4eba56d6ab
3
+ metadata.gz: 20dc769e214cf7c48d6b86d37bd17d0b806fada175e90e538fb728645d31e3ea
4
+ data.tar.gz: 885264043ab60ad951e9b9ddb2785e2d9710523ce1d342b82851899a720f362d
5
5
  SHA512:
6
- metadata.gz: c57e26695001a4be8cdec4a8850bfa6b0e49e5b33215c7163612b31805d28cc936e771f65333af1b0d219a706fe2ccb8fe65d952ed55a10a1d9929d868486154
7
- data.tar.gz: 2d35835dccdddb45c97604f11bf79c24f03c714e96b1d3ec969bac8320499ed126f210b54e1ae02e966d93d85ec59f301da3bbb05b84e9de8eb6a07450f87fcf
6
+ metadata.gz: 5c2f316c8753ab8e3855e45f08fa0355c40cb31a9bf4127de53f4d51761f801b1ed2c8313d7c4b19cfc597e1b4f2680259d4193d60bfd2a836d6d3c06adf7058
7
+ data.tar.gz: d79b0c5cff44a1de0b96474680f88440ddfe9c5942045437a63e1b6346961371d6cf1866331fbfa078f7b281351c219e962c351906b2309c2556024baf38e5dc
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.3.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
@@ -63,6 +63,20 @@ module FirebaseAdmin
63
63
  post("v1/projects/#{project_id}/accounts:update", params)
64
64
  end
65
65
 
66
+ # DELETES the account completely from firebase. Once deleted we can retreive the account
67
+ # Need to be used cautiously
68
+ #
69
+ # @return 200 OK
70
+ # @see https://firebase.google.com/docs/reference/rest/auth
71
+ #
72
+ # @param localId [String] profile ID
73
+ def delete_account(localId)
74
+ params = {'localId': localId}
75
+ path = "v1/projects/#{project_id}/accounts:delete"
76
+ post path, params
77
+ end
78
+
79
+
66
80
  # Sign in with a password
67
81
  #
68
82
  # @param params [Hash] A customizable set of params
@@ -117,6 +131,16 @@ module FirebaseAdmin
117
131
  # @example
118
132
  # FirebaseAdmin.create_custom_token('...')
119
133
  def create_custom_token(uid)
134
+ if service_account_email.nil? || service_account_email == ''
135
+ raise InvalidCredentials,
136
+ "No client email provided via options, 'GOOGLE_APPLICATION_CREDENTIALS' or 'GOOGLE_CLIENT_EMAIL'"
137
+ end
138
+
139
+ if service_account_private_key.nil? || service_account_private_key == ''
140
+ raise InvalidCredentials,
141
+ "No private key provided via options, 'GOOGLE_APPLICATION_CREDENTIALS' or 'GOOGLE_PRIVATE_KEY'"
142
+ end
143
+
120
144
  now_seconds = Time.now.to_i
121
145
  payload = {
122
146
  iss: service_account_email,
@@ -126,7 +150,7 @@ module FirebaseAdmin
126
150
  exp: now_seconds + (60 * 60), # Maximum expiration time is one hour
127
151
  uid: uid
128
152
  }
129
- JWT.encode(payload, private_key, 'RS256')
153
+ JWT.encode(payload, OpenSSL::PKey::RSA.new(unescape(service_account_private_key)), 'RS256')
130
154
  end
131
155
 
132
156
  # Get user by email/phone/uid
@@ -163,42 +187,6 @@ module FirebaseAdmin
163
187
  str = str[1..-2] if str.start_with?('"') && str.end_with?('"')
164
188
  str
165
189
  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
190
  end
203
191
  end
204
192
  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.3.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)
@@ -37,14 +35,31 @@ describe FirebaseAdmin::Client do
37
35
  end
38
36
  end
39
37
 
38
+ describe '.delete_account' do
39
+ before do
40
+ stub_post('v1/projects/test-project/accounts:delete')
41
+ .to_return(body: fixture('delete_account.json'), headers: { content_type: 'application/json; charset=utf-8' })
42
+ end
43
+
44
+ it 'should post to the delete endpoint' do
45
+ client.delete_account('local-id')
46
+ expect(
47
+ a_post('v1/projects/test-project/accounts:delete')
48
+ .with(body: { localId: 'local-id'}.to_json)
49
+ .with(headers: { 'Authorization' => 'Bearer owner' })
50
+ ).to have_been_made
51
+ end
52
+ end
53
+
40
54
  describe '.create_custom_token' do
41
55
  context 'when credentials are set via GOOGLE_APPLICATION_CREDENTIALS' do
42
56
  before do
43
57
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = fixture('google_credentials.json').path
58
+ FirebaseAdmin.reset
44
59
  end
45
60
 
46
61
  it 'returns a valid JWT token' do
47
- token = @client.create_custom_token('user-123')
62
+ token = client.create_custom_token('user-123')
48
63
  token_data, _alg = JWT.decode(token, nil, false)
49
64
  expect(token_data['uid']).to eq('user-123')
50
65
  end
@@ -53,10 +68,13 @@ describe FirebaseAdmin::Client do
53
68
  context 'when GOOGLE_APPLICATION_CREDENTIALS points to an invalid file' do
54
69
  before do
55
70
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = fixture('google_credentials_invalid.json').path
71
+ FirebaseAdmin.reset
56
72
  end
57
73
 
58
74
  it 'raises an error' do
59
- expect { @client.create_custom_token('user-123') }.to raise_error FirebaseAdmin::InvalidCredentials
75
+ expect {
76
+ client.create_custom_token('user-123')
77
+ }.to raise_error FirebaseAdmin::InvalidCredentials
60
78
  end
61
79
  end
62
80
 
@@ -68,10 +86,11 @@ describe FirebaseAdmin::Client do
68
86
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = nil
69
87
  ENV['GOOGLE_CLIENT_EMAIL'] = email
70
88
  ENV['GOOGLE_PRIVATE_KEY'] = private_key
89
+ FirebaseAdmin.reset
71
90
  end
72
91
 
73
92
  it 'returns a valid JWT token' do
74
- token = @client.create_custom_token('user-123')
93
+ token = client.create_custom_token('user-123')
75
94
  token_data, _alg = JWT.decode(token, nil, false)
76
95
  expect(token_data['uid']).to eq('user-123')
77
96
  end
@@ -82,19 +101,22 @@ describe FirebaseAdmin::Client do
82
101
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = nil
83
102
  ENV['GOOGLE_CLIENT_EMAIL'] = nil
84
103
  ENV['GOOGLE_PRIVATE_KEY'] = nil
104
+ FirebaseAdmin.reset
85
105
  end
86
106
 
87
107
  it 'raises an error' do
88
- expect { @client.create_custom_token('user-123') }.to raise_error FirebaseAdmin::InvalidCredentials
108
+ expect {
109
+ client.create_custom_token('user-123')
110
+ }.to raise_error FirebaseAdmin::InvalidCredentials
89
111
  end
90
112
  end
91
113
  end
92
114
 
93
115
  describe '.sign_in_for_uid' do
94
116
  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')
117
+ expect(client).to receive(:create_custom_token).with('user-123').and_return('token')
118
+ expect(client).to receive(:sign_in_with_custom_token).with('token').and_return('result')
119
+ result = client.sign_in_for_uid('user-123')
98
120
  expect(result).to eq('result')
99
121
  end
100
122
  end
@@ -0,0 +1 @@
1
+ { "kind": "identitytoolkit#DeleteAccountResponse"}
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.3.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-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -185,6 +185,7 @@ files:
185
185
  - spec/fixtures/400_error.json
186
186
  - spec/fixtures/bad_gateway.html
187
187
  - spec/fixtures/create_account.json
188
+ - spec/fixtures/delete_account.json
188
189
  - spec/fixtures/example_key
189
190
  - spec/fixtures/gateway_timeout.html
190
191
  - spec/fixtures/google_credentials.json
@@ -223,6 +224,7 @@ test_files:
223
224
  - spec/fixtures/400_error.json
224
225
  - spec/fixtures/bad_gateway.html
225
226
  - spec/fixtures/create_account.json
227
+ - spec/fixtures/delete_account.json
226
228
  - spec/fixtures/example_key
227
229
  - spec/fixtures/gateway_timeout.html
228
230
  - spec/fixtures/google_credentials.json