octoauth 1.0.2 → 1.1.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 +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +4 -0
- data/README.md +6 -0
- data/lib/octoauth/auth.rb +17 -6
- data/octoauth.gemspec +1 -1
- data/spec/octoauth/auth_spec.rb +55 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2fc370fac48f586e3832626f1979f7cea1b5649
|
4
|
+
data.tar.gz: e0cd4359ebe9fb0d4a06c9e1544edb2bd1cd3b72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fce5750ecd54f28ab452f227a6ac685ee251619e1f0e76e1620add2b6c4d64d84cb7943f0fcd195abcb6f24ae3760088eca66205783a69b420c3280d66eae752
|
7
|
+
data.tar.gz: 0abe260a1a69248c6f0c7d086a6db8c267fef7e3a97000a94ba5b0e6b747ff6b0e9ad77815381ecc3f5307ff23d26a3d7104550d6a43b1fbb9582756f3e1c6ac
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -35,6 +35,12 @@ puts "The other token is #{other_auth.token}"
|
|
35
35
|
other_auth.save
|
36
36
|
```
|
37
37
|
|
38
|
+
Multiple files can be specified, and it will try them in order looking for a valid token. If this fails, it will use the first file listed for any future saving of tokens:
|
39
|
+
|
40
|
+
```
|
41
|
+
auth = Octoauth.new note: 'my_app', files: ['./.octoauth.yml', '/etc/octoauth', :default]
|
42
|
+
```
|
43
|
+
|
38
44
|
The above examples get us the basic scope, which means some read-only public access. For other scopes, specify them when creating the token:
|
39
45
|
|
40
46
|
```
|
data/lib/octoauth/auth.rb
CHANGED
@@ -21,15 +21,14 @@ module Octoauth
|
|
21
21
|
class Auth
|
22
22
|
def initialize(params = {})
|
23
23
|
parse_params!(params)
|
24
|
-
@config = ConfigFile.new file: @options[:file], note: config_note
|
25
24
|
save if @options[:autosave]
|
26
25
|
end
|
27
26
|
|
28
27
|
def save
|
29
28
|
fail 'No token to save' unless token
|
30
|
-
fail 'No file given for config' unless
|
31
|
-
|
32
|
-
|
29
|
+
fail 'No file given for config' unless config.file
|
30
|
+
config.token = token
|
31
|
+
config.write
|
33
32
|
end
|
34
33
|
|
35
34
|
def token
|
@@ -38,9 +37,21 @@ module Octoauth
|
|
38
37
|
|
39
38
|
private
|
40
39
|
|
40
|
+
def config
|
41
|
+
return @config if @config
|
42
|
+
configs = config_files.map do |file|
|
43
|
+
ConfigFile.new file: file, note: config_note
|
44
|
+
end
|
45
|
+
@config = configs.find(&:token) || configs.first
|
46
|
+
end
|
47
|
+
|
48
|
+
def config_files
|
49
|
+
@options[:files] ||= [@options[:file]]
|
50
|
+
end
|
51
|
+
|
41
52
|
def parse_params!(params)
|
42
53
|
@options = params.subset(
|
43
|
-
:file, :note, :autosave, :scopes,
|
54
|
+
:file, :files, :note, :autosave, :scopes,
|
44
55
|
:login, :password, :twofactor, :api_endpoint
|
45
56
|
)
|
46
57
|
end
|
@@ -60,7 +71,7 @@ module Octoauth
|
|
60
71
|
end
|
61
72
|
|
62
73
|
def load_token(needs2fa = false)
|
63
|
-
return
|
74
|
+
return config.token if config.token
|
64
75
|
prompt!(needs2fa)
|
65
76
|
authenticate
|
66
77
|
end
|
data/octoauth.gemspec
CHANGED
data/spec/octoauth/auth_spec.rb
CHANGED
@@ -139,6 +139,61 @@ describe Octoauth do
|
|
139
139
|
expect(new_auth.token).to eql random
|
140
140
|
end
|
141
141
|
end
|
142
|
+
context 'when given multiple file paths' do
|
143
|
+
context 'when the first file exists' do
|
144
|
+
it 'reads the first file' do
|
145
|
+
auth = Octoauth::Auth.new(
|
146
|
+
note: 'foo',
|
147
|
+
files: ['spec/examples/conf_a.yml', 'spec/examples/bogus.yml']
|
148
|
+
)
|
149
|
+
expect(auth.token).to eql 'bcdebcdebcdebcdebcdebcdebcde'
|
150
|
+
end
|
151
|
+
end
|
152
|
+
context 'when the first file does not exist' do
|
153
|
+
context 'when the second file exists' do
|
154
|
+
it 'reads the second file' do
|
155
|
+
FileUtils.rm_f 'spec/examples/nil.yml'
|
156
|
+
auth = Octoauth::Auth.new(
|
157
|
+
note: 'foo',
|
158
|
+
files: ['spec/examples/bogus.yml', 'spec/examples/conf_a.yml']
|
159
|
+
)
|
160
|
+
expect(auth.token).to eql 'bcdebcdebcdebcdebcdebcdebcde'
|
161
|
+
end
|
162
|
+
end
|
163
|
+
context 'when the second file does not exist' do
|
164
|
+
it 'reads nil' do
|
165
|
+
FileUtils.rm_f ['spec/examples/nil.yml', 'spec/examples/nil2.yml']
|
166
|
+
auth = Octoauth::Auth.new(
|
167
|
+
note: 'foo',
|
168
|
+
files: ['spec/examples/nil.yml', 'spec/examples/nil2.yml']
|
169
|
+
)
|
170
|
+
expect(auth.send(:config).token).to be_nil
|
171
|
+
end
|
172
|
+
it 'writes to the first file' do
|
173
|
+
random = rand(36**30).to_s(30)
|
174
|
+
stub_request(:post, 'https://user:pw@api.github.com/authorizations')
|
175
|
+
.with(body: "{\"note\":\"foo\",\"scopes\":[]}")
|
176
|
+
.to_return(
|
177
|
+
status: 200,
|
178
|
+
body: AuthShim.new('foo', random)
|
179
|
+
)
|
180
|
+
FileUtils.rm_f ['spec/examples/nil.yml', 'spec/examples/nil2.yml']
|
181
|
+
auth = Octoauth::Auth.new(
|
182
|
+
note: 'foo',
|
183
|
+
files: ['spec/examples/nil.yml', 'spec/examples/nil2.yml'],
|
184
|
+
login: 'user',
|
185
|
+
password: 'pw'
|
186
|
+
)
|
187
|
+
auth.save
|
188
|
+
new_auth = Octoauth::Auth.new(
|
189
|
+
note: 'foo',
|
190
|
+
file: 'spec/examples/nil.yml'
|
191
|
+
)
|
192
|
+
expect(new_auth.token).to eql random
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
142
197
|
|
143
198
|
describe '#save' do
|
144
199
|
it 'saves the config to disk' do
|