access_token.rb revision 745541d1ed9a97715ee21919e3dc495a1add55c0
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksaclass AccessToken < ActiveRecord::Base
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa belongs_to :repository
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa attr_accessible :expiration, :token
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
5513a7cc4f5859d263e4cb66ae93e8dbfcf3681fEugen Kuksa scope :unexpired, ->() { where('expiration > ?', Time.now) }
5513a7cc4f5859d263e4cb66ae93e8dbfcf3681fEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa def to_s
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa token
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa end
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa def expired?
5513a7cc4f5859d263e4cb66ae93e8dbfcf3681fEugen Kuksa expiration <= Time.now
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa end
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksa
745541d1ed9a97715ee21919e3dc495a1add55c0Eugen Kuksa def self.create_for(ontology_version)
745541d1ed9a97715ee21919e3dc495a1add55c0Eugen Kuksa a = insecure_build_for(ontology_version) until !a.nil? && a.valid?
bae71386e6259d8b2f6941f21f37559b12a49b39Eugen Kuksa a.save!
bae71386e6259d8b2f6941f21f37559b12a49b39Eugen Kuksa a
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
bae71386e6259d8b2f6941f21f37559b12a49b39Eugen Kuksa
745541d1ed9a97715ee21919e3dc495a1add55c0Eugen Kuksa def self.insecure_build_for(ontology_version)
745541d1ed9a97715ee21919e3dc495a1add55c0Eugen Kuksa repository = ontology_version.repository
745541d1ed9a97715ee21919e3dc495a1add55c0Eugen Kuksa id = [repository.to_param, ontology_version.path,
745541d1ed9a97715ee21919e3dc495a1add55c0Eugen Kuksa Time.now.strftime("%Y-%m-%d-%H-%M-%S-%6N")].join("|")
bae71386e6259d8b2f6941f21f37559b12a49b39Eugen Kuksa AccessToken.new({repository: repository,
bae71386e6259d8b2f6941f21f37559b12a49b39Eugen Kuksa expiration: fresh_expiration_date,
745541d1ed9a97715ee21919e3dc495a1add55c0Eugen Kuksa token: Digest::SHA2.hexdigest(id)}, {without_protection: true})
bae71386e6259d8b2f6941f21f37559b12a49b39Eugen Kuksa end
490da89281b7ee019c87406fb8ce7359aff0932cEugen Kuksaend