decode_this 0.1.0
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 +9 -0
- data/.rspec +2 -0
- data/Gemfile +12 -0
- data/Rakefile +4 -0
- data/Readme.md +38 -0
- data/decode_this.gemspec +18 -0
- data/lib/decode_this.rb +57 -0
- data/lib/decode_this/version.rb +4 -0
- data/spec/decode_this_spec.rb +16 -0
- data/spec/fixtures/config.yml +3 -0
- data/spec/fixtures/unsecured.pem +27 -0
- data/spec/spec_helper.rb +9 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6aa17a4625e0ee56d82538d48b8bf254365ce750
|
4
|
+
data.tar.gz: d4b0835f60837ad36e7903b353e1995e7f7f1e6c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5879f3e3e4c7aa92b74e5809ca34eff17bc9b56132dcef11bebb1419f61c3108ad2336049f4d7b48d2da7e0d86e8fed1f00dafbe0a130cb86d63699db30920ac
|
7
|
+
data.tar.gz: 033b2afcd0f6292a1eed1d9a9c7a2214cbb0e3598af8b30c0b75f29b334f0277627a956b3370e57def6f88d0aa28b1cec85190e8f84cce7c8ccb4db98e80d7be
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# DecodeThis
|
2
|
+
|
3
|
+
Simple decoder JWT token by given key
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'decode_this'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install decode_this
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Configuration file scheme:
|
24
|
+
```
|
25
|
+
test:
|
26
|
+
algorightm: 'RS256'
|
27
|
+
path: 'paht/to/keys'
|
28
|
+
```
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
token = DecodeThis.call(token, config_file: '/path/to/config.yml', env: :my_env)
|
32
|
+
token['field1']
|
33
|
+
token['field2']
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/myxaluch/decode_this.
|
data/decode_this.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'decode_this/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'decode_this'
|
8
|
+
spec.version = DecodeThis::VERSION
|
9
|
+
spec.summary = 'Decode token. This token'
|
10
|
+
spec.description = 'Simple gem for decoding JWT token'
|
11
|
+
spec.authors = ['Sasha Kotov']
|
12
|
+
spec.email = 'amikotov@gmail.com'
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
end
|
data/lib/decode_this.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'decode_this/version'
|
3
|
+
require 'huyettings'
|
4
|
+
require 'openssl'
|
5
|
+
require 'jwt'
|
6
|
+
|
7
|
+
class DecodeThis
|
8
|
+
ConfigFileNotFoundError = Class.new(RuntimeError)
|
9
|
+
DecodeError = Class.new(RuntimeError)
|
10
|
+
|
11
|
+
attr_reader :token, :config_file, :env
|
12
|
+
|
13
|
+
def initialize(token, config_file:, env:)
|
14
|
+
@token = token
|
15
|
+
@config_file = config_file
|
16
|
+
@env = env
|
17
|
+
end
|
18
|
+
|
19
|
+
def call
|
20
|
+
JWT.decode(token, public_key, true, algorithm: algorithm).first
|
21
|
+
|
22
|
+
rescue JWT::ExpiredSignature => err
|
23
|
+
logger.warn("Expired JWT token #{err.class} - #{err.message}")
|
24
|
+
raise DecodeError
|
25
|
+
rescue JWT::VerificationError => err
|
26
|
+
logger.warn("Can't verify JWT token #{err.class} - #{err.message}")
|
27
|
+
raise DecodeError
|
28
|
+
rescue JWT::DecodeError => err
|
29
|
+
logger.warn("Can't decode JWT token '#{jwt_token}' #{err.class} - #{err.message}")
|
30
|
+
raise DecodeError
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def jwt_config
|
36
|
+
@jwt_config ||= Huyettings.new(config_file, env)
|
37
|
+
end
|
38
|
+
|
39
|
+
def algorithm
|
40
|
+
jwt_config.algorithm
|
41
|
+
end
|
42
|
+
|
43
|
+
def public_key
|
44
|
+
private_key.public_key
|
45
|
+
end
|
46
|
+
|
47
|
+
def private_key
|
48
|
+
OpenSSL::PKey::RSA.new(pem)
|
49
|
+
end
|
50
|
+
|
51
|
+
def pem
|
52
|
+
keys_absolute_path = File.expand_path(jwt_config.path)
|
53
|
+
|
54
|
+
raise KeyFileNotFoundError.new("Cannot found file in #{jwt_config.path}") unless File.readable?(keys_absolute_path)
|
55
|
+
File.read(jwt_config.path)
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
RSpec.describe DecodeThis do
|
5
|
+
let(:config_path) { File.expand_path('spec/fixtures/config.yml') }
|
6
|
+
let(:payload) { { field: 'foobar' } }
|
7
|
+
let(:token) { encode(payload) }
|
8
|
+
|
9
|
+
subject(:decoded_token) { described_class.new(token, config_file: config_path, env: :test).call }
|
10
|
+
|
11
|
+
it 'decodes given token correctly' do
|
12
|
+
payload.keys.each do |key|
|
13
|
+
expect(decoded_token[key.to_s]).to eq(payload[key])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
-----BEGIN RSA PRIVATE KEY-----
|
2
|
+
MIIEogIBAAKCAQEAoH9kIKKZKprgoS9p8h/sHKDlBcFOGOS1leaJ4L+oPlye/RtQ
|
3
|
+
P7gjQK50xGw7WgydS+utcY2rGE6EjWzG3FDzWpz1WGV5ecUxfypTxNbiQg4+WiZv
|
4
|
+
hoyW+to72245yVaTbxKAdht01naULQZ0KgniOoxFYl9yH8VVVU1aUrZkvePkAKPJ
|
5
|
+
rQfzLMYvEBEzHefKsyHPy2w2O9sTTQU0ZGfkcKWxTvnY9UWeDUNHkIdw2g39hLL5
|
6
|
+
NXgky1Bg/ofsjCasHpbnrbieIvymqruWpFfmglaVtCjxL7y0iiemLCpkRUVTnJeH
|
7
|
+
yIwZZL0WEsiaixAI3hFJfybx8lmil+UFm18wDwIDAQABAoIBAFKbHqrZZWITUthD
|
8
|
+
MhFnsrvNZ/L9Wjffx0I+5OojWvG19DKrkzMokCRjZkAOwMGJSpBf3eZbFxuslfbA
|
9
|
+
bN3KYGZb8L8tg0dUYjRkUbc3Lj73W8pEjBRBWhgEqNg4tE9XWwzJBgtD+G/FmmBO
|
10
|
+
OQ3GBoVB2xhVrcSCaXg9FucJb3J3/w4YS3tPbiSIG3lMOzcf87b10IdeiHjfRPUO
|
11
|
+
/PI89/DYX8CQfFa+AG7I30jBl/ihXdpUx+GUguu1dSKeUrNRavGD+N1pmRXKhx18
|
12
|
+
835ybmn4UNzt833M41TUQLFIGZGeaCPJ0Re0vGok9KDeX5760GxmK+gCtSXYYW4m
|
13
|
+
AXB0wfkCgYEA0GodtuZWSiZcqA42uSFEYhv61CE+7LJ2RLjhq1c+EbZZXa3IitNA
|
14
|
+
jpmn5kMyzjA+6gD1GD+AlBVfSadDg7TtAuY9zBsk+E+tfz+Nn9elGcaSIwm5vJis
|
15
|
+
Bc/CfiqzvwGrozDPbkL8P3WpNpPsz108klXiphI3+iCbp8jA5AM1//UCgYEAxSSF
|
16
|
+
EyLHDS70BQ62UvObB/B55tpiebJB458qrU/n3KA0BUyhCbZSoYE2aM+U8WUH2Wtr
|
17
|
+
l50RmdThkHfSkLQKJDUxBl9Oc22wdYluPUPDLcjO4IlWkw7Wz9nD+ZmHBGR4cQo8
|
18
|
+
EznhktwKW4n6I+O2L/qO8j+NK0lsgTrHc6+gQXMCgYBxxxzFtuWwwVSgInJZiwHM
|
19
|
+
Fb5PhH7XtW2/grcgVRZ7kPMkozTyJPX/Y383w8O+sLQIB9HGFRTawvgsO3YuNt2V
|
20
|
+
UaC3BrKeJNFwwH9OnelyMueg3TRa83YmDg/91M4gHodXJdR4O3V+J3vjYcpMQnhI
|
21
|
+
4z5Cr3uDv716UayMTfxy6QKBgCLQ3keQ3sJvRl0WShDAQOLYD087xiRj3MsMMwdE
|
22
|
+
k1gdWtKMw+vpLRKktS0xa8Zt50L4e5nc/VzDgcp3vtIbWacnVa8gacRs3LsFOFjN
|
23
|
+
fMy7J/3zgzyZZEp01O1htbyK2dJs0ks676F2BsY/ydNIxcYXY+NYYeABKY5xCQRf
|
24
|
+
QdgNAoGADTqtuc4dw2VegWSRJY3aAXUHwu1s4qLP5td4xuH6RKwMk6jLYzjyO4Xa
|
25
|
+
mgK4tG31CeDN7PuHDS4M5ZFGh3Iz5w9mLvV+oiWdL/Bk2bDV8ydy76Y3Gka885kZ
|
26
|
+
bw3t98Yp619IAje4RGi8VnW1EBfrtoTW3tpoqPReAbaSGFFofWE=
|
27
|
+
-----END RSA PRIVATE KEY-----
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'decode_this'
|
4
|
+
|
5
|
+
def encode(payload)
|
6
|
+
config = Huyettings.new(File.expand_path('spec/fixtures/config.yml'), :test)
|
7
|
+
private_key = OpenSSL::PKey::RSA.new(File.read(config.path))
|
8
|
+
JWT.encode(payload, private_key, config.algorithm)
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: decode_this
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sasha Kotov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Simple gem for decoding JWT token
|
14
|
+
email: amikotov@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- ".rspec"
|
21
|
+
- Gemfile
|
22
|
+
- Rakefile
|
23
|
+
- Readme.md
|
24
|
+
- decode_this.gemspec
|
25
|
+
- lib/decode_this.rb
|
26
|
+
- lib/decode_this/version.rb
|
27
|
+
- spec/decode_this_spec.rb
|
28
|
+
- spec/fixtures/config.yml
|
29
|
+
- spec/fixtures/unsecured.pem
|
30
|
+
- spec/spec_helper.rb
|
31
|
+
homepage:
|
32
|
+
licenses: []
|
33
|
+
metadata: {}
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 2.5.2.1
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: Decode token. This token
|
54
|
+
test_files:
|
55
|
+
- spec/decode_this_spec.rb
|
56
|
+
- spec/fixtures/config.yml
|
57
|
+
- spec/fixtures/unsecured.pem
|
58
|
+
- spec/spec_helper.rb
|