<commit_before>require "openid/filestore"
require "openid/consumer"

# Please choose a non-world readable directory for your OpenID store.
# I've set it to use /tmp for simplicity.
store = OpenID::FilesystemOpenIDStore.new("/tmp/railsgen")
$consumer = OpenID::OpenIDConsumer.new(store)

class <%= class_name %>Controller < ApplicationController
  layout  'scaffold'

  def login
    openid_url = @params[:openid_url]

    if @request.post?
      status, info = $consumer.begin_auth(openid_url)
      case status
      when OpenID::SUCCESS
        return_to = url_for(:action=>:complete_auth, :token=>info.token)
        trust_root = url_for(:controller=>"")
        redirect_url = $consumer.construct_redirect(info, return_to, trust_root)

        redirect_to(redirect_url)

      when OpenID::PARSE_ERROR
        flash[:notice] = "Could not find OpenID server for page #{openid_url}"
        
      when OpenID::HTTP_FAILURE
        flash[:notice] = "Could not fetch page #{openid_url}"

      end      
    end    
  end

  def complete_auth
    token = @params[:token]
    
    status, info = $consumer.complete_auth(token, @params)
    
    if status == OpenID::SUCCESS
      openid_url = info

      @user = User.get(openid_url)
      
      # create user object if one does not exist
      if @user.nil?
        @user = User.new(:openid_url => openid_url)
        @user.save
      end

      # storing both the openid_url and user id in the session for for quick
      # access to both bits of information.  Change as needed.
      @session[:user_id] = @user.id

      flash[:notice] = "Logged in as #{openid_url}"
       
      redirect_to :action => "welcome"
      return

    elsif status == OpenID::FAILURE and info
      flash[:notice] = "Verification of #{info} failed."

    else
      flash[:notice] = "Verification cancelled"
    end
  
    redirect_to :action => "login"
  end
  
  def logout
    @session[:user_id] = nil
  end
    
  def welcome
  end

  private

  # get the logged in user object
  def find_user
    return nil if session[:user_id].nil?
    User.find(session[:user_id])
  end
  
end<commit_msg>[project @ updated openid login generator to use new consumer api]<commit_after>require "pathname"
require "cgi"

require "openid/filestore"
require "openid/consumer"

class <%= class_name %>Controller < ApplicationController
  layout  'scaffold'
  
  # process the login request, disover the openid server, and
  # then redirect.
  def login
    openid_url = @params[:openid_url]

    if @request.post?
      request = consumer.begin(openid_url)

      p 'XXX', request

      case request.status
      when OpenID::SUCCESS
        return_to = url_for(:action=> 'complete')
        trust_root = url_for(:controller=>'')

        url = request.redirect_url(trust_root, return_to)
        p 'REDIRECTURL', url

        redirect_to(url)

        p 'REDIRECT SENT', @session
        return

      when OpenID::FAILURE
        escaped_url = CGI::escape(openid_url)
        flash[:notice] = "Could not find OpenID server for #{escaped_url}"
        
      else
        flash[:notice] = "An unknown error occured."

      end      
    end    

    p 'Done'

  end

  # handle the openid server response
  def complete
    token = @params[:token]
    
    response = consumer.complete(@params)
    
    case response.status
    when OpenID::SUCCESS

      @user = User.get(response.identity_url)
      
      # create user object if one does not exist
      if @user.nil?
        @user = User.new(:openid_url => response.identity_url)
        @user.save
      end

      # storing both the openid_url and user id in the session for for quick
      # access to both bits of information.  Change as needed.
      @session[:user_id] = @user.id

      flash[:notice] = "Logged in as #{CGI::escape(response.identity_url)}"
       
      redirect_to :action => "welcome"
      return

    when OpenID::FAILURE
      if response.identity_url
        flash[:notice] = "Verification of #{CGI::escape(response.identity_url)} failed."

      else
        flash[:notice] = 'Verification failed.'
      end

    when OpenID::CANCEL
      flash[:notice] = 'Verification cancelled.'

    else
      flash[:notice] = 'Unknown response from OpenID server.'
    end
  
    redirect_to :action => 'login'
  end
  
  def logout
    @session[:user_id] = nil
  end
    
  def welcome
  end

  private

  # Get the OpenID::Consumer object.
  def consumer
    # create the OpenID store for storing associations and nonces,
    # putting it in your app's db directory
    store_dir = Pathname.new(RAILS_ROOT).join('db').join('openid-store')
    store = OpenID::FilesystemStore.new(store_dir)

    return OpenID::Consumer.new(@session, store)
  end

  # get the logged in user object
  def find_user
    return nil if session[:user_id].nil?
    User.find(session[:user_id])
  end
  
end
{'fraction_non_alphanumeric': 0.09787059787059788, 'fraction_numerical': 0.0, 'mean_word_length': 2.66191904047976, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '3520962', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1472, 'n_words': 421}