Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/oauth2_basic_authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def walk_path(fragment, segments, seg_index = 0)
return nil unless fragment.is_a?(Hash) || fragment.is_a?(Array)
first_seg = segments[seg_index].scan(/([\d+])/).length > 0 ? first_seg.split("[")[0] : first_seg
if fragment.is_a?(Hash)
deref = fragment[first_seg] || fragment[first_seg.to_sym]
deref = fragment[first_seg]
else
array_index = 0
if (seg_index > 0)
Expand All @@ -98,7 +98,7 @@ def walk_path(fragment, segments, seg_index = 0)
end
end

if (deref.blank? || seg_index == segments.size - 1)
if deref.blank? || seg_index == segments.size - 1
deref
else
seg_index += 1
Expand All @@ -113,7 +113,8 @@ def json_walk(result, user_json, prop, custom_path: nil)
path = path.gsub(".[].", ".").gsub(".[", "[")
segments = parse_segments(path)
val = walk_path(user_json, segments)
result[prop] = val if val.present?
# [] should be nil, false should be false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a test for the []->nil
it "can walk json and handle an empty array" do

result[prop] = val.presence || (val == [] ? nil : val)
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,21 @@
expect(result).to eq "http://example.com/1.png"
end

it "can walk json and appropriately assign a `false`" do
authenticator = OAuth2BasicAuthenticator.new
json_string = '{"user":{"id":1234, "data": {"address":"test@example.com", "is_cat": false}}}'
SiteSetting.oauth2_json_email_verified_path = "user.data.is_cat"
result =
authenticator.json_walk(
{},
JSON.parse(json_string),
"extra:user.data.is_cat",
custom_path: "user.data.is_cat",
)

expect(result).to eq false
end

describe "token_callback" do
let(:user) { Fabricate(:user) }
let(:strategy) { OmniAuth::Strategies::Oauth2Basic.new({}) }
Expand Down