Skip to content

Commit

Permalink
Merge pull request #18 from simplybusiness/generate-access-token
Browse files Browse the repository at this point in the history
Generate access token
  • Loading branch information
Punit Jain authored Feb 15, 2021
2 parents 9c87540 + 4ce7df0 commit afd37ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

source 'https://rubygems.org'

gem 'jwt'
gem 'octokit'
gem 'semantic'

group :development, :test do
gem 'pry'
gem 'rspec'
Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ GEM
i18n (1.8.8)
concurrent-ruby (~> 1.0)
json (2.5.1)
jwt (2.2.2)
method_source (1.0.0)
minitest (5.14.3)
multipart-post (2.1.1)
Expand Down Expand Up @@ -94,6 +95,7 @@ PLATFORMS
ruby

DEPENDENCIES
jwt
octokit
pry
rspec
Expand All @@ -102,4 +104,4 @@ DEPENDENCIES
simplycop

BUNDLED WITH
2.1.4
2.2.3
25 changes: 24 additions & 1 deletion lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,38 @@

require 'octokit'
require 'json'
require 'jwt'

TEN_MINUTES = 600 # seconds

# configuration for octokit
class Config
attr_reader :client, :payload, :version_file_path, :event_name

def initialize
@client = Octokit::Client.new(access_token: ENV['ACCESS_TOKEN'])
@payload = JSON.parse(File.read(ENV['GITHUB_EVENT_PATH']))
@event_name = ENV['GITHUB_EVENT_NAME']
@version_file_path = ENV['VERSION_FILE_PATH']
@client = Octokit::Client.new(access_token: access_token)
end

private

def access_token
bearer_client = Octokit::Client.new(bearer_token: bearer_token)
installation = bearer_client.find_repository_installation(payload['repository']['full_name'])
response = bearer_client.create_app_installation_access_token(installation[:id])
response[:token]
end

def bearer_token
payload = {
iat: Time.now.to_i,
exp: Time.now.to_i + TEN_MINUTES,
iss: ENV['DOBBY_APP_ID']
}
private_key = OpenSSL::PKey::RSA.new(ENV['DOBBY_PRIVATE_KEY'])

JWT.encode(payload, private_key, 'RS256')
end
end

0 comments on commit afd37ba

Please sign in to comment.