access_token.rb revision 490da89281b7ee019c87406fb8ce7359aff0932c
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksaclass AccessToken < ActiveRecord::Base
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa LENGTH = 20
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa belongs_to :repository
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa attr_accessible :expiration, :token
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa def to_s
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa token
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa end
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa def expired?
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa Time.now > expiration
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa end
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa def refresh!
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa self.expiration = self.class.fresh_expiration_date
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa save!
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa end
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa def replace
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa self.destroy
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa self.class.build_for(repository)
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa end
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa def self.build_for(repository)
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa AccessToken.new({repository: repository,
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa expiration: fresh_expiration_date,
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa token: SecureRandom.hex(LENGTH)}, {without_protection: true})
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa end
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa protected
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa def self.fresh_expiration_date
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa Settings.access_token.expiration_minutes.minutes.from_now
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa end
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksaend