Skip to content

Commit f11229a

Browse files
authored
FIX: Appropriately assign values when fetching user details (discourse#100)
FIX: Appropriately assign values when fetching user details
1 parent 5cd7a79 commit f11229a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/oauth2_basic_authenticator.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def walk_path(fragment, segments, seg_index = 0)
8484
return nil unless fragment.is_a?(Hash) || fragment.is_a?(Array)
8585
first_seg = segments[seg_index].scan(/([\d+])/).length > 0 ? first_seg.split("[")[0] : first_seg
8686
if fragment.is_a?(Hash)
87-
deref = fragment[first_seg] || fragment[first_seg.to_sym]
87+
deref = fragment[first_seg]
8888
else
8989
array_index = 0
9090
if (seg_index > 0)
@@ -98,7 +98,7 @@ def walk_path(fragment, segments, seg_index = 0)
9898
end
9999
end
100100

101-
if (deref.blank? || seg_index == segments.size - 1)
101+
if deref.blank? || seg_index == segments.size - 1
102102
deref
103103
else
104104
seg_index += 1
@@ -113,7 +113,8 @@ def json_walk(result, user_json, prop, custom_path: nil)
113113
path = path.gsub(".[].", ".").gsub(".[", "[")
114114
segments = parse_segments(path)
115115
val = walk_path(user_json, segments)
116-
result[prop] = val if val.present?
116+
# [] should be nil, false should be false
117+
result[prop] = val.presence || (val == [] ? nil : val)
117118
end
118119
end
119120

spec/plugin_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,21 @@
312312
expect(result).to eq "http://example.com/1.png"
313313
end
314314

315+
it "can walk json and appropriately assign a `false`" do
316+
authenticator = OAuth2BasicAuthenticator.new
317+
json_string = '{"user":{"id":1234, "data": {"address":"test@example.com", "is_cat": false}}}'
318+
SiteSetting.oauth2_json_email_verified_path = "user.data.is_cat"
319+
result =
320+
authenticator.json_walk(
321+
{},
322+
JSON.parse(json_string),
323+
"extra:user.data.is_cat",
324+
custom_path: "user.data.is_cat",
325+
)
326+
327+
expect(result).to eq false
328+
end
329+
315330
describe "token_callback" do
316331
let(:user) { Fabricate(:user) }
317332
let(:strategy) { OmniAuth::Strategies::Oauth2Basic.new({}) }

0 commit comments

Comments
 (0)