wor-authentication 0.2.1 → 0.2.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 543e92dae4b149469a024d4b4a9576816728687f
4
- data.tar.gz: 49ec509fff4c4ce7556dca6a1cc86321be9f1e18
3
+ metadata.gz: da2c5de4a000be9110116463cde26134cacbb045
4
+ data.tar.gz: 10213d92f0bb546e0ffe9b6817225b11f38504bd
5
5
  SHA512:
6
- metadata.gz: fcdc1a635bca880a7020523522887603ac27e05897fbd06f04338725bda76dde51c0b8b1b2b0ac1d1f695566bd488b0a19161d2e89e56540688165dee669f494
7
- data.tar.gz: e28dfd0059431608c9c1c5cc78e68555ffee312175d49b2f9cf225eb9ed11e3568699d2dc5c78d59e97adb48b9d8657adf17d391c54d2ef11c4b81cc4087eb3a
6
+ metadata.gz: feabb5866d16333fae1266099e3fc1eb1adee1faf0e3801af563006a5d39d3f4539da6d500f01cc929fb25e6683465b5d73dbf30fbf7aed3308be562206d7307
7
+ data.tar.gz: 258fbf3a9e1a43e1cf75369eaf371ca1dd41046a922bcf0237718504d82385f1dc42cc7db5075ff50d61d4df7dcd8c6e8e899c9953055a33b535eee1930de317
data/README.md CHANGED
@@ -63,13 +63,14 @@ class AuthenticationController < ApplicationController
63
63
  end
64
64
  ```
65
65
  > Note that our controller extends from ApplicationController.
66
+ > It could also extend from your custom ApiController, for example.
66
67
 
67
- ### <a name='custom-validations'> Entity tracking and custom validations
68
+ ### Entity tracking
68
69
 
69
- #### Validations before giving out a token? Override `authenticate_entity`:
70
+ #### Override `authenticate_entity`. Add validations for your entity:
70
71
 
71
72
  ```ruby
72
- # application_controller.rb
73
+ # authentication_controller.rb
73
74
  def authenticate_entity(params)
74
75
  entity = Entity.find_by(some_unique_id: params[:some_unique_id])
75
76
  return nil unless entity.present? && entity.valid_password?(params[:password])
@@ -78,7 +79,7 @@ end
78
79
  ```
79
80
  > Returning no value or false won't create the authentication token.
80
81
 
81
- #### Keeping track of entities? Override: `entity_payload`:
82
+ #### Override `entity_payload`, `find_authenticable_entity` to have access to `current_entity`:
82
83
 
83
84
  ```ruby
84
85
  # application_controller.rb
@@ -93,6 +94,11 @@ def find_authenticable_entity(entity_payload_returned_object)
93
94
  end
94
95
  ```
95
96
 
97
+ Overriding these methods will give you access to `current_entity` from any controller that extends `application_controller`.
98
+ It will return the entity you used to authenticate.
99
+
100
+ ### Custom Validations
101
+
96
102
  #### Validations in every request? Override `entity_custom_validation_value` to get it verified as the following:
97
103
 
98
104
  ```ruby
@@ -22,8 +22,12 @@ module Wor
22
22
  Wor::Authentication.maximum_useful_days.days.from_now.to_i
23
23
  end
24
24
 
25
+ def current_entity
26
+ @current_entity ||= find_authenticable_entity(decoded_token)
27
+ end
28
+
25
29
  ##
26
- # Helpers which may be overrided
30
+ # Helpers which may be overridden
27
31
  ##
28
32
 
29
33
  def token_renew_id
@@ -3,18 +3,20 @@ require 'jwt'
3
3
  module Wor
4
4
  module Authentication
5
5
  class TokenManager
6
+ ENCODING_ALGORITHM = 'HS256'.freeze
7
+
6
8
  def initialize(key)
7
9
  @key = key
8
10
  end
9
11
 
10
12
  def encode(payload)
11
- JWT.encode(payload, @key)
13
+ JWT.encode(payload, @key, ENCODING_ALGORITHM)
12
14
  end
13
15
 
14
16
  def decode(token)
15
- payload = JWT.decode(token, @key)[0]
17
+ payload = JWT.decode(token, @key, true, algorithm: ENCODING_ALGORITHM)[0]
16
18
  Wor::Authentication::DecodedToken.new(payload)
17
- rescue
19
+ rescue StandardError
18
20
  raise Wor::Authentication::Exceptions::InvalidAuthorizationToken
19
21
  end
20
22
  end
@@ -1,5 +1,5 @@
1
1
  module Wor
2
2
  module Authentication
3
- VERSION = '0.2.1'.freeze
3
+ VERSION = '0.2.2'.freeze
4
4
  end
5
5
  end
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_dependency 'railties', '>= 4.1.0', '< 5.2'
25
25
  spec.add_dependency 'devise', '>= 4.2.0'
26
- spec.add_dependency 'jwt', '>= 1.5'
26
+ spec.add_dependency 'jwt', '~> 2.0'
27
27
  spec.add_dependency 'rails', '>= 4.0'
28
28
 
29
29
  spec.add_development_dependency 'byebug', '~> 9.0'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wor-authentication
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - alebian
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-08-01 00:00:00.000000000 Z
12
+ date: 2017-09-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -49,16 +49,16 @@ dependencies:
49
49
  name: jwt
50
50
  requirement: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.5'
54
+ version: '2.0'
55
55
  type: :runtime
56
56
  prerelease: false
57
57
  version_requirements: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.5'
61
+ version: '2.0'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rails
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -274,7 +274,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
274
274
  version: '0'
275
275
  requirements: []
276
276
  rubyforge_project:
277
- rubygems_version: 2.6.7
277
+ rubygems_version: 2.6.13
278
278
  signing_key:
279
279
  specification_version: 4
280
280
  summary: Easily add authentication to your application!.