Sha256: afd3ac7c2cc113efc9cb3ffabd16d4d43a74f0c1b9c6d6cac945c089a5da2fc0

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

# :namespace
module Credentials
  
# Associates a Facebook account and OAuth2 token with an account.
class Facebook < ::Credential
  # The Graph API object ID of the Facebook account.
  alias_attribute :facebook_uid, :name
  validates :name, :format => /^\d+$/, :presence => true,
       :uniqueness => { :scope => [:type],
       :message => 'Your Facebook user is already associated to an account' }

  # A user can be associated to a single Facebook account.
  validates :user_id, :uniqueness => {
      :message => 'Your account is already associated to a Facebook user' }

  # OAuth2 token issued by Facebook.
  alias_attribute :access_token, :key
  validates :key, :presence => true

  # FBGraph client loaded with this access token.
  def facebook_client
    @client ||= FBGraphRails.fbclient(access_token)
  end  

  # Finds or creates the model containing a token.
  #
  # If a model for the same user exists, the model is updated with the given
  # token. Otherwise, a new model will be created, together with a user.
  def self.for(access_token)
    uid = uid_from_token access_token
    credential = self.where(:name => uid.to_str).first
    if credential
      credential.key = access_token
      credential.save!
    else
      User.transaction do
        user = User.create!
        credential = self.new
        user.credentials << credential
        credential.facebook_uid = uid
        credential.access_token = access_token
        credential.save!
      end
    end
    credential
  end
  
  # Extracts the Facebook user ID from a OAuth2 token.
  #
  # This used to be a hack that pulled the UID out of an OAuth2 token. The new
  # encrypted OAuth2 tokens don't have UIDs anymore, so this method is an
  # interim hack for old code that still depends on it.
  def self.uid_from_token(access_token)
    FBGraphRails.fbclient(access_token).selection.me.info!.id.to_s
  end
  
  # Forms should not be able to touch any attribute.
  attr_accessible
end  # class Credentials::Facebook 

end  # namespace Credentials

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
authpwn_rails-0.10.7 app/models/credentials/facebook.rb
authpwn_rails-0.10.6 app/models/credentials/facebook.rb
authpwn_rails-0.10.5 app/models/credentials/facebook.rb
authpwn_rails-0.10.4 app/models/credentials/facebook.rb
authpwn_rails-0.10.3 app/models/credentials/facebook.rb