garage-jwt 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/README.md +9 -3
- data/lib/garage/jwt.rb +6 -0
- data/lib/garage/jwt/config.rb +22 -13
- data/lib/garage/jwt/utils.rb +52 -54
- data/lib/garage/jwt/version.rb +1 -1
- data/lib/garage/strategy/jwt.rb +3 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13f07bcd332497e28c698ec45ca57eaa618adc9b
|
4
|
+
data.tar.gz: 03c719dd6486dda32b4c2250c4c356b4be52c121
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f0772388dba8a69db7e8685bfd356fe16b71f259149f420f6c61c36603257dad03cc81933c1b3e96d91f8fd9d2f8b5c073d7c67026bf779adb45791e38efe64
|
7
|
+
data.tar.gz: df7feb68b2521bce23fa86b8b0fbe85b5ade73b6a8c9922b9a724d6b84278d09492007afba69d73e520002733c52ac63a463f190999a39be2b71b7d028134fd2
|
data/README.md
CHANGED
@@ -2,8 +2,11 @@
|
|
2
2
|
[![Build Status](https://travis-ci.org/izumin5210/garage-jwt.svg?branch=master)](https://travis-ci.org/izumin5210/garage-jwt)
|
3
3
|
[![Test Coverage](https://codeclimate.com/github/izumin5210/garage-jwt/badges/coverage.svg)](https://codeclimate.com/github/izumin5210/garage-jwt/coverage)
|
4
4
|
[![Code Climate](https://codeclimate.com/github/izumin5210/garage-jwt/badges/gpa.svg)](https://codeclimate.com/github/izumin5210/garage-jwt)
|
5
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/izumin5210/garage-jwt.svg)](https://gemnasium.com/github.com/izumin5210/garage-jwt)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/garage-jwt.svg)](https://badge.fury.io/rb/garage-jwt)
|
7
|
+
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://izumin.mit-license.org/2016)
|
5
8
|
|
6
|
-
Garage extension to use JWT as authentication strategy.
|
9
|
+
[Garage](https://github.com/cookpad/garage) extension to use JWT as authentication strategy.
|
7
10
|
|
8
11
|
|
9
12
|
## Installation
|
@@ -57,16 +60,19 @@ The following cryptographic signing algorithms are available:
|
|
57
60
|
|
58
61
|
|
59
62
|
### Generate token
|
63
|
+
You can encode token with the following method:
|
60
64
|
|
61
65
|
```ruby
|
62
|
-
Garage::Jwt
|
63
|
-
resource_owner_id: user.
|
66
|
+
Garage::Jwt.encode_token(
|
67
|
+
resource_owner_id: user.id,
|
64
68
|
application_id: 128,
|
65
69
|
scope: "read write",
|
66
70
|
expired_at: Time.zone.now + 15.minutes
|
67
71
|
)
|
68
72
|
```
|
69
73
|
|
74
|
+
Or if you use `Garage::ControllerHelper`, you can use `encode_token` method.
|
75
|
+
|
70
76
|
|
71
77
|
## Development
|
72
78
|
|
data/lib/garage/jwt.rb
CHANGED
data/lib/garage/jwt/config.rb
CHANGED
@@ -17,23 +17,32 @@ module Garage
|
|
17
17
|
block.call(@config)
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
-
(
|
24
|
-
(!@config.algorithm.need_common_key? || @config.common_key.present?) &&
|
25
|
-
(!@config.algorithm.need_public_key? || @config.public_key.present?) &&
|
26
|
-
(!@config.algorithm.need_private_key? || @config.private_key.present?)
|
27
|
-
)
|
20
|
+
def build
|
21
|
+
validate!
|
22
|
+
@config
|
28
23
|
end
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
fail Garage::Jwt::InitializeError
|
25
|
+
private
|
26
|
+
|
27
|
+
def validate!
|
28
|
+
unless valid_algorithm?
|
29
|
+
fail Garage::Jwt::InitializeError.new("Invalid algorithm")
|
30
|
+
end
|
31
|
+
unless valid_keys?
|
32
|
+
fail Garage::Jwt::InitializeError.new("Invalid keys")
|
35
33
|
end
|
36
34
|
end
|
35
|
+
|
36
|
+
def valid_algorithm?
|
37
|
+
@config.algorithm.present? &&
|
38
|
+
@config.algorithm.is_a?(Garage::Jwt::Algorithm)
|
39
|
+
end
|
40
|
+
|
41
|
+
def valid_keys?
|
42
|
+
(!@config.algorithm.need_common_key? || @config.common_key.present?) &&
|
43
|
+
(!@config.algorithm.need_public_key? || @config.public_key.present?) &&
|
44
|
+
(!@config.algorithm.need_private_key? || @config.private_key.present?)
|
45
|
+
end
|
37
46
|
end
|
38
47
|
end
|
39
48
|
end
|
data/lib/garage/jwt/utils.rb
CHANGED
@@ -1,60 +1,58 @@
|
|
1
1
|
module Garage
|
2
2
|
module Jwt
|
3
3
|
module Utils
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
!algorithm.none?
|
57
|
-
end
|
4
|
+
def encode_token(resource_owner_id:, application_id:, expired_at:, scope:)
|
5
|
+
payload = {
|
6
|
+
sub: resource_owner_id,
|
7
|
+
aud: application_id,
|
8
|
+
exp: expired_at.to_i,
|
9
|
+
scope: (scope.is_a?(Array) ? scope.join(" ") : scope)
|
10
|
+
}
|
11
|
+
JWT.encode(payload, private_key, algorithm.type)
|
12
|
+
end
|
13
|
+
|
14
|
+
def decode_token(token, token_type)
|
15
|
+
payload, _ = JWT.decode(token, public_key, verify?, decoding_options)
|
16
|
+
{ token: token,
|
17
|
+
token_type: token_type,
|
18
|
+
scope: payload["scope"],
|
19
|
+
application_id: payload["aud"],
|
20
|
+
resource_owner_id: payload["sub"],
|
21
|
+
expired_at: payload["exp"],
|
22
|
+
revoked_at: nil
|
23
|
+
}
|
24
|
+
rescue JWT::DecodeError => e
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def configuration
|
31
|
+
Garage::Jwt.configuration
|
32
|
+
end
|
33
|
+
|
34
|
+
def algorithm
|
35
|
+
configuration.algorithm
|
36
|
+
end
|
37
|
+
|
38
|
+
def public_key
|
39
|
+
algorithm.need_public_key? ? configuration.public_key : common_key
|
40
|
+
end
|
41
|
+
|
42
|
+
def private_key
|
43
|
+
algorithm.need_public_key? ? configuration.private_key : common_key
|
44
|
+
end
|
45
|
+
|
46
|
+
def common_key
|
47
|
+
algorithm.need_common_key? ? configuration.common_key : nil
|
48
|
+
end
|
49
|
+
|
50
|
+
def decoding_options
|
51
|
+
{ algorithm: algorithm.type, verify_expiration: false }
|
52
|
+
end
|
53
|
+
|
54
|
+
def verify?
|
55
|
+
!algorithm.none?
|
58
56
|
end
|
59
57
|
end
|
60
58
|
end
|
data/lib/garage/jwt/version.rb
CHANGED
data/lib/garage/strategy/jwt.rb
CHANGED
@@ -2,6 +2,8 @@ module Garage
|
|
2
2
|
module Strategy
|
3
3
|
module Jwt
|
4
4
|
extend ActiveSupport::Concern
|
5
|
+
include Garage::Jwt::Utils
|
6
|
+
|
5
7
|
included do
|
6
8
|
before_action :verify_auth, if: -> (_) { verify_permission? }
|
7
9
|
end
|
@@ -15,7 +17,7 @@ module Garage
|
|
15
17
|
@access_token
|
16
18
|
else
|
17
19
|
token_type, token = request.authorization.try { |h| h.split(/\s+/) }
|
18
|
-
decoded_token =
|
20
|
+
decoded_token = decode_token(token, token_type)
|
19
21
|
if decoded_token.present?
|
20
22
|
@access_token = Garage::Strategy::AccessToken.new(decoded_token)
|
21
23
|
else
|