Skip to content

Commit

Permalink
FIX: Appropriately assign values when fetching user details (#100)
Browse files Browse the repository at this point in the history
FIX: Appropriately assign values when fetching user details
  • Loading branch information
nattsw committed Mar 21, 2024
1 parent 5cd7a79 commit f11229a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
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
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

0 comments on commit f11229a

Please sign in to comment.