Class: Googlepub::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/googlepub/auth.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Auth

Returns a new instance of Auth.



12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/googlepub/auth.rb', line 12

def initialize(options = {})
  if !options["package"] && !ENV['PACKAGE']
    puts "Please provide the package you want to EDIT on (Use option -p or --package for ease): "
    package = gets.chomp
    package = eval %Q{#{package}}
  else
    package = options["package"] || ENV['PACKAGE']
  end
  @package = package
  ENV['PACKAGE'] = package
  does_accesstoken_exists
  if !ENV['ACCESS_TOKEN']
    find_key_and_iss
    if !options["key"] && !ENV['KEY']
      puts "Please provide the key for the Account on Google Developer Console (use option -k or --key for ease): "
      @key = gets.chomp
      @key = eval %Q{#{@key}}
    else
      @key = options["key"] || ENV['KEY']
    end
    ENV['KEY'] = @key

    if !options["iss"] && !ENV['ISS']
      puts "Please provide the ISS for the account on Google Developer Console (use -i or -iss for ease): "
      @iss = gets.chomp
      @iss = eval %Q{#{@iss}}
    else
      @iss  = options["iss"] || ENV['ISS']
    end
    ENV['ISS'] = @iss

    if (!@key || !@iss || !@package)
      puts "Invalid Arguments!".red
    end

    self.new_access_token
  else
    @access_token = ENV['ACCESS_TOKEN']
  end

end

Instance Method Details

#commit_editObject

Commit the Edit ##########################



120
121
122
123
124
125
126
127
128
129
# File 'lib/googlepub/auth.rb', line 120

def commit_edit
  response_commit = HTTParty.post("https://www.googleapis.com/androidpublisher/v2/applications/#{@package}/edits/#{@edit_id}:commit?access_token=#{@access_token}", :body =>{}).parsed_response
  if response_commit["id"] == @edit_id
    puts "Committed!".green
  else
    puts "Commit Error! please check!".red
    puts response_commit
    exit 1
  end
end

#does_accesstoken_existsObject



152
153
154
155
156
157
158
159
160
161
# File 'lib/googlepub/auth.rb', line 152

def does_accesstoken_exists
  accessfile = Dir["access_token"][0]
  if accessfile
    file = File.open("#{accessfile}", 'r')
    if file.size != 0
      @access_token = eval %Q{"#{file.read}"}
      validate_token_or_generate
    end
  end
end

#editObject

A new EDIT ##########################



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/googlepub/auth.rb', line 91

def edit
  puts "Generating Edit for package: #{@package}"
  headers = {"Authorization" => "Bearer #{@access_token}"}

  edit_response = HTTParty.post("https://www.googleapis.com/androidpublisher/v2/applications/#{@package}/edits", :headers => headers, :body => {}).parsed_response

  if !edit_response["id"]
    puts "Invlaid Edit, No id returned, please check!".red
    p edit_response
    exit 1
  end
  ENV['EDIT_ID'] = edit_response["id"]
  @edit_id = edit_response["id"]
  puts "Edit ID generated: #{ENV['EDIT_ID']}".green
end

#find_key_and_issObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/googlepub/auth.rb', line 131

def find_key_and_iss
  keyfile = Dir["keyfile"][0]
  if keyfile
    file = File.open("#{keyfile}", 'r')
    if file.size != 0
      @key = eval %Q{"#{file.read}"}
      ENV['KEY'] = @key
    end
  end

  issfile = Dir["issfile"][0]
  if issfile
    file = File.open("#{issfile}", 'r')
    if file.size != 0
      @iss = eval %Q{"#{file.read}"}
      ENV['ISS'] = @iss
    end
  end

end

#new_access_tokenObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/googlepub/auth.rb', line 54

def new_access_token
  header = {"alg" => "RS256","typ" => "JWT"}
  claim_set = {
    "iss"=>@iss,
    "scope"=> 'https://www.googleapis.com/auth/androidpublisher',
    "aud"=>"https://www.googleapis.com/oauth2/v3/token",
    "exp" => Time.now.to_i +  3600,
    "iat" => Time.now.to_i
  }
  private_key = @key.to_s
  @key = OpenSSL::PKey.read(private_key)
  if !@key
    puts "Invlaid OPENSLL Key, please check!".red
    exit 1
  end
  jwt = JWT.encode(claim_set, @key, "RS256", header)
  if !jwt
    puts "Invlaid JWT, please check!".red
    exit 1
  end
  data = {"grant_type" => "urn:ietf:params:oauth:grant-type:jwt-bearer", "assertion" => "#{jwt}"}
  response = HTTParty.post("https://www.googleapis.com/oauth2/v3/token", :body => data)
  if !response.parsed_response["access_token"]
    puts "Invlaid or Not generated Access Token, please try again!".red
    puts response.parsed_response
    exit 1
  end
  access_token = response.parsed_response["access_token"] #Access Token valid for 60 minutes
  ENV['ACCESS_TOKEN'] = access_token
  @access_token = access_token
  accessfile = File.new("access_token", "w")
  accessfile.write(@access_token)
  accessfile.close
  puts "Access Token: #{@access_token}".green
end

#validate_editObject

Validate Edit ##########################



108
109
110
111
112
113
114
115
116
117
# File 'lib/googlepub/auth.rb', line 108

def validate_edit
  response_validate = HTTParty.post("https://www.googleapis.com/androidpublisher/v2/applications/#{@package}/edits/#{@edit_id}:validate?access_token=#{@access_token}", :body =>{}).parsed_response
  if response_validate["id"] == @edit_id
    puts "Validated: Good to Go".green
  else
    puts "Invalid Edit, please check!".red
    puts response_validate
    exit 1
  end
end

#validate_token_or_generateObject



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/googlepub/auth.rb', line 163

def validate_token_or_generate
  response_validate = HTTParty.get("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=#{@access_token}").parsed_response
  if response_validate["issued_to"] && response_validate["expires_in"] >=300
    ENV['ACCESS_TOKEN'] = @access_token
    puts "Using Access Token: #{ENV['ACCESS_TOKEN']}"
  else
    @access_token = nil
    accessfile =  Dir["access_token"][0]
    if accessfile
      File.delete("access_token")
    end
  end
end