<commit_before># Copyright 2011-2013, The Trustees of Indiana University and Northwestern
#   University.  Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
# 
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software distributed 
#   under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
#   CONDITIONS OF ANY KIND, either express or implied. See the License for the 
#   specific language governing permissions and limitations under the License.
# ---  END LICENSE_HEADER BLOCK  ---

class StreamToken < ActiveRecord::Base
  class Unauthorized < Exception; end

  attr_accessible :token, :target, :expires
  
  def self.find_or_create_session_token(session, target)
    self.purge_expired!
    result = self.find_or_create_by_token_and_target(session[:session_id], target)
    result.renew!
    result.token
  end

  def self.logout!(session)
    self.find_all_by_token(session[:session_id]).each &:delete
  end

  def self.purge_expired!
    self.where("expires <= :now", :now => Time.now).each &:delete
  end

  def self.validate_token(value)
    raise Unauthorized, "Unauthorized" if value.nil?

    (target, token_string) = value.scan(/^(.+)-(.+)$/).first
    token = self.find_by_token_and_target(token_string, target)
    if token.present? and token.expires > Time.now
      token.renew!
      return target
    else
      raise Unauthorized, "Unauthorized"
    end
  end

  def renew!
    self.update_attribute :expires, ( Time.now + Avalon::Configuration['streaming']['stream_token_ttl'].minutes )
  end
end<commit_msg>Replace session token with model-generated media token<commit_after># Copyright 2011-2013, The Trustees of Indiana University and Northwestern
#   University.  Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
# 
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software distributed 
#   under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
#   CONDITIONS OF ANY KIND, either express or implied. See the License for the 
#   specific language governing permissions and limitations under the License.
# ---  END LICENSE_HEADER BLOCK  ---

class StreamToken < ActiveRecord::Base
  class Unauthorized < Exception; end

  attr_accessible :token, :target, :expires

  def self.media_token(session)
    session[:media_token] ||= SecureRandom.hex(16)
  end

  def self.find_or_create_session_token(session, target)
    self.purge_expired!
    result = self.find_or_create_by_token_and_target(media_token(session), target)
    result.renew!
    result.token
  end

  def self.logout!(session)
    self.find_all_by_token(media_token(session)).each &:delete
  end

  def self.purge_expired!
    self.where("expires <= :now", :now => Time.now).each &:delete
  end

  def self.validate_token(value)
    raise Unauthorized, "Unauthorized" if value.nil?

    (target, token_string) = value.scan(/^(.+)-(.+)$/).first
    token = self.find_by_token_and_target(token_string, target)
    if token.present? and token.expires > Time.now
      token.renew!
      return target
    else
      raise Unauthorized, "Unauthorized"
    end
  end

  def renew!
    self.update_attribute :expires, ( Time.now + Avalon::Configuration['streaming']['stream_token_ttl'].minutes )
  end
end
{'fraction_non_alphanumeric': 0.09731078458552814, 'fraction_numerical': 0.007208206265594677, 'mean_word_length': 3.908843537414966, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1329445', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1065, 'n_words': 332}