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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d2f84ca42c260e1f8b86567cab7ccf79e5c25358
4
- data.tar.gz: 79637ad570d302e411fe5ea58f6a29df910211cb
3
+ metadata.gz: 13f07bcd332497e28c698ec45ca57eaa618adc9b
4
+ data.tar.gz: 03c719dd6486dda32b4c2250c4c356b4be52c121
5
5
  SHA512:
6
- metadata.gz: d9489f57ec2759f93140a82e13e1da6e122d43e61be4d20aaa9ab239f42b7040833c479168eefab73a1152c1e7f3e3367eabba6f4ed1e63cb1a8d9b3b93dd54a
7
- data.tar.gz: daac7867ea8900bd68d7f685ba4dcb9a55b09df1af5579ffabd5601a677bc947ca406ead6ac0976276faa751ce855aa919ef3dd12e01e58dd9ab656ee1414ba8
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::Utils.encode(
63
- resource_owner_id: user.di,
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
 
@@ -7,3 +7,9 @@ require "garage/jwt/config"
7
7
  require "garage/jwt/error"
8
8
  require "garage/jwt/utils"
9
9
  require "garage/strategy/jwt"
10
+
11
+ module Garage
12
+ module Jwt
13
+ extend Utils
14
+ end
15
+ end
@@ -17,23 +17,32 @@ module Garage
17
17
  block.call(@config)
18
18
  end
19
19
 
20
- def valid?
21
- @config.algorithm.present? &&
22
- @config.algorithm.is_a?(Garage::Jwt::Algorithm) &&
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
- def build
31
- if valid?
32
- @config
33
- else
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
@@ -1,60 +1,58 @@
1
1
  module Garage
2
2
  module Jwt
3
3
  module Utils
4
- class << self
5
- def encode(resource_owner_id:, application_id:, expired_at:, scope:)
6
- payload = {
7
- sub: resource_owner_id,
8
- aud: application_id,
9
- exp: expired_at.to_i,
10
- scope: (scope.is_a?(Array) ? scope.join(" ") : scope)
11
- }
12
- JWT.encode(payload, private_key, algorithm.type)
13
- end
14
-
15
- def decode(token, token_type)
16
- payload, _ = JWT.decode(token, public_key, verify?, decoding_options)
17
- { token: token,
18
- token_type: token_type,
19
- scope: payload["scope"],
20
- application_id: payload["aud"],
21
- resource_owner_id: payload["sub"],
22
- expired_at: payload["exp"],
23
- revoked_at: nil
24
- }
25
- rescue JWT::DecodeError => e
26
- nil
27
- end
28
-
29
- private
30
-
31
- def configuration
32
- Garage::Jwt.configuration
33
- end
34
-
35
- def algorithm
36
- configuration.algorithm
37
- end
38
-
39
- def public_key
40
- algorithm.need_public_key? ? configuration.public_key : common_key
41
- end
42
-
43
- def private_key
44
- algorithm.need_public_key? ? configuration.private_key : common_key
45
- end
46
-
47
- def common_key
48
- algorithm.need_common_key? ? configuration.common_key : nil
49
- end
50
-
51
- def decoding_options
52
- { algorithm: algorithm.type, verify_expiration: false }
53
- end
54
-
55
- def verify?
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
@@ -1,5 +1,5 @@
1
1
  module Garage
2
2
  module Jwt
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -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 = Garage::Jwt::Utils.decode(token, token_type)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garage-jwt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - izumin5210