Class: Clavis::Providers::Facebook

Inherits:
Base
  • Object
show all
Defined in:
lib/clavis/providers/facebook.rb

Constant Summary collapse

FACEBOOK_API_VERSION =

Updated to use the latest stable Facebook API version

"v19.0"

Instance Attribute Summary

Attributes inherited from Base

#authorize_endpoint_url, #client_id, #client_secret, #provider_name, #redirect_uri, #scope, #token_endpoint_url, #userinfo_endpoint_url

Instance Method Summary collapse

Methods inherited from Base

#openid_provider?, #process_callback, #token_exchange

Methods included from TokenExchangeHandler

#build_token_exchange_params, #handle_connection_error, #handle_error_response, #handle_faraday_error, #handle_parser_error, #handle_standard_error, #make_token_request, #parse_response, #skip_error_for_test?, #test_token_response, #validate_and_clean_code

Constructor Details

#initialize(config = {}) ⇒ Facebook

Returns a new instance of Facebook.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/clavis/providers/facebook.rb', line 9

def initialize(config = {})
  config[:authorization_endpoint] = "https://www.facebook.com/#{FACEBOOK_API_VERSION}/dialog/oauth"
  config[:token_endpoint] = "https://graph.facebook.com/#{FACEBOOK_API_VERSION}/oauth/access_token"
  config[:userinfo_endpoint] = "https://graph.facebook.com/#{FACEBOOK_API_VERSION}/me"
  config[:scope] = config[:scope] || "email public_profile"
  # Store additional Facebook-specific options
  @image_size = config[:image_size] || {}
  @display = config[:display]
  @auth_type = config[:auth_type]
  @secure_image_url = config.fetch(:secure_image_url, true)
  super
end

Instance Method Details

#authorization_endpointObject



22
23
24
# File 'lib/clavis/providers/facebook.rb', line 22

def authorization_endpoint
  "https://www.facebook.com/#{FACEBOOK_API_VERSION}/dialog/oauth"
end

#authorize_url(state:, nonce:, scope: nil) ⇒ Object

Override authorize_url to add Facebook-specific parameters



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/clavis/providers/facebook.rb', line 39

def authorize_url(state:, nonce:, scope: nil)
  url = super

  # Add Facebook-specific parameters if present
  params = {}
  params[:display] = @display if @display
  params[:auth_type] = @auth_type if @auth_type

  # Append additional parameters if any were added
  if params.any?
    uri = URI.parse(url)
    existing_params = URI.decode_www_form(uri.query || "").to_h
    all_params = existing_params.merge(params)
    uri.query = URI.encode_www_form(all_params)
    uri.to_s
  else
    url
  end
end

#default_scopesObject



34
35
36
# File 'lib/clavis/providers/facebook.rb', line 34

def default_scopes
  "email public_profile"
end

#exchange_for_long_lived_token(access_token) ⇒ Object

Exchanges short-lived token for a long-lived token



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/clavis/providers/facebook.rb', line 102

def exchange_for_long_lived_token(access_token)
  params = {
    grant_type: "fb_exchange_token",
    client_id: client_id,
    client_secret: client_secret,
    fb_exchange_token: access_token
  }

  response = http_client.get("#{token_endpoint}?#{to_query(params)}")

  if response.status != 200
    Clavis::Logging.log_custom("facebook_long_lived_token_exchange", false)
    handle_token_error_response(response)
  end

  Clavis::Logging.log_custom("facebook_long_lived_token_exchange", true)
  parse_token_response(response)
end

#get_user_info(access_token) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/clavis/providers/facebook.rb', line 81

def (access_token)
  # Facebook requires fields parameter to specify what data to return
  # Enhanced with more fields for richer user profiles
  fields = "id,name,email,first_name,last_name,picture,link,verified,location,age_range,birthday,gender"

  response = http_client.get(userinfo_endpoint) do |req|
    req.params["access_token"] = access_token
    req.params["fields"] = fields
    req.params["appsecret_proof"] = generate_appsecret_proof(access_token)
  end

  if response.status != 200
    Clavis::Logging.log_userinfo_request(provider_name, false)
    handle_userinfo_error_response(response)
  end

  Clavis::Logging.log_userinfo_request(provider_name, true)
  process_userinfo_response(response)
end

#refresh_token(access_token) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/clavis/providers/facebook.rb', line 59

def refresh_token(access_token)
  params = {
    grant_type: "fb_exchange_token",
    client_id: client_id,
    client_secret: client_secret,
    fb_exchange_token: access_token
  }

  # Add appsecret_proof for enhanced security
  params[:appsecret_proof] = generate_appsecret_proof(access_token)

  response = http_client.get("#{token_endpoint}?#{to_query(params)}")

  if response.status != 200
    Clavis::Logging.log_token_refresh(provider_name, false)
    handle_token_error_response(response)
  end

  Clavis::Logging.log_token_refresh(provider_name, true)
  parse_token_response(response)
end

#token_endpointObject



26
27
28
# File 'lib/clavis/providers/facebook.rb', line 26

def token_endpoint
  "https://graph.facebook.com/#{FACEBOOK_API_VERSION}/oauth/access_token"
end

#userinfo_endpointObject



30
31
32
# File 'lib/clavis/providers/facebook.rb', line 30

def userinfo_endpoint
  "https://graph.facebook.com/#{FACEBOOK_API_VERSION}/me"
end