tinytokenauth-rails 0.1.6 → 0.1.8

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
  SHA256:
3
- metadata.gz: a51cd8dbe7faddbd28aa43eab713a841bb3ea8b629d4753c949e522e09627048
4
- data.tar.gz: 35168279b0cb122da90c5a56502adb804e15f3f05b5bfe96a2a238dd7179ca53
3
+ metadata.gz: 16db7cdd60c7295a08eb0ca07fbcc229289cff1a0f867d5147c98fd1c10929e7
4
+ data.tar.gz: 483600dc44ab04a814b04f21ef9cf0fe24b09908327d46c6a29b12a5f480bc78
5
5
  SHA512:
6
- metadata.gz: '08f62d165b73b1b0e0e9517643b143e708e40600dfdf7bdbbbb1fe4cb6daa02d53c808bac82c5feaef1d3d8a71831ffbbac55f9c08ef0ba80f9e6a6161ae29b2'
7
- data.tar.gz: 5cb15b86e8d527860b646155a49f7dc318346f6d96a3a91876c53e6fdf5b9d18b8275661e96aceef537cc01943b868e6837089f325351efd4446fa760cd8212a
6
+ metadata.gz: 482c5de56d33c9135b2ee74b1a7fa0bc1d5aa43386d9a6ef6dc30acfaed128ad3ef4dc6274e17f6dda82a8370f22bca64566da6d8f62efb3c91c94003695c8d8
7
+ data.tar.gz: a7e8813692d44a322b74e37b5562f5e6a9228d1a82b2ec724cef16004615b29856e385029a9d3c7ba05614b71fb0a51b9e1939825a7807aa6ac59b56b3875715
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tinytokenauth-rails (0.1.6)
4
+ tinytokenauth-rails (0.1.7)
5
5
  jwt (~> 2.7)
6
6
  rails (>= 6.0)
7
7
 
@@ -1,15 +1,17 @@
1
1
  module Tinytokenauth
2
2
 
3
- module Authorizable
4
- class << self
5
- def configuration
6
- @configuration ||= Configuration.new
7
- end
3
+ class << self
4
+ def configuration
5
+ @configuration ||= Configuration.new
6
+ end
8
7
 
9
- def configure
10
- yield(configuration)
11
- end
8
+ def configure
9
+ yield(configuration)
12
10
  end
11
+ end
12
+
13
+ module Authorizable
14
+
13
15
 
14
16
  def authorize_with_header
15
17
  token = ''
@@ -17,9 +19,8 @@ module Tinytokenauth
17
19
  token = header.split(' ').last if header
18
20
 
19
21
  begin
20
- @decoded = JsonWebToken.decode(token)
21
- # @current_user = User.find(@decoded[:user_id])
22
- @current_user = Authorizable.configuration.user_class.send 'find', @decoded[:user_id]
22
+ @decoded = JsonWebToken.decode(Tinytokenauth.configuration.token_secret, token)
23
+ @current_user = Tinytokenauth.configuration.user_class.send 'find', @decoded[:user_id]
23
24
  rescue ActiveRecord::RecordNotFound => e
24
25
  render json: { errors: e.message }, status: :unauthorized
25
26
  rescue JWT::DecodeError => e
@@ -27,48 +28,14 @@ module Tinytokenauth
27
28
  end
28
29
  end
29
30
 
30
- # def require_current_user(klass = User)
31
- # token = cookies['klap-auth']
32
- # # p "token from cookie: #{token}"
33
- #
34
- # begin
35
- # @decoded = JsonWebToken.decode(token)
36
- # # @current_user = User.find(@decoded[:user_id])
37
- # @current_user = klass.send 'find', @decoded[:user_id]
38
- # @exp = @decoded[:exp]
39
- # # if @exp < 24.hours.from_now.to_i # Always refresh token
40
- # if @exp < 4.hours.from_now.to_i # Always refresh token
41
- # sign_in @current_user
42
- # end
43
- # rescue ActiveRecord::RecordNotFound => e
44
- # # TODO: evaluate if we should always forward
45
- # redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
46
- # rescue JWT::DecodeError => e
47
- # # TODO: evaluate if we should always forward
48
- # # render json: { errors: e.message }, status: :unauthorized
49
- # redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
50
- # end
51
- # end
52
-
53
- # def require_current_user2(klass = User, &block)
54
- # current_user = set_current_user(klass)
55
- # if block_given? && current_user.nil?
56
- # block.call
57
- # else
58
- # raise MissingArgumentError
59
- # end
60
- # end
61
-
62
31
  def require_current_user(&block)
