firebase-admin 0.1.2 → 0.1.3

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: a4c4443edc00425f17776c80d7ac2891f9910facd4139229a8fafdcee89c4fe9
4
- data.tar.gz: 27af55f9e6a327a65d993140210caaf704c014eb074c5f255a93e32b407944a5
3
+ metadata.gz: e89fa65facd66a7e21d693667661a8606669580f70d8c538a21fe08a3ef8cff0
4
+ data.tar.gz: 27ca309672952d220435693027320e58497055345054a80a6ae41b6a2b541707
5
5
  SHA512:
6
- metadata.gz: 503314268323bac0314143979cecf76c887d95381df7ed02c9b7fe4979a8ef74d7c961a9bbc3e211134a4c20bec3a8ea88f4ab455d1291015b7269563fb54f3a
7
- data.tar.gz: 0c47a474b014ebadb40110e4ba4cb285424949f5d49fac7eeb07623bd32690be905d093755fb93ca365cb9688b35b819359a0a78a258f8053fbb234b07416e31
6
+ metadata.gz: 7f0b56ee6d005dad74eb3bdeae6fbb2b6f6ea3e151e0957690cee98d521348893e74ef179eebe9f67e08c9066903aaf79a25a4b8870c979105aa01ed5020800d
7
+ data.tar.gz: c3a6dc64332ee9b362f2c6399c57f684233b1e697d3ddd14d64f55f77915bebec36309e173049ea6d2150359971de68361dedb219b401870ba24f473297dabff
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- firebase-admin (0.1.2)
4
+ firebase-admin (0.1.3)
5
5
  addressable (~> 2.7)
6
6
  faraday (~> 1.3)
7
7
  faraday_middleware (~> 1.0)
@@ -93,6 +93,20 @@ module FirebaseAdmin
93
93
  post('v1/accounts:signInWithCustomToken', { token: token, returnSecureToken: true })
94
94
  end
95
95
 
96
+ # Sign in based on the UID of an account
97
+ # This generates a custom token for the UID and signs in using the custom token
98
+ #
99
+ # @param uid [String] The uid of a user
100
+ #
101
+ # @return [Resource] with idToken
102
+ #
103
+ # @example
104
+ # FirebaseAdmin.sign_in_for_uid("...")
105
+ def sign_in_for_uid(uid)
106
+ custom_token = create_custom_token(uid)
107
+ sign_in_with_custom_token(custom_token)
108
+ end
109
+
96
110
  # Create a custom JWT token for a UID
97
111
  #
98
112
  # @param uid [String] The uid of a user
@@ -103,11 +117,6 @@ module FirebaseAdmin
103
117
  # @example
104
118
  # FirebaseAdmin.create_custom_token('...')
105
119
  def create_custom_token(uid)
106
- credentials = default_credentials
107
-
108
- service_account_email = credentials.fetch('client_email', ENV['GOOGLE_CLIENT_EMAIL'])
109
- private_key = OpenSSL::PKey::RSA.new credentials.fetch('private_key', unescape(ENV['GOOGLE_PRIVATE_KEY']))
110
-
111
120
  now_seconds = Time.now.to_i
