github-app-auth 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7de14445c693279f09a86e7e1cd7c349c492b30a532b0951d49aed52eec90a96
4
+ data.tar.gz: b9d44e07749d0e017430f7614d6250ad1370f185839a0ed778573d39a6046e25
5
+ SHA512:
6
+ metadata.gz: c10995031a23f2b0ee9b4fe6f6e5dbced77fe1fdcd9575d61f59de493a3d4b3190abff8212134e125d6e0bdb6cdc118e7a584141312372c0faa6aa8ef63b956f
7
+ data.tar.gz: 0db53d013d400c848debb9b5f38092128a8704d199947af608215a911d12bbf5586a945db23509c31692d1f4b232d22ec67afea1a4076e0fbe04f5c99006301f
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hortoncd@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Chris Horton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # GitHub::App::Auth
2
+
3
+ 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
+ instantiate if preferred.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'github-app-auth'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install github-app-auth
21
+
22
+ ## Usage
23
+
24
+ ```
25
+ require "github-app-auth"
26
+ ```
27
+
28
+ Include the module in your class
29
+ ```
30
+ include GitHub::App::Auth
31
+ ```
32
+
33
+ Instantiate the AuthClass class and use the methods from there.
34
+ ```
35
+ auth = GitHub::App:Auth::AuthClass.new
36
+ ```
37
+
38
+ ### Authenticating as an App
39
+
40
+ See [the GitHub documentation](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app) for more information.
41
+
42
+ Applicaiton authentication is required for most (all?) of the other methods of authentication. To authenticate as the application, two
43
+ secrets are required:
44
+
45
+ - Private Key
46
+ - Application ID
47
+
48
+ The two supported methods are ENV variables, or setting the options in the `options` hash to be passed to the various method calls.
49
+
50
+ #### Env Vars
51
+ ```
52
+ GITHUB_APP_ID="123456"
53
+ GITHUB_APP_PRIVATE_KEY="RSA Private Key ..."
54
+ ```
55
+
56
+ #### Options hash
57
+ ```
58
+ {
59
+ github_app_id: "123456",
60
+ github_app_private_key: "RSA Private Key ..."
61
+ }
62
+ ```
63
+
64
+ ### Authenticating as an App Installation
65
+
66
+ See [the GitHub documentation](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation#generating-an-installation-access-token) for more information.
67
+
68
+ The examples are using the gem as an includable module, but can also be used with the available AuthClass class..
69
+
70
+ Auth as an application installation for a repo and return an Octokit::Client.
71
+ ```
72
+ client = app_installation_client("myaccount/myrepo")
73
+ ```
74
+
75
+ Alternatively you can retrieve the token, and then set up your own GitHub client (Octokit or whatever you prefer) as needed.
76
+ ```
77
+ token = app_instalation_token("myaccount/myrepo")
78
+ client = Octokit::Client.new({ bearer_token: token, ... })
79
+ ```
80
+
81
+ ### Application Auth
82
+
83
+ If you need to accomplish somehting other than authenticating as an application installation, you can use the app auth to get the initial client authenticated with the app JWT.
84
+ ```
85
+ client = app_client
86
+ ```
87
+
88
+ It's also possible to get just the JWT token for use with your own client setup.
89
+ ```
90
+ token = app_token
91
+ client = Octokit::Client.enw({ bearer_token: token, ... })
92
+ ```
93
+
94
+ ## Development
95
+
96
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
97
+
98
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
99
+
100
+ ## Contributing
101
+
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hortoncd/github-app-auth. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/hortoncd/github-app-auth/blob/master/CODE_OF_CONDUCT.md).
103
+
104
+ Don't forget to add/fix tests for your changes.
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
109
+
110
+ ## Code of Conduct
111
+
112
+ Everyone interacting in the GitHub::App::Auth project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hortoncd/github-app-auth/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,51 @@
1
+ require "jwt"
2
+ require "openssl"
3
+ require_relative "client"
4
+
5
+ module GitHub
6
+ module App
7
+ module Auth
8
+ def app_client(options = {})
9
+ client(:bearer_token => app_token(options))
10
+ end
11
+
12
+ # options: the following can be passed via the options hash. if missing
13
+ # they will be read from ENV.
14
+ # github_app_private_key: String, The private key for the GitHub app
15
+ # github_app_id: String, the app id for the GitHub app
16
+ def app_token(options = {})
17
+ # Private key contents
18
+ private_pem = app_private_key(options)
19
+ private_key = OpenSSL::PKey::RSA.new(private_pem)
20
+
21
+ # Generate the JWT
22
+ payload = {
23
+ # issued at time, 60 seconds in the past to allow for clock drift
24
+ iat: Time.now.to_i - 60,
25
+ # JWT expiration time (10 minute maximum)
26
+ exp: Time.now.to_i + (10 * 60),
27
+ # GitHub App's identifier
28
+ iss: app_id(options)
29
+ }
30
+
31
+ JWT.encode(payload, private_key, "RS256")
32
+ end
33
+
34
+ def app_id(options = {})
35
+ if options[:github_app_id]
36
+ options[:github_app_id]
37
+ else
38
+ ENV["GITHUB_APP_ID"]
39
+ end
40
+ end
41
+
42
+ 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
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,16 @@
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
@@ -0,0 +1,11 @@
1
+ require "octokit"
2
+
3
+ module GitHub
4
+ module App
5
+ module Auth
6
+ def client(options = {})
7
+ Octokit::Client.new(options)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module GitHub
2
+ module App
3
+ module Auth
4
+ VERSION = "0.2.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
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
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github-app-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Horton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: octokit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: openssl
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ description: GitHub App auth methods.
56
+ email:
57
+ - hortoncd@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CODE_OF_CONDUCT.md
63
+ - LICENSE.txt
64
+ - 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
70
+ homepage: https://github.com/hortoncd/github-app-auth
71
+ licenses:
72
+ - MIT
73
+ metadata:
74
+ homepage_uri: https://github.com/hortoncd/github-app-auth
75
+ source_code_uri: https://github.com/hortoncd/github-app-auth
76
+ changelog_uri: https://github.com/hortoncd/github-app-auth
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 2.3.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.4.10
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: GitHub App auth methods.
96
+ test_files: []