github-app-auth 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7de14445c693279f09a86e7e1cd7c349c492b30a532b0951d49aed52eec90a96
4
- data.tar.gz: b9d44e07749d0e017430f7614d6250ad1370f185839a0ed778573d39a6046e25
3
+ metadata.gz: 5dfa01cb297c9e23c8f0342a92500065145151061ccf3d5ccc0c9a88f6882500
4
+ data.tar.gz: 4e320ac747cde80c1734b40d67463be810beadb303d08ed87ce35f461f1c6f0f
5
5
  SHA512:
6
- metadata.gz: c10995031a23f2b0ee9b4fe6f6e5dbced77fe1fdcd9575d61f59de493a3d4b3190abff8212134e125d6e0bdb6cdc118e7a584141312372c0faa6aa8ef63b956f
7
- data.tar.gz: 0db53d013d400c848debb9b5f38092128a8704d199947af608215a911d12bbf5586a945db23509c31692d1f4b232d22ec67afea1a4076e0fbe04f5c99006301f
6
+ metadata.gz: fb5af34f92a868bdfe6276e19784ae2f8366bee7df538e38aa279558f1038510485a295b714a266b9cc11f778968507316214c276565e3fc9c46b51cdee20015
7
+ data.tar.gz: 7c203243829d4d54557616f0d514f0ac4da37aff788846d6bfcc89253e4013a16ef6bad1710bf75441dfa7138e061a9a8a4274a92c3bca1f5cca3f12841e32f3
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ ## 0.4.0
4
+ - remove deprecated app_* methods that were only repository installations
5
+ - change require to 'github_app_auth' when adding linter
6
+
7
+ ## 0.3.0
8
+ - deprecates app_* methods that were only repository installations
9
+ - adds methods to do organization, repository, and user installations
10
+
11
+ ## 0.2.0
12
+ - changed lib path to `github-app-auth` instead of `github/app/auth`
13
+
14
+ ## 0.1.0
15
+ - initial release with support for repository installation authentication
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # GitHub::App::Auth
2
2
 
