Files
clinch/app/models/oidc_access_token.rb
Dan Milne 56f7dd7b3c
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / system-test (push) Has been cancelled
First crack
2025-10-23 16:45:00 +11:00

35 lines
667 B
Ruby

class OidcAccessToken < ApplicationRecord
belongs_to :application
belongs_to :user
before_validation :generate_token, on: :create
before_validation :set_expiry, on: :create
validates :token, presence: true, uniqueness: true
scope :valid, -> { where("expires_at > ?", Time.current) }
scope :expired, -> { where("expires_at <= ?", Time.current) }
def expired?
expires_at <= Time.current
end
def active?
!expired?
end
def revoke!
update!(expires_at: Time.current)
end
private
def generate_token
self.token ||= SecureRandom.urlsafe_base64(48)
end
def set_expiry
self.expires_at ||= 1.hour.from_now
end
end