module Applidget::Oauth2

Constants

VERSION

Public Instance Methods

api_response(options, params) click to toggle source
# File lib/applidget/oauth2.rb, line 33
def api_response(options, params)
  @options = options
  @params = params
  if check_csrf
    @access_token = build_access_token
    @access_token.get(@options[:api]).parsed
  end
end
request_uri(options) click to toggle source

Any Oauth2 protocol with Applidget Accounts should be implemented by inheriting from this controller. You should provide a method '@options' that defines a hash with the right parameters, e.g. :

def @options
  {
    model: "guest",
    api: "/api/v1/me.json",
    request_params: { hd: params[:hd], auth: params[:auth], scope: "public" },
    callback_url: generic_url_from callback_guests_auth_applidget_accounts_path,
    client_id: "785439208457639203847539208374",
    client_secret: "7468539205733452975829047568892"
  }
end

You should also override callback method : the parsed response from the api will be given by calling the super method, e.g. :

def callback
  guest_hash = super
  # your code ...
end
# File lib/applidget/oauth2.rb, line 28
def request_uri(options)
  @options = options
  client.auth_code.authorize_url({:redirect_uri => @options[:callback_url]}.merge(request_params))
end

Private Instance Methods

build_access_token() click to toggle source
# File lib/applidget/oauth2.rb, line 48
def build_access_token
  client.auth_code.get_token(@params['code'], {:redirect_uri => @options[:callback_url]}, {})
end
check_csrf() click to toggle source
# File lib/applidget/oauth2.rb, line 59
def check_csrf
  state = @params[:state]
  state != cookies.delete("oauth2.csrf_token")
end
client() click to toggle source
# File lib/applidget/oauth2.rb, line 44
def client
  @client ||= ::OAuth2::Client.new(@options[:client_id], @options[:client_secret], { :site => "https://accounts.applidget.com" })
end
request_params() click to toggle source
# File lib/applidget/oauth2.rb, line 64
def request_params
  state = set_csrf_token
  @options[:request_params].merge({state: state})
end
set_csrf_token() click to toggle source
# File lib/applidget/oauth2.rb, line 52
def set_csrf_token
  csrf_token = SecureRandom.urlsafe_base64(15).tr('lIO0', 'sxyz')
  state = csrf_token #TODO: embed other information here if necessary
  cookies["oauth2.csrf_token"] = state
  state
end