Class: Bluevia::Oauth

Inherits:
BaseClient show all
Defined in:
lib/bluevia/oauth.rb

Overview

This class can be used to launch oAuth authentication mechanism when a user is using the application for the first time

User authentication is launched using oAuth protocol, so user is not required to use credentials in third party applications. If you want to learn more about oAuth please check this URL. When user wants to launch the oAuth process, once the Bluevia client has been created only the two lines below are required to retrieve a valid token for user:

@service = @bc.get_service(:oAuth)
token, secret, url = @service.get_request_token({:callback =>"http://foo.bar"})

The retrieved parameter token and secret should be use during the oAuth process, and url is the endpoint where Bluevia shall authenticate the user. In case of a Rails application, the lines below could be used:

token, token_secret, url = @service.get_request_token("http://juan.pollinimini.net/bluevia/get_access")
cookies[:token] = "#{token}|#{token_secret}"
redirect_to(url)

Once user is authenticated and she authorized the application in BlueVia portal, she should be redirected to the URL used as parameter before. Now it’s time to fetch the valid token and token secret that shall identify the new user during any call to BlueVia API. Lines below show an example using Rails:

def get_access
   oauth_verifier = params[:oauth_verifier]
   get_token_from_cookie
   @bc = BlueviaClient.new(
     { :consumer_key   => CONSUMER_KEY,
       :consumer_secret=> CONSUMER_SECRET
     })
   @service = @bc.get_service(:oAuth)
   @token, @token_secret = @service.get_access_token(@request_token, @request_secret, oauth_verifier)
end

private
 def get_token_from_cookie
   cookie_token = cookies[:token]
   unless cookie_token.nil?
     cookie_token = cookie_token.split("|")
     if cookie_token.size != 2
       raise SyntaxError, "The cookie is not valid"
     end
     @request_token = cookie_token[0]
     @request_secret = cookie_token[1]
   end
 end

Constant Summary collapse

AUTHORIZE_URI =
"http://connect.bluevia.com/authorise/"

Constants inherited from BaseClient

BaseClient::BASEPATH, BaseClient::BASEPATH_COMMERCIAL, BaseClient::BASEPATH_SANDBOX, BaseClient::BASEURI, BaseClient::DEFAULT_PARAMS, BaseClient::PROXY

Instance Attribute Summary

Attributes inherited from BaseClient

#base_uri

Instance Method Summary collapse

Methods inherited from BaseClient

#DELETE, #GET, #POST, #[]=, #authorized_client, create_rest_client, #get_basepath, #get_headers, #include_params, #set_http_client, #set_path, #set_timeout

Methods included from BlueviaLogger

#create_logger, #log_level=, #logger, #logger=

Constructor Details

#initialize(params = nil) ⇒ Oauth

Returns a new instance of Oauth.



57
58
59
# File 'lib/bluevia/oauth.rb', line 57

def initialize(params = nil)
  super(params)
end

Instance Method Details

#get_access_token(token, token_secret, oauth_verifier) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bluevia/oauth.rb', line 97

def get_access_token(token, token_secret, oauth_verifier)
  begin
    consumer = OAuth::Consumer.new \
      @consumer_key, @consumer_secret,
      {
        :site               => BASEURI,
        :signature_method   => "HMAC-SHA1",
        :request_token_path => "#{BASEPATH}/Oauth/getRequestToken",
        :access_token_path  => "#{BASEPATH}/Oauth/getAccessToken",
        :http_method        => :post
      }
    request_token = OAuth::RequestToken.new(consumer, token, token_secret)
    access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier)

    return access_token.params[:oauth_token], access_token.params[:oauth_token_secret]
  rescue => ex
    return nil, nil # error
  end
end

#get_request_token(_params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bluevia/oauth.rb', line 61

def get_request_token(_params)
  
  consumer=OAuth::Consumer.new \
    @consumer_key,
    @consumer_secret,
    {
      :site               => @@base_uri,
      :signature_method   => "HMAC-SHA1",
      :request_token_path => "#{BASEPATH}/Oauth/getRequestToken",
      :access_token_path  => "#{BASEPATH}/Oauth/getAccessToken",
      #:proxy              => "http://localhost:8888",
      :http_method        => :post
    }

  params = Hash.new
  specific_params = Hash.new
  if _params.instance_of?(String)
    params[:oauth_callback] = _params
    uri = AUTHORIZE_URI
  elsif _params.instance_of?(Hash)
    if _params.has_key?(:callback)
      params[:oauth_callback] = _params[:callback]
    else
      Raise SyntaxError, "Callback parameter must be provided"
    end
    if _params.has_key?(:uri)
      uri = _params[:uri]
    else
      uri = AUTHORIZE_URI
    end
  end
  request_token = consumer.get_request_token(params, {:v => "1"}, specific_params)
  return request_token.token, request_token.secret, "#{uri}?oauth_token=#{request_token.token}"

end