Class: OmniAuth::Strategies::OAuth2

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

Overview

Authentication strategy for connecting with APIs constructed using the [OAuth 2.0 Specification](tools.ietf.org/html/draft-ietf-oauth-v2-10). You must generally register your application with the provider and utilize an application id and secret in order to authenticate using OAuth 2.0.

Defined Under Namespace

Classes: CallbackError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



28
29
30
# File 'lib/omniauth/strategies/oauth2.rb', line 28

def access_token
  @access_token
end

Instance Method Details

#authorize_paramsObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/omniauth/strategies/oauth2.rb', line 50

def authorize_params
  if options.authorize_params[:state].to_s.empty?
    options.authorize_params[:state] = SecureRandom.hex(24)
  end
  params = options.authorize_params.merge(options.authorize_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
  if OmniAuth.config.test_mode
    @env ||= {}
    @env['rack.session'] ||= {}
  end
  session['omniauth.state'] = params[:state]
  params
end

#callback_phaseObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/omniauth/strategies/oauth2.rb', line 67

def callback_phase
  if request.params['error'] || request.params['error_reason']
    raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
  end
  if request.params['state'].to_s.empty? || request.params['state'] != session.delete('omniauth.state')
    raise CallbackError.new(nil, :csrf_detected)
  end

  self.access_token = build_access_token
  self.access_token = access_token.refresh! if access_token.expired?

  super
rescue ::OAuth2::Error, CallbackError => e
  fail!(:invalid_credentials, e)
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)
end

#callback_urlObject



34
35
36
# File 'lib/omniauth/strategies/oauth2.rb', line 34

def callback_url
  full_host + script_name + callback_path
end

#clientObject



30
31
32
# File 'lib/omniauth/strategies/oauth2.rb', line 30

def client
  ::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options))
end

#request_phaseObject



46
47
48
# File 'lib/omniauth/strategies/oauth2.rb', line 46

def request_phase
  redirect client.auth_code.authorize_url({:redirect_uri => callback_url}.merge(authorize_params))
end

#token_paramsObject



63
64
65
# File 'lib/omniauth/strategies/oauth2.rb', line 63

def token_params
  options.token_params.merge(options.token_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
end