63
- token = cookies[Authorizable.configuration.cookie_name]
64
- # p "token from cookie: #{token}"
32
+ token = cookies[Tinytokenauth.configuration.cookie_name]
65
33
  begin
66
- @decoded = JsonWebToken.decode(token)
67
- # @current_user = User.find(@decoded[:user_id])
68
- @current_user = Authorizable.configuration.user_class.send 'find', @decoded[:user_id]
34
+ @decoded = JsonWebToken.decode(Tinytokenauth.configuration.token_secret, token)
35
+ @current_user = Tinytokenauth.configuration.user_class.send 'find', @decoded[:user_id]
69
36
  @exp = @decoded[:exp]
70
- if Authorizable.configuration.token_auto_renew_hours &&
71
- @exp < Authorizable.configuration.token_auto_renew_hours.hours.from_now.to_i
37
+ if Tinytokenauth.configuration.token_auto_renew_hours &&
38
+ @exp < Tinytokenauth.configuration.token_auto_renew_hours.hours.from_now.to_i
72
39
  sign_in @current_user
73
40
  end
74
41
  rescue ActiveRecord::RecordNotFound, JWT::DecodeError => e
@@ -80,27 +47,6 @@ module Tinytokenauth
80
47
  end
81
48
  end
82
49
 
83
- # def set_current_user(klass = User)
84
- # token = cookies[Authorizable.configuration.cookie_name]
85
- # begin
86
- # @decoded = JsonWebToken.decode(token)
87
- # # @current_user = User.find(@decoded[:user_id])
88
- # @current_user = klass.send 'find', @decoded[:user_id]
89
- # @exp = @decoded[:exp]
90
- # # if @exp < 24.hours.from_now.to_i # Always refresh token
91
- # if @exp < 4.hours.from_now.to_i # Always refresh token
92
- # # token = JsonWebToken.encode(user_id: @current_user.id)
93
- # # cookies['klap-auth'] = token
94
- # sign_in @current_user
95
- # end
96
- # rescue ActiveRecord::RecordNotFound
97
- # # Ignored
98
- # rescue JWT::DecodeError
99
- # # Ignored
100
- # end
101
- # @current_user
102
- # end
103
-
104
50
  def set_current_user
105
51
  begin
106
52
  require_current_user
@@ -114,10 +60,10 @@ module Tinytokenauth
114
60
 
115
61
  def sign_in(user)
116
62
  @current_user = user
117
- jwt = JsonWebToken.encode(user_id: user.id,
118
- exp: Authorizable.configuration.token_validity_hours.hours.from_now,
119
- secret: Authorizable.configuration.token_secret)
120
- cookies[Authorizable.configuration.cookie_name] = jwt
63
+ jwt = JsonWebToken.encode(Tinytokenauth.configuration.token_validity_hours.hours.from_now,
64
+ Tinytokenauth.configuration.token_secret,
65
+ user_id: user.id,)
66
+ cookies[Tinytokenauth.configuration.cookie_name] = jwt
121
67
  end
122
68
 
123
69
  def current_user
@@ -2,19 +2,12 @@ require 'jwt'
2
2
 
3
3
  module Tinytokenauth
4
4
  class JsonWebToken
5
- # def self.encode(payload, exp = 24.hours.from_now, secret = Rails.application.credentials.secret_key_base)
6
- def self.encode(payload, options = {})
7
- exp = options[:exp]
8
- secret = options[:secret]
9
- puts exp
10
- puts exp.to_i
11
- puts payload
5
+ def self.encode(exp, secret, payload)
12
6
  payload[:exp] = exp.to_i
13
7
  JWT.encode(payload, secret)
14
8
  end
15
9
 
16
- def self.decode(token, options = {})
17
- secret = options[:secret]
10
+ def self.decode(secret, token)
18
11
  decoded = JWT.decode(token, secret)[0]
19
12
  HashWithIndifferentAccess.new decoded
20
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tinytokenauth
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.8"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tinytokenauth-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kim Laplume