firebase-admin-sdk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ require "json"
2
+
3
+ module Firebase
4
+ module Admin
5
+ # The name of the environment variable to lookup a Firebase config.
6
+ FIREBASE_CONFIG_ENV_VAR = "FIREBASE_CONFIG"
7
+
8
+ # Configuration options used to initialize an App.
9
+ class Config
10
+ attr_reader :project_id, :service_account_id
11
+
12
+ class << self
13
+ # Loads a configuration using the FIREBASE_CONFIG environment variable.
14
+ #
15
+ # If the value of the FIREBASE_CONFIG environment variable starts with "{" it is
16
+ # parsed as a JSON object, otherwise it is interpreted as a path to the config file.
17
+ #
18
+ # @return [Firebase::Admin::Config]
19
+ def from_env
20
+ config = ENV[FIREBASE_CONFIG_ENV_VAR]
21
+ return new if config.nil?
22
+ return from_json(config) if config.start_with?("{")
23
+ from_file(config)
24
+ end
25
+
26
+ # Loads a configuration from a file.
27
+ #
28
+ # @param [File, String] file
29
+ # The path of the configuration file.
30
+ #
31
+ # @return [Firebase::Admin::Config]
32
+ def from_file(file)
33
+ json = file.is_a?(File) ? file.read : File.read(file)
34
+ from_json(json)
35
+ end
36
+
37
+ # Loads a configuration from JSON.
38
+ #
39
+ # @param [String] json
40
+ # A configuration encoded as a JSON string.
41
+ #
42
+ # @return [Firebase::Admin::Config]
43
+ def from_json(json)
44
+ data = JSON.parse(json)
45
+ new(project_id: data["projectId"],
46
+ service_account_id: data["serviceAccountId"])
47
+ end
48
+ end
49
+
50
+ # Initializes the configuration object.
51
+ def initialize(project_id: nil, service_account_id: nil)
52
+ @project_id = project_id
53
+ @service_account_id = service_account_id
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,79 @@
1
+ require "stringio"
2
+ require "googleauth/default_credentials"
3
+ require "googleauth/application_default"
4
+
5
+ module Firebase
6
+ module Admin
7
+ SCOPE = %w[
8
+ https://www.googleapis.com/auth/cloud-platform
9
+ https://www.googleapis.com/auth/datastore
10
+ https://www.googleapis.com/auth/devstorage.read_write
11
+ https://www.googleapis.com/auth/firebase
12
+ https://www.googleapis.com/auth/identitytoolkit
13
+ https://www.googleapis.com/auth/userinfo.email
14
+ ]
15
+
16
+ # Firebase credentials.
17
+ class Credentials
18
+ class << self
19
+ # Loads google credentials from a specified file path.
20
+ #
21
+ # @param [File, String] file
22
+ # The credentials file path
23
+ #
24
+ # @return [Firebase::Admin::Credentials]
25
+ def from_file(file)
26
+ json = file.is_a?(File) ? file.read : File.read(file)
27
+ from_json(json)
28
+ end
29
+
30
+ # Loads google credentials from a JSON string.
31
+ #
32
+ # @param [String] json
33
+ # A JSON string containing valid google credentials.
34
+ #
35
+ # @return [Firebase::Admin::Credentials]
36
+ def from_json(json)
37
+ io = StringIO.new(json)
38
+ new(Google::Auth::DefaultCredentials.make_creds(scope: SCOPE, json_key_io: io))
39
+ end
40
+
41
+ # Loads application default credentials.
42
+ #
43
+ # @return [Firebase::Admin::Credentials]
44
+ def from_default
45
+ new(Google::Auth.get_application_default(SCOPE))
46
+ end
47
+ end
48
+
49
+ # Gets the google credentials
50
+ attr_reader :credentials
51
+
52
+ # Constructs a Credential from the specified Google Credentials.
53
+ #
54
+ # @param [Google::Auth::ServiceAccountCredentials, Google::Auth::UserRefreshCredentials, Google::Auth::GCECredentials] credentials
55
+ # The google credentials to connect with.
56
+ def initialize(credentials)
57
+ raise ArgumentError, "credentials cannot be nil" if credentials.nil?
58
+ @credentials = credentials
59
+ end
60
+
61
+ # Gets the google project id
62
+ def project_id
63
+ @credentials.project_id
64
+ end
65
+
66
+ # Apply the credentials
67
+ def apply!(hash, opts = {})
68
+ @credentials.apply!(hash, opts)
69
+ end
70
+ end
71
+
72
+ # Firebase Admin emulator credentials.
73
+ class EmulatorCredentials
74
+ def apply!(hash, opts = {})
75
+ hash[:authorization] = "Bearer owner"
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,9 @@
1
+ module Firebase
2
+ module Admin
3
+ # A base class for errors raised by the admin sdk.
4
+ class Error < StandardError; end
5
+
6
+ # Raised when there is an incorrect argument.
7
+ class ArgumentError < Error; end
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ require "faraday"
2
+ require "googleauth/compute_engine"
3
+
4
+ module Firebase
5
+ module Admin
6
+ module GCE
7
+ # The compute engine metadata project id endpoint.
8
+ PROJECT_ID_URI =
9
+ "#{Google::Auth::GCECredentials.compute_check_uri}/computeMetadata/v1/project/project-id".freeze
10
+
11
+ # Patches GCECredentials to lookup the project id using the metadata service when on compute engine.
12
+ class ::Google::Auth::GCECredentials
13
+ # The compute engine project id
14
+ def project_id
15
+ @project_id ||= fetch_project_id
16
+ end
17
+
18
+ private
19
+
20
+ # Fetches the project id using the compute engine metadata service.
21
+ def fetch_project_id
22
+ connection = build_default_connection || Faraday.default_connection
23
+ retry_with_error do
24
+ uri = Firebase::Admin::GCE::PROJECT_ID_URI
25
+ resp = connection.get(uri) do |req|
26
+ req.headers["Metadata-Flavor"] = "Google"
27
+ end
28
+ case resp.status
29
+ when 200
30
+ resp.body
31
+ when 404
32
+ raise Signet::AuthorizationError, NO_METADATA_SERVER_ERROR
33
+ else
34
+ msg = "Unexpected error code #{resp.status} #{UNEXPECTED_ERROR_SUFFIX}"
35
+ raise Signet::AuthorizationError, msg
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,78 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+
4
+ module Firebase
5
+ module Admin
6
+ module Internal
7
+ # A class to work with
8
+ class HTTPClient
9
+ # The admin sdk user agent.
10
+ USER_AGENT = "Firebase/HTTP/#{VERSION}/#{RUBY_VERSION}/AdminRuby"
11
+
12
+ def initialize(uri: nil, credentials: nil)
13
+ @uri = uri ? Faraday::Utils::URI(uri) : nil
14
+ @credentials = credentials
15
+ end
16
+
17
+ def head(url = nil, params = nil, headers = nil)
18
+ connection.head(url, params, headers)
19
+ end
20
+
21
+ def get(url = nil, params = nil, headers = nil)
22
+ connection.get(url, params, headers)
23
+ end
24
+
25
+ def delete(url = nil, params = nil, headers = nil)
26
+ connection.delete(url, params, headers)
27
+ end
28
+
29
+ def trace(url = nil, params = nil, headers = nil)
30
+ connection.trace(url, params, headers)
31
+ end
32
+
33
+ def put(url = nil, params = nil, headers = nil)
34
+ connection.put(url, params, headers)
35
+ end
36
+
37
+ def post(url = nil, params = nil, headers = nil)
38
+ connection.post(url, params, headers)
39
+ end
40
+
41
+ def patch(url = nil, params = nil, headers = nil)
42
+ connection.patch(url, params, headers)
43
+ end
44
+
45
+ private
46
+
47
+ # @return [Faraday::Connection]
48
+ def connection
49
+ options = {
50
+ headers: {
51
+ Accept: "application/json; charset=utf-8",
52
+ "Content-Type": "application/json; charset=utf-8",
53
+ "User-Agent": USER_AGENT
54
+ }
55
+ }
56
+
57
+ @connection ||= Faraday::Connection.new(@uri, options) do |c|
58
+ c.use CredentialsMiddleware, credentials: @credentials unless @credentials.nil?
59
+ c.use Faraday::Request::UrlEncoded
60
+ c.use FaradayMiddleware::EncodeJson
61
+ c.use Faraday::Response::ParseJson
62
+ c.use Faraday::Response::RaiseError
63
+ c.adapter(Faraday.default_adapter)
64
+ end
65
+ end
66
+
67
+ # Middleware for applying credentials to authenticate requests.
68
+ class CredentialsMiddleware < Faraday::Middleware
69
+ def on_request(env)
70
+ creds = options[:credentials]
71
+ creds.apply!(env[:request_headers])
72
+ end
73
+ end
74
+ private_constant :CredentialsMiddleware
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Firebase
4
+ module Admin
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,230 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: firebase-admin-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tariq Zaid
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: googleauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.16.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.16.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "<"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jwt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '3.0'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '1.5'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '13.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '13.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: webmock
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '3.13'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '3.13'
117
+ - !ruby/object:Gem::Dependency
118
+ name: fakefs
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 1.3.2
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 1.3.2
131
+ - !ruby/object:Gem::Dependency
132
+ name: climate_control
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ - !ruby/object:Gem::Dependency
146
+ name: standard
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ - !ruby/object:Gem::Dependency
160
+ name: activesupport
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ description:
174
+ email:
175
+ - tariq@cheddar.me
176
+ executables: []
177
+ extensions: []
178
+ extra_rdoc_files: []
179
+ files:
180
+ - ".github/workflows/main.yml"
181
+ - ".gitignore"
182
+ - ".rspec"
183
+ - ".ruby-version"
184
+ - Gemfile
185
+ - LICENSE.txt
186
+ - README.md
187
+ - Rakefile
188
+ - bin/console
189
+ - bin/setup
190
+ - firebase-admin-sdk.gemspec
191
+ - lib/firebase-admin-sdk.rb
192
+ - lib/firebase/admin/app.rb
193
+ - lib/firebase/admin/auth/certificates_fetcher.rb
194
+ - lib/firebase/admin/auth/client.rb
195
+ - lib/firebase/admin/auth/error.rb
196
+ - lib/firebase/admin/auth/token_verifier.rb
197
+ - lib/firebase/admin/auth/user_info.rb
198
+ - lib/firebase/admin/auth/user_manager.rb
199
+ - lib/firebase/admin/auth/user_record.rb
200
+ - lib/firebase/admin/auth/utils.rb
201
+ - lib/firebase/admin/config.rb
202
+ - lib/firebase/admin/credentials.rb
203
+ - lib/firebase/admin/error.rb
204
+ - lib/firebase/admin/gce.rb
205
+ - lib/firebase/admin/internal/http_client.rb
206
+ - lib/firebase/admin/version.rb
207
+ homepage: https://github.com/cheddar-me/firebase-admin-sdk-ruby
208
+ licenses:
209
+ - MIT
210
+ metadata: {}
211
+ post_install_message:
212
+ rdoc_options: []
213
+ require_paths:
214
+ - lib
215
+ required_ruby_version: !ruby/object:Gem::Requirement
216
+ requirements:
217
+ - - ">"
218
+ - !ruby/object:Gem::Version
219
+ version: '2.7'
220
+ required_rubygems_version: !ruby/object:Gem::Requirement
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ requirements: []
226
+ rubygems_version: 3.2.15
227
+ signing_key:
228
+ specification_version: 4
229
+ summary: Firebase Admin SDK for Ruby
230
+ test_files: []