secure_token 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/lib/secure_token.rb +145 -0
- data/lib/secure_token/version.rb +3 -0
- data/secure_token.gemspec +23 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1c42b95c82d58f115e0d710dae4d9d5b41f06401
|
4
|
+
data.tar.gz: 0ffeaf8544d0312ceca94c09d066f7e7adcac077
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dbfa21e93912bc288c1ab588beced4c5954cbb2405faa8aec13e66c1fd762aff35a30a9778cd7b1e187cd5f21419c076224ba117681d1fcacdcd9c5a89d22d62
|
7
|
+
data.tar.gz: 7e9e4cc07258a3d2dd259c6ebb04b4cccd7b1efbbfbc59ffd600e9f34c0a7bec5e806ba9b822f408193bb5ab201a3403534687c8bf160ce6c8cb6f92ebd4d3ba
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ivan Kasatenko
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# SecureToken
|
2
|
+
|
3
|
+
Gem that provides JWT token like solution. You can convert any hash into
|
4
|
+
a string that is suitable for storage at the client-side. It is
|
5
|
+
URL-safe, encrypted and digitally signed.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'secure_token'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install secure_token
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'secure_token'
|
25
|
+
|
26
|
+
data = SecureToken::SecureTokenHash.new
|
27
|
+
|
28
|
+
data.merge!({
|
29
|
+
key: 'value',
|
30
|
+
another: {
|
31
|
+
place: 1,
|
32
|
+
subdata: [ 4, 5, 6 ]
|
33
|
+
}
|
34
|
+
})
|
35
|
+
|
36
|
+
key = SecureToken::KeyPair.new('s3cret', 'service')
|
37
|
+
|
38
|
+
encrypted = data.to_token(key)
|
39
|
+
# encrypted is url-safe
|
40
|
+
# looks like "0qaai-mRZZY0WMJci13QST11UI80NiSc9YXb4ABW-pgauFH2wDtDpbH7Vm408BOP5xlq2jO3Srz_WqDlehi1AYP3VtFoUdtNtjuvObess0Lh35Yml1opZ2QOlJ2brwmjNxNWsEoC6JMsdzMUSuF-1JrQwvarPC5B"
|
41
|
+
|
42
|
+
decrypted = SecureToken::SecureTokenHash.from_token(encrypted, key)
|
43
|
+
# assert(decrypted == data)
|
44
|
+
|
45
|
+
invalid_key = SecureToken::KeyPair.new('secret', 'serv1ce')
|
46
|
+
not_decrypted = SecureToken::SecureTokenHash.from_token(encrypted, invalid_key)
|
47
|
+
# assert(not_decrypted == nil)
|
48
|
+
```
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it ( http://github.com/skywriter/secure_token/fork )
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/secure_token.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'base64'
|
3
|
+
require 'openssl'
|
4
|
+
require 'json'
|
5
|
+
require 'securerandom'
|
6
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
7
|
+
require 'active_support/hash_with_indifferent_access'
|
8
|
+
|
9
|
+
module SecureToken
|
10
|
+
|
11
|
+
class KeyPair < Struct.new(:encryption_key, :signing_key)
|
12
|
+
|
13
|
+
def initialize(*args)
|
14
|
+
super
|
15
|
+
freeze
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
class SecureTokenHash < HashWithIndifferentAccess
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def from_token(token, key_pair)
|
24
|
+
decryptor = SecureTokenService::Decryptor.new(SecureTokenService::JSONSerializer.new)
|
25
|
+
decrypted_token = decryptor.decrypt_and_verify(token, key_pair)
|
26
|
+
decrypted_token ? new.merge(decrypted_token) : nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_token(key_pair)
|
31
|
+
encryptor = SecureTokenService::Encryptor.new(SecureTokenService::JSONSerializer.new)
|
32
|
+
encryptor.encrypt_and_sign(self, key_pair)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
module SecureTokenService
|
38
|
+
|
39
|
+
HASH_ALGO = 'sha256'
|
40
|
+
SIGNATURE_LENGTH = OpenSSL::HMAC.digest(OpenSSL::Digest.new(HASH_ALGO), 'key', 'data').size
|
41
|
+
|
42
|
+
CRYPT_ALGO = 'AES-128-CBC'
|
43
|
+
|
44
|
+
class JSONSerializer
|
45
|
+
|
46
|
+
def serialize(object)
|
47
|
+
object.to_json
|
48
|
+
end
|
49
|
+
|
50
|
+
def deserialize(data)
|
51
|
+
begin
|
52
|
+
JSON.parse(data)
|
53
|
+
rescue JSON::ParserError
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
class Encryptor
|
61
|
+
|
62
|
+
def initialize(serializer)
|
63
|
+
@salt = SecureRandom.random_bytes(8)
|
64
|
+
@serializer = serializer
|
65
|
+
end
|
66
|
+
|
67
|
+
def encrypt_and_sign(data, key_pair)
|
68
|
+
serialized = @serializer.serialize(data)
|
69
|
+
encrypted = encrypt(serialized, key_pair.encryption_key)
|
70
|
+
signed = sign(encrypted, key_pair.signing_key)
|
71
|
+
Base64.urlsafe_encode64(signed)
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def sign(data, key)
|
77
|
+
signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new(HASH_ALGO), key, data)
|
78
|
+
"#{signature.force_encoding('ascii-8bit')}#{data.force_encoding('ascii-8bit')}"
|
79
|
+
end
|
80
|
+
|
81
|
+
def encrypt(data, key)
|
82
|
+
encrypter = OpenSSL::Cipher::Cipher.new CRYPT_ALGO
|
83
|
+
encrypter.encrypt
|
84
|
+
encrypter.pkcs5_keyivgen key, @salt
|
85
|
+
|
86
|
+
encrypted = encrypter.update data
|
87
|
+
encrypted << encrypter.final
|
88
|
+
|
89
|
+
"#{@salt}#{encrypted}"
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
class Decryptor
|
95
|
+
|
96
|
+
def initialize(serializer)
|
97
|
+
@serializer = serializer
|
98
|
+
end
|
99
|
+
|
100
|
+
def decrypt_and_verify(message, key_pair)
|
101
|
+
begin
|
102
|
+
message = Base64.urlsafe_decode64(message)
|
103
|
+
rescue ArgumentError
|
104
|
+
return nil
|
105
|
+
end
|
106
|
+
|
107
|
+
verified = verify(message, key_pair.signing_key)
|
108
|
+
return nil unless verified
|
109
|
+
|
110
|
+
|
111
|
+
begin
|
112
|
+
decrypted = decrypt(verified, key_pair.encryption_key).force_encoding('utf-8')
|
113
|
+
rescue OpenSSL::Cipher::CipherError
|
114
|
+
return nil
|
115
|
+
end
|
116
|
+
|
117
|
+
@serializer.deserialize(decrypted)
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
|
122
|
+
def verify(message, key)
|
123
|
+
signature, payload = message[0, SIGNATURE_LENGTH], message[SIGNATURE_LENGTH..-1]
|
124
|
+
valid_signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new(HASH_ALGO), key, payload)
|
125
|
+
signature == valid_signature ? payload : nil
|
126
|
+
end
|
127
|
+
|
128
|
+
def decrypt(data, key)
|
129
|
+
salt, data = data[0, 8], data[8..-1]
|
130
|
+
|
131
|
+
decrypter = OpenSSL::Cipher::Cipher.new CRYPT_ALGO
|
132
|
+
decrypter.decrypt
|
133
|
+
decrypter.pkcs5_keyivgen key, salt
|
134
|
+
|
135
|
+
decrypted = decrypter.update data
|
136
|
+
decrypted << decrypter.final
|
137
|
+
|
138
|
+
decrypted
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'secure_token/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "secure_token"
|
8
|
+
spec.version = SecureToken::VERSION
|
9
|
+
spec.authors = ["Ivan Kasatenko"]
|
10
|
+
spec.email = ["sky.31338@gmail.com"]
|
11
|
+
spec.summary = %q{JWT-like solution, that enables you to store encrypted and signed Hash'es on the client side, decrypt and verify them upon retreival}
|
12
|
+
spec.homepage = "https://github.com/SkyWriter/secure_token"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_runtime_dependency 'activesupport'
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: secure_token
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Kasatenko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- sky.31338@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/secure_token.rb
|
68
|
+
- lib/secure_token/version.rb
|
69
|
+
- secure_token.gemspec
|
70
|
+
homepage: https://github.com/SkyWriter/secure_token
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.2.1
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: JWT-like solution, that enables you to store encrypted and signed Hash'es
|
94
|
+
on the client side, decrypt and verify them upon retreival
|
95
|
+
test_files: []
|