josebuilder 0.0.6 → 1.0.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 829439b805873f4fc7316f4cb4cff959f0d3a575
4
- data.tar.gz: e38b3821283ce3f1d46fb318b32b6fbee46f3de6
3
+ metadata.gz: bc8f3e4feb7186c05e4532e03f1e6fb64bd59776
4
+ data.tar.gz: be946b2e10a98650aa784aef1d7174ec7d1afe77
5
5
  SHA512:
6
- metadata.gz: 14c50307fab5e8a189c8ac785453d8eedcba47f07018c6aad24522f0132adfc3c096e322ef2d249b1fbf11f3326cec1901a6f167b8e26d0ad63294f8436c018c
7
- data.tar.gz: 235e76fcfafc51970b4097409058e0ce1fe9db87f730d6a995569b12fb644b3c126d94170c2b6aea2960f4b60924b9ff1fe3d9ce3e2cce4915f20b3bd6e58fd4
6
+ metadata.gz: abecbb72c4dba704c89461105732a80da7c95c1a686794c17da7ca6e2ac40cf131b0fc1398f929cd5c873ee33e7e9cf3263ee9a617f1420a5a2e5249912e55c8
7
+ data.tar.gz: 2a119b54ce0ec00940ceb840abf1e3473cf40c1c2ae92df11ccccb2a39589f0373936e7be90d32bfafd46ed3079d5dd8e07bd47d7971ca50006e673ee8bf8afc
@@ -0,0 +1,74 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/resource_helpers'
3
+ require 'rails/generators/named_base'
4
+ module Josebuilder
5
+ class JosebuilderGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ argument :resource_name, :type => :string, :default => "defaultResourceName"
9
+ argument :secret, :type => :string, :default => "secret"
10
+ argument :algorithm, :type => :string, :default => "HS256"
11
+
12
+ class_option :signature, :type => :boolean, :default => true,
13
+ :description => "include signature"
14
+ class_option :encryption, :type => :boolean, :default => false,
15
+ :description => "include encryption"
16
+ class_option :combination, :type => :boolean, :default => false,
17
+ :description => "combine digital signature and encryption"
18
+
19
+ def generate_json_web_signature
20
+ ["index", "show"].each do |view|
21
+ file = filename_with_directory('app/views', view)
22
+ source = "signature_" + filename_with_extensions(view)
23
+ template source, file
24
+ end if options.signature?
25
+ end
26
+
27
+ def generate_json_web_encription
28
+ ["index", "show"].each do |view|
29
+ file = filename_with_directory('app/views', view)
30
+ source = "encryption_" + filename_with_extensions(view)
31
+ template source, file
32
+ end if options.encryption?
33
+ end
34
+
35
+
36
+ private
37
+
38
+ def get_secret
39
+ secret
40
+ end
41
+ def file_name
42
+ resource_name.underscore
43
+ end
44
+
45
+ def filename_with_extensions(name)
46
+ [name, :json, :jbuilder] * '.'
47
+ end
48
+
49
+ def pluralize(count, singular, plural = nil)
50
+ word = if (count == 1 || count =~ /^1(\.0+)?$/)
51
+ singular
52
+ else
53
+ plural || singular.pluralize
54
+ end
55
+
56
+ "#{count || 0} #{word}"
57
+ end
58
+
59
+ def filename_with_directory(directory, file_name)
60
+ file_name = filename_with_extensions(file_name)
61
+ File.join(directory, controller_file_path, file_name)
62
+ end
63
+
64
+ def controller_file_path
65
+ pluralize_without_count(2, resource_name)
66
+ end
67
+ def pluralize_without_count(count, noun, text=nil)
68
+ if count!=0
69
+ count == 1? "#{noun}#{text}": "#{noun.pluralize}#{text}"
70
+ end
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,4 @@
1
+ json.protected = JWE.encode_protected_header('aes-256-cbc')
2
+ json.jwe_header = JWE.encode_header('RSA')
3
+ json.encrypted_key = JWE.encode_key('123456789012345678901234678901234567890')
4
+ json.iv, json.cipher_text = JWE.generate_cipher_text(@users.as_json, 'aes-256-cbc', '123456789012345678901234678901234567890')
@@ -0,0 +1,4 @@
1
+ json.protected = JWE.encode_protected_header('aes-256-cbc')
2
+ json.jwe_header = JWE.encode_header('RSA')
3
+ json.encrypted_key = JWE.encode_key('123456789012345678901234678901234567890')
4
+ json.iv, json.cipher_text = JWE.generate_cipher_text(@user.as_json, 'aes-256-cbc', '123456789012345678901234678901234567890')
@@ -0,0 +1,3 @@
1
+ json.header = {alg: "<%= algorithm %>", typ: "JWS"}
2
+ json.payload = @<%= file_name %>.as_json
3
+ json.signature = JWS.encode(@<%= file_name %>.as_json, "<%= secret %>", "<%= algorithm %>")
data/lib/jwe.rb ADDED
@@ -0,0 +1,117 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module JWE
5
+ class InvalidFormat < StandardError; end
6
+ class DecryptionFailed < StandardError; end
7
+ class UnexpectedAlgorithm < StandardError; end
8
+
9
+ module_function
10
+
11
+ def generate_cipher_text(plain_text, cipher_name, private_key)
12
+ cipher = get_cipher_method(get_cipher_name(cipher_name))
13
+ iv = cipher.random_iv
14
+ cipher_text = encrypt(private_key, iv, cipher_name,plain_text)
15
+
16
+ iv_encode = encode_iv(iv)
17
+ cipher_text_encode = encode_cipher_text(cipher_text)
18
+ [iv_encode, cipher_text_encode]
19
+ end
20
+ def encode_cipher_text(cipher_text)
21
+ base64url_encode(cipher_text)
22
+ end
23
+ def decode_cipher_text(cipher_text, iv, private_key, cipher_name)
24
+ cipher_text = base64url_decode(cipher_text)
25
+ text = decrypt(private_key, decode_iv(iv), cipher_name, cipher_text)
26
+ decode_json base64url_decode(text)
27
+ end
28
+ def encode_header(alg)
29
+ alg = {'alg' => alg}
30
+ end
31
+ def encode_key(key)
32
+ base64url_encode generate_jwe_encrypted_key(key)
33
+ end
34
+ def decode_key(code)
35
+ code = base64url_decode(code)
36
+ decrypt_jwe_encrypted_key(code)
37
+ end
38
+ def encode_protected_header(encrypt_method, p_header={})
39
+ header = {'enc' => encrypt_method}.merge(p_header)
40
+ base64url_encode(encode_json(header))
41
+ end
42
+ def decode_protected_header(code)
43
+ decode_json(base64url_decode(code))
44
+ end
45
+ def encode_iv(iv)
46
+ base64url_encode(iv)
47
+ end
48
+
49
+ def decode_iv(iv_code)
50
+ base64url_decode(iv_code)
51
+ end
52
+
53
+ def get_cipher_name(name)
54
+ 'aes-256-cbc'
55
+ end
56
+
57
+ def get_cipher_method(cipher_name)
58
+ OpenSSL::Cipher.new cipher_name
59
+ end
60
+
61
+ def encrypt(private_key, iv, cipher_name, plain_text)
62
+ encryption = get_cipher_method(get_cipher_name(cipher_name))
63
+ encryption.encrypt
64
+ encryption.key = private_key
65
+ encryption.iv = iv
66
+ encrypt_input = base64url_encode(encode_json(plain_text))
67
+ encryption.update(encrypt_input) + encryption.final
68
+ end
69
+ def decrypt(private_key, iv, cipher_name, cipher_text)
70
+ decryption = get_cipher_method(get_cipher_name(cipher_name))
71
+ decryption.decrypt
72
+ decryption.key = private_key
73
+ decryption.iv = iv
74
+ decryption.update(cipher_text) + decryption.final
75
+ end
76
+
77
+ def generate_jwe_encrypted_key(key)
78
+ rsa = OpenSSL::PKey::RSA.new 2048
79
+ rsa_pem = rsa.to_pem
80
+ open('rsa.pem', 'w'){ |file|
81
+ file.puts rsa_pem
82
+ }
83
+ rsa.private_encrypt key
84
+ end
85
+ def decrypt_jwe_encrypted_key(encrypted_key)
86
+ encrypted_key = base64url_decode encrypted_key
87
+ rsa = OpenSSL::PKey::RSA.new File.read 'rsa.pem'
88
+ rsa.public_decrypt encrypted_key
89
+ end
90
+
91
+ def decode_json(encoded_json)
92
+ JSON.parse(encoded_json)
93
+ rescue JSON::ParseError
94
+ raise JOSE::DecodeError.new("Invalid encoding")
95
+ end
96
+
97
+ def encode_json(raw)
98
+ JSON.generate(raw)
99
+ end
100
+ def base64url_decode(str)
101
+ str += '=' *(4 - str.length.modulo(4))
102
+ Base64.decode64(str.tr('-_', '+/'))
103
+ end
104
+
105
+ def base64url_encode(str)
106
+ Base64.encode64(str).tr('+/', '-_').gsub(/[\n=]/, '')
107
+ end
108
+ def secure_compare(a, b)
109
+ return false if a.nil? || b.nil? || a.empty? || b.empty? || a.bytesize != b.bytesize
110
+ l = a.unpack "C#{a.bytesize}"
111
+
112
+ res = 0
113
+ b.each_byte { |byte| res |= byte ^ l.shift }
114
+ res == 0
115
+ end
116
+
117
+ end
data/lib/jws.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  require 'base64'
2
2
  require 'openssl'
