Module: Devise::Jwt::RevocationStrategies::Redis

Defined in:
lib/devise/jwt/revocation_strategies/redis.rb,
lib/devise/jwt/revocation_strategies/redis/version.rb,
lib/devise/jwt/revocation_strategies/redis/generator.rb,
lib/devise/jwt/revocation_strategies/redis/jwt_dispatcher.rb

Defined Under Namespace

Modules: JwtDispatcher Classes: Error, Generator

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.jwt_revoked?(payload, _user) ⇒ Boolean

Checks if the JWT has been revoked.

Parameters:

  • payload (Hash)

    the payload of the JWT, which includes the ‘jti’ (JWT ID).

  • _user (Object)

    the user object (unused in this method).

Returns:

  • (Boolean)

    true if the JWT has been revoked or if there is an error accessing Redis, false otherwise.



22
23
24
25
26
27
28
29
# File 'lib/devise/jwt/revocation_strategies/redis.rb', line 22

def self.jwt_revoked?(payload, _user)
  return true if payload.nil? || payload['jti'].nil? || payload['sub'].nil?  # Check if JTI or user ID is nil

  redis_key = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_key(payload)
  redis_value = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_value(payload)
  # now we can logout per device, but if we have multiple devices, we wont know the device name to logout
  !$redis_auth.sismember(redis_key, redis_value)
end

.revoke_all_jwts_for_user(user_id) ⇒ Object

TODO: implement this method



49
50
51
52
# File 'lib/devise/jwt/revocation_strategies/redis.rb', line 49

def self.revoke_all_jwts_for_user(user_id)
  # redis_key = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_key(payload)
  # $redis_auth.del(redis_key)  # Delete the entire Set to revoke all tokens
end

.revoke_jwt(payload, _user = nil) ⇒ Object

Revokes a JWT by deleting its entry from Redis.

Parameters:

  • payload (Hash)

    The payload of the JWT, which should include the ‘jti’ (JWT ID).

  • _user (Object) (defaults to: nil)

    The user object (not used in this method).

Returns:

  • nil



37
38
39
40
41
42
43
44
45
46
# File 'lib/devise/jwt/revocation_strategies/redis.rb', line 37

def self.revoke_jwt(payload, _user = nil)
  user_id = payload['sub'] rescue nil

  return if user_id.nil?

  redis_key = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_key(payload)
  redis_value = Devise::Jwt::RevocationStrategies::Redis::Generator.redis_value(payload)

  $redis_auth.srem(redis_key, redis_value)  # Remove the specific JWT from the Set
end