<commit_before>require 'securerandom'
require 'addressable/uri'

#TODO: add comments!

module RailsBook
  class FacebookRedirectLoginHelper < ActionController
    
    def initialize(redirect_url)
      @redirect_url = redirect_url
    end
    
    def get_login_url(scope = [], display_as_popup=false)
      
      puts session[:test]
      
      state = random_bytes(16)
      uri = Addressable::URI.new
      
      uri_params = {
        client_id:      ENV["app_id"],
        redirect_uri:   @redirect_url,
        state:          :state,
        sdk:            RailsBook::SDK_NAME,
        scope:          scope.join(",")
      }
      
      uri_params[:display] = :popup if display_as_popup
      
      uri.query_values = uri_params
      
      return "https://www.facebook.com/" +
              RailsBook::GRAPH_API_VERSION +
              "/dialog/oauth?" +
              uri.query
    end
    
    def get_logout_url(session, next_page)
      if !session.instance_of? FacebookSession
        raise FacebookSDKException.new "not a valid session"
      end
      uri = Addressable::URI.new
      uri_params = {
        next:           next_page,
        access_token:   session.get_token 
      }
      uri.query_values = uri_params
      return "https://www.facebook.com/logout.php?" +
             uri.query
    end
    
    def get_session_from_redirect 
      load_state
      if is_valid_redirect
        response_params = {
          client_id: ENV["app_id"],
          redirect_uri: @redirect_uri,
          client_secret: ENV["app_secret"],
          code: get_code
        }
        facebook_response = {}
      end
      return nil
    end
    
    private 
    
    def is_valid_redirect
      get_code and 
      get_state and
      get_state.eql? get_internal_state
    end
    
    def get_internal_state
      session[:state]
    end
    
    def get_state
      params[:state]
    end
    
    def get_code
      params[:code]
    end
    
    def random_bytes(bytes)
      if !bytes.is_a? Integer
        raise FacebookSDKException.new "random expects an integer"
      end
      if bytes < 1
        raise FacebookSDKException.new "random expects an integer greater than zero"
      end
      SecureRandom.hex(bytes)
    end
    
  end
end
<commit_msg>Update facebook_redirect_login_helper.rb<commit_after>require 'securerandom'
require 'addressable/uri'

#TODO: add comments!

module RailsBook
  class FacebookRedirectLoginHelper
    
    def initialize(redirect_url)
      @redirect_url = redirect_url
    end
    
    def get_login_url(scope = [], display_as_popup=false)
      
      puts session[:test]
      
      state = random_bytes(16)
      uri = Addressable::URI.new
      
      uri_params = {
        client_id:      ENV["app_id"],
        redirect_uri:   @redirect_url,
        state:          :state,
        sdk:            RailsBook::SDK_NAME,
        scope:          scope.join(",")
      }
      
      uri_params[:display] = :popup if display_as_popup
      
      uri.query_values = uri_params
      
      return "https://www.facebook.com/" +
              RailsBook::GRAPH_API_VERSION +
              "/dialog/oauth?" +
              uri.query
    end
    
    def get_logout_url(session, next_page)
      if !session.instance_of? FacebookSession
        raise FacebookSDKException.new "not a valid session"
      end
      uri = Addressable::URI.new
      uri_params = {
        next:           next_page,
        access_token:   session.get_token 
      }
      uri.query_values = uri_params
      return "https://www.facebook.com/logout.php?" +
             uri.query
    end
    
    def get_session_from_redirect 
      load_state
      if is_valid_redirect
        response_params = {
          client_id: ENV["app_id"],
          redirect_uri: @redirect_uri,
          client_secret: ENV["app_secret"],
          code: get_code
        }
        facebook_response = {}
      end
      return nil
    end
    
    private 
    
    def is_valid_redirect
      get_code and 
      get_state and
      get_state.eql? get_internal_state
    end
    
    def get_internal_state
      session[:state]
    end
    
    def get_state
      params[:state]
    end
    
    def get_code
      params[:code]
    end
    
    def random_bytes(bytes)
      if !bytes.is_a? Integer
        raise FacebookSDKException.new "random expects an integer"
      end
      if bytes < 1
        raise FacebookSDKException.new "random expects an integer greater than zero"
      end
      SecureRandom.hex(bytes)
    end
    
  end
end

{'fraction_non_alphanumeric': 0.07302142544818539, 'fraction_numerical': 0.0013117621337997377, 'mean_word_length': 1.801592161665646, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4324844', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1354, 'n_words': 290}