3
+ ![Tests](https://github.com/hortoncd/github-app-auth/actions/workflows/tests.yml/badge.svg)
4
+ [![Gem Version](https://badge.fury.io/rb/github-app-auth.svg)](https://badge.fury.io/rb/github-app-auth)
5
+
3
6
  A gem to make (at least) some forms of GitHub App authentication easy. It is built as an includable module, with the option of a class to
4
7
  instantiate if preferred.
5
8
 
@@ -22,7 +25,7 @@ Or install it yourself as:
22
25
  ## Usage
23
26
 
24
27
  ```
25
- require "github-app-auth"
28
+ require "github_app_auth"
26
29
  ```
27
30
 
28
31
  Include the module in your class
@@ -67,14 +70,44 @@ See [the GitHub documentation](https://docs.github.com/en/apps/creating-github-a
67
70
 
68
71
  The examples are using the gem as an includable module, but can also be used with the available AuthClass class..
69
72
 
70
- Auth as an application installation for a repo and return an Octokit::Client.
73
+ There are several methods of authenticating as an application installation.
74
+
75
+ #### Organization Installation
76
+
77
+ Auth as an application installation for an organization and return an Octokit::Client.
78
+ ```
79
+ client = organization_installation_client("myorg")
80
+ ```
81
+
82
+ Alternatively you can retrieve the token, and then set up your own GitHub client (Octokit or whatever you prefer) as needed.
83
+ ```
84
+ token = organization_installation_token("myorg")
85
+ client = Octokit::Client.new({ bearer_token: token, ... })
86
+ ```
87
+
88
+ #### Repository Installation
89
+
90
+ Auth as an application installation for a repository and return an Octokit::Client.
91
+ ```
92
+ client = repository_installation_client("myaccount/myrepo")
93
+ ```
94
+
95
+ Alternatively you can retrieve the token, and then set up your own GitHub client (Octokit or whatever you prefer) as needed.
96
+ ```
97
+ token = repository_installation_token("myaccount/myrepo")
98
+ client = Octokit::Client.new({ bearer_token: token, ... })
99
+ ```
100
+
101
+ #### User Installation
102
+
103
+ Auth as an application installation for a user and return an Octokit::Client.
71
104
  ```
72
- client = app_installation_client("myaccount/myrepo")
105
+ client = user_installation_client("myuser")
73
106
  ```
74
107
 
75
108
  Alternatively you can retrieve the token, and then set up your own GitHub client (Octokit or whatever you prefer) as needed.
76
109
  ```
77
- token = app_instalation_token("myaccount/myrepo")
110
+ token = user_installation_token("myuser")
78
111
  client = Octokit::Client.new({ bearer_token: token, ... })
79
112
  ```
80
113
 
@@ -1,12 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "jwt"
2
4
  require "openssl"
3
5
  require_relative "client"
4
6
 
5
7
  module GitHub
6
8
  module App
9
+ # GitHub App Authentication
7
10
  module Auth
8
11
  def app_client(options = {})
9
- client(:bearer_token => app_token(options))
12
+ client(bearer_token: app_token(options))
10
13
  end
11
14
 
12
15
  # options: the following can be passed via the options hash. if missing
@@ -32,19 +35,11 @@ module GitHub
32
35
  end
33
36
 
34
37
  def app_id(options = {})
35
- if options[:github_app_id]
36
- options[:github_app_id]
37
- else
38
- ENV["GITHUB_APP_ID"]
39
- end
38
+ options[:github_app_id] || ENV["GITHUB_APP_ID"]
40
39
  end
41
40
 
42
41
  def app_private_key(options = {})
43
- if options[:github_app_private_key]
44
- options[:github_app_private_key]
45
- else
46
- ENV["GITHUB_APP_PRIVATE_KEY"]
47
- end
42
+ options[:github_app_private_key] || ENV["GITHUB_APP_PRIVATE_KEY"]
48
43
  end
49
44
  end
50
45
  end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitHub
4
+ module App
5
+ # GitHub App Installation Authentication
6
+ module Auth
7
+ def organization_installation_client(org, options = {})
8
+ client(bearer_token: organization_installation_token(org, options))
9
+ end
10
+
11
+ def organization_installation_token(org, options = {})
12
+ installation_token(:organization, org, options)
13
+ end
14
+
15
+ def repository_installation_client(repo, options = {})
16
+ client(bearer_token: repository_installation_token(repo, options))
17
+ end
18
+
19
+ def repository_installation_token(repo, options = {})
20
+ installation_token(:repository, repo, options)
21
+ end
22
+
23
+ def user_installation_client(user, options = {})
24
+ client(bearer_token: user_installation_token(user, options))
25
+ end
26
+
27
+ def user_installation_token(user, options = {})
28
+ installation_token(:user, user, options)
29
+ end
30
+
31
+ # Supported types are :organization, :repository, :user
32
+ def installation_token(type, name, options = {})
33
+ application_client(options)
34
+ installation = installation_by_type(type, name)
35
+
36
+ if installation.nil? || installation[:id].nil?
37
+ raise GitHub::App::Auth::InstallationError, "Could not find installation for #{type}: #{name}"
38
+ end
39
+
40
+ resp = application_client.create_app_installation_access_token(installation[:id])
41
+ if resp.nil? || resp[:token].nil?
42
+ raise GitHub::App::Auth::TokenError, "Could generate installation token for #{type}: #{name}"
43
+ end
44
+
45
+ resp[:token]
46
+ end
47
+
48
+ def installation_by_type(type, name)
49
+ case type
50
+ when :organization
51
+ application_client.find_organization_installation(name)
52
+ when :repository
53
+ application_client.find_repository_installation(name)
54
+ when :user
55
+ application_client.find_user_installation(name)
56
+ else
57
+ raise ArgumentError, "Unsupported installation type: #{type}"
58
+ end
59
+ end
60
+
61
+ def application_client(options = {})
62
+ @application_client ||= app_client(options)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "octokit"
2
4
 
3
5
  module GitHub
4
6
  module App
7
+ # GitHub App client
5
8
  module Auth
6
9
  def client(options = {})
7
10
  Octokit::Client.new(options)
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GitHub
2
4
  module App
3
5
  module Auth
4
- VERSION = "0.2.0"
6
+ VERSION = "0.4.0"
5
7
  end
6
8
  end
7
9
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "github_app_auth/app"
4
+ require "github_app_auth/app_installation"
5
+ require "github_app_auth/version"
6
+
7
+ module GitHub
8
+ module App
9
+ # GitHub App Authentication
10
+ module Auth
11
+ class Error < StandardError; end
12
+ class InstallationError < Error; end
13
+ class TokenError < Error; end
14
+
15
+ class AuthClass
16
+ include GitHub::App::Auth
17
+ end
18
+ end
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github-app-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Horton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-05 00:00:00.000000000 Z
11
+ date: 2023-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -59,14 +59,15 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - CHANGELOG.md
62
63
  - CODE_OF_CONDUCT.md
63
64
  - LICENSE.txt
64
65
  - README.md
65
- - lib/github-app-auth.rb
66
- - lib/github-app-auth/app.rb
67
- - lib/github-app-auth/app_installation.rb
68
- - lib/github-app-auth/client.rb
69
- - lib/github-app-auth/version.rb
66
+ - lib/github_app_auth.rb
67
+ - lib/github_app_auth/app.rb
68
+ - lib/github_app_auth/app_installation.rb
69
+ - lib/github_app_auth/client.rb
70
+ - lib/github_app_auth/version.rb
70
71
  homepage: https://github.com/hortoncd/github-app-auth
71
72
  licenses:
72
73
  - MIT
@@ -82,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
83
  requirements:
83
84
  - - ">="
84
85
  - !ruby/object:Gem::Version
85
- version: 2.3.0
86
+ version: 3.0.0
86
87
  required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  requirements:
88
89
  - - ">="
@@ -1,16 +0,0 @@
1
- module GitHub
2
- module App
3
- module Auth
4
- def app_installation_client(repo, options = {})
5
- client(bearer_token: app_installation_token(repo, options))
6
- end
7
-
8
- def app_installation_token(repo, options = {})
9
- application_client = app_client
10
- installation = application_client.find_repository_installation(repo)
11
- resp = application_client.create_app_installation_access_token(installation[:id])
12
- resp[:token]
13
- end
14
- end
15
- end
16
- end
@@ -1,13 +0,0 @@
1
- require "github-app-auth/app"
2
- require "github-app-auth/app_installation"
3
- require "github-app-auth/version"
4
-
5
- module GitHub
6
- module App
7
- module Auth
8
- class AuthClass
9
- include GitHub::App::Auth
10
- end
11
- end
12
- end
13
- end