discourse/lib/secure_session.rb
Jarek Radosz 694b5f108b
DEV: Fix various rubocop lints (#24749)
These (21 + 3 from previous PRs) are soon to be enabled in rubocop-discourse:

Capybara/VisibilityMatcher
Lint/DeprecatedOpenSSLConstant
Lint/DisjunctiveAssignmentInConstructor
Lint/EmptyConditionalBody
Lint/EmptyEnsure
Lint/LiteralInInterpolation
Lint/NonLocalExitFromIterator
Lint/ParenthesesAsGroupedExpression
Lint/RedundantCopDisableDirective
Lint/RedundantRequireStatement
Lint/RedundantSafeNavigation
Lint/RedundantStringCoercion
Lint/RedundantWithIndex
Lint/RedundantWithObject
Lint/SafeNavigationChain
Lint/SafeNavigationConsistency
Lint/SelfAssignment
Lint/UnreachableCode
Lint/UselessMethodDefinition
Lint/Void

Previous PRs:
Lint/ShadowedArgument
Lint/DuplicateMethods
Lint/BooleanSymbol
RSpec/SpecFilePathSuffix
2023-12-06 23:25:00 +01:00

45 lines
827 B
Ruby

# frozen_string_literal: true
# session that is not stored in cookie, expires after 1.hour unconditionally
class SecureSession
def initialize(prefix)
@prefix = prefix
end
def self.expiry
@expiry ||= 1.hour.to_i
end
def self.expiry=(val)
@expiry = val
end
def set(key, val, expires: nil)
expires ||= SecureSession.expiry
Discourse.redis.setex(prefixed_key(key), expires.to_i, val.to_s)
true
end
def ttl(key)
Discourse.redis.ttl(prefixed_key(key))
end
def [](key)
Discourse.redis.get(prefixed_key(key))
end
def []=(key, val)
if val == nil
Discourse.redis.del(prefixed_key(key))
else
Discourse.redis.setex(prefixed_key(key), SecureSession.expiry.to_i, val.to_s)
end
end
private
def prefixed_key(key)
"#{@prefix}#{key}"
end
end