oauth2-cli 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/bin/oauth2-cli +23 -2
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76187c5b942695b8ee0626735c7a762f76dba841
4
- data.tar.gz: bf31d9f78aee657bfd091d96c40b81bf8bfa72a0
3
+ metadata.gz: 9097f026f5b1eca0dc1801e3c316c240bb768b7d
4
+ data.tar.gz: a2cf31e5ad4845a37034efe503da7cd78e06da87
5
5
  SHA512:
6
- metadata.gz: 0dcb7367f6597f3311c5e07982e8655f234fc3100a7160193af810cc0e71c050d4b71de2589a14e2935d69fd2035e33115e9d8b6383b8db2c9ba27079603309c
7
- data.tar.gz: e7bbd220f94a2e494eada6131b22481d8cef9db51ff7ff7311c229813c3686c8eb73533535a289cfa995fab884f097a4c7091a4ef6acb27311be4ac14746ef68
6
+ metadata.gz: 06bda1c71bcb161c6baeaab5ddc208312fc60c05346aef2bbd60e952dfa36210f1db0c1462dd6bb39d44d01ac181051374a895449fe74cbdc5b76ebc99d96c4b
7
+ data.tar.gz: 267d37799a50d6d48fcc5bb2c46ffe9d8f4d198bc7b561ea69e2d64b5a4bc0f6e07c4372bc5e5f692c2a3ba5c64169b3ea1640dbfd1ffe1bf41f509280880c7c
data/README.md CHANGED
@@ -4,7 +4,7 @@ Command line utility to get an OAuth access token for three-legged flows where y
4
4
 
5
5
  The reason for rewriting this in Ruby was the difficulty to debug the OAuth2 flow when things don't go as expected. Go's OAuth2 library is pretty opaque and trying to figure out why for some services the client_id is not passed when sending the the token request after obtaining the authorisation code has been fruitless - even though sending the request via curl using the produced authorisation code proved successful.
6
6
 
7
- The purpose for this tool is to obtain access and refresh tokens for applications which run as services (i.e daemons) where doing the OAuth2 flow is a bit difficult.
7
+ The purpose for this tool is to obtain access and refresh tokens for applications which run as services (e.g daemons) where doing the OAuth2 flow is a bit difficult. However, the scope is not limited to just this. For this use case, the `client_credentials` grant type would be more appropriate, but unfortunately not all API providers support this because they make the rather wrong assumption that all apps are used interactively. They only support `authorization_code` grant type or worse, `password` grant type.
8
8
 
9
9
  ## Install
10
10
 
@@ -14,12 +14,12 @@ gem install oauth2-cli
14
14
 
15
15
  ## Usage
16
16
 
17
- For services which validate the callback URL, you must use `http://127.0.0.1:8000/oauth/callback` in your OAuth2 application. Bear in mind that `8000` is the default port which may be changed via CLI argument. Adapt as necessary.
17
+ For services which validate the callback URL, you must use `http://127.0.0.1:8000/oauth/callback` in your OAuth2 application. Bear in mind that `8000` is the default port which may be changed via CLI argument. `127.0.0.1` is the default host which may be changed via CLI argument. Adapt as necessary.
18
18
 
19
19
  The `oauth2-cli` script has a built in help:
20
20
 
21
21
  ```bash
22
- oauth2-cli -h
22
+ oauth2-cli --help
23
23
  Usage: oauth2-cli --auth AUTHORISATION_URL --token TOKEN_URL --id CLIENT_ID --secret CLIENT_SECRET
24
24
 
25
25
  -a, --auth AUTHORISATION_URL Authorisation URL (required)
@@ -28,7 +28,9 @@ Usage: oauth2-cli --auth AUTHORISATION_URL --token TOKEN_URL --id CLIENT_ID --se
28
28
  -s, --secret CLIENT_SECRET Client secret (required)
29
29
  -o, --scope SCOPE1,SCOPE2,etc OAuth2 scope to authorise (not used if not specified)
30
30
  -e, --separator OAuth2 scope separator character (defaults to space) n.b the scope arg is always passed as array and joined with the separator char for the request
31
+ -h, --host 127.0.0.1 Callback host (defaults to 127.0.0.1) n.b this allows you to run this tool on a remote machine and have the authorisation code go there; the callback HTTP server always binds to all available network interfaces irrespective of this value
31
32
  -p, --port 8000 Callback port (defaults to 8000)
33
+ -w, --write Write the returned token as JSON using TOKEN_URL as filename with the current working directory being the destination
32
34
  -d, --debug Turn on OAuth2 library debug and WEBrick log
33
35
  ```
34
36
 
data/bin/oauth2-cli CHANGED
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'pp'
4
+ require 'uri'
5
+ require 'json'
4
6
  require 'oauth2'
5
7
  require 'webrick'
6
8
  require 'optparse'
@@ -51,12 +53,28 @@ optp = OptionParser.new do |opts|
51
53
  options[:separator] = opt
52
54
  end
53
55
 
56
+ options[:host] = '127.0.0.1'
57
+ desc_host = 'Callback host (defaults to 127.0.0.1) n.b this allows you to '\
58
+ 'run this tool on a remote machine and have the authorisation code go '\
59
+ 'there; the callback HTTP server always binds to all available network '\
60
+ 'interfaces irrespective of this value'
61
+ opts.on('-h', '--host 127.0.0.1', String, desc_host) do |opt|
62
+ options[:host] = opt
63
+ end
64
+
54
65
  options[:port] = 8000
55
66
  desc_port = 'Callback port (defaults to 8000)'
56
67
  opts.on('-p', '--port 8000', Integer, desc_port) do |opt|
57
68
  options[:port] = opt
58
69
  end
59
70
 
71
+ options[:write] = false
72
+ desc_write = 'Write the returned token as JSON using TOKEN_URL as filename '\
73
+ 'with the current working directory being the destination'
74
+ opts.on('-w', '--write', desc_write) do
75
+ options[:write] = true
76
+ end
77
+
60
78
  options[:debug] = false
61
79
  opts.on('-d', '--debug', 'Turn on OAuth2 library debug and WEBrick log') do
62
80
  ENV['OAUTH_DEBUG'] = 'true'
@@ -70,11 +88,12 @@ required.each do |arg|
70
88
  next unless options[arg].nil?
71
89
 
72
90
  STDERR.puts "Error: missing required argument #{arg}. "\
73
- "See #{src} -h for help."
91
+ "See #{src} --help for help."
74
92
  exit 1
75
93
  end
76
94
 
77
- redirect_uri = "http://127.0.0.1:#{options[:port]}#{path}"
95
+ token_uri = URI(options[:token])
96
+ redirect_uri = "http://#{options[:host]}:#{options[:port]}#{path}"
78
97
  client = OAuth2::Client.new(
79
98
  options[:id],
80
99
  options[:secret],
@@ -133,6 +152,8 @@ server.mount_proc('/') do |req, res|
133
152
  pp token.to_hash
134
153
  puts ''
135
154
 
155
+ File.write("#{token_uri.host}.json", token.to_hash.to_json) if options[:write]
156
+
136
157
  res.status = 200
137
158
  res.body = 'You may now close this tab'
138
159
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth2-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ștefan Rusu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-05 00:00:00.000000000 Z
11
+ date: 2019-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2