<commit_before>module Cassy
  class SessionsController < ApplicationController
    include Cassy::Utils
    include Cassy::CAS

    def new
      # optional params
      @service = clean_service_url(params['service'])
      @renew = params['renew']
      @gateway = params['gateway'] == 'true' || params['gateway'] == '1'

      if tgc = request.cookies['tgt']
        tgt, tgt_error = validate_ticket_granting_ticket(tgc)
      end

      if tgt and !tgt_error
        flash[:notice] = "You are currently logged in as '%s'. If this is not you, please log in below." % tgt.username
      end

      if params['redirection_loop_intercepted']
        flash[:error] = "The client and server are unable to negotiate authentication. Please try logging in again later."
      end

      begin
        if @service
          if !@renew && tgt && !tgt_error
            st = generate_service_ticket(@service, tgt.username, tgt)
            service_with_ticket = service_uri_with_ticket(@service, st)
            redirect_to service_with_ticket, :status => 303 # response code 303 means "See Other" (see Appendix B in CAS Protocol spec)
          elsif @gateway
            redirect @service, 303
          end
        elsif @gateway
          flash[:error] = "The server cannot fulfill this gateway request because no service parameter was given."
        end
      rescue URI::InvalidURIError
        flash[:error] = "The target service your browser supplied appears to be invalid. Please contact your system administrator for help."
      end

      @lt = generate_login_ticket.ticket
    end
    
    def create
      setup_from_params!

      if error = validate_login_ticket(@lt)
        flash[:error] = error
        @lt = generate_login_ticket.ticket
        render(:new, :status => 500) and return
      end
      
      # generate another login ticket to allow for re-submitting the form after a post
      @lt = generate_login_ticket.ticket

      logger.debug("Logging in with username: #{@username}, lt: #{@lt}, service: #{@service}, auth: #{settings[:auth].inspect}")

      begin
        if valid_credentials?
          # 3.6 (ticket-granting cookie)
          tgt = generate_ticket_granting_ticket(@username, @extra_attributes)
          response.set_cookie('tgt', tgt.to_s)

          if @service.blank?
            flash[:notice] = "You have successfully logged in."
            render :new
          else
            @st = generate_service_ticket(@service, @username, tgt)

            begin
              service_with_ticket = service_uri_with_ticket(@service, @st)
              redirect_to service_with_ticket, :status => 303 # response code 303 means "See Other" (see Appendix B in CAS Protocol spec)
            rescue URI::InvalidURIError
              flash[:error] = "The target service your browser supplied appears to be invalid. Please contact your system administrator for help."
            end
          end
        else
          incorrect_credentials!
        end
      rescue Cassy::AuthenticatorError => e
        logger.error(e)
        # generate another login ticket to allow for re-submitting the form
        @lt = generate_login_ticket.ticket
        flash[:error] = e.to_s
        render :status => 401
      end
    end
    
    def destroy
      # The behaviour here is somewhat non-standard. Rather than showing just a blank
      # "logout" page, we take the user back to the login page with a "you have been logged out"
      # message, allowing for an opportunity to immediately log back in. This makes it
      # easier for the user to log out and log in as someone else.
      @service = clean_service_url(params['service'] || params['destination'])
      @continue_url = params['url']

      @gateway = params['gateway'] == 'true' || params['gateway'] == '1'

      tgt = Cassy::TicketGrantingTicket.find_by_ticket(request.cookies['tgt'])

      response.delete_cookie 'tgt'

      if tgt
        Cassy::TicketGrantingTicket.transaction do
          pgts = Cassy::ProxyGrantingTicket.find(:all,
            :conditions => [ActiveRecord::Base.connection.quote_table_name(Cassy::ServiceTicket.table_name)+".username = ?", tgt.username],
            :include => :service_ticket)
          pgts.each do |pgt|
            pgt.destroy
          end

          tgt.destroy
        end

        # $LOG.info("User '#{tgt.username}' logged out.")
      else
        # $LOG.warn("User tried to log out without a valid ticket-granting ticket.")
      end
      
      flash[:notice] = "You have successfully logged out."
      @lt = generate_login_ticket

      if @gateway && @service
        redirect_to @service, :status => 303
      else
        render :new
      end<commit_msg>Redirect back to login page after successful logout rather than simply rendering it. Also include service parameter with /users/service suffix<commit_after>module Cassy
  class SessionsController < ApplicationController
    include Cassy::Utils
    include Cassy::CAS

    def new
      # optional params
      @service = clean_service_url(params['service'])
      @renew = params['renew']
      @gateway = params['gateway'] == 'true' || params['gateway'] == '1'

      if tgc = request.cookies['tgt']
        tgt, tgt_error = validate_ticket_granting_ticket(tgc)
      end

      if tgt and !tgt_error
        flash[:notice] = "You are currently logged in as '%s'. If this is not you, please log in below." % tgt.username
      end

      if params['redirection_loop_intercepted']
        flash[:error] = "The client and server are unable to negotiate authentication. Please try logging in again later."
      end

      begin
        if @service
          if !@renew && tgt && !tgt_error
            st = generate_service_ticket(@service, tgt.username, tgt)
            service_with_ticket = service_uri_with_ticket(@service, st)
            redirect_to service_with_ticket, :status => 303 # response code 303 means "See Other" (see Appendix B in CAS Protocol spec)
          elsif @gateway
            redirect @service, 303
          end
        elsif @gateway
          flash[:error] = "The server cannot fulfill this gateway request because no service parameter was given."
        end
      rescue URI::InvalidURIError
        flash[:error] = "The target service your browser supplied appears to be invalid. Please contact your system administrator for help."
      end

      @lt = generate_login_ticket.ticket
    end
    
    def create
      setup_from_params!

      if error = validate_login_ticket(@lt)
        flash[:error] = error
        @lt = generate_login_ticket.ticket
        render(:new, :status => 500) and return
      end
      
      # generate another login ticket to allow for re-submitting the form after a post
      @lt = generate_login_ticket.ticket

      logger.debug("Logging in with username: #{@username}, lt: #{@lt}, service: #{@service}, auth: #{settings[:auth].inspect}")

      begin
        if valid_credentials?
          # 3.6 (ticket-granting cookie)
          tgt = generate_ticket_granting_ticket(@username, @extra_attributes)
          response.set_cookie('tgt', tgt.to_s)

          if @service.blank?
            flash[:notice] = "You have successfully logged in."
            render :new
          else
            @st = generate_service_ticket(@service, @username, tgt)

            begin
              service_with_ticket = service_uri_with_ticket(@service, @st)
              redirect_to service_with_ticket, :status => 303 # response code 303 means "See Other" (see Appendix B in CAS Protocol spec)
            rescue URI::InvalidURIError
              flash[:error] = "The target service your browser supplied appears to be invalid. Please contact your system administrator for help."
            end
          end
        else
          incorrect_credentials!
        end
      rescue Cassy::AuthenticatorError => e
        logger.error(e)
        # generate another login ticket to allow for re-submitting the form
        @lt = generate_login_ticket.ticket
        flash[:error] = e.to_s
        render :status => 401
      end
    end
    
    def destroy
      # The behaviour here is somewhat non-standard. Rather than showing just a blank
      # "logout" page, we take the user back to the login page with a "you have been logged out"
      # message, allowing for an opportunity to immediately log back in. This makes it
      # easier for the user to log out and log in as someone else.
      @service = clean_service_url(params['service'] || params['destination'])
      @continue_url = params['url']

      @gateway = params['gateway'] == 'true' || params['gateway'] == '1'

      tgt = Cassy::TicketGrantingTicket.find_by_ticket(request.cookies['tgt'])

      response.delete_cookie 'tgt'

      if tgt
        Cassy::TicketGrantingTicket.transaction do
          pgts = Cassy::ProxyGrantingTicket.find(:all,
            :conditions => [ActiveRecord::Base.connection.quote_table_name(Cassy::ServiceTicket.table_name)+".username = ?", tgt.username],
            :include => :service_ticket)
          pgts.each do |pgt|
            pgt.destroy
          end

          tgt.destroy
        end

        # $LOG.info("User '#{tgt.username}' logged out.")
      else
        # $LOG.warn("User tried to log out without a valid ticket-granting ticket.")
      end
      
      flash[:notice] = "You have successfully logged out."
      @lt = generate_login_ticket

      if @gateway && @service
        redirect_to @service, :status => 303
      else
        redirect_to "/cas/login?service=#{@service}/users/service"
      end
{'fraction_non_alphanumeric': 0.08996251561849229, 'fraction_numerical': 0.0058309037900874635, 'mean_word_length': 2.451311534315487, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '5206868', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2551, 'n_words': 842}