has_jwt_token 0.1.0 → 0.2.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 +1 -1
- data/README.md +13 -1
- data/lib/has_jwt_token/has_jwt_model_configuration.rb +5 -3
- data/lib/has_jwt_token/jwt_configuration.rb +44 -19
- data/lib/has_jwt_token/jwt_proxy.rb +4 -3
- data/lib/has_jwt_token/jwt_tokenable.rb +7 -20
- data/lib/has_jwt_token/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bd6891ae551f486aecfa6de00f54b1318bd0383c7a19db1d590a371181f1db2
|
4
|
+
data.tar.gz: f1855497a20c4f4713bf58ae2d79480973342ef86fe71d641102888cfbac41db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0ec13c04d6175004eca4dfb51e9f19f620fa022c09c1322b883fcf6e47b49473972a0a82bf27ed8c7c583fdd7d67fb8afe3c57020ffed555edf8b7f1e0464b5
|
7
|
+
data.tar.gz: ad1c861ab14616785139c30025097e4665d4d2f172914b30b617d67bc7ef758e5fc94f7b62f906df8dfa76c245256858fd4ec0cd00c19529793f6b5cffd33395
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -31,7 +31,10 @@ class User
|
|
31
31
|
|
32
32
|
has_jwt_token do |jwt|
|
33
33
|
jwt.algorithm 'HS256'
|
34
|
-
jwt.
|
34
|
+
jwt.payload :name # model attribute #name
|
35
|
+
jwt.payload :custom_proc_class_method, -> { dummy_class_method }
|
36
|
+
jwt.payload :custom_proc_istance_method, ->(model) { model.dummy_instance_method }
|
37
|
+
jwt.payload :custom_plain_value, 321
|
35
38
|
jwt.secret 'secret'
|
36
39
|
|
37
40
|
jwt.expiration_time -> { Time.now.to_i + 60 }
|
@@ -43,8 +46,17 @@ class User
|
|
43
46
|
jwt.subject :dummy_app
|
44
47
|
end
|
45
48
|
end
|
49
|
+
|
50
|
+
user = User.last
|
51
|
+
|
52
|
+
user.authenicate(password) # => user with @token
|
53
|
+
user.auhtenticate_with_jwt(token) # => user with @token
|
46
54
|
```
|
47
55
|
|
56
|
+
## Roadmap
|
57
|
+
* Add blacklisted tokens managment
|
58
|
+
* Remove Rails depedency
|
59
|
+
|
48
60
|
## Development
|
49
61
|
|
50
62
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -4,10 +4,12 @@ require 'has_jwt_token/jwt_configuration'
|
|
4
4
|
|
5
5
|
module HasJwtToken
|
6
6
|
module HasJwtModelConfiguration
|
7
|
-
def has_jwt_token
|
8
|
-
@has_jwt_token ||= JwtConfiguration.new
|
7
|
+
def has_jwt_token(model = nil)
|
8
|
+
@has_jwt_token ||= JwtConfiguration.new
|
9
9
|
yield(@has_jwt_token) if block_given?
|
10
|
-
@has_jwt_token
|
10
|
+
@has_jwt_token.tap do |config|
|
11
|
+
config.model = model if model
|
12
|
+
end
|
11
13
|
end
|
12
14
|
end
|
13
15
|
end
|
@@ -12,13 +12,12 @@ module HasJwtToken
|
|
12
12
|
subject: :sub
|
13
13
|
}.freeze
|
14
14
|
|
15
|
-
attr_accessor :
|
16
|
-
attr_reader :
|
17
|
-
attr_writer :payload_attribute
|
15
|
+
attr_accessor :model
|
16
|
+
attr_reader :defined_claims
|
18
17
|
|
19
|
-
def initialize
|
20
|
-
@
|
21
|
-
@
|
18
|
+
def initialize
|
19
|
+
@payload = {}
|
20
|
+
@header = {}
|
22
21
|
@defined_claims = []
|
23
22
|
end
|
24
23
|
|
@@ -28,29 +27,55 @@ module HasJwtToken
|
|
28
27
|
@algorithm = value
|
29
28
|
end
|
30
29
|
|
31
|
-
def
|
32
|
-
return @
|
30
|
+
def secret(value = nil)
|
31
|
+
return @secret unless value
|
33
32
|
|
34
|
-
|
33
|
+
@secret = value
|
35
34
|
end
|
36
35
|
|
37
|
-
CLAIMS.
|
36
|
+
CLAIMS.each_key do |claim_name|
|
38
37
|
define_method(claim_name) do |value = nil|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
return value
|
38
|
+
unless value
|
39
|
+
claim_value = instance_variable_get("@#{claim_name}".to_sym)
|
40
|
+
return claim_value.is_a?(Proc) ? claim_value.call : claim_value
|
43
41
|
end
|
44
42
|
|
45
|
-
|
46
|
-
|
43
|
+
@defined_claims |= [claim_name]
|
44
|
+
instance_variable_set("@#{claim_name}".to_sym, value)
|
47
45
|
end
|
48
46
|
end
|
49
47
|
|
50
|
-
def
|
51
|
-
|
48
|
+
def model_payload
|
49
|
+
@payload.transform_values do |val|
|
50
|
+
next val if !val.is_a?(Proc) || !model
|
52
51
|
|
53
|
-
|
52
|
+
begin
|
53
|
+
val.call(model)
|
54
|
+
rescue ArgumentError
|
55
|
+
val.call
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def payload(name = nil, value = nil)
|
61
|
+
@payload[name] = value || ->(model) { model.respond_to?(name) && model.public_send(name) } if name
|
62
|
+
end
|
63
|
+
|
64
|
+
def claims_payload
|
65
|
+
defined_claims.each_with_object({}) do |claim_name, memo|
|
66
|
+
claim_key = CLAIMS[claim_name]
|
67
|
+
memo[claim_key] = public_send(claim_name)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def header(name = nil, value = nil)
|
72
|
+
@header[name] = value if name
|
73
|
+
end
|
74
|
+
|
75
|
+
def header_fields
|
76
|
+
@header.transform_values do |val|
|
77
|
+
val.is_a?(Proc) ? val.call : val
|
78
|
+
end
|
54
79
|
end
|
55
80
|
end
|
56
81
|
end
|
@@ -4,16 +4,17 @@ require 'jwt'
|
|
4
4
|
|
5
5
|
module HasJwtToken
|
6
6
|
class JwtProxy
|
7
|
-
attr_reader :algorithm, :payload, :secret
|
7
|
+
attr_reader :algorithm, :payload, :secret, :header_fields
|
8
8
|
|
9
|
-
def initialize(algorithm:
|
9
|
+
def initialize(algorithm: '', payload: {}, secret: '', header_fields: {})
|
10
10
|
@algorithm = algorithm
|
11
11
|
@payload = payload
|
12
12
|
@secret = secret
|
13
|
+
@header_fields = header_fields
|
13
14
|
end
|
14
15
|
|
15
16
|
def encode
|
16
|
-
JWT.encode(payload, secret, algorithm)
|
17
|
+
JWT.encode(payload, secret, algorithm, header_fields)
|
17
18
|
end
|
18
19
|
|
19
20
|
def decode(token)
|
@@ -4,8 +4,7 @@ require 'has_jwt_token/jwt_proxy'
|
|
4
4
|
|
5
5
|
module HasJwtToken
|
6
6
|
module JwtTokenable
|
7
|
-
delegate :algorithm, :secret, :
|
8
|
-
:defined_claims, to: :has_jwt_token
|
7
|
+
delegate :algorithm, :secret, :claims_payload, :header_fields, to: :has_jwt_token
|
9
8
|
|
10
9
|
def encode
|
11
10
|
with_jwt_configuration(&:encode)
|
@@ -19,27 +18,14 @@ module HasJwtToken
|
|
19
18
|
with_jwt_configuration { |jwt| jwt.decode!(token) }
|
20
19
|
end
|
21
20
|
|
22
|
-
private
|
23
|
-
|
24
21
|
def has_jwt_token
|
25
|
-
self.class.has_jwt_token
|
26
|
-
end
|
27
|
-
|
28
|
-
def payload
|
29
|
-
@payload ||= model_payload.merge(claims_payload)
|
22
|
+
self.class.has_jwt_token(self)
|
30
23
|
end
|
31
24
|
|
32
|
-
|
33
|
-
payload_attribute.each_with_object({}) do |attribute, memo|
|
34
|
-
memo[attribute] = public_send(attribute)
|
35
|
-
end
|
36
|
-
end
|
25
|
+
private
|
37
26
|
|
38
|
-
def
|
39
|
-
|
40
|
-
claim_key = HasJwtToken::JwtConfiguration::CLAIMS[claim_name]
|
41
|
-
memo[claim_key] = has_jwt_token.public_send(claim_name)
|
42
|
-
end
|
27
|
+
def payload
|
28
|
+
@payload ||= has_jwt_token.model_payload.merge(claims_payload)
|
43
29
|
end
|
44
30
|
|
45
31
|
def with_jwt_configuration
|
@@ -50,7 +36,8 @@ module HasJwtToken
|
|
50
36
|
@jwt_proxy ||= JwtProxy.new(
|
51
37
|
algorithm: algorithm,
|
52
38
|
payload: payload,
|
53
|
-
secret: secret
|
39
|
+
secret: secret,
|
40
|
+
header_fields: header_fields
|
54
41
|
)
|
55
42
|
end
|
56
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_jwt_token
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jokūbas Pučinskas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bcrypt
|