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

Commit d62c69c

Browse files
committed
Decouple module authentication methods
1 parent a0e3456 commit d62c69c

File tree

3 files changed

+49
-42
lines changed

3 files changed

+49
-42
lines changed

lib/wpxf/core.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'wpxf/core/event_emitter'
77
require 'wpxf/core/output_emitters'
88
require 'wpxf/core/module_info'
9+
require 'wpxf/core/module_authentication'
910

1011
require 'wpxf/versioning/browser_versions'
1112
require 'wpxf/versioning/os_versions'

lib/wpxf/core/module.rb

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Module
99
include Wpxf::WordPress::Login
1010
include Wpxf::WordPress::Options
1111
include Wpxf::WordPress::Urls
12+
include Wpxf::ModuleAuthentication
1213

1314
def initialize
1415
super
@@ -31,29 +32,9 @@ def initialize
3132
)
3233
])
3334

34-
if requires_authentication
35-
register_options([
36-
StringOption.new(
37-
name: 'username',
38-
desc: 'The WordPress username to authenticate with',
39-
required: true
40-
),
41-
StringOption.new(
42-
name: 'password',
43-
desc: 'The WordPress password to authenticate with',
44-
required: true
45-
)
46-
])
47-
end
48-
4935
self.event_emitter = EventEmitter.new
5036
end
5137

52-
# @return [Boolean] true if the module requires the user to authenticate.
53-
def requires_authentication
54-
false
55-
end
56-
5738
# @return [Boolean] true if all the required options are set.
5839
def can_execute?
5940
all_options_valid? && (
@@ -93,11 +74,8 @@ def set_option_value(name, value)
9374
res = super(name, value)
9475

9576
if payload
96-
if res == :not_found
97-
return payload.set_option_value(name, value)
98-
else
99-
payload.set_option_value(name, value)
100-
end
77+
return payload.set_option_value(name, value) if res == :not_found
78+
payload.set_option_value(name, value)
10179
end
10280

10381
res
@@ -110,23 +88,6 @@ def unset_option(name)
11088
payload.unset_option(name) if payload
11189
end
11290

113-
# Authenticate with WordPress and return the cookie.
114-
# @param username [String] the username to authenticate with.
115-
# @param password [String] the password to authenticate with.
116-
# @return [CookieJar, Boolean] the cookie in a CookieJar if successful,
117-
# otherwise, returns false.
118-
def authenticate_with_wordpress(username, password)
119-
emit_info "Authenticating with WordPress using #{username}:#{password}..."
120-
cookie = wordpress_login(username, password)
121-
if cookie.nil?
122-
emit_error 'Failed to authenticate with WordPress'
123-
return false
124-
else
125-
emit_success 'Authenticated with WordPress', true
126-
return cookie
127-
end
128-
end
129-
13091
# Run the module.
13192
# @return [Boolean] true if successful.
13293
def run
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Wpxf
2+
# Provides functionality for authenticating modules with a WordPress target.
3+
module ModuleAuthentication
4+
def initialize
5+
super
6+
7+
if requires_authentication
8+
register_options([
9+
StringOption.new(
10+
name: 'username',
11+
desc: 'The WordPress username to authenticate with',
12+
required: true
13+
),
14+
StringOption.new(
15+
name: 'password',
16+
desc: 'The WordPress password to authenticate with',
17+
required: true
18+
)
19+
])
20+
end
21+
end
22+
23+
# @return [Boolean] true if the module requires the user to authenticate.
24+
def requires_authentication
25+
false
26+
end
27+
28+
# Authenticate with WordPress and return the cookie.
29+
# @param username [String] the username to authenticate with.
30+
# @param password [String] the password to authenticate with.
31+
# @return [CookieJar, Boolean] the cookie in a CookieJar if successful,
32+
# otherwise, returns false.
33+
def authenticate_with_wordpress(username, password)
34+
emit_info "Authenticating with WordPress using #{username}:#{password}..."
35+
cookie = wordpress_login(username, password)
36+
if cookie.nil?
37+
emit_error 'Failed to authenticate with WordPress'
38+
return false
39+
else
40+
emit_success 'Authenticated with WordPress', true
41+
return cookie
42+
end
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)