Sha256: 05411fa132ff8fe7a8006f6aedca805550a6e6e04d38da62ca22bbfa8b843b4d

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

require 'fb/request'
require 'fb/configuration'
# Ruby client to authenticate a Facebook user.
# @see http://www.rubydoc.info/gems/Fb/
module Fb
  # Provides methods to authenticate a user with the Facebook OAuth flow.
  # @see https://developers.facebook.com/docs/facebook-login
  class Auth
    # @param [Hash] options the options to initialize an instance of Fb::Auth.
    # @option [String] :redirect_uri The URI to redirect users to
    #   after they have completed the Facebook OAuth flow.
    # @option [String] :code A single-use authorization code provided
    #   by Facebook OAuth to obtain an access token.
    def initialize(options = {})
      @redirect_uri = options[:redirect_uri]
      @code = options[:code]
    end

    # @return [String] a url to Facebook's account authentication.
    def url
      Fb::Request.new(url_options).url
    end

    # @return [String] the non-expiring access token of an authenticated Facebook account.
    def access_token
      response_body = Fb::Request.new(path: '/oauth/access_token',
      params: long_term_token_params).run
      response_body["access_token"]
    end

  private

    def short_term_access_token
      response_body = Fb::Request.new(path: '/oauth/access_token',
      params: short_term_token_params).run
      response_body["access_token"]
    end

    def url_options
      url_params = {scope: 'email,manage_pages', redirect_uri: @redirect_uri}
      {host: 'www.facebook.com', path: '/dialog/oauth', params: url_params}
    end

    def short_term_token_params
      {}.tap do |params|
        params[:client_secret] = Fb.configuration.client_id
        params[:redirect_uri] = @redirect_uri
        params[:code] = @code
      end
    end

    def long_term_token_params
      {}.tap do |params|
        params[:client_secret] = Fb.configuration.client_secret
        params[:grant_type] = :fb_exchange_token
        params[:fb_exchange_token] = short_term_access_token
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fb-auth-0.1.2 lib/fb/auth.rb
fb-auth-0.1.1 lib/fb/auth.rb
fb-auth-0.1.0 lib/fb/auth.rb