112
121
  payload = {
113
122
  iss: service_account_email,
@@ -155,10 +164,20 @@ module FirebaseAdmin
155
164
  str
156
165
  end
157
166
 
158
- def default_credentials
159
- return {} if ENV['GOOGLE_APPLICATION_CREDENTIALS'].nil?
167
+ def service_account_email
168
+ default_credentials.fetch('client_email') { ENV['GOOGLE_CLIENT_EMAIL'] }
169
+ end
160
170
 
161
- JSON.parse(File.read(ENV['GOOGLE_APPLICATION_CREDENTIALS']))
171
+ def private_key
172
+ key = default_credentials.fetch('private_key') { unescape(ENV['GOOGLE_PRIVATE_KEY']) }
173
+ OpenSSL::PKey::RSA.new(key)
174
+ end
175
+
176
+ def default_credentials
177
+ @default_credentials ||= begin
178
+ credentials_path = ENV['GOOGLE_APPLICATION_CREDENTIALS']
179
+ JSON.parse(File.read(credentials_path)) if credentials_path
180
+ end
162
181
  end
163
182
  end
164
183
  end
@@ -1,3 +1,3 @@
1
1
  module FirebaseAdmin
2
- VERSION = '0.1.2'.freeze unless defined?(::FirebaseAdmin::VERSION)
2
+ VERSION = '0.1.3'.freeze unless defined?(::FirebaseAdmin::VERSION)
3
3
  end
@@ -20,4 +20,59 @@ describe FirebaseAdmin::Client do
20
20
  ).to have_been_made
21
21
  end
22
22
  end
23
+
24
+ describe '.update_account' do
25
+ before do
26
+ stub_post('v1/projects/test-project/accounts:update')
27
+ .to_return(body: fixture('update_account.json'), headers: { content_type: 'application/json; charset=utf-8' })
28
+ end
29
+
30
+ it 'should post to the update endpoint' do
31
+ @client.update_account(email: 'john@smith.com', password: 'supersecret')
32
+ expect(
33
+ a_post('v1/projects/test-project/accounts:update')
34
+ .with(body: { email: 'john@smith.com', password: 'supersecret' }.to_json)
35
+ .with(headers: { 'Authorization' => 'Bearer owner' })
36
+ ).to have_been_made
37
+ end
38
+ end
39
+
40
+ describe '.create_custom_token' do
41
+ context 'when credentials are set via GOOGLE_APPLICATION_CREDENTIALS' do
42
+ before do
43
+ ENV['GOOGLE_APPLICATION_CREDENTIALS'] = fixture('google_credentials.json').path
44
+ end
45
+
46
+ it 'returns a valid JWT token' do
47
+ token = @client.create_custom_token('user-123')
48
+ token_data, _alg = JWT.decode(token, nil, false)
49
+ expect(token_data['uid']).to eq('user-123')
50
+ end
51
+ end
52
+
53
+ context 'when credentials are set via GOOGLE_CLIENT_EMAIL / GOOGLE_PRIVATE_KEY' do
54
+ let(:email) { 'example@example.com' }
55
+ let(:private_key) { fixture('example_key').read }
56
+
57
+ before do
58
+ ENV['GOOGLE_CLIENT_EMAIL'] = email
59
+ ENV['GOOGLE_PRIVATE_KEY'] = private_key
60
+ end
61
+
62
+ it 'returns a valid JWT token' do
63
+ token = @client.create_custom_token('user-123')
64
+ token_data, _alg = JWT.decode(token, nil, false)
65
+ expect(token_data['uid']).to eq('user-123')
66
+ end
67
+ end
68
+ end
69
+
70
+ describe '.sign_in_for_uid' do
71
+ it 'should post to the update endpoint' do
72
+ expect(@client).to receive(:create_custom_token).with('user-123').and_return('token')
73
+ expect(@client).to receive(:sign_in_with_custom_token).with('token').and_return('result')
74
+ result = @client.sign_in_for_uid('user-123')
75
+ expect(result).to eq('result')
76
+ end
77
+ end
23
78
  end
