Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit 346af93

Browse files
committed
Add restrictions to credential overwriting and plug into authentication
1 parent 35e9a13 commit 346af93

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

lib/wpxf/core/module_authentication.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def authenticate_with_wordpress(username, password)
3939
emit_error 'Failed to authenticate with WordPress'
4040
return false
4141
else
42+
store_credentials username, password
4243
emit_success 'Authenticated with WordPress', true
4344
return cookie
4445
end

lib/wpxf/db/credentials.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ module Wpxf::Db::Credentials
77
# @param password [String] the password.
88
# @param type [String] the type of string stored in the password field.
99
# @return [Models::Credential] the newly created {Models::Credential}.
10-
def store_credentials(username, password, type = 'plain')
10+
def store_credentials(username, password = nil, type = 'plain')
1111
credential = Models::Credential.first(
1212
host: target_host,
1313
port: target_port,
1414
username: username,
15+
password: nil,
1516
type: type,
1617
workspace: active_workspace
1718
)

spec/lib/wpxf/core/module_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def requires_authentication
1414
end.new
1515
end
1616

17+
before :each, 'setup subject' do
18+
subject.active_workspace = Models::Workspace.first
19+
subject.set_option_value('host', '127.0.0.1')
20+
end
21+
1722
describe '#new' do
1823
it 'registers the verbose option' do
1924
expect(subject.get_option('verbose')).to_not be_nil
@@ -127,7 +132,7 @@ def requires_authentication
127132
subject.register_options([opt_a, opt_c])
128133
subject.payload.register_option(opt_b)
129134

130-
expect(subject.missing_options).to eq %w(host opt_a opt_b)
135+
expect(subject.missing_options).to eq %w[opt_a opt_b]
131136
end
132137
end
133138

@@ -182,6 +187,13 @@ def requires_authentication
182187
subject.authenticate_with_wordpress('user', 'pass')
183188
end
184189

190+
it 'should store the credentials' do
191+
allow(subject).to receive(:wordpress_login).and_return('cookie')
192+
subject.authenticate_with_wordpress('user', 'pass')
193+
count = Models::Credential.count(username: 'user', password: 'pass')
194+
expect(count).to eql 1
195+
end
196+
185197
it 'returns the cookie' do
186198
allow(subject).to receive(:wordpress_login).and_return('cookie')
187199
res = subject.authenticate_with_wordpress('user', 'pass')

spec/lib/wpxf/db/credentials_spec.rb

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,37 @@
4141

4242
context 'if a record with the same host + username + type already exists' do
4343
context 'and the record is in the same workspace' do
44-
it 'should overwrite the previous credential' do
45-
subject.store_credentials 'foo', 'bar', 'test'
46-
subject.store_credentials 'foo', 'foo', 'test'
47-
expect(Models::Credential.count).to eql 1
44+
context 'and has no password' do
45+
it 'should overwrite the previous credential' do
46+
subject.store_credentials 'foo', nil, 'test'
47+
subject.store_credentials 'foo', 'bar', 'test'
48+
expect(Models::Credential.count).to eql 1
4849

49-
credential = Models::Credential.first
50-
expect(credential.password).to eql 'foo'
51-
Models::Credential.truncate
50+
credential = Models::Credential.first
51+
expect(credential.password).to eql 'bar'
52+
Models::Credential.truncate
5253

53-
subject.store_credentials 'foo', 'bar', 'unique'
54-
subject.store_credentials 'foo', 'foo', 'unique2'
55-
expect(Models::Credential.count).to eql 2
54+
subject.store_credentials 'foo', 'bar', 'unique'
55+
subject.store_credentials 'foo', 'foo', 'unique2'
56+
expect(Models::Credential.count).to eql 2
57+
end
58+
end
59+
60+
context 'and has a password' do
61+
it 'should add a new entry' do
62+
Models::Credential.create(
63+
host: subject.target_host,
64+
port: subject.target_port,
65+
username: 'foo',
66+
password: 'bar',
67+
type: 'plain',
68+
workspace: Models::Workspace.first(name: 'default')
69+
)
70+
71+
expect(Models::Credential.count).to eql 1
72+
subject.store_credentials 'foo', 'foo'
73+
expect(Models::Credential.count).to eql 2
74+
end
5675
end
5776
end
5877

0 commit comments

Comments
 (0)