-
Notifications
You must be signed in to change notification settings - Fork 12
[WIP] Add base64 serialization #28
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
Closed
+113
−17
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c9911c5
Add cursor base64 conversion
FabienChaynes eb7790c
Raise InvalidBase64Error in case of invalid Base64 string
FabienChaynes 1096bd7
Add spec
FabienChaynes 9d1f2f6
Add missing requires
FabienChaynes 0dda48e
Update CHANGELOG
FabienChaynes af8e84a
Fix module namespacing
FabienChaynes 73bb694
Use Mongoid::Scroll::Cursor subclasses for serialization
FabienChaynes b22a195
Add Mongoid::Scroll::SignedBase64EncodedCursor
FabienChaynes 27bd29c
Raise if a cursor passed to Mongoid::Criteria::Scrollable#scroll does…
FabienChaynes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'base64' | ||
require 'json' | ||
|
||
module Mongoid | ||
module Scroll | ||
# Allows to serialize/deserialize the cursor using RFC 4648 | ||
class Base64EncodedCursor < Cursor | ||
def to_s | ||
Base64.strict_encode64({ value: super, field_type: field_type, field_name: field_name, direction: direction }.to_json) | ||
end | ||
|
||
class << self | ||
def deserialize(str) | ||
config_hash = ::JSON.parse(::Base64.strict_decode64(str)) | ||
new(config_hash['value'], field_type: config_hash['field_type'], field_name: config_hash['field_name'], direction: config_hash['direction']) | ||
rescue ArgumentError | ||
raise Mongoid::Scroll::Errors::InvalidBase64Error.new(str: str) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Mongoid | ||
module Scroll | ||
module Errors | ||
# Raised when a string expected to be encoded in Base64 (following RFC 4648) is invalid | ||
class InvalidBase64Error < Mongoid::Scroll::Errors::Base | ||
def initialize(opts = {}) | ||
super(compose_message('invalid_base64', opts)) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'base64' | ||
require 'json' | ||
|
||
module Mongoid | ||
module Scroll | ||
# Allows to serialize/deserialize the cursor using RFC 4648 and sign it to avoid tampering | ||
class SignedBase64EncodedCursor < Cursor | ||
def to_s(&_block) | ||
config_hash = { value: super, field_type: field_type, field_name: field_name, direction: direction } | ||
sign = yield(config_hash.to_json) | ||
config_hash[:sign] = sign | ||
Base64.strict_encode64(config_hash.to_json) | ||
end | ||
|
||
class << self | ||
def deserialize(str, &_block) | ||
config_hash = ::JSON.parse(::Base64.strict_decode64(str)) | ||
sign = config_hash.delete('sign') | ||
raise ::Exception.new('Invalid signature') if sign != yield(config_hash.to_json) # TODO: Add custom exception | ||
new(config_hash['value'], field_type: config_hash['field_type'], field_name: config_hash['field_name'], direction: config_hash['direction']) | ||
rescue ArgumentError | ||
raise Mongoid::Scroll::Errors::InvalidBase64Error.new(str: str) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'spec_helper' | ||
|
||
describe Mongoid::Scroll::Base64EncodedCursor do | ||
context 'an empty cursor' do | ||
subject do | ||
Mongoid::Scroll::Base64EncodedCursor.new nil, field_name: 'a_string', field_type: String | ||
end | ||
its(:tiebreak_id) { should be_nil } | ||
its(:value) { should be_nil } | ||
its(:criteria) { should eq({}) } | ||
describe 'base64' do | ||
let(:base64_string) { 'eyJ2YWx1ZSI6bnVsbCwiZmllbGRfdHlwZSI6IlN0cmluZyIsImZpZWxkX25hbWUiOiJhX3N0cmluZyIsImRpcmVjdGlvbiI6MX0=' } | ||
its(:to_s) { should eq(base64_string) } | ||
it 'is properly decoded' do | ||
cursor = Mongoid::Scroll::Base64EncodedCursor.deserialize(base64_string) | ||
expect(cursor.tiebreak_id).to be_nil | ||
expect(cursor.value).to be_nil | ||
expect(cursor.criteria).to eq({}) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.