3
- require 'jose/json'
3
+ require 'json'
4
4
 
5
5
  module JWS
6
6
  class DecodeError < StandardError; end
7
7
  class VerificationError < DecodeError; end
8
- extend JWS::Json
9
8
 
10
9
  module_function
11
10
 
@@ -18,7 +17,7 @@ module JWS
18
17
  end
19
18
 
20
19
  def sign_hmac(algorithm, msg, key)
21
- OpenSSL::HMAC.digest(OpenSSL::Digest.new(algorithm.sub('HS', 'Sha')), key, msg)
20
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new(algorithm.sub('HS', 'SHA')), key, msg)
22
21
  end
23
22
 
24
23
  def base64url_decode(str)
@@ -124,4 +123,14 @@ module JWS
124
123
  res == 0
125
124
  end
126
125
 
126
+ def decode_json(encoded_json)
127
+ JSON.parse(encoded_json)
128
+ rescue JSON::ParseError
129
+ raise JOSE::DecodeError.new("Invalid encoding")
130
+ end
131
+
132
+ def encode_json(raw)
133
+ JSON.generate(raw)
134
+ end
135
+
127
136
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: josebuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 1.0.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nguyen Ngo Dinh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-24 00:00:00.000000000 Z
11
+ date: 2015-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -71,18 +71,13 @@ executables: []
71
71
  extensions: []
