Module: GitHub::App::Auth
- Included in:
- AuthClass
- Defined in:
- lib/github_app_auth.rb,
lib/github_app_auth/app.rb,
lib/github_app_auth/client.rb,
lib/github_app_auth/version.rb,
lib/github_app_auth/app_installation.rb
Overview
GitHub App Installation Authentication
Defined Under Namespace
Classes: AuthClass, Error, InstallationError, TokenError
Constant Summary
collapse
- VERSION =
"0.4.2"
Instance Method Summary
collapse
-
#app_client(options = {}) ⇒ Object
-
#app_id(options = {}) ⇒ Object
-
#app_private_key(options = {}) ⇒ Object
-
#app_token(options = {}) ⇒ Object
options: the following can be passed via the options hash.
-
#application_client(options = {}) ⇒ Object
-
#client(options = {}) ⇒ Object
-
#installation_by_type(type, name) ⇒ Object
-
#installation_token(type, name, options = {}) ⇒ Object
Supported types are :organization, :repository, :user.
-
#organization_installation_client(org, options = {}) ⇒ Object
-
#organization_installation_token(org, options = {}) ⇒ Object
-
#repository_installation_client(repo, options = {}) ⇒ Object
-
#repository_installation_token(repo, options = {}) ⇒ Object
-
#user_installation_client(user, options = {}) ⇒ Object
-
#user_installation_token(user, options = {}) ⇒ Object
Instance Method Details
#app_client(options = {}) ⇒ Object
11
12
13
|
# File 'lib/github_app_auth/app.rb', line 11
def app_client(options = {})
client(bearer_token: app_token(options))
end
|
#app_id(options = {}) ⇒ Object
37
38
39
|
# File 'lib/github_app_auth/app.rb', line 37
def app_id(options = {})
options[:github_app_id] || ENV["GITHUB_APP_ID"]
end
|
#app_private_key(options = {}) ⇒ Object
41
42
43
|
# File 'lib/github_app_auth/app.rb', line 41
def app_private_key(options = {})
options[:github_app_private_key] || ENV["GITHUB_APP_PRIVATE_KEY"]
end
|
#app_token(options = {}) ⇒ Object
options: the following can be passed via the options hash. if missing
they will be read from ENV.
github_app_private_key: String, The private key for the GitHub app github_app_id: String, the app id for the GitHub app
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/github_app_auth/app.rb', line 19
def app_token(options = {})
private_pem = app_private_key(options)
private_key = OpenSSL::PKey::RSA.new(private_pem)
payload = {
iat: Time.now.to_i - 60,
exp: Time.now.to_i + (10 * 60),
iss: app_id(options)
}
JWT.encode(payload, private_key, "RS256")
end
|
#application_client(options = {}) ⇒ Object
61
62
63
|
# File 'lib/github_app_auth/app_installation.rb', line 61
def application_client(options = {})
@application_client ||= app_client(options)
end
|
#client(options = {}) ⇒ Object
9
10
11
|
# File 'lib/github_app_auth/client.rb', line 9
def client(options = {})
Octokit::Client.new(options)
end
|
#installation_by_type(type, name) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/github_app_auth/app_installation.rb', line 48
def installation_by_type(type, name)
case type
when :organization
application_client.find_organization_installation(name)
when :repository
application_client.find_repository_installation(name)
when :user
application_client.find_user_installation(name)
else
raise ArgumentError, "Unsupported installation type: #{type}"
end
end
|
#installation_token(type, name, options = {}) ⇒ Object
Supported types are :organization, :repository, :user
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/github_app_auth/app_installation.rb', line 32
def installation_token(type, name, options = {})
application_client(options)
installation = installation_by_type(type, name)
if installation.nil? || installation[:id].nil?
raise GitHub::App::Auth::InstallationError, "Could not find installation for #{type}: #{name}"
end
resp = application_client.create_app_installation_access_token(installation[:id])
if resp.nil? || resp[:token].nil?
raise GitHub::App::Auth::TokenError, "Could not generate installation token for #{type}: #{name}"
end
resp[:token]
end
|
#organization_installation_client(org, options = {}) ⇒ Object
7
8
9
|
# File 'lib/github_app_auth/app_installation.rb', line 7
def organization_installation_client(org, options = {})
client(bearer_token: organization_installation_token(org, options))
end
|
#organization_installation_token(org, options = {}) ⇒ Object
11
12
13
|
# File 'lib/github_app_auth/app_installation.rb', line 11
def organization_installation_token(org, options = {})
installation_token(:organization, org, options)
end
|
#repository_installation_client(repo, options = {}) ⇒ Object
15
16
17
|
# File 'lib/github_app_auth/app_installation.rb', line 15
def repository_installation_client(repo, options = {})
client(bearer_token: repository_installation_token(repo, options))
end
|
#repository_installation_token(repo, options = {}) ⇒ Object
19
20
21
|
# File 'lib/github_app_auth/app_installation.rb', line 19
def repository_installation_token(repo, options = {})
installation_token(:repository, repo, options)
end
|
#user_installation_client(user, options = {}) ⇒ Object
23
24
25
|
# File 'lib/github_app_auth/app_installation.rb', line 23
def user_installation_client(user, options = {})
client(bearer_token: user_installation_token(user, options))
end
|
#user_installation_token(user, options = {}) ⇒ Object
27
28
29
|
# File 'lib/github_app_auth/app_installation.rb', line 27
def user_installation_token(user, options = {})
installation_token(:user, user, options)
end
|