Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use new firebase admin sdk private key #81

Merged
merged 3 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ response.raw_body # => '{"name":"-INOQPH-aV_psbk3ZXEX"}'

If you have a read-only namespace, set your secret key as follows:
```ruby
firebase = Firebase::Client.new(base_uri, secret_key)
# Using Firebase Admin SDK private key
firebase = Firebase::Client.new(base_uri, "/path/to/private_key.json")

response = firebase.push("todos", { :name => 'Pick the milk', :'.priority' => 1 })
# Using Firebase Database Secret (deprecated)
firebase = Firebase::Client.new(base_uri, db_secret)
```

You can now pass custom query options to firebase:
Expand Down
1 change: 1 addition & 0 deletions firebase.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'httpclient', '>= 2.5.3'
s.add_runtime_dependency 'json'
s.add_runtime_dependency 'googleauth'
s.add_development_dependency 'rake'
s.add_development_dependency 'rdoc'
s.add_development_dependency 'rspec'
Expand Down
18 changes: 14 additions & 4 deletions lib/firebase.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'firebase/response'
require 'firebase/server_value'
require 'googleauth'
require 'httpclient'
require 'json'
require 'uri'
Expand All @@ -13,13 +14,22 @@ def initialize(base_uri, auth=nil)
raise ArgumentError.new('base_uri must be a valid https uri')
end
base_uri += '/' unless base_uri.end_with?('/')
default_header = {
'Content-Type' => 'application/json'
}
if auth && File.file?(auth)
# Using Admin SDK private key file
scopes = %w(https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email)
credentials = Google::Auth::DefaultCredentials.make_creds(json_key_io: File.open(auth), scope: scopes)
default_header = credentials.apply(default_header)
else
# Using deprecated Database Secret
@auth = auth
end
@request = HTTPClient.new({
:base_url => base_uri,
:default_header => {
'Content-Type' => 'application/json'
}
:default_header => default_header
})
@auth = auth
end

# Writes and returns the data
Expand Down
18 changes: 17 additions & 1 deletion spec/firebase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
end

describe "http processing" do
it "sends custom auth" do
it "sends custom auth query" do
firebase = Firebase::Client.new('https://test.firebaseio.com', 'secret')
expect(firebase.request).to receive(:request).with(:get, "todos.json", {
:body => nil,
Expand All @@ -133,5 +133,21 @@
})
firebase.get('todos', :foo => 'bar')
end

it "sets custom auth header" do
expect(File).to receive(:file?).with('test_private_key.json').and_return(true)
expect(File).to receive(:open).with('test_private_key.json').and_return('private key')
credentials = double()
expect(credentials).to receive(:apply).with({ 'Content-Type' => 'application/json' }).and_return({ :authorization => 'Bearer abcdef', 'Content-Type' => 'application/json' })
expect(Google::Auth::DefaultCredentials).to receive(:make_creds).with(json_key_io: 'private key', scope: instance_of(Array)).and_return(credentials)
expect(HTTPClient).to receive(:new).with({
:base_url => 'https://test.firebaseio.com/',
:default_header => {
:authorization => 'Bearer abcdef',
'Content-Type' => 'application/json'
}
})
firebase = Firebase::Client.new('https://test.firebaseio.com/', 'test_private_key.json')
end
end
end