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

Allow to configure guest_token cookie options #3621

Merged
merged 1 commit into from
May 14, 2020
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
4 changes: 4 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class AppConfiguration < Preferences::Configuration
# @return [Boolean] When false, customers must create an account to complete an order (default: +true+)
preference :allow_guest_checkout, :boolean, default: true

# @!attribute [rw] guest_token_cookie_options
# @return [Hash] Add additional guest_token cookie options here (ie. domain or path)
preference :guest_token_cookie_options, :hash, default: {}

# @!attribute [rw] allow_return_item_amount_editing
# @return [Boolean] Determines whether an admin is allowed to change a return item's pre-calculated amount (default: +false+)
preference :allow_return_item_amount_editing, :boolean, default: false
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/core/controller_helpers/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def redirect_back_or_default(default)

def set_guest_token
unless cookies.signed[:guest_token].present?
cookies.permanent.signed[:guest_token] = {
cookies.permanent.signed[:guest_token] = Spree::Config[:guest_token_cookie_options].merge(
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
value: SecureRandom.urlsafe_base64(nil, false),
httponly: true
}
)
end
end

Expand Down
19 changes: 19 additions & 0 deletions core/spec/lib/spree/core/controller_helpers/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ def controller.index
expect(response.headers["Set-Cookie"]).to match(/guest_token.*HttpOnly/)
expect(response.cookies['guest_token']).not_to be_nil
end

context 'with guest_token_cookie_options configured' do
it 'sends cookie with these options' do
stub_spree_preferences(guest_token_cookie_options: {
domain: :all,
path: '/api'
})
get :index
expect(response.headers["Set-Cookie"]).to match(/domain=\.test\.host; path=\/api/)
end

it 'never overwrites httponly' do
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
stub_spree_preferences(guest_token_cookie_options: {
httponly: false
})
get :index
expect(response.headers["Set-Cookie"]).to match(/guest_token.*HttpOnly/)
end
end
end

describe '#store_location' do
Expand Down