lib/firebase-ruby/auth.rb in firebase-ruby-0.0.2 vs lib/firebase-ruby/auth.rb in firebase-ruby-0.1.0
- old
+ new
@@ -13,12 +13,17 @@
attr_reader :project_id
attr_reader :client_email
attr_reader :access_token
attr_reader :expires
- def initialize(path)
- load_privatekeyfile(path)
+ # Creates Firebase OAuth based auth object; one argument must be specified
+ def initialize(json: nil, path: nil)
+ if json
+ load_privatekeyjson(json)
+ elsif path
+ load_privatekeyfile(path)
+ end
end
# Return a valid access token; it will retrieve a new token if necessary
def valid_token
return access_token if access_token && !expiring?
@@ -38,18 +43,24 @@
return false
end
private
- # @param path [String] JSON file with private key
- def load_privatekeyfile(path)
- raise ArgumentError, 'private key file path missing' unless path
- Firebase.logger.debug("Loading private key file: #{path}")
- cred = JSON.parse(IO.read(path), {symbolize_names: true})
+ # @param json [String] JSON with private key
+ def load_privatekeyjson(json)
+ raise ArgumentError, 'private key JSON missing' unless json
+ cred = JSON.parse(json, {symbolize_names: true})
@private_key = cred[:private_key]
@project_id = cred[:project_id]
@client_email = cred[:client_email]
- Firebase.logger.info('Auth.load_privatekeyfile done.')
+ Firebase.logger.info('Private key loaded from JSON')
+ end
+
+ # @param path [String] path to JSON file with private key
+ def load_privatekeyfile(path)
+ raise ArgumentError, 'private key file path missing' unless path
+ Firebase.logger.debug("Loading private key file: #{path}")
+ load_privatekeyjson(IO.read(path))
end
# Request new token from Google
def request_access_token
Firebase.logger.info('Requesting access token to Google')