72
72
  extra_rdoc_files: []
73
73
  files:
74
- - josebuilder-0.0.0.gem
75
- - josebuilder-0.0.1.gem
76
- - josebuilder-0.0.2.gem
77
- - josebuilder-0.0.3.gem
78
- - josebuilder-0.0.4.gem
79
- - josebuilder.gemspec
80
- - lib/jose/generators/josebuilder/USAGE
81
- - lib/jose/generators/josebuilder/josebuilder_generator.rb
82
- - lib/jose/generators/josebuilder/templates/index.json.jbuilder
83
- - lib/jose/generators/josebuilder/templates/show.json.jbuilder
84
- - lib/jose/json.rb
85
- - lib/josebuilder.rb
74
+ - lib/generators/josebuilder/USAGE
75
+ - lib/generators/josebuilder/josebuilder_generator.rb
76
+ - lib/generators/josebuilder/templates/encryption_index.json.jbuilder
77
+ - lib/generators/josebuilder/templates/encryption_show.json.jbuilder
78
+ - lib/generators/josebuilder/templates/signature_index.json.jbuilder
79
+ - lib/generators/josebuilder/templates/signature_show.json.jbuilder
80
+ - lib/jwe.rb
86
81
  - lib/jws.rb
87
82
  homepage: https://github.com/nguyenngodinh/josebuilder
88
83
  licenses:
@@ -99,9 +94,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
94
  version: 1.9.3
