<commit_before>require 'addressable/uri'

class LoginController < ApplicationController

  respond_to :html, :xml, :json

  def index
    if params[:renew]
      renew
    elsif params[:gateway]
      gateway
    elsif params[:service]
      service
    else
      login
    end
  end

  def create
    # TODO: Raise an InvalidRequestError and write a rescuer
    return render(text:'Missing required username parameter', status: :bad_request) unless params[:username]
    return render(text:'Missing required password parameter', status: :bad_request) unless params[:password]
    return render(text:'Missing required lt parameter', status: :bad_request) unless params[:lt]

    if params[:warn] == 'true'
      render login_form
    elsif authorized?
      if params[:service]
        redirect_to append_ticket( params[:service], @ticket.id )
      else
        render signed_in_notice
      end
    else
      redirect_to login_form_url, status: :unauthorized, alert:'Invalid username or password'
    end
  end

protected

  def renew
    if params[:renew] == 'true'
      render login_form
    else
      # TODO: Raise an InvalidRequestError and write a rescuer
      render text:'Invalid value for :renew parameter', status: :bad_request
    end
  end

  def gateway
    if params[:gateway] == 'true'
      if params[:service]
        if signed_in?
          @ticket = ServiceTicket.new( username:@sso_session.username, url:params[:service] )

          if @ticket.save
            redirect_to append_ticket( params[:service], @ticket.id )
          else
            raise NotImplementedError
          end
        else
          redirect_to params[:service]
        end
      else
        render login_form
      end
    else
      render text:'Invalid value for :gateway parameter', status: :bad_request
    end
  end

  def service
    if signed_in?
      @ticket = ServiceTicket.new( username:@sso_session.username, url:params[:service] )

      if @ticket.save
        redirect_to append_ticket( params[:service], @ticket.id )
      else
        raise NotImplementedError
      end
    else
      @ticket = LoginTicket.new

      render login_form
    end
  end

  def login
    if signed_in?
      render text:'already signed in'
    else
      render login_form
    end
  end

private

  def warden
    request.env['warden']
  end

  def signed_in?
    @sso_session ||= TicketGrantingTicket.find_by_tgc( cookies['tgc'] )
  end

  def authorized?
    return unless warden.authenticate?

    @ticket = if params[:username]
      ServiceTicket.new( username:params[:username] )
    elsif params[:ticket]
      ServiceTicket.find_by_ticket( params[:ticket] )
    end
  end

  def login_form
    { text:'[LOGIN FORM]' }
  end

  def signed_in_notice
    :create
  end

  def append_ticket( url, id )
    url = Addressable::URI.parse( url )

    url.query_values = (url.query_values||{}).merge( ticket:id )

    url.to_s
  end
<commit_msg>Adds TGTs into the login sequence which are not covered by the CAS protocol for some reason...<commit_after>require 'addressable/uri'

class LoginController < ApplicationController

  respond_to :html, :xml, :json

  def index
    if params[:renew]
      renew
    elsif params[:gateway]
      gateway
    elsif params[:service]
      service
    else
      login
    end
  end

  def create
    # TODO: Raise an InvalidRequestError and write a rescuer
    return render(text:'Missing required username parameter', status: :bad_request) unless params[:username]
    return render(text:'Missing required password parameter', status: :bad_request) unless params[:password]
    return render(text:'Missing required lt parameter', status: :bad_request) unless params[:lt]

    if params[:warn] == 'true'
      render login_form
    elsif authorized?
      if @ticket
        redirect_to append_ticket( params[:service], @ticket.id )
      else
        render signed_in_notice
      end
    else
      redirect_to login_form_url, status: :unauthorized, alert:'Invalid username or password'
    end
  end

protected

  def renew
    if params[:renew] == 'true'
      render login_form
    else
      # TODO: Raise an InvalidRequestError and write a rescuer
      render text:'Invalid value for :renew parameter', status: :bad_request
    end
  end

  def gateway
    if params[:gateway] == 'true'
      if params[:service]
        if signed_in?
          @ticket = ServiceTicket.new( username:@sso_session.username, url:params[:service] )

          if @ticket.save
            redirect_to append_ticket( params[:service], @ticket.id )
          else
            raise NotImplementedError
          end
        else
          redirect_to params[:service]
        end
      else
        render login_form
      end
    else
      render text:'Invalid value for :gateway parameter', status: :bad_request
    end
  end

  def service
    if signed_in?
      @ticket = ServiceTicket.new( username:@sso_session.username, url:params[:service] )

      if @ticket.save
        redirect_to append_ticket( params[:service], @ticket.id )
      else
        raise NotImplementedError
      end
    else
      @ticket = LoginTicket.new

      render login_form
    end
  end

  def login
    if signed_in?
      render text:'already signed in'
    else
      render login_form
    end
  end

private

  def warden
    request.env['warden']
  end

  def signed_in?
    @sso_session ||= TicketGrantingTicket.find_by_tgc( cookies['tgc'] )
  end

  def authorized?
    return unless warden.authenticate?

    tgt = TicketGrantingTicket.create( username:params[:username] )

    cookies['tgc'] = tgt.to_tgc( request ).to_cookie

    if params[:service]
      @ticket = ServiceTicket.create( username:params[:username], url:params[:service] )

      @ticket
    else
      true
    end
  end

  def login_form
    { text:'[LOGIN FORM]' }
  end

  def signed_in_notice
    :create
  end

  def append_ticket( url, id )
    url = Addressable::URI.parse( url )

    url.query_values = (url.query_values||{}).merge( ticket:id )

    url.to_s
  end

{'fraction_non_alphanumeric': 0.08325090669304319, 'fraction_numerical': 0.0, 'mean_word_length': 2.6725181598062955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5267797', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1756, 'n_words': 511}