Files
akkounts/app/models/rs_authorization_code.rb
T
raucao 1e69957777
CI / Test (pull_request) Failing after 45s
Shorten the authorization code lifetime to 60 seconds
The code is redeemed by the client immediately in the OAuth callback, so
a 60-second window is plenty (and matches common practice; RFC 6749 caps
authorization codes at 10 minutes).

Part of #254
2026-11-03 09:17:44 +04:00

38 lines
706 B
Ruby

class RsAuthorizationCode < ApplicationRecord
belongs_to :user
CODE_TTL = 60.seconds
serialize :permissions, coder: YAML
attr_accessor :code
before_validation :generate_code
scope :active, -> { where(expires_at: Time.current..) }
def self.find_by_code(raw_code)
return nil if raw_code.blank?
find_by(code_digest: Digest::SHA256.hexdigest(raw_code))
end
def expired?
expires_at <= Time.current
end
def consume!
destroy!
end
private
def generate_code
return if code_digest.present?
self.code = SecureRandom.urlsafe_base64(32)
self.code_digest = Digest::SHA256.hexdigest(code)
self.expires_at ||= CODE_TTL.from_now
end
end