Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeviseTokenAuth::Url.generate should accept relative path #1252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 3 additions & 8 deletions lib/devise_token_auth/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ module DeviseTokenAuth::Url

def self.generate(url, params = {})
uri = URI(url)
query_params = Hash[URI.decode_www_form(uri.query || '')].merge(params)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The problem is that expiry gets lost here in rails 4.2 (/controllers/devise_token_auth/passwords_controller_test.rb:207). I thought URI is from ruby, so it's weird

uri.query = URI.encode_www_form(query_params)

res = "#{uri.scheme}://#{uri.host}"
res += ":#{uri.port}" if (uri.port && uri.port != 80 && uri.port != 443)
res += uri.path.to_s if uri.path
query = [uri.query, params.to_query].reject(&:blank?).join('&')
res += "?#{query}"
res += "##{uri.fragment}" if uri.fragment

res
uri.to_s
end

def self.whitelisted?(url)
Expand Down
6 changes: 6 additions & 0 deletions test/lib/devise_token_auth/url_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class DeviseTokenAuth::UrlTest < ActiveSupport::TestCase
assert_equal DeviseTokenAuth::Url.send(:generate, url, params), 'http://example.com?client_id=123#fragment'
end

test 'relative path should be returned if url is relative' do
params = { client_id: 123 }
url = '/foobar'
assert_equal DeviseTokenAuth::Url.send(:generate, url, params), '/foobar?client_id=123'
end

describe 'with existing query params' do
test 'should preserve existing query params' do
url = 'http://example.com?a=1'
Expand Down