-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.rb
More file actions
executable file
·47 lines (37 loc) · 1.13 KB
/
handler.rb
File metadata and controls
executable file
·47 lines (37 loc) · 1.13 KB
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
45
46
#!/usr/bin/ruby
require 'httparty'
require 'openssl'
require 'jwt'
def getJWT()
# Private key contents
pem_location = ENV["GITHUB_APP_KEY"] ? ENV["GITHUB_APP_KEY"] : "/opt/certs/private-key.pem"
private_key = OpenSSL::PKey::RSA.new(File.read(pem_location))
# Generate the JWT
payload = {
# issued at time, 60 seconds in the past to allow for clock drift
iat: Time.now.to_i - 60,
# JWT expiration time (10 minute maximum)
exp: Time.now.to_i + (10 * 60),
# GitHub App's identifier
iss: ENV["APP_ID"]
}
return JWT.encode(payload, private_key, "RS256")
end
jwt_token = getJWT()
$baseURL = 'https://api.github.com/app/installations'
$headers = { Authorization: "Bearer #{jwt_token}" }
# Get installation_id
def getInstallationID()
response = HTTParty.get($baseURL, headers: $headers)
id = JSON.parse(response.body)[0]["id"] if response.code == 200
return id
end
# Generate auth token
def generateToken(id)
response = HTTParty.post("#{$baseURL}/#{id}/access_tokens", headers: $headers)
result = JSON.parse(response.body)
print result["token"]
STDOUT.flush
end
id = getInstallationID()
generateToken(id)