Class: Facepalm::User

Inherits:
Object
  • Object
show all
Defined in:
lib/facepalm/user.rb

Overview

A class for Facebook user

Defined Under Namespace

Classes: InvalidSignature, UnsupportedAlgorithm

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ User

Returns a new instance of User.



43
44
45
# File 'lib/facepalm/user.rb', line 43

def initialize(options = {})
  @options = options
end

Class Method Details

.base64_url_decode(str) ⇒ Object



35
36
37
38
39
# File 'lib/facepalm/user.rb', line 35

def base64_url_decode(str)
  str += '=' * (4 - str.length.modulo(4))

  Base64.decode64(str.tr('-_', '+/'))
end

.from_signed_request(config, input) ⇒ Object

Creates an instance of Facepalm::User using application config and signed_request



10
11
12
13
14
# File 'lib/facepalm/user.rb', line 10

def from_signed_request(config, input)
  return if input.blank?

  new(parse_signed_request(config, input))
end

.parse_signed_request(config, input) ⇒ Object

Originally provided directly by Facebook, however this has changed as their concept of crypto changed. For historic purposes, this is their proposal: developers.facebook.com/docs/authentication/canvas/encryption_proposal/ Currently see github.com/facebook/php-sdk/blob/master/src/facebook.php#L758 for a more accurate reference implementation strategy.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/facepalm/user.rb', line 21

def parse_signed_request(config, input)
  encoded_sig, encoded_envelope = input.split('.', 2)
  signature = base64_url_decode(encoded_sig).unpack("H*").first

  MultiJson.decode(base64_url_decode(encoded_envelope)).tap do |envelope|
    raise UnsupportedAlgorithm.new("Unsupported encryption algorithm: #{ envelope['algorithm'] }") unless envelope['algorithm'] == 'HMAC-SHA256'

    # now see if the signature is valid (digest, key, data)
    hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, config.secret, encoded_envelope)

    raise InvalidSignature.new('Invalid request signature') if (signature != hmac)
  end
end

Instance Method Details

#access_tokenObject

OAuth 2.0 access token generated for this user



63
64
65
# File 'lib/facepalm/user.rb', line 63

def access_token
  @options['access_token'] || @options['oauth_token']
end

#access_token_expires_atObject

Token expiration time



68
69
70
# File 'lib/facepalm/user.rb', line 68

def access_token_expires_at
  Time.at(@options['expires'])
end

#api_clientObject

Koala Facebook API client instantiated with user’s access token



73
74
75
# File 'lib/facepalm/user.rb', line 73

def api_client
  @api_client ||= Koala::Facebook::API.new(access_token)
end

#authenticated?Boolean

Checks if user is authenticated in the application

Returns:

  • (Boolean)


48
49
50
# File 'lib/facepalm/user.rb', line 48

def authenticated?
  access_token && !access_token.empty?
end

#oauth_codeObject

The code used for OAuth 2.0



58
59
60
# File 'lib/facepalm/user.rb', line 58

def oauth_code
  @options['code']
end

#uidObject

Facebook UID



53
54
55
# File 'lib/facepalm/user.rb', line 53

def uid
  @options['user_id']
end