100
95
  required_rubygems_version: !ruby/object:Gem::Requirement
101
96
  requirements:
102
- - - '>='
97
+ - - '>'
103
98
  - !ruby/object:Gem::Version
104
- version: '0'
99
+ version: 1.3.1
105
100
  requirements: []
106
101
  rubyforge_project:
107
102
  rubygems_version: 2.4.6
Binary file
Binary file
Binary file
Binary file
Binary file
data/josebuilder.gemspec DELETED
@@ -1,19 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = 'josebuilder'
3
- s.version = '0.0.6'
4
- s.authors = ['Nguyen Ngo Dinh']
5
- s.email = ['nguyenngodinh@outlook.com']
6
- s.summary = 'Create JSON Signature and encryption structures'
7
- s.description = "json signature and encryption builder"
8
- s.homepage = 'https://github.com/nguyenngodinh/josebuilder'
9
- s.license = 'MIT'
10
-
11
- s.required_ruby_version = '>= 1.9.3'
12
-
13
- s.add_dependency 'activesupport', '>= 3.0.0', '< 5'
14
- s.add_dependency 'multi_json', '~> 1.2'
15
- s.add_runtime_dependency 'jwt', '~> 1.4', '>= 1.4.1'
16
-
17
- s.files = `git ls-files`.split("\n")
18
- end
19
-
@@ -1,63 +0,0 @@
1
- require 'rails/generators/resource_helpers'
2
- require 'rails/generators/named_base'
3
-
4
- class JosebuilderGenerator < Rails::Generators::Base
5
- source_root File.expand_path('../templates', __FILE__)
6
-
7
- argument :resource_name, :type => :string, :default => "defaultResourceName"
8
- argument :secret, :type => :string, :default => "secret"
9
- argument :algorithm, :type => :string, :default => "HS256"
10
-
11
- class_option :signature, :type => :boolean, :default => true,
12
- :description => "include signature"
13
- class_option :encryption, :type => :boolean, :default => false,
14
- :description => "include encryption"
15
- class_option :combination, :type => :boolean, :default => false,
16
- :description => "combine digital signature and encryption"
17
-
18
- def generate_json_web_signature_file
19
- ["index", "show"].each do |view|
20
- file = filename_with_directory(view)
21
- template filename_with_extensions(view), file
22
- end if options.signature?
23
- end
24
-
25
-
26
- private
27
-
28
- def get_secret
29
- secret
30
- end
31
- def file_name
32
- resource_name.underscore
33
- end
34
-
35
- def filename_with_extensions(name)
36
- [name, :json, :jbuilder] * '.'
37
- end
38
-
39
- def pluralize(count, singular, plural = nil)
40
- word = if (count == 1 || count =~ /^1(\.0+)?$/)
41
- singular
42
- else
43
- plural || singular.pluralize
44
- end
45
-
46
- "#{count || 0} #{word}"
47
- end
48
-
49
- def filename_with_directory(file_name)
50
- file_name = filename_with_extensions(file_name)
51
- File.join('app/views', controller_file_path, file_name)
52
- end
53
-
54
- def controller_file_path
55
- pluralize_without_count(2, resource_name)
56
- end
57
- def pluralize_without_count(count, noun, text=nil)
58
- if count!=0
59
- count == 1? "#{noun}#{text}": "#{noun.pluralize}#{text}"
60
- end
61
- end
62
-
63
- end
@@ -1,3 +0,0 @@
1
- json.header = {alg: "<%= algorithm %>", typ: "JWT"}
2
- json.payload = @<%= file_name %>.as_json
3
- json.signature = JWT.encode(@<%= file_name %>.as_json, "<%= secret %>", "<%= algorithm %>")
data/lib/jose/json.rb DELETED
@@ -1,15 +0,0 @@
1
- module JWS
2
- module Json
3
- require 'json'
4
-
5
- def decode_json(encoded_json)
6
- JSON.parse(encoded_json)
7
- rescue JSON::ParseError
8
- raise JOSE::DecodeError.new("Invalid encoding")
9
- end
10
-
11
- def encode_json(raw)
12
- JSON.generate(raw)
13
- end
14
- end
15
- end
data/lib/josebuilder.rb DELETED
File without changes