Skip to content

Commit

Permalink
use urlsafe base64 encoding. #307
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jul 12, 2019
1 parent d97c652 commit 268a418
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def index
self: request.original_url,
next: @activities.size < page[:size] ? nil : request.base_url + "/activities?" + {
query: params[:query],
"page[cursor]" => page[:cursor] ? Base64.strict_encode64(Array.wrap(@activities.to_a.last[:sort]).join(',')) : nil,
"page[cursor]" => page[:cursor] ? Base64.urlsafe_encode64(Array.wrap(@activities.to_a.last[:sort]).join(","), padding: false) : nil,
"page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size],
sort: params[:sort] }.compact.to_query
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/concerns/paginatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def page_from_params(params)
if page.has_key?(:cursor)
begin
# When we decode and split, we'll always end up with an array
page[:cursor] = Base64.strict_decode64(page[:cursor].to_s).split(",")
# use urlsafe_decode to not worry about url-unsafe characters + and /
page[:cursor] = Base64.urlsafe_decode64(page[:cursor].to_s).split(",")
rescue ArgumentError
# If we fail to decode we'll just default back to an empty cursor
page[:cursor] = []
Expand Down
15 changes: 8 additions & 7 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DoisController < ApplicationController
include Crosscitable

prepend_before_action :authenticate_user!
before_action :set_doi, only: [:show, :destroy, :get_url]
before_action :set_doi, only: [:show, :get_url]
before_action :set_include, only: [:index, :show, :create, :update]
before_action :set_raven_context, only: [:create, :update, :validate]

Expand Down Expand Up @@ -181,7 +181,7 @@ def index
"provider-id" => params[:provider_id],
"client-id" => params[:client_id],
# The cursor link should be an array of values, but we want to encode it into a single string for the URL
"page[cursor]" => page[:cursor] ? Base64.strict_encode64(Array.wrap(results.to_a.last[:sort]).join(',')) : nil,
"page[cursor]" => page[:cursor] ? Base64.urlsafe_encode64(Array.wrap(results.to_a.last[:sort]).join(','), padding: false) : nil,
"page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size] }.compact.to_query
}.compact
Expand Down Expand Up @@ -316,7 +316,7 @@ def update
@doi.assign_attributes(safe_params.slice(:client_id))
else
authorize! :update, @doi
@doi.assign_attributes(safe_params.except(:doi, :client_id).merge(exists: exists))
@doi.assign_attributes(safe_params.except(:doi, :client_id))
end
else
doi_id = validate_doi(params[:id])
Expand Down Expand Up @@ -371,6 +371,9 @@ def undo

def destroy
logger = Logger.new(STDOUT)
@doi = Doi.where(doi: params[:id]).first
fail ActiveRecord::RecordNotFound unless @doi.present?

authorize! :destroy, @doi

if @doi.draft?
Expand Down Expand Up @@ -451,11 +454,9 @@ def status
protected

def set_doi
@doi = Doi.where(doi: params[:id]).first
response = Doi.find_by_id(params[:id])
@doi = response.records.first
fail ActiveRecord::RecordNotFound unless @doi.present?

# capture username and password for reuse in the handle system
@doi.current_user = current_user
end

def set_include
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def index
"registrant-id" => params[:registrant_id],
"publication-year" => params[:publication_year],
"year-month" => params[:year_month],
"page[cursor]" => page[:cursor] ? Base64.strict_encode64(Array.wrap(results.to_a.last[:sort]).join(',')) : nil,
"page[cursor]" => page[:cursor] ? Base64.urlsafe_encode64(Array.wrap(results.to_a.last[:sort]).join(","), padding: false) : nil,
"page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size] }.compact.to_query
}.compact
Expand Down

0 comments on commit 268a418

Please sign in to comment.