Class: Fastlane::Actions::AuthWithGithubAppAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/auth_with_github_app/actions/auth_with_github_app_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



54
55
56
# File 'lib/fastlane/plugin/auth_with_github_app/actions/auth_with_github_app_action.rb', line 54

def self.authors
  ["k-kohey"]
end

.available_optionsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fastlane/plugin/auth_with_github_app/actions/auth_with_github_app_action.rb', line 67

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :pem,
                                 env_name: "GITHUB_APP_PEM",
                                 description: "Private key available from the GitHub App administration screen",
                                 optional: false,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :app_id,
                                 env_name: "GITHUB_APP_ID",
                                 description: "App ID available from the GitHub App administration screen",
                                 optional: false,
                                 type: Integer),
    FastlaneCore::ConfigItem.new(key: :installation_id,
                                 env_name: "GITHUB_APP_INSTLLATION_ID",
                                 description: "Installation id available from the GitHub App administration screen",
                                 optional: false,
                                 type: Integer)
  ]
end

.descriptionObject



50
51
52
# File 'lib/fastlane/plugin/auth_with_github_app/actions/auth_with_github_app_action.rb', line 50

def self.description
  "Get a GitHub access token using the GitHub App"
end

.detailsObject



62
63
64
65
# File 'lib/fastlane/plugin/auth_with_github_app/actions/auth_with_github_app_action.rb', line 62

def self.details
  # Optional:
  ""
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/fastlane/plugin/auth_with_github_app/actions/auth_with_github_app_action.rb', line 87

def self.is_supported?(platform)
  # Adjust this if your plugin only works for a particular platform (iOS vs. Android, for example)
  # See: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
  #
  # [:ios, :mac, :android].include?(platform)
  true
end

.return_valueObject



58
59
60
# File 'lib/fastlane/plugin/auth_with_github_app/actions/auth_with_github_app_action.rb', line 58

def self.return_value
  "API Token obtained using the GitHub App."
end

.run(params) ⇒ Object



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
47
48
# File 'lib/fastlane/plugin/auth_with_github_app/actions/auth_with_github_app_action.rb', line 9

def self.run(params)
  private_pem = params[:pem]
  app_id = params[:app_id]
  installation_id = params[:installation_id]

  private_key = OpenSSL::PKey::RSA.new(private_pem)

  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: app_id
  }

  jwt = JWT.encode(payload, private_key, "RS256")

  uri = URI.parse("https://api.github.com/app/installations/#{installation_id}/access_tokens")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  headers = { Authorization: "Bearer #{jwt}", Accept: "application/vnd.github+json", 'User-Agent': "auth_with_github_app" }
  req = Net::HTTP::Post.new(uri.path)
  req.initialize_http_header(headers)
  response = http.request(req)

  if response.code.to_i < 200 || response.code.to_i > 299
    UI.error("HTTP request to GitHub failed with status code #{response.code}\n detail: #{response.body}")
    exit(1)
  end

  token = JSON.parse(response.body)['token']
  if token == "" || token.nil?
    UI.error("The Token obtained is empty; the response from the API may be invalid.")
    exit(1)
  end

  return token
end