<commit_before>module FbGraph
  # = Parse & verify facebook auth cookie
  # 
  # Used with Facebook JavaScript SDK
  # 
  #   app = FbGraph::Auth.new(APP_ID, APP_SECRET)
  #   app.from_cookie(cookie_hash)
  #   auth.access_token
  #   # => OAuth2::AccessToken (not String!)
  #   auth.user # only initialized
  #   auth.user.fetch # fetch whole profile
  # 
  # This method is called automatically if cookie is given when initializing
  # 
  #   auth = FbGraph::Auth.new(APP_ID, APP_SECRET, :cookie => {..})
  #   auth.access_token # already parsed
  class Auth
    class VerificationFailed < Exception; end

    attr_accessor :client, :access_token, :user

    def initialize(client_id, client_secret, options = {})
      @client = OAuth2::Client.new(client_id, client_secret, options.merge(
        :site => ROOT_URL
      ))
      if options[:cookie]
        from_cookie options[:cookie]
      elsif options[:signed_request]
        from_signed_request options[:signed_request]
      end
    end

    def from_cookie(cookie)
      data = Cookie.parse(self.client, cookie)
      self.access_token = build_access_token(data)
      self.user = User.new(data[:uid], :access_token => self.access_token)
      self
    end

    def from_signed_request(signed_request)
      data = SignedRequest.verify(self.client, signed_request)
      self.access_token = build_access_token(data)
      self.user = User.new(data[:user_id], data[:user].merge(:access_token => self.access_token))
      self
    end

    private
<commit_msg>signed_request might not have access token<commit_after>module FbGraph
  # = Parse & verify facebook auth cookie
  # 
  # Used with Facebook JavaScript SDK
  # 
  #   app = FbGraph::Auth.new(APP_ID, APP_SECRET)
  #   app.from_cookie(cookie_hash)
  #   auth.access_token
  #   # => OAuth2::AccessToken (not String!)
  #   auth.user # only initialized
  #   auth.user.fetch # fetch whole profile
  # 
  # This method is called automatically if cookie is given when initializing
  # 
  #   auth = FbGraph::Auth.new(APP_ID, APP_SECRET, :cookie => {..})
  #   auth.access_token # already parsed
  class Auth
    class VerificationFailed < Exception; end

    attr_accessor :client, :access_token, :user, :data

    def initialize(client_id, client_secret, options = {})
      @client = OAuth2::Client.new(client_id, client_secret, options.merge(
        :site => ROOT_URL
      ))
      if options[:cookie]
        from_cookie options[:cookie]
      elsif options[:signed_request]
        from_signed_request options[:signed_request]
      end
    end

    def authorized?
      self.access_token.present?
    end

    def from_cookie(cookie)
      data = Cookie.parse(self.client, cookie)
      self.access_token = build_access_token(data)
      self.user = User.new(data[:uid], :access_token => self.access_token)
      self.data = data
      self
    end

    def from_signed_request(signed_request)
      data = SignedRequest.verify(self.client, signed_request)
      if data[:oauth_token]
        self.access_token = build_access_token(data)
        self.user = User.new(data[:user_id], data[:user].merge(:access_token => self.access_token))
      end
      self.data = data
      self
    end

    private

{'fraction_non_alphanumeric': 0.10192069392812887, 'fraction_numerical': 0.0012391573729863693, 'mean_word_length': 3.082174462705436, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5069950', 'n_tokens_mistral': 1029, 'n_tokens_neox': 1001, 'n_words': 221}