Class: FirebaseTokenAuth::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/firebase_token_auth/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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'] = '[email protected]'
  # ENV['GOOGLE_PRIVATE_KEY'] = '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n'
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



6
7
8
# File 'lib/firebase_token_auth/configuration.rb', line 6

def auth
  @auth
end

#client_emailObject

Returns the value of attribute client_email.



6
7
8
# File 'lib/firebase_token_auth/configuration.rb', line 6

def client_email
  @client_email
end

#exp_leewayObject

Returns the value of attribute exp_leeway.



6
7
8
# File 'lib/firebase_token_auth/configuration.rb', line 6

def exp_leeway
  @exp_leeway
end

#json_key_ioObject

Returns the value of attribute json_key_io.



6
7
8
# File 'lib/firebase_token_auth/configuration.rb', line 6

def json_key_io
  @json_key_io
end

#private_keyObject

Returns the value of attribute private_key.



6
7
8
# File 'lib/firebase_token_auth/configuration.rb', line 6

def private_key
  @private_key
end

#project_idObject

Returns the value of attribute project_id.



6
7
8
# File 'lib/firebase_token_auth/configuration.rb', line 6

def project_id
  @project_id
end

#scopeObject

Returns the value of attribute scope.



6
7
8
# File 'lib/firebase_token_auth/configuration.rb', line 6

def scope
  @scope
end

Instance Method Details

#configured_for_custom_token?Boolean

Returns:

  • (Boolean)


54
55
56
# 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

#prepareObject

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



58
59
60
61
62
# 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