From c9a8f4dc8f4f8c813152d1f3390327a61c6e7f98 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Fri, 27 Jul 2018 23:12:21 +0530 Subject: [PATCH 1/2] Use Octokit to generate installation access token --- Gemfile | 2 ++ github/token | 20 +++++--------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index 3c94252..1be4b7f 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,8 @@ else gem "jekyll", git: repo, branch: "master" end +gem "octokit" + gem "jekyll-sass-converter" gem "kramdown" gem "jekyll-commonmark-ghpages" diff --git a/github/token b/github/token index 1f67a28..06a6ac8 100755 --- a/github/token +++ b/github/token @@ -5,6 +5,7 @@ require "English" require "json" require "jwt" # https://rubygems.org/gems/jwt require "net/http" +require "octokit" require "openssl" require "uri" @@ -24,19 +25,8 @@ payload = { :iss => 13_446, } -jwt = JWT.encode(payload, private_key, "RS256") +jwt = JWT.encode(payload, private_key, "RS256") +client = Octokit::Client.new(:bearer_token => jwt) +token = client.create_installation_access_token(ENV.fetch("INSTALLATION"))[:token] -installation = ENV.fetch("INSTALLATION") - -uri = URI("https://api.github.com/installations/#{installation}/access_tokens") -req = Net::HTTP::Post.new(uri) -req["Authorization"] = "Bearer #{jwt}" -req["Accept"] = "application/vnd.github.machine-man-preview+json" - -http = Net::HTTP.new(uri.hostname, uri.port) -http.use_ssl = (uri.scheme == "https") -res = http.request(req) - -json = JSON.parse(res.body) - -puts json["token"] +puts token From 142f2e3663c539c59debc50b0bb2a4048251959a Mon Sep 17 00:00:00 2001 From: Pat Hawks Date: Fri, 27 Jul 2018 13:42:35 -0500 Subject: [PATCH 2/2] Minor cleanup * Remove unused requires * Continue to use `installation` variable --- github/token | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/github/token b/github/token index 06a6ac8..664ec96 100755 --- a/github/token +++ b/github/token @@ -2,12 +2,9 @@ # frozen_string_literal: true require "English" -require "json" require "jwt" # https://rubygems.org/gems/jwt -require "net/http" require "octokit" require "openssl" -require "uri" # Private key contents get_secret = File.expand_path("../aws/secretsmanager/get", __dir__) @@ -25,8 +22,9 @@ payload = { :iss => 13_446, } -jwt = JWT.encode(payload, private_key, "RS256") +jwt = JWT.encode(payload, private_key, "RS256") client = Octokit::Client.new(:bearer_token => jwt) -token = client.create_installation_access_token(ENV.fetch("INSTALLATION"))[:token] +installation = ENV.fetch("INSTALLATION") +token = client.create_installation_access_token(installation)[:token] puts token