Class: Fastlane::Firebase::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/firebase/lib/api.rb

Defined Under Namespace

Classes: BadRequestError, LoginError

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ Api

Returns a new instance of Api.



16
17
18
19
20
21
22
23
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 16

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"

  (email, password)
end

Instance Method Details

#add_client(project_number, type, bundle_id, app_name, ios_appstore_id) ⇒ Object



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 125

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

#create_authorization_headersObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 69

def create_authorization_headers 
  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"]["/"])
  cookie_header = cookies.map { |el, cookie| "#{el}=#{cookie.value}" }.join(";")

  sapisid_hash = generate_sapisid_hash(time, sapisid, origin)
  sapisid_header = "SAPISIDHASH #{sapisid_hash}"

  json_headers = {
    'Authorization' => sapisid_header,
    'Cookie' => cookie_header,
    'X-Origin' => origin
  }

  json_headers
end

#delete_client(project_number, client_id) ⇒ Object



152
153
154
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 152

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 170

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

#generate_sapisid_hash(time, sapisid, origin) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 60

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



25
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
53
54
55
56
57
58
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 25

def (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}/")
  
  #First step - email
  google_form = page.form()
  google_form.Email = email

  #Send
  page = @agent.submit(google_form, google_form.buttons.first)
  
  #Second step - password
  google_form = page.form()
  google_form.Passwd = password

  #Send
  page = @agent.submit(google_form, google_form.buttons.first)
  match = page.search("script").text.scan(/\\x22api-key\\x22:\\x22(.*?)\\x22/)
  
  if match.count == 1 then
    @api_key = match[0][0]
    @authorization_headers = create_authorization_headers()
    UI.success "Successfuly logged in"
    true
  else
    if error = page.at("#errormsg_0_Passwd") then
      message = error.text.strip
    else
      message = "Unknown error"
    end
    raise LoginError, "Login failed: #{message}"
  end 
end

#project_listObject



117
118
119
120
121
122
123
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 117

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 92

def request_json(path, method = :get, parameters = Hash.new, headers = Hash.new)
    begin
    if method == :get then
      parameters["key"] = @api_key
      page = @agent.get("#{@sdk_url}#{path}", parameters, nil, headers.merge(@authorization_headers))
    elsif method == :post then
      headers['Content-Type'] = 'application/json'
      page = @agent.post("#{@sdk_url}#{path}?key=#{@api_key}", parameters.to_json, headers.merge(@authorization_headers))
    elsif method == :delete then
      page = @agent.delete("#{@sdk_url}#{path}?key=#{@api_key}", parameters, headers.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, body["error"]["message"]
        end
      end
      UI.crash! e.page.body
    end
end

#upload_certificate(project_number, client_id, type, certificate_value, certificate_password) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fastlane/plugin/firebase/lib/api.rb', line 156

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