validators 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +10 -8
- data/lib/validators/validates_ssh_private_key.rb +55 -0
- data/lib/validators/validates_ssh_public_key.rb +27 -0
- data/lib/validators/version.rb +1 -1
- data/lib/validators.rb +2 -0
- data/spec/support/translations.yml +4 -0
- data/spec/validators/validates_ssh_private_key_spec.rb +152 -0
- data/spec/validators/validates_ssh_public_key_spec.rb +39 -0
- data/validators.gemspec +1 -0
- metadata +22 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f11819491da6f74f88f456177da2ad932488f72
|
4
|
+
data.tar.gz: 544d396ec5a0a9e84990d704334d77efc229e97a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e41c830254e68cd62daaac392604151bab5acf4d159925ea300d27a6066f30a4fab00284812de4745126f2d57714fef755a86b79d5e0946cea9f691437d3971e
|
7
|
+
data.tar.gz: 3f425a149171a58f09e23b71a9683f6a4253cbf505efaf609d50b4e33a9e89dda7826b8cdad59039a44bbdb86684f2316944fddc2638e98f546ca67b385cfbf5
|
data/Gemfile.lock
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
validators (2.
|
4
|
+
validators (2.3.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
8
8
|
specs:
|
9
|
-
activemodel (4.1.
|
10
|
-
activesupport (= 4.1.
|
9
|
+
activemodel (4.1.1)
|
10
|
+
activesupport (= 4.1.1)
|
11
11
|
builder (~> 3.1)
|
12
|
-
activerecord (4.1.
|
13
|
-
activemodel (= 4.1.
|
14
|
-
activesupport (= 4.1.
|
12
|
+
activerecord (4.1.1)
|
13
|
+
activemodel (= 4.1.1)
|
14
|
+
activesupport (= 4.1.1)
|
15
15
|
arel (~> 5.0.0)
|
16
|
-
activesupport (4.1.
|
16
|
+
activesupport (4.1.1)
|
17
17
|
i18n (~> 0.6, >= 0.6.9)
|
18
18
|
json (~> 1.7, >= 1.7.7)
|
19
19
|
minitest (~> 5.1)
|
@@ -30,7 +30,7 @@ GEM
|
|
30
30
|
debugger-linecache (~> 1.2.0)
|
31
31
|
debugger-ruby_core_source (~> 1.3.2)
|
32
32
|
debugger-linecache (1.2.0)
|
33
|
-
debugger-ruby_core_source (1.3.
|
33
|
+
debugger-ruby_core_source (1.3.4)
|
34
34
|
diff-lcs (1.2.5)
|
35
35
|
i18n (0.6.9)
|
36
36
|
json (1.8.1)
|
@@ -68,6 +68,7 @@ GEM
|
|
68
68
|
sqlite3 (1.3.9)
|
69
69
|
sqlite3-ruby (1.3.3)
|
70
70
|
sqlite3 (>= 1.3.3)
|
71
|
+
sshkey (1.6.1)
|
71
72
|
thread_safe (0.3.3)
|
72
73
|
tzinfo (1.1.0)
|
73
74
|
thread_safe (~> 0.1)
|
@@ -82,4 +83,5 @@ DEPENDENCIES
|
|
82
83
|
rake
|
83
84
|
rspec (= 3.0.0.beta2)
|
84
85
|
sqlite3-ruby
|
86
|
+
sshkey
|
85
87
|
validators!
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class SshPrivateKeyValidator < EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
sshkey = SSHKey.new(value.to_s)
|
6
|
+
validate_type(record, attribute, sshkey)
|
7
|
+
validate_bits(record, attribute, sshkey)
|
8
|
+
rescue OpenSSL::PKey::DSAError, OpenSSL::PKey::RSAError
|
9
|
+
record.errors.add(attribute, :invalid_ssh_private_key,
|
10
|
+
message: options[:message],
|
11
|
+
value: value
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def validate_type(record, attribute, sshkey)
|
18
|
+
return unless options[:type]
|
19
|
+
|
20
|
+
valid = [options[:type]]
|
21
|
+
.flatten.compact.map(&:to_s).include?(sshkey.type)
|
22
|
+
|
23
|
+
record.errors.add(attribute, :invalid_ssh_private_key_type,
|
24
|
+
message: options[:message],
|
25
|
+
value: (%w[rsa dsa] - [sshkey.type])[0].upcase
|
26
|
+
) unless valid
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_bits(record, attribute, sshkey)
|
30
|
+
return unless options[:bits]
|
31
|
+
|
32
|
+
record.errors.add(attribute, :invalid_ssh_private_key_bits,
|
33
|
+
message: options[:message],
|
34
|
+
value: sshkey.bits,
|
35
|
+
required: options[:bits]
|
36
|
+
) unless sshkey.bits >= options[:bits].to_i
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module ClassMethods
|
41
|
+
# Validates whether or not the specified CNPJ is valid.
|
42
|
+
#
|
43
|
+
# class User < ActiveRecord::Base
|
44
|
+
# validates_ssh_private_key :key
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
def validates_ssh_private_key(*attr_names)
|
48
|
+
require "sshkey"
|
49
|
+
validates_with SshPrivateKeyValidator, _merge_attributes(attr_names)
|
50
|
+
rescue LoadError
|
51
|
+
fail "sshkey is not part of the bundle. Add it to Gemfile."
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class SshPublicKeyValidator < EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
record.errors.add(attribute, :invalid_ssh_public_key,
|
6
|
+
message: options[:message],
|
7
|
+
value: value
|
8
|
+
) unless SSHKey.valid_ssh_public_key?(value)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
# Validates whether or not the specified CNPJ is valid.
|
14
|
+
#
|
15
|
+
# class User < ActiveRecord::Base
|
16
|
+
# validates_ssh_public_key :key
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
def validates_ssh_public_key(*attr_names)
|
20
|
+
require "sshkey"
|
21
|
+
validates_with SshPublicKeyValidator, _merge_attributes(attr_names)
|
22
|
+
rescue LoadError
|
23
|
+
fail "sshkey is not part of the bundle. Add it to Gemfile."
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/validators/version.rb
CHANGED
data/lib/validators.rb
CHANGED
@@ -7,6 +7,8 @@ require "validators/validates_url_format_of"
|
|
7
7
|
require "validators/validates_ownership_of"
|
8
8
|
require "validators/validates_cpf_format_of"
|
9
9
|
require "validators/validates_cnpj_format_of"
|
10
|
+
require "validators/validates_ssh_private_key"
|
11
|
+
require "validators/validates_ssh_public_key"
|
10
12
|
|
11
13
|
module Validators
|
12
14
|
end
|
@@ -28,6 +28,10 @@ pt-BR:
|
|
28
28
|
invalid_owner: "não está associado ao seu usuário"
|
29
29
|
invalid_cpf: "não é um CPF válido"
|
30
30
|
invalid_cnpj: "não é um CNPJ válido"
|
31
|
+
invalid_ssh_public_key: "não é uma chave pública de SSH válida"
|
32
|
+
invalid_ssh_private_key: "não é uma chave privada de SSH válida"
|
33
|
+
invalid_ssh_private_key_type: "precisa ser uma chave %{value}"
|
34
|
+
invalid_ssh_private_key_bits: "precisa ter pelo menos %{required} bits; a sua chave tem %{value} bits"
|
31
35
|
|
32
36
|
activemodel:
|
33
37
|
<<: *activerecord
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ".validates_ssh_private_key" do
|
4
|
+
context "common" do
|
5
|
+
let(:model) { Class.new {
|
6
|
+
def self.name
|
7
|
+
"User"
|
8
|
+
end
|
9
|
+
|
10
|
+
include ActiveModel::Model
|
11
|
+
validates_ssh_private_key :key
|
12
|
+
attr_accessor :key
|
13
|
+
} }
|
14
|
+
|
15
|
+
it "requires valid key" do
|
16
|
+
record = model.new(key: "invalid")
|
17
|
+
record.valid?
|
18
|
+
|
19
|
+
expect(record.errors[:key]).not_to be_empty
|
20
|
+
end
|
21
|
+
|
22
|
+
it "accepts valid key" do
|
23
|
+
record = model.new(key: SSHKey.generate.private_key)
|
24
|
+
record.valid?
|
25
|
+
|
26
|
+
expect(record.errors[:key]).to be_empty
|
27
|
+
end
|
28
|
+
|
29
|
+
it "sets translated error message" do
|
30
|
+
I18n.locale = "pt-BR"
|
31
|
+
message = "não é uma chave privada de SSH válida"
|
32
|
+
|
33
|
+
record = model.new(key: "invalid")
|
34
|
+
record.valid?
|
35
|
+
|
36
|
+
expect(record.errors[:key]).to include(message)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "type validation (RSA)" do
|
41
|
+
let(:model) { Class.new {
|
42
|
+
def self.name
|
43
|
+
"User"
|
44
|
+
end
|
45
|
+
|
46
|
+
include ActiveModel::Model
|
47
|
+
validates_ssh_private_key :key, type: 'rsa'
|
48
|
+
attr_accessor :key
|
49
|
+
} }
|
50
|
+
|
51
|
+
it "accepts rsa key" do
|
52
|
+
record = model.new(key: SSHKey.generate(type: "rsa").private_key)
|
53
|
+
record.valid?
|
54
|
+
|
55
|
+
expect(record.errors[:key]).to be_empty
|
56
|
+
end
|
57
|
+
|
58
|
+
it "rejects dsa key" do
|
59
|
+
record = model.new(key: SSHKey.generate(type: "dsa").private_key)
|
60
|
+
record.valid?
|
61
|
+
|
62
|
+
expect(record.errors[:key]).not_to be_empty
|
63
|
+
end
|
64
|
+
|
65
|
+
it "sets translated error message" do
|
66
|
+
I18n.locale = "pt-BR"
|
67
|
+
|
68
|
+
record = model.new(key: SSHKey.generate(type: "dsa").private_key)
|
69
|
+
record.valid?
|
70
|
+
|
71
|
+
expect(record.errors[:key]).to include("precisa ser uma chave RSA")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "type validation (DSA)" do
|
76
|
+
let(:model) { Class.new {
|
77
|
+
def self.name
|
78
|
+
"User"
|
79
|
+
end
|
80
|
+
|
81
|
+
include ActiveModel::Model
|
82
|
+
validates_ssh_private_key :key, type: "dsa"
|
83
|
+
attr_accessor :key
|
84
|
+
} }
|
85
|
+
|
86
|
+
it "accepts dsa key" do
|
87
|
+
record = model.new(key: SSHKey.generate(type: "dsa").private_key)
|
88
|
+
record.valid?
|
89
|
+
|
90
|
+
expect(record.errors[:key]).to be_empty
|
91
|
+
end
|
92
|
+
|
93
|
+
it "rejects rsa key" do
|
94
|
+
record = model.new(key: SSHKey.generate(type: 'rsa').private_key)
|
95
|
+
record.valid?
|
96
|
+
|
97
|
+
expect(record.errors[:key]).not_to be_empty
|
98
|
+
end
|
99
|
+
|
100
|
+
it "sets translated error message" do
|
101
|
+
I18n.locale = "pt-BR"
|
102
|
+
|
103
|
+
record = model.new(key: SSHKey.generate(type: 'rsa').private_key)
|
104
|
+
record.valid?
|
105
|
+
|
106
|
+
expect(record.errors[:key]).to include("precisa ser uma chave DSA")
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "bits" do
|
111
|
+
let(:model) { Class.new {
|
112
|
+
def self.name
|
113
|
+
"User"
|
114
|
+
end
|
115
|
+
|
116
|
+
include ActiveModel::Model
|
117
|
+
validates_ssh_private_key :key, bits: 2048
|
118
|
+
attr_accessor :key
|
119
|
+
} }
|
120
|
+
|
121
|
+
it "accepts bits equals to required" do
|
122
|
+
record = model.new(key: SSHKey.generate(bits: 2048).private_key)
|
123
|
+
record.valid?
|
124
|
+
|
125
|
+
expect(record.errors[:key]).to be_empty
|
126
|
+
end
|
127
|
+
|
128
|
+
it "accepts bits greater than required" do
|
129
|
+
record = model.new(key: SSHKey.generate(bits: 4096).private_key)
|
130
|
+
record.valid?
|
131
|
+
|
132
|
+
expect(record.errors[:key]).to be_empty
|
133
|
+
end
|
134
|
+
|
135
|
+
it "rejects invalid bits" do
|
136
|
+
record = model.new(key: SSHKey.generate(bits: 1024).private_key)
|
137
|
+
record.valid?
|
138
|
+
|
139
|
+
expect(record.errors[:key]).not_to be_empty
|
140
|
+
end
|
141
|
+
|
142
|
+
it "sets translated error message" do
|
143
|
+
I18n.locale = "pt-BR"
|
144
|
+
message = "precisa ter pelo menos 2048 bits; a sua chave tem 1024 bits"
|
145
|
+
|
146
|
+
record = model.new(key: SSHKey.generate(bits: 1024).private_key)
|
147
|
+
record.valid?
|
148
|
+
|
149
|
+
expect(record.errors[:key]).to include(message)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ".validates_ssh_public_key" do
|
4
|
+
context "common" do
|
5
|
+
let(:model) { Class.new {
|
6
|
+
def self.name
|
7
|
+
"User"
|
8
|
+
end
|
9
|
+
|
10
|
+
include ActiveModel::Model
|
11
|
+
validates_ssh_public_key :key
|
12
|
+
attr_accessor :key
|
13
|
+
} }
|
14
|
+
|
15
|
+
it "requires valid key" do
|
16
|
+
record = model.new(key: nil)
|
17
|
+
record.valid?
|
18
|
+
|
19
|
+
expect(record.errors[:key]).not_to be_empty
|
20
|
+
end
|
21
|
+
|
22
|
+
it "accepts valid key" do
|
23
|
+
record = model.new(key: SSHKey.generate.ssh_public_key)
|
24
|
+
record.valid?
|
25
|
+
|
26
|
+
expect(record.errors[:key]).to be_empty
|
27
|
+
end
|
28
|
+
|
29
|
+
it "sets translated error message" do
|
30
|
+
I18n.locale = "pt-BR"
|
31
|
+
message = "não é uma chave pública de SSH válida"
|
32
|
+
|
33
|
+
record = model.new(key: "invalid")
|
34
|
+
record.valid?
|
35
|
+
|
36
|
+
expect(record.errors[:key]).to include(message)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/validators.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sshkey
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
97
111
|
description: Add some nice ActiveRecord validators.
|
98
112
|
email:
|
99
113
|
- fnando.vieira@gmail.com
|
@@ -116,6 +130,8 @@ files:
|
|
116
130
|
- lib/validators/validates_email_format_of.rb
|
117
131
|
- lib/validators/validates_ip_address.rb
|
118
132
|
- lib/validators/validates_ownership_of.rb
|
133
|
+
- lib/validators/validates_ssh_private_key.rb
|
134
|
+
- lib/validators/validates_ssh_public_key.rb
|
119
135
|
- lib/validators/validates_url_format_of.rb
|
120
136
|
- lib/validators/version.rb
|
121
137
|
- spec/schema.rb
|
@@ -133,6 +149,8 @@ files:
|
|
133
149
|
- spec/validators/validates_email_format_of_spec.rb
|
134
150
|
- spec/validators/validates_ip_address_spec.rb
|
135
151
|
- spec/validators/validates_ownership_of_spec.rb
|
152
|
+
- spec/validators/validates_ssh_private_key_spec.rb
|
153
|
+
- spec/validators/validates_ssh_public_key_spec.rb
|
136
154
|
- spec/validators/validates_url_format_of_spec.rb
|
137
155
|
- validators.gemspec
|
138
156
|
homepage: http://rubygems.org/gems/validators
|
@@ -174,4 +192,6 @@ test_files:
|
|
174
192
|
- spec/validators/validates_email_format_of_spec.rb
|
175
193
|
- spec/validators/validates_ip_address_spec.rb
|
176
194
|
- spec/validators/validates_ownership_of_spec.rb
|
195
|
+
- spec/validators/validates_ssh_private_key_spec.rb
|
196
|
+
- spec/validators/validates_ssh_public_key_spec.rb
|
177
197
|
- spec/validators/validates_url_format_of_spec.rb
|