lib/firebase-ruby/auth.rb in firebase-ruby-0.2.0.1 vs lib/firebase-ruby/auth.rb in firebase-ruby-0.3.0
- old
+ new
@@ -1,19 +1,20 @@
require 'jwt'
+require 'firebase-ruby/neko-http'
-module Firebase
+module Firebase
class Auth
-
GOOGLE_JWT_SCOPE = 'https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email'
- GOOGLE_JWT_AUD = 'https://www.googleapis.com/oauth2/v4/token'
+ GOOGLE_JWT_AUD = 'https://oauth2.googleapis.com/token'
GOOGLE_ALGORITHM = 'RS256'
GOOGLE_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
- GOOGLE_TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token'
+ GOOGLE_TOKEN_URL = 'https://oauth2.googleapis.com/token'
attr_reader :project_id
attr_reader :client_email
+ attr_reader :token_uri
attr_reader :access_token
attr_reader :expires
# Creates Firebase OAuth based auth object; one argument must be specified
def initialize(json: nil, path: nil)
@@ -26,11 +27,11 @@
# Return a valid access token; it will retrieve a new token if necessary
def valid_token
return access_token if access_token && !expiring?
return access_token if request_access_token
- return nil
+ raise 'No valid access token.'
end
# If token is expiring within a minute
def expiring?
return true if expires - Time.now < 60
@@ -50,11 +51,15 @@
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]
+ @token_uri = cred[:token_uri]
+ @token_uri ||= GOOGLE_TOKEN_URL
Firebase.logger.info('Private key loaded from JSON')
+ s = [:project_id, :client_email].map{ |x| "#{x}: #{self.public_send(x)}" }
+ Firebase.logger.debug("The key contained:\n#{s.join("\n")}")
end
# @param path [String] path to JSON file with private key
def load_privatekeyfile(path)
raise ArgumentError, 'private key file path missing' unless path
@@ -62,18 +67,25 @@
load_privatekeyjson(IO.read(path))
end
# Request new token from Google
def request_access_token
- Firebase.logger.info('Requesting access token to Google')
- res = HTTP.post_form(GOOGLE_TOKEN_URL, jwt)
+ Firebase.logger.info('Requesting access token...')
+ Firebase.logger.debug("token_uri: #{token_uri}")
+ res = Neko::HTTP.post_form(token_uri, jwt)
Firebase.logger.debug("HTTP response code: #{res[:code]}")
if res.class == Hash && res[:code] == 200
data = JSON.parse(res[:body], {symbolize_names: true})
@access_token = data[:access_token]
@expires = Time.now + data[:expires_in]
+ Firebase.logger.info('Access token acquired.')
+ s = "Token #{@access_token.length} bytes, expires #{@expires}"
+ Firebase.logger.debug(s)
return true
+ else
+ Firebase.logger.error('Access token request failed.')
+ Firebase.logger.debug("HTTP #{res[:code]} #{res[:message]}")
end
return false
end
# Generate JWT claim
@@ -88,9 +100,7 @@
exp: now_ts + 60
}
jwt = JWT.encode payload, pkey, GOOGLE_ALGORITHM
return {grant_type: GOOGLE_GRANT_TYPE, assertion: jwt}
end
-
end
-
end