Class: Fastlane::Firebase::Api
- Inherits:
-
Object
- Object
- Fastlane::Firebase::Api
show all
- Defined in:
- lib/fastlane/plugin/firebase/lib/api.rb
Defined Under Namespace
Classes: BadRequestError, LoginError
Instance Method Summary
collapse
-
#add_client(project_number, type, bundle_id, app_name, ios_appstore_id) ⇒ Object
-
#captcha_challenge(page) ⇒ Object
-
#create_authorization_headers ⇒ Object
-
#delete_client(project_number, client_id) ⇒ Object
-
#download_config_file(project_number, client_id) ⇒ Object
-
#extract_api_key(page) ⇒ Object
-
#generate_sapisid_hash(time, sapisid, origin) ⇒ Object
-
#initialize(email, password) ⇒ Api
constructor
-
#login(email, password) ⇒ Object
-
#project_list ⇒ Object
-
#request_json(path, method = :get, parameters = Hash.new, headers = Hash.new) ⇒ Object
-
#signin_challenge(page) ⇒ Object
-
#upload_certificate(project_number, client_id, type, certificate_value, certificate_password) ⇒ Object
Constructor Details
#initialize(email, password) ⇒ Api
Returns a new instance of Api.
20
21
22
23
24
25
26
27
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 20
def initialize(email, password)
@agent = Mechanize.new
@base_url = "https://console.firebase.google.com"
@sdk_url = "https://mobilesdk-pa.clients6.google.com/"
@login_url = "https://accounts.google.com/ServiceLogin"
login(email, password)
end
|
Instance Method Details
#add_client(project_number, type, bundle_id, app_name, ios_appstore_id) ⇒ Object
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 217
def add_client(project_number, type, bundle_id, app_name, ios_appstore_id )
parameters = {
"requestHeader" => { "clientVersion" => "FIREBASE" },
"displayName" => app_name || ""
}
case type
when :ios
parameters["iosData"] = {
"bundleId" => bundle_id,
"iosAppStoreId" => ios_appstore_id || ""
}
when :android
parameters["androidData"] = {
"packageName" => bundle_id
}
end
json = request_json("v1/projects/#{project_number}/clients", :post, parameters)
if client = json["client"] then
UI.success "Successfuly added client #{bundle_id}"
client
else
UI.error "Client could not be added"
end
end
|
#captcha_challenge(page) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 83
def captcha_challenge(page)
if UI.confirm "To proceed you need to fill in captcha. Do you want to download captcha image?" then
img_src = page.xpath("//div[@class='captcha-img']/img").attribute("src").value
image = @agent.get(img_src)
if image != nil then
UI.success "Captcha image downloaded"
else
UI.crash! "Failed to download captcha image"
end
file = Tempfile.new(["firebase_captcha_image", ".jpg"])
path = file.path
image.save!(path)
UI.success "Captcha image saved at #{path}"
if UI.confirm "Preview image?" then
if system("qlmanage -p #{path} >& /dev/null &") != true && system("open #{path} 2> /dev/null") != true then
UI.error("Unable to find program to preview the image, open it manually")
end
end
captcha = UI.input "Enter captcha (case insensitive):"
password = UI.password "Re-enter password:"
captcha_form = page.form()
captcha_form.logincaptcha = captcha
captcha_form.Passwd = password
page = @agent.submit(captcha_form, captcha_form.buttons.first)
return page
else
return nil
end
end
|
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 161
def
cookie = @agent.cookie_jar.jar["google.com"]["/"]["SAPISID"]
sapisid = cookie.value
origin = @base_url
time = Time.now.to_i
sapisid_hash = generate_sapisid_hash(time, sapisid, origin)
cookies = @agent.cookie_jar.jar["google.com"]["/"].merge(@agent.cookie_jar.jar["console.firebase.google.com"]["/"])
= cookies.map { |el, cookie| "#{el}=#{cookie.value}" }.join(";")
sapisid_hash = generate_sapisid_hash(time, sapisid, origin)
= "SAPISIDHASH #{sapisid_hash}"
= {
'Authorization' => ,
'Cookie' => ,
'X-Origin' => origin
}
end
|
#delete_client(project_number, client_id) ⇒ Object
244
245
246
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 244
def delete_client(project_number, client_id)
json = request_json("v1/projects/#{project_number}/clients/#{client_id}", :delete)
end
|
#download_config_file(project_number, client_id) ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 262
def download_config_file(project_number, client_id)
request = "[\"getArtifactRequest\",null,\"#{client_id}\",\"1\",\"#{project_number}\"]"
code = (client_id.start_with? "ios") ? "1" : "2"
url = @base_url + "/m/mobilesdk/projects/" + project_number + "/clients/" + CGI.escape(client_id) + "/artifacts/#{code}?param=" + CGI.escape(request)
UI.message "Downloading config file"
begin
config = @agent.get url
UI.success "Successfuly downloaded #{config.filename}"
config
rescue Mechanize::ResponseCodeError => e
UI.crash! e.page.body
end
end
|
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 71
def (page)
match = page.search("script").text.scan(/\\x22api-key\\x22:\\x22(.*?)\\x22/)
if match.count == 1 then
@api_key = match[0][0]
@authorization_headers = ()
return true
end
return false
end
|
#generate_sapisid_hash(time, sapisid, origin) ⇒ Object
152
153
154
155
156
157
158
159
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 152
def generate_sapisid_hash(time, sapisid, origin)
to_hash = time.to_s + " " + sapisid + " " + origin.to_s
hash = Digest::SHA1.hexdigest(to_hash)
sapisid_hash = time.to_s + "_" + hash
sapisid_hash
end
|
#login(email, password) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 29
def login(email, password)
UI.message "Logging in to Google account #{email}"
page = @agent.get("#{@login_url}?passive=1209600&osid=1&continue=#{@base_url}/&followup=#{@base_url}/")
google_form = page.form()
google_form.Email = email
page = @agent.submit(google_form, google_form.buttons.first)
google_form = page.form()
google_form.Passwd = password
page = @agent.submit(google_form, google_form.buttons.first)
while page do
if (page) then
UI.success "Successfuly logged in"
return true
else
if error = page.at("#errormsg_0_Passwd") then
message = error.text.strip
elsif page.xpath("//div[@class='captcha-img']").count > 0 then
page = captcha_challenge(page)
next
elsif page.form.action.include? "/signin/challenge" then
page = signin_challenge(page)
next
else
message = "Unknown error"
end
raise LoginError, "Login failed: #{message}"
end
end
end
|
#project_list ⇒ Object
209
210
211
212
213
214
215
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 209
def project_list
UI.message "Retrieving project list"
json = request_json("v1/projects")
projects = json["project"] || []
UI.success "Found #{projects.count} projects"
projects
end
|
#request_json(path, method = :get, parameters = Hash.new, headers = Hash.new) ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 184
def request_json(path, method = :get, parameters = Hash.new, = Hash.new)
begin
if method == :get then
parameters["key"] = @api_key
page = @agent.get("#{@sdk_url}#{path}", parameters, nil, .merge(@authorization_headers))
elsif method == :post then
['Content-Type'] = 'application/json'
page = @agent.post("#{@sdk_url}#{path}?key=#{@api_key}", parameters.to_json, .merge(@authorization_headers))
elsif method == :delete then
page = @agent.delete("#{@sdk_url}#{path}?key=#{@api_key}", parameters, .merge(@authorization_headers))
end
JSON.parse(page.body)
rescue Mechanize::ResponseCodeError => e
code = e.response_code.to_i
if code >= 400 && code < 500 then
if body = JSON.parse(e.page.body) then
raise BadRequestError.new(body["error"]["message"], code)
end
end
UI.crash! e.page.body
end
end
|
#signin_challenge(page) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 122
def signin_challenge(page)
UI. "Sign-in challenge"
form_id = "challenge"
form = page.form_with(:id => form_id)
type = (form["challengeType"] || "-1").to_i
if type == 9 || type == 6 then
div = page.at("##{form_id} div")
if div != nil then
UI.important div.xpath("div[1]").text
UI.important div.xpath("div[2]").text
end
prefix = type == 9 ? " G-" : ""
code = UI.input "Enter code#{prefix}:"
form.Pin = code
page = @agent.submit(form, form.buttons.first)
return page
elsif type == 4 then
UI.user_error! "Google prompt is not supported as a two-step verification"
else
html = page.at("##{form_id}").to_html
UI.user_error! "Unknown challenge type \n\n#{html}"
end
return nil
end
|
#upload_certificate(project_number, client_id, type, certificate_value, certificate_password) ⇒ Object
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 248
def upload_certificate(project_number, client_id, type, certificate_value, certificate_password)
prefix = type == :development ? "debug" : "prod"
parameters = {
"#{prefix}ApnsCertificate" => {
"certificateValue" => certificate_value,
"apnsPassword" => certificate_password
}
}
json = request_json("v1/projects/#{project_number}/clients/#{client_id}:setApnsCertificate", :post, parameters)
end
|