wor-authentication 0.2.0 → 0.2.1

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: 787ae1d673913fc47b4ab942d4fca187c13a0e33
4
- data.tar.gz: c4d4697f041f984c6fc37e3f36f7cbca3c10e1c2
3
+ metadata.gz: 543e92dae4b149469a024d4b4a9576816728687f
4
+ data.tar.gz: 49ec509fff4c4ce7556dca6a1cc86321be9f1e18
5
5
  SHA512:
6
- metadata.gz: 6f52f09a7d4068a8bab032f5d6705d785be43afc08aafa5e5eb7ef90c35e779c487448e69940b54ec7bb34ac543987eecbeb7af7639eef85597443af73617dac
7
- data.tar.gz: 8b87dddd1e8cb02d02e86e5eb32194f0c9123a72b9bfeac00cc2e250579a7326e592825559f4462e3c8372d5590209d136d32be9af815318731f87d643c064af
6
+ metadata.gz: fcdc1a635bca880a7020523522887603ac27e05897fbd06f04338725bda76dde51c0b8b1b2b0ac1d1f695566bd488b0a19161d2e89e56540688165dee669f494
7
+ data.tar.gz: e28dfd0059431608c9c1c5cc78e68555ffee312175d49b2f9cf225eb9ed11e3568699d2dc5c78d59e97adb48b9d8657adf17d391c54d2ef11c4b81cc4087eb3a
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
 
2
2
  ## Change log
3
3
 
4
+ ### [0.2.1] - 2017-07-27
5
+
6
+ - Throws Wor::Authentication::MissingAuthorizationHeader when no Authorization header is sent
7
+ - Throws Wor::Authentication::InvalidAuthorizationToken when the Authorization token is invalid
8
+
4
9
  ### [0.2.0]
5
10
 
6
11
  #### Added
data/README.md CHANGED
@@ -34,7 +34,17 @@ class ApplicationController < ActionController::Base
34
34
  before_action :authenticate_request
35
35
  end
36
36
  ```
37
- > To know which exceptions can be thrown by the gem, please check the [exceptions file](./lib/wor/authentication/exceptions.rb).
37
+
38
+ When a validation fails, an exception will be raised. Feel free to use `Wor::Authentication` helpers to render those errors like the following:
39
+ ```ruby
40
+ rescue_from Wor::Authentication::Exceptions::NotRenewableTokenError, with: :render_not_renewable_token
41
+ rescue_from Wor::Authentication::Exceptions::ExpiredTokenError, with: :render_expired_token
42
+ rescue_from Wor::Authentication::Exceptions::EntityCustomValidationError, with: :render_entity_invalid_custom_validation
43
+ rescue_from Wor::Authentication::Exceptions::MissingAuthorizationHeader, with: :render_missing_authorization_token
44
+ rescue_from Wor::Authentication::Exceptions::InvalidAuthorizationToken, with: :render_invalid_authorization_token
45
+ ```
46
+
47
+ > To know all the exceptions that can be thrown by the gem, please check the [exceptions file](./lib/wor/authentication/exceptions.rb).
38
48
 
39
49
  Second and last step, we have to define the routes to achieve authentication and a controller to handle them.
40
50
  ```ruby
@@ -15,13 +15,11 @@ module Wor
15
15
  end
16
16
 
17
17
  def new_token_expiration_date
18
- expiration_days = Wor::Authentication.expiration_days
19
- (Time.zone.now + expiration_days.days).to_i
18
+ Wor::Authentication.expiration_days.days.from_now.to_i
20
19
  end
21
20
 
22
21
  def token_maximum_useful_date
23
- maximum_useful_days = Wor::Authentication.maximum_useful_days
24
- (Time.zone.now + maximum_useful_days.days).to_i
22
+ Wor::Authentication.maximum_useful_days.days.from_now.to_i
25
23
  end
26
24
 
27
25
  ##
@@ -33,6 +31,9 @@ module Wor
33
31
  end
34
32
 
35
33
  def authentication_token
34
+ if request.headers['Authorization'].blank?
35
+ raise Wor::Authentication::Exceptions::MissingAuthorizationHeader
36
+ end
36
37
  request.headers['Authorization'].split(' ').last
37
38
  end
38
39
 
@@ -16,33 +16,28 @@ module Wor
16
16
  end
17
17
 
18
18
  def fetch(key)
