devise-jwt-revocation_strategies-redis 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +2 -2
- data/application.html.erb +23 -0
- data/devise-jwt-revocation_strategies-redis-0.1.0.gem +0 -0
- data/lib/devise/jwt/revocation_strategies/redis/generator.rb +20 -0
- data/lib/devise/jwt/revocation_strategies/redis/jwt_dispatcher.rb +9 -10
- data/lib/devise/jwt/revocation_strategies/redis/version.rb +1 -1
- data/lib/devise/jwt/revocation_strategies/redis.rb +13 -8
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f23269ef26a11c6d48d0a35efa82e21121ac6f4023086535365d9923ce837bf4
|
4
|
+
data.tar.gz: '083f8a42cf3111d33fe4e3d06a3e9d699f956ea2e2da082ebfe779f43deed896'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d214f880ee148ad65d0846521cf3e800b4c0294d05fe51a7ca6abaaf21341adc96a8327f59119b92ee6b680084b0a0961d112c9740be7c72a15251255d57c91
|
7
|
+
data.tar.gz: bc2860e2fd99e8eab772c7d5feaf0f05b6efdfddd1ce02366dd397d746a57a8af9d324cbd12972c64a6ff2c080f7a03e4ccb6cbc0c45cb64c1c15badcaa1660f
|
data/README.md
CHANGED
@@ -6,11 +6,11 @@ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_O
|
|
6
6
|
|
7
7
|
Install the gem and add to the application's Gemfile by executing:
|
8
8
|
|
9
|
-
$ bundle add
|
9
|
+
$ bundle add devise-jwt-revocation_strategies-redis
|
10
10
|
|
11
11
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
12
|
|
13
|
-
$ gem install
|
13
|
+
$ gem install devise-jwt-revocation_strategies-redis
|
14
14
|
|
15
15
|
## Usage
|
16
16
|
|
@@ -0,0 +1,23 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title><%= content_for(:title) || "Sudox Fridge" %></title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<meta name="apple-mobile-web-app-capable" content="yes">
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
<%= csp_meta_tag %>
|
9
|
+
|
10
|
+
<%= yield :head %>
|
11
|
+
|
12
|
+
<link rel="manifest" href="/manifest.json">
|
13
|
+
<link rel="icon" href="/icon.png" type="image/png">
|
14
|
+
<link rel="icon" href="/icon.svg" type="image/svg+xml">
|
15
|
+
<link rel="apple-touch-icon" href="/icon.png">
|
16
|
+
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
|
17
|
+
<%= javascript_importmap_tags %>
|
18
|
+
</head>
|
19
|
+
|
20
|
+
<body>
|
21
|
+
<%= yield %>
|
22
|
+
</body>
|
23
|
+
</html>
|
Binary file
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Devise::Jwt::RevocationStrategies::Utils.redis_key
|
4
|
+
module Devise
|
5
|
+
module Jwt
|
6
|
+
module RevocationStrategies
|
7
|
+
module Redis
|
8
|
+
class Generator
|
9
|
+
def self.redis_key(payload, prefix = 'jwt')
|
10
|
+
"#{prefix}:#{payload['sub']}:#{payload['d_uuid']}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.redis_value(payload, prefix = 'jwt', key = 'jti')
|
14
|
+
"#{payload[key]}:#{payload['d_name']}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -12,24 +12,23 @@ module Devise
|
|
12
12
|
def on_jwt_dispatch(token, payload)
|
13
13
|
raise ArgumentError, 'payload cannot be nil' if payload.nil?
|
14
14
|
|
15
|
-
|
16
|
-
save_token_in_redis(jti, payload['sub'], payload['exp'])
|
15
|
+
save_token_in_redis(payload)
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
19
|
private
|
21
20
|
|
22
|
-
def save_token_in_redis(
|
23
|
-
raise ArgumentError, 'sub cannot be nil' if
|
24
|
-
raise ArgumentError, 'jti cannot be nil' if jti.
|
25
|
-
raise ArgumentError, 'exp cannot be nil' if exp.
|
21
|
+
def save_token_in_redis(payload)
|
22
|
+
raise ArgumentError, 'sub cannot be nil' if payload['sub'].blank?
|
23
|
+
raise ArgumentError, 'jti cannot be nil' if payload['jti'].blank?
|
24
|
+
raise ArgumentError, 'exp cannot be nil' if payload['exp'].blank?
|
26
25
|
|
27
|
-
redis_key =
|
28
|
-
$redis_auth.sadd(redis_key, jti)
|
29
|
-
$redis_auth.expireat(redis_key, exp)
|
26
|
+
redis_key = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_key(payload)
|
27
|
+
$redis_auth.sadd(redis_key, "#{payload['jti']}:#{payload['d_name']}")
|
28
|
+
$redis_auth.expireat(redis_key, payload['exp'])
|
30
29
|
end
|
31
30
|
end
|
32
31
|
end
|
33
32
|
end
|
34
33
|
end
|
35
|
-
end
|
34
|
+
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require_relative "redis/version"
|
4
4
|
require_relative 'redis/jwt_dispatcher'
|
5
|
+
require_relative 'redis/generator'
|
5
6
|
require 'redis'
|
6
7
|
require 'dotenv-rails'
|
7
8
|
|
@@ -21,8 +22,10 @@ module Devise
|
|
21
22
|
def self.jwt_revoked?(payload, _user)
|
22
23
|
return true if payload.nil? || payload['jti'].nil? || payload['sub'].nil? # Check if JTI or user ID is nil
|
23
24
|
|
24
|
-
redis_key =
|
25
|
-
|
25
|
+
redis_key = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_key(payload)
|
26
|
+
redis_value = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_value(payload)
|
27
|
+
# now we can logout per device, but if we have multiple devices, we wont know the device name to logout
|
28
|
+
!$redis_auth.sismember(redis_key, redis_value)
|
26
29
|
end
|
27
30
|
|
28
31
|
# Revokes a JWT by deleting its entry from Redis.
|
@@ -31,19 +34,21 @@ module Devise
|
|
31
34
|
# @param _user [Object] The user object (not used in this method).
|
32
35
|
#
|
33
36
|
# @return nil
|
34
|
-
def self.revoke_jwt(payload, _user)
|
37
|
+
def self.revoke_jwt(payload, _user = nil)
|
35
38
|
user_id = payload['sub'] rescue nil
|
36
|
-
jti = payload['jti'] rescue nil
|
37
39
|
|
38
40
|
return if user_id.nil?
|
39
41
|
|
40
|
-
redis_key =
|
41
|
-
|
42
|
+
redis_key = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_key(payload)
|
43
|
+
redis_value = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_value(payload)
|
44
|
+
|
45
|
+
$redis_auth.srem(redis_key, redis_value) # Remove the specific JWT from the Set
|
42
46
|
end
|
43
47
|
|
48
|
+
# TODO: implement this method
|
44
49
|
def self.revoke_all_jwts_for_user(user_id)
|
45
|
-
redis_key =
|
46
|
-
$redis_auth.del(redis_key) # Delete the entire Set to revoke all tokens
|
50
|
+
# redis_key = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_key(payload)
|
51
|
+
# $redis_auth.del(redis_key) # Delete the entire Set to revoke all tokens
|
47
52
|
end
|
48
53
|
end
|
49
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise-jwt-revocation_strategies-redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kokorolx
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -36,7 +36,10 @@ files:
|
|
36
36
|
- ".rubocop.yml"
|
37
37
|
- README.md
|
38
38
|
- Rakefile
|
39
|
+
- application.html.erb
|
40
|
+
- devise-jwt-revocation_strategies-redis-0.1.0.gem
|
39
41
|
- lib/devise/jwt/revocation_strategies/redis.rb
|
42
|
+
- lib/devise/jwt/revocation_strategies/redis/generator.rb
|
40
43
|
- lib/devise/jwt/revocation_strategies/redis/jwt_dispatcher.rb
|
41
44
|
- lib/devise/jwt/revocation_strategies/redis/version.rb
|
42
45
|
- sig/devise/jwt/revocation_strategies/redis.rbs
|