Class: JwtAuthCognito::RedisService

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt_auth_cognito/redis_service.rb

Constant Summary collapse

BLACKLIST_PREFIX =
'jwt_blacklist:'
USER_TOKENS_PREFIX =
'user_tokens:'

Instance Method Summary collapse

Constructor Details

#initialize(config = JwtAuthCognito.configuration) ⇒ RedisService

Returns a new instance of RedisService.



12
13
14
15
# File 'lib/jwt_auth_cognito/redis_service.rb', line 12

def initialize(config = JwtAuthCognito.configuration)
  @config = config
  @redis = nil
end

Instance Method Details

#clear_revoked_tokensObject



42
43
44
45
46
47
48
49
# File 'lib/jwt_auth_cognito/redis_service.rb', line 42

def clear_revoked_tokens
  connect_redis
  keys = @redis.keys("#{BLACKLIST_PREFIX}*")
  @redis.del(*keys) if keys.any?
  keys.length
rescue Redis::BaseError => e
  raise BlacklistError, "Failed to clear revoked tokens: #{e.message}"
end

#generate_token_id(token) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jwt_auth_cognito/redis_service.rb', line 86

def generate_token_id(token)
  # Try to extract jti from token first
  begin
    payload = JWT.decode(token, nil, false).first
    return payload['jti'] if payload['jti']
  rescue JWT::DecodeError
    # Fall back to hash if token can't be decoded
  end

  # Generate hash-based ID
  Digest::SHA256.hexdigest(token)[0, 16]
end

#invalidate_user_tokens(user_id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jwt_auth_cognito/redis_service.rb', line 51

def invalidate_user_tokens(user_id)
  connect_redis

  # Get all tokens for the user
  user_key = "#{USER_TOKENS_PREFIX}#{user_id}"
  token_ids = @redis.smembers(user_key)

  # Add all tokens to blacklist
  token_ids.each do |token_id|
    save_revoked_token(token_id)
  end

  # Clear the user's token set
  @redis.del(user_key)

  token_ids.length
rescue Redis::BaseError => e
  raise BlacklistError, "Failed to invalidate user tokens: #{e.message}"
end

#is_token_revoked?(token_id) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
# File 'lib/jwt_auth_cognito/redis_service.rb', line 32

def is_token_revoked?(token_id)
  connect_redis
  key = "#{BLACKLIST_PREFIX}#{token_id}"
  result = @redis.exists?(key)
  result.is_a?(Integer) ? result.positive? : result
rescue Redis::BaseError
  # Graceful degradation - if Redis is down, don't block validation
  false
end

#save_revoked_token(token_id, ttl = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jwt_auth_cognito/redis_service.rb', line 17

def save_revoked_token(token_id, ttl = nil)
  connect_redis
  key = "#{BLACKLIST_PREFIX}#{token_id}"

  if ttl
    @redis.setex(key, ttl, 'revoked')
  else
    @redis.set(key, 'revoked')
  end

  true
rescue Redis::BaseError => e
  raise BlacklistError, "Failed to save revoked token: #{e.message}"
end

#track_user_token(user_id, token_id, ttl = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jwt_auth_cognito/redis_service.rb', line 71

def track_user_token(user_id, token_id, ttl = nil)
  connect_redis

  user_key = "#{USER_TOKENS_PREFIX}#{user_id}"
  @redis.sadd(user_key, token_id)

  # Set expiration on the user's token set
  @redis.expire(user_key, ttl) if ttl

  true
rescue Redis::BaseError
  # Non-critical operation, log but don't fail
  false
end