google-oauth-cli 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3c1098a8c935b0a923a921c05a3261fbe4a2d36e4696e06429b62fcd1ee31dcd
4
+ data.tar.gz: d1c5de860873466f3262791e937be3bd94843a7ded20d75ca5cdde4a873ea555
5
+ SHA512:
6
+ metadata.gz: f868d39de9f0a48276d147dbc4dee234a18edcf1b9501d41ac944ef990b25817bb76acb581dd391e8f037631dff3a764d1f16542f0f62df761e46f0871ca8ee4
7
+ data.tar.gz: 457c0e60ac397ae92fe657028762db5690a354c51d79f84d650702083745815bea773316e746141d8e07c365ec92684989f7e0da0edd250140171a607dac6cd4
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.6
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Naming/FileName:
17
+ Exclude:
18
+ - 'lib/google-oauth-cli.rb'
19
+ - 'spec/google-oauth-cli_spec.rb'
20
+
21
+ Metrics/BlockLength:
22
+ Max: 28
23
+
24
+ Metrics/MethodLength:
25
+ Max: 15
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-04-06
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in google-oauth-cli.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.12"
10
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ google-oauth-cli (0)
5
+ googleauth (~> 1.5)
6
+ launchy (~> 2.5)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.3)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ ast (2.4.2)
14
+ diff-lcs (1.5.0)
15
+ faraday (2.7.4)
16
+ faraday-net_http (>= 2.0, < 3.1)
17
+ ruby2_keywords (>= 0.0.4)
18
+ faraday-net_http (3.0.2)
19
+ googleauth (1.5.0)
20
+ faraday (>= 0.17.3, < 3.a)
21
+ jwt (>= 1.4, < 3.0)
22
+ memoist (~> 0.16)
23
+ multi_json (~> 1.11)
24
+ os (>= 0.9, < 2.0)
25
+ signet (>= 0.16, < 2.a)
26
+ json (2.6.3)
27
+ jwt (2.7.0)
28
+ launchy (2.5.2)
29
+ addressable (~> 2.8)
30
+ memoist (0.16.2)
31
+ multi_json (1.15.0)
32
+ os (1.1.4)
33
+ parallel (1.22.1)
34
+ parser (3.2.2.0)
35
+ ast (~> 2.4.1)
36
+ public_suffix (5.0.1)
37
+ rainbow (3.1.1)
38
+ rake (13.0.6)
39
+ regexp_parser (2.7.0)
40
+ rexml (3.2.5)
41
+ rspec (3.12.0)
42
+ rspec-core (~> 3.12.0)
43
+ rspec-expectations (~> 3.12.0)
44
+ rspec-mocks (~> 3.12.0)
45
+ rspec-core (3.12.1)
46
+ rspec-support (~> 3.12.0)
47
+ rspec-expectations (3.12.2)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.12.0)
50
+ rspec-mocks (3.12.5)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.12.0)
53
+ rspec-support (3.12.0)
54
+ rubocop (1.49.0)
55
+ json (~> 2.3)
56
+ parallel (~> 1.10)
57
+ parser (>= 3.2.0.0)
58
+ rainbow (>= 2.2.2, < 4.0)
59
+ regexp_parser (>= 1.8, < 3.0)
60
+ rexml (>= 3.2.5, < 4.0)
61
+ rubocop-ast (>= 1.28.0, < 2.0)
62
+ ruby-progressbar (~> 1.7)
63
+ unicode-display_width (>= 2.4.0, < 3.0)
64
+ rubocop-ast (1.28.0)
65
+ parser (>= 3.2.1.0)
66
+ ruby-progressbar (1.13.0)
67
+ ruby2_keywords (0.0.5)
68
+ signet (0.17.0)
69
+ addressable (~> 2.8)
70
+ faraday (>= 0.17.5, < 3.a)
71
+ jwt (>= 1.5, < 3.0)
72
+ multi_json (~> 1.10)
73
+ unicode-display_width (2.4.2)
74
+
75
+ PLATFORMS
76
+ arm64-darwin-22
77
+ x86_64-linux
78
+
79
+ DEPENDENCIES
80
+ google-oauth-cli!
81
+ rake (~> 13.0)
82
+ rspec (~> 3.12)
83
+ rubocop (~> 1.21)
84
+
85
+ BUNDLED WITH
86
+ 2.4.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Yusuke Fujiki
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,40 @@
1
+ # google-oauth-cli
2
+
3
+ google-oauth-cli is a RubyGems library that allows you to perform OAuth authentication with Google from the command-line interface (CLI).
4
+
5
+ ## Features
6
+
7
+ - Authenticates with Google using OAuth from CLI.
8
+ - Spins up a simple web server to receive the authorization code.
9
+ - Supports passing a file path as an optional argument to persist the refresh token, making it easier to handle token refreshing in subsequent requests.
10
+
11
+ ## Motivation
12
+
13
+ Google deprecates to the OOB flow by Google in February 2022.
14
+
15
+ ## Usage
16
+
17
+ ```rb
18
+ auth = GoogleOAuthCli.new(
19
+ client_id: ENV["client_id"],
20
+ client_secret: ENV["client_secret"],
21
+ scope: ["https://www.googleapis.com/auth/drive", "https://spreadsheets.google.com/feeds"],
22
+ credentials_file: "~/.config/google-test.json"
23
+ )
24
+
25
+ # Starts authentication flow and returns Google::Auth::UserRefreshCredentials
26
+ credentials = auth.login
27
+
28
+ drive = Google::Apis::DriveV3::DriveService.new
29
+ drive.authorization = credentials
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## License
39
+
40
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/google-oauth-cli/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "google-oauth-cli"
7
+ spec.version = GoogleOAuthCli::VERSION
8
+ spec.authors = ["Yusuke Fujiki"]
9
+ spec.email = ["yusuke@fujikkys.com"]
10
+
11
+ spec.summary = "Library for Google OAuth authentication from CLI"
12
+ spec.description = <<~DESC
13
+ Perform Google OAuth authentication from the CLI using redirect flows.
14
+ Authentication information is saved in a file and can be used for the next authentication.
15
+ DESC
16
+ spec.homepage = "https://github.com/fujikky/google-oauth-cli"
17
+ spec.license = "MIT"
18
+ spec.required_ruby_version = ">= 2.6.0"
19
+
20
+ spec.metadata["rubygems_mfa_required"] = "true"
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/fujikky/google-oauth-cli"
23
+ spec.metadata["changelog_uri"] = "https://github.com/fujikky/google-oauth-cli/tree/main/CHANGELOG"
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(__dir__) do
28
+ `git ls-files -z`.split("\x0").reject do |f|
29
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
30
+ end
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ # Uncomment to register a new dependency of your gem
37
+ spec.add_dependency "googleauth", "~> 1.5"
38
+ spec.add_dependency "launchy", "~> 2.5"
39
+
40
+ # For more information and examples about making a new gem, check out our
41
+ # guide at: https://bundler.io/guides/creating_gem.html
42
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ class GoogleOAuthCli
4
+ class Server
5
+ def initialize(port:, state:)
6
+ @server = TCPServer.new port
7
+ @state = state
8
+ end
9
+
10
+ def start
11
+ loop do
12
+ connection = server.accept
13
+ request = connection.gets
14
+ data = handle_request(request)
15
+ connection.write <<~DOC
16
+ HTTP/1.1 200 OK
17
+ Content-Type: text/plain; charset=UTF-8
18
+
19
+ OAuth request received. You can close this window now.
20
+ DOC
21
+ connection.close
22
+ if data
23
+ server.close
24
+ return data
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :server, :state
32
+
33
+ def handle_request(request)
34
+ _, full_path = request.split
35
+ return if URI(full_path).path != "/authorize"
36
+
37
+ params = CGI.parse(URI.parse(full_path).query)
38
+ raise(Error, "Invalid oauth request received") if @state != params["state"][0]
39
+
40
+ params["code"][0]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class GoogleOAuthCli
4
+ VERSION = ""
5
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "googleauth"
4
+ require "launchy"
5
+ require_relative "google-oauth-cli/version"
6
+ require_relative "google-oauth-cli/server"
7
+
8
+ class GoogleOAuthCli
9
+ class Error < StandardError; end
10
+
11
+ def initialize(client_id:, client_secret:, scope:, credentials_file: nil, port: 9876)
12
+ @credentials = Google::Auth::UserRefreshCredentials.new(
13
+ client_id: client_id,
14
+ client_secret: client_secret,
15
+ scope: scope,
16
+ redirect_uri: "http://localhost:#{port}/authorize",
17
+ additional_parameters: { access_type: "offline" }
18
+ )
19
+ if credentials_file
20
+ @credentials_file = Pathname.new(File.expand_path(credentials_file))
21
+ @credentials_file
22
+ end
23
+ @port = port
24
+ end
25
+
26
+ def login
27
+ authorize_from_credentials || authorize_from_authorization_code_flow
28
+ end
29
+
30
+ private
31
+
32
+ attr_reader :credentials, :credentials_file, :port
33
+
34
+ def authorize_from_credentials
35
+ return nil unless credentials_file&.exist?
36
+
37
+ token = JSON.parse(credentials_file.read)["refresh_token"]
38
+ return nil if token.nil?
39
+
40
+ credentials.refresh_token = token
41
+ fetch_and_save_token!
42
+
43
+ credentials
44
+ end
45
+
46
+ def authorize_from_authorization_code_flow
47
+ state = SecureRandom.base64(16)
48
+ credentials.state = state
49
+
50
+ uri = credentials.authorization_uri
51
+ Launchy.open(uri) do |exception|
52
+ raise(Error, "Attempted to open #{uri} and failed because #{exception}")
53
+ end
54
+
55
+ credentials.code = Server.new(port: port, state: state).start
56
+ fetch_and_save_token!
57
+
58
+ credentials
59
+ end
60
+
61
+ def fetch_and_save_token!
62
+ credentials.fetch_access_token!
63
+ credentials_file&.write({ refresh_token: credentials.refresh_token }.to_json)
64
+ end
65
+ end
@@ -0,0 +1,7 @@
1
+ class GoogleOAuthCli
2
+ VERSION: String
3
+
4
+ def initialize(client_id: String, client_secret: String, scope: String | Array[String], credentials_file: String?, port: Integer?) -> void
5
+
6
+ def login -> Google::Auth::UserRefreshCredentials
7
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-oauth-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: '0'
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke Fujiki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: googleauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: launchy
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.5'
41
+ description: |
42
+ Perform Google OAuth authentication from the CLI using redirect flows.
43
+ Authentication information is saved in a file and can be used for the next authentication.
44
+ email:
45
+ - yusuke@fujikkys.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - CHANGELOG.md
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - google-oauth-cli.gemspec
59
+ - lib/google-oauth-cli.rb
60
+ - lib/google-oauth-cli/server.rb
61
+ - lib/google-oauth-cli/version.rb
62
+ - sig/google-oauth-cli.rbs
63
+ homepage: https://github.com/fujikky/google-oauth-cli
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ rubygems_mfa_required: 'true'
68
+ homepage_uri: https://github.com/fujikky/google-oauth-cli
69
+ source_code_uri: https://github.com/fujikky/google-oauth-cli
70
+ changelog_uri: https://github.com/fujikky/google-oauth-cli/tree/main/CHANGELOG
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.6.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.4.10
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Library for Google OAuth authentication from CLI
90
+ test_files: []