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

fixed broken auto-detection of auth--legacy database secret v.s. admin json pk #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
41 changes: 24 additions & 17 deletions lib/firebase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,35 @@ module Firebase
class Client
attr_reader :auth, :request

def initialize(base_uri, auth=nil)

# @param base_uri [type] [description]
# @param auth=nil Firebase Admin SDK private key (as string), or the legacy Firebase Database Secret
# @param use_legacy_database_secret set to true if the auth param is a legacy firebase database secret. otherwise leave nil or false
def initialize(base_uri, auth=nil, use_legacy_database_secret=nil)
if base_uri !~ URI::regexp(%w(https))
raise ArgumentError.new('base_uri must be a valid https uri')
end
base_uri += '/' unless base_uri.end_with?('/')
@request = HTTPClient.new({
:base_url => base_uri,
:default_header => {
'Content-Type' => 'application/json'
}
})
if auth && valid_json?(auth)
# Using Admin SDK service account
@credentials = Google::Auth::DefaultCredentials.make_creds(
json_key_io: StringIO.new(auth),
scope: %w(https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email)
)
@credentials.apply!(@request.default_header)
@expires_at = @credentials.issued_at + 0.95 * @credentials.expires_in
else
if auth && use_legacy_database_secret
# Using deprecated Database Secret
@secret = auth
else

base_uri += '/' unless base_uri.end_with?('/')
@request = HTTPClient.new({
:base_url => base_uri,
:default_header => {
'Content-Type' => 'application/json'
}
})
if auth && valid_json?(auth)
# Using Admin SDK service account
@credentials = Google::Auth::DefaultCredentials.make_creds(
json_key_io: StringIO.new(auth),
scope: %w(https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email)
)
@credentials.apply!(@request.default_header)
@expires_at = @credentials.issued_at + 0.95 * @credentials.expires_in
end
end
end

Expand Down