<commit_before># AUTHOR: blink <<EMAIL>>; <EMAIL>
require 'rack/auth/abstract/handler'
require 'rubygems'
require 'openid'
module Rack
  module Auth
    # Rack::Auth::OpenID provides a simple method for permitting openid
    # based logins. It requires the ruby-openid lib from janrain to operate,
    # as well as some method of session management of a Hash type.
    #
    # NOTE: Due to the amount of data that ruby-openid stores in the session,
    # Rack::Session::Cookie may fault.
    #
    # A hash of data is stored in the session hash at the key of :openid.
    # The fully canonicalized identity url is stored within at 'identity'.
    # Extension data from 'openid.sreg.nickname' would be stored as
    # { 'nickname' => value }.
    #
    # NOTE: To my knowledge there is no collision at this point from storage
    # of this manner, if there is please let me know so I may adjust this app
    # to cope.
    class OpenID < AbstractHandler
      # Required for ruby-openid
      OIDStore = ::OpenID::MemoryStore.new

      # A Hash of options is taken as it's single initializing argument. String
      # keys are taken to be openid protocol extension namespaces. For example:
      #   'sreg' => { 'required' => 'nickname' }
      #
      # Other keys are taken as options for Rack::Auth::OpenID, normally Symbols.
      # Only :return is required. :trust is highly recommended to be set.
      #
      # * :return defines the url to return to after the client authenticates
      #   with the openid service provider. Should point to where this app is
      #   mounted. (ex: 'http://mysite.com/openid')
      # * :trust defines the url identifying the site they are actually logging
      #   into. (ex: 'http://mysite.com/')
      # * :session_key defines the key to the session hash in the env.
      #   (by default it uses 'rack.session')
      def initialize opts={}
        raise 'No return url provided.' unless opts[:return]
        unless opts[:trust]
          warn 'No trust url provided.'
          opts[:trust] = opts[:return]
        end
        opts[:session_key] ||= 'rack.session'
        @options = opts
      end

      def call env
        request = Rack::Request.new env
        session = request.env[@options[:session_key]]
        if session.nil? then E_NoSession
        elsif request.GET['openid.mode']
          finish session, request.GET
        elsif request.GET['openid_url']
          check session, request.GET['openid_url']
        else bad_request end
      end

      def check session, oid_url
        consumer = ::OpenID::Consumer.new session, OIDStore
        oid = consumer.begin oid_url
        return auth_fail unless oid.status == ::OpenID::SUCCESS
        @options.each do |ns,s|
          next unless String === ns
          s.each {|k,v| oid.add_extension_arg(ns, k, v) }
        end
        r_url = @options.fetch :return
        t_url = @options.fetch :trust
        return [303, {'Location'=>oid.redirect_url( t_url, r_url )}, []]
      end

      def finish session, params
        consumer = ::OpenID::Consumer.new session, OIDStore
        oid = consumer.complete params
        return bad_login unless oid.status == ::OpenID::SUCCESS
        session[:openid] = {'identity' => oid.identity_url}
        @options.each do |ns,s|
          next unless String === ns
          oid.extension_response(ns).each{|k,v| session[k]=v }
        end
        return [303, {'Location'=>@options[:trust]}, []]
      end

      def no_session
        @options.
          fetch :no_session, [500,{'Content-Type'=>'text/plain'},'No session available.']
      end
      def auth_fail
        @options.
          fetch :auth_fail, [500, {'Content-Type'=>'text/plain'},'Foreign server failure.']
      end
      def bad_login
        @options.
          fetch :bad_login, [401, {'Content-Type'=>'text/plain'},'Identification has failed.']
      end
    end
  end
end
<commit_msg>Cleanup of code, errant error call<commit_after># AUTHOR: blink <<EMAIL>>; blink#<EMAIL>
require 'rack/auth/abstract/handler'
require 'rubygems'
require 'openid'
module Rack
  module Auth
    # Rack::Auth::OpenID provides a simple method for permitting openid
    # based logins. It requires the ruby-openid lib from janrain to operate,
    # as well as some method of session management of a Hash type.
    #
    # NOTE: Due to the amount of data that ruby-openid stores in the session,
    # Rack::Session::Cookie may fault.
    #
    # A hash of data is stored in the session hash at the key of :openid.
    # The fully canonicalized identity url is stored within at 'identity'.
    # Extension data from 'openid.sreg.nickname' would be stored as
    # { 'nickname' => value }.
    #
    # NOTE: To my knowledge there is no collision at this point from storage
    # of this manner, if there is please let me know so I may adjust this app
    # to cope.
    class OpenID < AbstractHandler
      # Required for ruby-openid
      OIDStore = ::OpenID::MemoryStore.new

      # A Hash of options is taken as it's single initializing argument. String
      # keys are taken to be openid protocol extension namespaces. For example:
      #   'sreg' => { 'required' => 'nickname' }
      #
      # Other keys are taken as options for Rack::Auth::OpenID, normally Symbols.
      # Only :return is required. :trust is highly recommended to be set.
      #
      # * :return defines the url to return to after the client authenticates
      #   with the openid service provider. Should point to where this app is
      #   mounted. (ex: 'http://mysite.com/openid')
      # * :trust defines the url identifying the site they are actually logging
      #   into. (ex: 'http://mysite.com/')
      # * :session_key defines the key to the session hash in the env.
      #   (by default it uses 'rack.session')
      def initialize options={}
        raise 'No return url provided.' unless options[:return]
        warn  'No trust url provided.'  unless options[:trust]
        options[:trust] ||= options[:return]

        @options  = {
          :session_key => 'rack.session'
        }.merge(options)
      end

      def call env
        request = Rack::Request.new env
        return no_session unless session = request.env[@options[:session_key]]
        resp = if request.GET['openid.mode']
                 finish session, request.GET
               elsif request.GET['openid_url']
                 check session, request.GET['openid_url'], request
               else
                 bad_request
               end
      end

      def check session, oid_url
        consumer = ::OpenID::Consumer.new session, OIDStore
        oid = consumer.begin oid_url
        return auth_fail unless oid.status == ::OpenID::SUCCESS
        @options.each do |ns,s|
          next unless ns.is_a? String
          s.each {|k,v| oid.add_extension_arg(ns, k, v) }
        end
        r_url = @options.fetch :return
        t_url = @options.fetch :trust
        return [303, {'Location'=>oid.redirect_url( t_url, r_url )}, []]
      end

      def finish session, params
        consumer = ::OpenID::Consumer.new session, OIDStore
        oid = consumer.complete params
        return bad_login unless oid.status == ::OpenID::SUCCESS
        session[:openid] = {'identity' => oid.identity_url}
        @options.each do |ns,s|
          next unless ns.is_a? String
          oid.extension_response(ns).each{|k,v| session[k]=v }
        end
        return [303, {'Location'=>@options[:trust]}, []]
      end

      def no_session
        @options.
          fetch :no_session, [500,{'Content-Type'=>'text/plain'},'No session available.']
      end
      def auth_fail
        @options.
          fetch :auth_fail, [500, {'Content-Type'=>'text/plain'},'Foreign server failure.']
      end
      def bad_login
        @options.
          fetch :bad_login, [401, {'Content-Type'=>'text/plain'},'Identification has failed.']
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.10311871227364185, 'fraction_numerical': 0.003772635814889336, 'mean_word_length': 2.4016253207869975, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, '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': '4367303', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2238, 'n_words': 797}