Class: OmniAuth::Strategies::Deezer

Inherits:
Object
  • Object
show all
Includes:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategies/deezer.rb

Defined Under Namespace

Classes: CredentialsError

Constant Summary collapse

DEFAULT_PERMS =
"basic_access"

Instance Method Summary collapse

Instance Method Details

#callback_phaseObject

callback phase



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/omniauth/strategies/deezer.rb', line 37

def callback_phase
  begin
    if request.params['error_reason'] then
      raise CredentialsError, request.params['error_reason']
    end

    # get token from Deezer
    token_url = "#{options.client_options.token_url}?app_id=#{options.app_id}&secret=#{options.app_secret}&code=#{request.params['code']}"

    connection = nil
    if options.client_options.ssl && options.client_options.ssl.ca_path then
      connection = Faraday::Connection.new token_url, :ssl => {:ca_path => options.client_options.ssl.ca_path }
    else
      connection = Faraday::Connection.new token_url
    end
    response = connection.get token_url

    response_hash = CGI::parse(response.body.chomp)
    @access_token = response_hash['access_token'][0]
    expires_in = response_hash['expires'][0].to_i
    @token_expires_at = Time.now + expires_in if expires_in > 0

    # get info from current user
    me_path = options.client_options.me_url+'?access_token='+@access_token
    response = Faraday.get me_path
    @raw_info = MultiJson.decode(response.body.chomp)

    super
  rescue ::MultiJson::DecodeError => e
    fail!(:invalid_response, e)
  rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
    fail!(:timeout, e)
  rescue ::SocketError => e
    fail!(:failed_to_connect, e)
  rescue TypeError => e
    fail!(:invalid_response, e)
  rescue CredentialsError => e
    fail!(:invalid_credentials, e)
  end
end

#request_phaseObject



28
29
30
31
32
33
# File 'lib/omniauth/strategies/deezer.rb', line 28

def request_phase
  options.perms ||= DEFAULT_PERMS
  new_url_to_go = callback_url.split('?')[0]

  redirect "#{options.client_options.authorize_url}?app_id=#{options.app_id}&redirect_uri=#{CGI.escape(new_url_to_go)}&perms=#{options.perms}"
end