@@ -0,0 +1 @@
1
+ -----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoI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kvft\n8GL9g+8QLQKBgQCdDvH9olGLJB9pW/QhyGjfek8Ykibld8eJ5YPgXfDJJcQCb7rM\nrHV5HxGqe14SZ6I8Ozy9qtnmTnyVw/iV2SjigqIvGnPqBk/qGlohTH9wHEI/KaDZ\nz1IMrakN0lxnmb6sViCirIQMFSKAGjFNJBNmVd81GAMrU6NrR8gND2d9wQKBgEbF\ntPlYQkTJdOtJ+crWeSOdZXYB63dML9voJ9W2ePkGiZwSg4gxYN1LUIhEiiNU7+Cv\nFW49Axd48s8DbgKSS03pVFXMqLZLtJzSs9qJftaDAfYvW75ASxEoWo1Ub+gTpt7v\nWIKbxcSXXCnzBRtxQOsLZiY6YSGW4vZnrfJ5atoxAoGBAJF9oxseUWLFi8G9jaMD\nBnGqha7wCrQE6rFjgG9olbYxVB0IYN6hnPoelkrJWDJDz9bpvXSsbO7lwFv0x3Ks\n6ehj1fDjibTz9BNiqNwFzoPWq4Th2TqHrYY34ohtcW4tQdtLYPgQyhmNASUytAlG\nzipYnskyVze/3An/VEFrhSdy\n-----END PRIVATE KEY-----\n
@@ -0,0 +1,12 @@
1
+ {
2
+ "type": "service_account",
3
+ "project_id": "example-project",
4
+ "private_key_id": "123",
5
+ "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDW04sWbA5qIYUQ\nLU+dVoEf0ipYYmu9/2Z+Teobkhd9f6ZQ++1a0hnpelIx3vulrO0uPU/CZIh75cPR\nl/UGg2wxB2QnQcznUCJylOKoV412L7nTauYolNkI71X95npQyQtP+i3E7mQHAEJc\nOqF+cd6ufH5KouKtkJvSioHVKMr1YWDAmCfvQdYQ9Chv4K3Fvr1yniFk7OqcB1Td\nf9XcMpCaC6XokOzgvZ0TbNoqG2ceR66E04J33nRenqiRv8I/jmzsvYb692CuzKEL\nf/F4eY8bnIRYrMgT+7qgBkLMbNN/7m95uoBvJASvosx9+Fp3h6q9DhdphyV0iFjL\nHtvFp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nzBRtxQOsLZiY6YSGW4vZnrfJ5atoxAoGBAJF9oxseUWLFi8G9jaMD\nBnGqha7wCrQE6rFjgG9olbYxVB0IYN6hnPoelkrJWDJDz9bpvXSsbO7lwFv0x3Ks\n6ehj1fDjibTz9BNiqNwFzoPWq4Th2TqHrYY34ohtcW4tQdtLYPgQyhmNASUytAlG\nzipYnskyVze/3An/VEFrhSdy\n-----END PRIVATE KEY-----\n",
6
+ "client_email": "example@example.iam.gserviceaccount.com",
7
+ "client_id": "456",
8
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
9
+ "token_uri": "https://oauth2.googleapis.com/token",
10
+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11
+ "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/example%40example.iam.gserviceaccount.com"
12
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "kind": "identitytoolkit#SignupNewUserResponse",
3
+ "localId": "abcdefghijklmnopqrstuvwxyzab",
4
+ "displayName": "John Smith",
5
+ "email": "john@smith.com"
6
+ }
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.2
4
+ version: 0.1.3
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-03-11 00:00:00.000000000 Z
11
+ date: 2021-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -185,7 +185,10 @@ 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/example_key
188
189
  - spec/fixtures/gateway_timeout.html
190
+ - spec/fixtures/google_credentials.json
191
+ - spec/fixtures/update_account.json
189
192
  - spec/spec_helper.rb
190
193
  homepage: https://github.com/col/firebase-admin
191
194
  licenses:
@@ -219,5 +222,8 @@ test_files:
219
222
  - spec/fixtures/400_error.json
220
223
  - spec/fixtures/bad_gateway.html
221
224
  - spec/fixtures/create_account.json
225
+ - spec/fixtures/example_key
222
226
  - spec/fixtures/gateway_timeout.html
227
+ - spec/fixtures/google_credentials.json
228
+ - spec/fixtures/update_account.json
223
229
  - spec/spec_helper.rb