summaryrefslogtreecommitdiff
path: root/bin/jwt
blob: 8b8c1f7271169683e7e7fd42ca8df03c48ea3141 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
#!/usr/bin/env ruby

require 'bundler/inline'
require 'openssl'

gemfile do
  source 'https://rubygems.org'
  gem 'jwt'
  gem 'net-hippie'
end

private_pem = IO.read('config/gh-app.pem')
private_key = OpenSSL::PKey::RSA.new(private_pem)

jwt = JWT.encode(
  {
    iat: Time.now.to_i - 60,
    exp: Time.now.to_i + (10 * 60),
    iss: 125988
  },
  private_key,
  "RS256"
)

client = Net::Hippie::Client.new(logger: Logger.new('/dev/null'), headers: {
  'Accept' => 'application/vnd.github.v3+json',
  'Authorization' => "Bearer #{jwt}",
})

response = client.get("https://api.github.com/app")
puts JSON.pretty_generate(JSON.parse(response.body))

response = client.get("https://api.github.com/app/installations")
json = JSON.parse(response.body)
json.each do |installation|
  installation_id = installation['id']
  response = client.post("https://api.github.com/app/installations/#{installation_id}/access_tokens")
  json = JSON.parse(response.body)
  token = json['token']
  response = client.get("https://api.github.com/installation/repositories", headers:{
    'Authorization': "token #{token}"
  })
  puts JSON.pretty_generate(JSON.parse(response.body))
end