<commit_before>require 'casino/authenticator'

class SessionsController < ApplicationController
  include SessionsHelper

  before_filter :validate_login_ticket, only: :create
  before_filter :redirect_signed_in, only: [:create, :new]
  before_filter :authenticate, only: :index

  def index
    @ticket_granting_tickets = TicketGrantingTicket.where(username: current_ticket_granting_ticket.username).order('updated_at DESC')
  end

  def new
  end

  def create
    login_data = validate_login(params[:username], params[:password])
    if login_data
      ticket_granting_ticket = acquire_ticket_granting_ticket(login_data[:username], login_data[:extra_attributes])
      cookies[:tgt] = ticket_granting_ticket.ticket
      logger.info "Successfully generated ticket-granting ticket for user '#{login_data[:username]}'"
      if params[:service].nil?
        redirect_to sessions_path
      else
        redirect_to params[:serivce]
      end
    else
      logger.info "Could not login user '#{params[:username]}': Invalid credentials supplied."
      flash.now[:error] = 'Incorrect username or password.'
      render 'new', status: 403
    end
  end

  def destroy
    ticket_granting_ticket = TicketGrantingTicket.find params[:id]
    if ticket_granting_ticket.username != current_ticket_granting_ticket.username
      logger.info "Did not allow '#{ticket_granting_ticket.username}' to delete ticket owned by #{current_ticket_granting_ticket.username}"
    elsif !current_ticket_granting_ticket?(ticket_granting_ticket)
      destroy_ticket_granting_ticket(ticket_granting_ticket)
    end
    redirect_to sessions_path
  end

  def logout
    if signed_in?
      destroy_ticket_granting_ticket(current_ticket_granting_ticket)
      cookies.delete(:tgt)
    end
    redirect_to login_path
  end

  private
  def authenticate
    deny_access unless signed_in?
  end

  def deny_access
    flash.now[:error] = 'Please sign in to access this page.'
    render 'new', status: 403
  end

  def redirect_signed_in
    redirect_to sessions_path if signed_in?
  end

  def validate_login(username, password)
    user_data = nil
    Yetting.authenticators.each do |authenticator|
      instance = "#{authenticator['class']}".constantize.new(authenticator['options'])
      data = instance.validate(username, password)
      if data
        if data[:username].nil?
          data[:username] = username
        end
        user_data = data
        break
      end
    end
    user_data
  end

  def validate_login_ticket
    login_ticket = params[:lt]
    ticket = LoginTicket.find_by_ticket login_ticket
    valid = if ticket.nil?
      logger.info "Login ticket '#{login_ticket}' not found"
      false
    elsif ticket.created_at < Yetting.login_ticket['lifetime'].seconds.ago
      logger.info "Login ticket '#{ticket.ticket}' expired"
      false
    else
      logger.info "Login ticket '#{ticket.ticket}' successfully validated"
      ticket.delete
      true
    end
    unless valid
      flash.now[:error] = 'No valid login ticket found. Please try again.'
      render 'new', status: 403
    end
  end

  def acquire_ticket_granting_ticket(username, extra_attributes = nil)
    TicketGrantingTicket.create!({<commit_msg>logging<commit_after>require 'casino/authenticator'

class SessionsController < ApplicationController
  include SessionsHelper

  before_filter :validate_login_ticket, only: :create
  before_filter :redirect_signed_in, only: [:create, :new]
  before_filter :authenticate, only: :index

  def index
    @ticket_granting_tickets = TicketGrantingTicket.where(username: current_ticket_granting_ticket.username).order('updated_at DESC')
  end

  def new
  end

  def create
    login_data = validate_login(params[:username], params[:password])
    if login_data
      ticket_granting_ticket = acquire_ticket_granting_ticket(login_data[:username], login_data[:extra_attributes])
      cookies[:tgt] = ticket_granting_ticket.ticket
      logger.info "Successfully generated ticket-granting ticket for user '#{login_data[:username]}'"
      if params[:service].nil?
        redirect_to sessions_path
      else
        redirect_to params[:serivce]
      end
    else
      logger.info "Could not login user '#{params[:username]}': Invalid credentials supplied."
      flash.now[:error] = 'Incorrect username or password.'
      render 'new', status: 403
    end
  end

  def destroy
    ticket_granting_ticket = TicketGrantingTicket.find params[:id]
    if ticket_granting_ticket.username != current_ticket_granting_ticket.username
      logger.info "Did not allow '#{ticket_granting_ticket.username}' to delete ticket owned by #{current_ticket_granting_ticket.username}"
    elsif !current_ticket_granting_ticket?(ticket_granting_ticket)
      destroy_ticket_granting_ticket(ticket_granting_ticket)
    end
    redirect_to sessions_path
  end

  def logout
    if signed_in?
      destroy_ticket_granting_ticket(current_ticket_granting_ticket)
      cookies.delete(:tgt)
    end
    redirect_to login_path
  end

  private
  def authenticate
    deny_access unless signed_in?
  end

  def deny_access
    flash.now[:error] = 'Please sign in to access this page.'
    render 'new', status: 403
  end

  def redirect_signed_in
    redirect_to sessions_path if signed_in?
  end

  def validate_login(username, password)
    user_data = nil
    Yetting.authenticators.each do |authenticator|
      instance = "#{authenticator['class']}".constantize.new(authenticator['options'])
      data = instance.validate(username, password)
      if data
        if data[:username].nil?
          data[:username] = username
        end
        user_data = data
        logger.info("Credentials for username '#{data[:username]}' successfully validated using #{authenticator['class']}")
        break
      end
    end
    user_data
  end

  def validate_login_ticket
    login_ticket = params[:lt]
    ticket = LoginTicket.find_by_ticket login_ticket
    valid = if ticket.nil?
      logger.info "Login ticket '#{login_ticket}' not found"
      false
    elsif ticket.created_at < Yetting.login_ticket['lifetime'].seconds.ago
      logger.info "Login ticket '#{ticket.ticket}' expired"
      false
    else
      logger.debug "Login ticket '#{ticket.ticket}' successfully validated"
      ticket.delete
      true
    end
    unless valid
      flash.now[:error] = 'No valid login ticket found. Please try again.'
      render 'new', status: 403
    end
  end

  def acquire_ticket_granting_ticket(username, extra_attributes = nil)
    TicketGrantingTicket.create!({
{'fraction_non_alphanumeric': 0.0825338815288564, 'fraction_numerical': 0.002740977615349475, 'mean_word_length': 3.6981402002861232, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5510620', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1876, 'n_words': 465}