19
- return payload[key.to_sym] if payload[key.to_sym]
20
- return payload[key.to_s] if payload[key.to_s]
21
- nil
19
+ payload[key.to_sym] || payload[key.to_s]
22
20
  end
23
21
 
24
22
  def expired?
25
- return false if fetch(:expiration_date).blank?
26
23
  # TODO: Use a ruby standard library for time
27
- Time.zone.now.to_i > fetch(:expiration_date)
24
+ fetch(:expiration_date).present? && Time.zone.now.to_i > fetch(:expiration_date)
28
25
  end
29
26
 
30
27
  def able_to_renew?
31
- return false if fetch(:maximum_useful_date).blank?
32
28
  # TODO: Use a ruby standard library for time
33
- Time.zone.now.to_i < fetch(:maximum_useful_date)
29
+ fetch(:maximum_useful_date).present? && Time.zone.now.to_i < fetch(:maximum_useful_date)
34
30
  end
35
31
 
36
32
  def valid_renew_id?(renew_id)
37
- return true unless fetch(:renew_id).present? && renew_id.present?
38
- renew_id == fetch(:renew_id)
33
+ (fetch(:renew_id).blank? || renew_id.blank?) || renew_id == fetch(:renew_id)
39
34
  end
40
35
 
41
36
  private
42
37
 
43
38
  def valid_entity_custom_validation?(entity_custom_validation)
44
- return true if fetch(:entity_custom_validation).blank?
45
- entity_custom_validation == fetch(:entity_custom_validation)
39
+ fetch(:entity_custom_validation).blank? ||
40
+ entity_custom_validation == fetch(:entity_custom_validation)
46
41
  end
47
42
  end
48
43
  end
@@ -8,6 +8,8 @@ module Wor
8
8
  class ExpiredTokenError < StandardError; end
9
9
  class NotRenewableTokenError < StandardError; end
10
10
  class EntityCustomValidationError < StandardError; end
11
+ class MissingAuthorizationHeader < StandardError; end
12
+ class InvalidAuthorizationToken < StandardError; end
11
13
  end
12
14
  end
13
15
  end
@@ -71,6 +71,14 @@ module Wor
71
71
  params.require(:session).permit(:renew_id)
72
72
  end
73
73
 
74
+ def render_missing_authorization_token
75
+ render_error('You must pass an Authorization Header with the access token', :unauthorized)
76
+ end
77
+
78
+ def render_invalid_authorization_token
79
+ render_error('Invalid authorization token', :unauthorized)
80
+ end
81
+
74
82
  def render_not_renewable_token
75
83
  render_error('Access token is not valid anymore', :unauthorized)
76
84
  end
@@ -15,7 +15,7 @@ module Wor
15
15
  payload = JWT.decode(token, @key)[0]
16
16
  Wor::Authentication::DecodedToken.new(payload)
17
17
  rescue
18
- nil
18
+ raise Wor::Authentication::Exceptions::InvalidAuthorizationToken
19
19
  end
20
20
  end
21
21
  end
@@ -1,5 +1,5 @@
1
1
  module Wor
2
2
  module Authentication
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.2.1'.freeze
4
4
  end
5
5
  end
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.test_files = spec.files.grep(%r{^(test|spec)/})
22
22
  spec.require_paths = ['lib']
23
23
 
24
- spec.add_dependency 'railties', '>= 4.1.0', '< 5.1'
24
+ spec.add_dependency 'railties', '>= 4.1.0', '< 5.2'
25
25
  spec.add_dependency 'devise', '>= 4.2.0'
26
26
  spec.add_dependency 'jwt', '>= 1.5'
27
27
  spec.add_dependency 'rails', '>= 4.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.0
4
+ version: 0.2.1
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-05-05 00:00:00.000000000 Z
12
+ date: 2017-08-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -20,7 +20,7 @@ dependencies:
20
20
  version: 4.1.0
21
21
  - - "<"
22
22
  - !ruby/object:Gem::Version
23
- version: '5.1'
23
+ version: '5.2'
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
@@ -30,7 +30,7 @@ dependencies:
30
30
  version: 4.1.0
31
31
  - - "<"
32
32
  - !ruby/object:Gem::Version
33
- version: '5.1'
33
+ version: '5.2'
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: devise
36
36
  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.4.5
277
+ rubygems_version: 2.6.7
278
278
  signing_key:
279
279
  specification_version: 4
280
280
  summary: Easily add authentication to your application!.