class FirebaseTokenAuth::Configuration

Attributes

auth[RW]
client_email[RW]
exp_leeway[RW]
json_key_io[RW]
private_key[RW]
project_id[RW]
scope[RW]

Public Class Methods

new() click to toggle source
# File lib/firebase_token_auth/configuration.rb, line 8
def initialize
  @project_id = nil
  @exp_leeway = 60 * 60 * 24 * 7
  @scope = ['https://www.googleapis.com/auth/identitytoolkit']

  # if you want to create custom_token,
  # you need credentials which a) json_key_io or b) admin_email and admin_private_key

  # set file path or StringIO
  @json_key_io = nil

  # Or set these
  # ENV['GOOGLE_ACCOUNT_TYPE'] = 'service_account'
  # ENV['GOOGLE_CLIENT_ID'] = '000000000000000000000'
  # ENV['GOOGLE_CLIENT_EMAIL'] = 'xxxx@xxxx.iam.gserviceaccount.com'
  # ENV['GOOGLE_PRIVATE_KEY'] = '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n'
end

Public Instance Methods

configured_for_custom_token?() click to toggle source
# File lib/firebase_token_auth/configuration.rb, line 54
def configured_for_custom_token?
  json_key_io || (ENV['GOOGLE_PRIVATE_KEY'] && ENV['GOOGLE_CLIENT_EMAIL'])
end
prepare() click to toggle source
# File lib/firebase_token_auth/configuration.rb, line 26
def prepare
  raise ConfigurationError, 'project_id is required to use firebase_token_auth gem.' unless project_id
  return unless configured_for_custom_token?

  @auth = if json_key_io
            io = json_key_io.respond_to?(:read) ? json_key_io : File.open(json_key_io)
            io.rewind if io.respond_to?(:read) 
            Google::Auth::ServiceAccountCredentials.make_creds(
              json_key_io: io,
              scope: scope
            )
          else
            # from ENV
            Google::Auth::ServiceAccountCredentials.make_creds(scope: scope)
          end

  if json_key_io
    json_io = json_key_io.respond_to?(:read) ? json_key_io : File.open(json_key_io)
    json_io.rewind if json_key_io.respond_to?(:read) 
    parsed = JSON.parse(json_io.read)
    @private_key = OpenSSL::PKey::RSA.new(parsed['private_key'])
    @client_email = parsed['client_email']
  else
    @private_key = OpenSSL::PKey::RSA.new(unescape(ENV['GOOGLE_PRIVATE_KEY']))
    @client_email = ENV['GOOGLE_CLIENT_EMAIL']
  end
end
unescape(str) click to toggle source
# File lib/firebase_token_auth/configuration.rb, line 58
def unescape(str)
  str = str.gsub('\n', "\n")
  str = str[1..-2] if str.start_with?('"') && str.end_with?('"')
  str
end