mongoid-encrypted_string 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +25 -0
- data/Gemfile +6 -0
- data/Rakefile +7 -0
- data/lib/mongoid/encrypted_string.rb +34 -0
- data/lib/mongoid/encrypted_string/global.rb +3 -0
- data/lib/mongoid/encrypted_string/version.rb +5 -0
- data/mongoid-encrypted_string.gemspec +18 -0
- data/spec/encrypted_string_spec.rb +55 -0
- data/spec/global_spec.rb +10 -0
- data/spec/integration_spec.rb +45 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/support/config.rb +1 -0
- data/spec/support/connection.rb +1 -0
- data/spec/support/document.rb +17 -0
- data/spec/support/mongoid.yml +8 -0
- metadata +92 -0
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Because this is a gem, ignore Gemfile.lock:
|
2
|
+
|
3
|
+
Gemfile.lock
|
4
|
+
|
5
|
+
# And because this is Ruby, ignore the following
|
6
|
+
# (source: https://github.com/github/gitignore/blob/master/Ruby.gitignore):
|
7
|
+
|
8
|
+
*.gem
|
9
|
+
*.rbc
|
10
|
+
.bundle
|
11
|
+
.config
|
12
|
+
coverage
|
13
|
+
InstalledFiles
|
14
|
+
lib/bundler/man
|
15
|
+
pkg
|
16
|
+
rdoc
|
17
|
+
spec/reports
|
18
|
+
test/tmp
|
19
|
+
test/version_tmp
|
20
|
+
tmp
|
21
|
+
|
22
|
+
# YARD artifacts
|
23
|
+
.yardoc
|
24
|
+
_yardoc
|
25
|
+
doc/
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'gibberish'
|
3
|
+
require 'mongoid'
|
4
|
+
|
5
|
+
module Mongoid
|
6
|
+
class EncryptedString < String
|
7
|
+
def mongoize
|
8
|
+
self.class.mongoize(self)
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :key
|
13
|
+
|
14
|
+
def mongoize(string)
|
15
|
+
aes.encrypt(string)
|
16
|
+
end
|
17
|
+
|
18
|
+
def demongoize(string)
|
19
|
+
return if string.nil?
|
20
|
+
|
21
|
+
new(aes.decrypt(string))
|
22
|
+
end
|
23
|
+
|
24
|
+
def config
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def aes
|
30
|
+
@aes ||= Gibberish::AES.new(key || raise(StandardError, "Mongoid::EncryptedString.config.key is not set"))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
$: << File.expand_path('../lib', __FILE__)
|
2
|
+
require 'mongoid/encrypted_string/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 'mongoid-encrypted_string'
|
6
|
+
gem.version = Mongoid::EncryptedString::VERSION
|
7
|
+
gem.authors = 'Mario Uher'
|
8
|
+
gem.email = 'uher.mario@gmail.com'
|
9
|
+
gem.homepage = 'https://github.com/haihappen/mongoid-encrypted_string'
|
10
|
+
gem.summary = 'Encrypted string type for Mongoid.'
|
11
|
+
gem.description = 'Mongoid::EncryptedString defines an encrypted string type for your Mongoid apps.'
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split("\n")
|
14
|
+
gem.require_path = 'lib'
|
15
|
+
|
16
|
+
gem.add_dependency 'gibberish'
|
17
|
+
gem.add_dependency 'mongoid'
|
18
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::EncryptedString do
|
4
|
+
let(:string) { 'foo' }
|
5
|
+
subject { Mongoid::EncryptedString.new(string) }
|
6
|
+
|
7
|
+
|
8
|
+
describe 'not setting a key' do
|
9
|
+
it 'raises an error' do
|
10
|
+
key = Mongoid::EncryptedString.config.key
|
11
|
+
Mongoid::EncryptedString.config.key = nil
|
12
|
+
Mongoid::EncryptedString.instance_variable_set :@aes, nil
|
13
|
+
|
14
|
+
->{
|
15
|
+
subject.mongoize
|
16
|
+
}.must_raise StandardError
|
17
|
+
|
18
|
+
Mongoid::EncryptedString.config.key = key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
describe :mongoize do
|
24
|
+
let(:value) { subject.mongoize }
|
25
|
+
|
26
|
+
|
27
|
+
it 'returns database friendly value' do
|
28
|
+
value.must_be_kind_of String
|
29
|
+
value.wont_equal string
|
30
|
+
(value.length > string.length).must_equal true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
describe 'self.mongoize' do
|
36
|
+
let(:value) { Mongoid::EncryptedString.mongoize(subject) }
|
37
|
+
|
38
|
+
|
39
|
+
it 'returns database friendly value' do
|
40
|
+
value.must_be_kind_of String
|
41
|
+
value.wont_equal string
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
describe 'self.demongoize' do
|
47
|
+
let(:value) { Mongoid::EncryptedString.demongoize(Mongoid::EncryptedString.mongoize(string)) }
|
48
|
+
|
49
|
+
|
50
|
+
it 'returns a EncryptedString object' do
|
51
|
+
value.must_be_kind_of Mongoid::EncryptedString
|
52
|
+
value.must_equal string
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/global_spec.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Mongoid::EncryptedString integration' do
|
4
|
+
let(:string) { 'foo' }
|
5
|
+
|
6
|
+
|
7
|
+
describe 'create document without any attributes' do
|
8
|
+
subject { Document.create }
|
9
|
+
|
10
|
+
|
11
|
+
it 'sets encrypted string to nil' do
|
12
|
+
subject.string.must_be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
describe "when default is set to 'foo' }" do
|
17
|
+
subject { DocumentWithImplicitDefault.create }
|
18
|
+
|
19
|
+
|
20
|
+
it 'sets encrypted string to foo' do
|
21
|
+
subject.string.must_equal string
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
describe "when default is set to ->{ EncryptedString.new('foo') }" do
|
27
|
+
subject { DocumentWithExplicitDefault.create }
|
28
|
+
|
29
|
+
|
30
|
+
it 'sets encrypted string to foo' do
|
31
|
+
subject.string.must_equal string
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
describe 'create document with attribute' do
|
38
|
+
subject { Document.create(string: string) }
|
39
|
+
|
40
|
+
|
41
|
+
it 'sets encrypted string to foo' do
|
42
|
+
subject.string.must_equal string
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$: << File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'minitest/pride'
|
5
|
+
require 'minitest/spec'
|
6
|
+
|
7
|
+
require 'mongoid/encrypted_string'
|
8
|
+
|
9
|
+
# Load support *.rb files in ./support
|
10
|
+
Dir[File.expand_path('../support/*.rb', __FILE__)].each { |file| require_relative file }
|
@@ -0,0 +1 @@
|
|
1
|
+
Mongoid::EncryptedString.config.key = 'some super secure key'
|
@@ -0,0 +1 @@
|
|
1
|
+
Mongoid.load!(File.expand_path('../mongoid.yml', __FILE__), 'test')
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Document
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
field :string, type: Mongoid::EncryptedString
|
5
|
+
end
|
6
|
+
|
7
|
+
class DocumentWithImplicitDefault
|
8
|
+
include Mongoid::Document
|
9
|
+
|
10
|
+
field :string, type: Mongoid::EncryptedString, default: 'foo'
|
11
|
+
end
|
12
|
+
|
13
|
+
class DocumentWithExplicitDefault
|
14
|
+
include Mongoid::Document
|
15
|
+
|
16
|
+
field :string, type: Mongoid::EncryptedString, default: ->{ Mongoid::EncryptedString.new('foo') }
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-encrypted_string
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mario Uher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
none: false
|
21
|
+
name: gibberish
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
none: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
version_requirements: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
none: false
|
37
|
+
name: mongoid
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
none: false
|
46
|
+
description: Mongoid::EncryptedString defines an encrypted string type for your Mongoid
|
47
|
+
apps.
|
48
|
+
email: uher.mario@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- Rakefile
|
56
|
+
- lib/mongoid/encrypted_string.rb
|
57
|
+
- lib/mongoid/encrypted_string/global.rb
|
58
|
+
- lib/mongoid/encrypted_string/version.rb
|
59
|
+
- mongoid-encrypted_string.gemspec
|
60
|
+
- spec/encrypted_string_spec.rb
|
61
|
+
- spec/global_spec.rb
|
62
|
+
- spec/integration_spec.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
- spec/support/config.rb
|
65
|
+
- spec/support/connection.rb
|
66
|
+
- spec/support/document.rb
|
67
|
+
- spec/support/mongoid.yml
|
68
|
+
homepage: https://github.com/haihappen/mongoid-encrypted_string
|
69
|
+
licenses: []
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
none: false
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
none: false
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.23
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Encrypted string type for Mongoid.
|
92
|
+
test_files: []
|