class SparkApi::Authentication::OAuth2
OAuth2
¶ ↑
Implementation the BaseAuth interface for API style authentication
Attributes
client[R]
provider[R]
Public Class Methods
new(client)
click to toggle source
Calls superclass method
SparkApi::Authentication::BaseAuth::new
# File lib/spark_api/authentication/oauth2.rb, line 23 def initialize(client) super(client) @provider = client.oauth2_provider end
Public Instance Methods
authenticate()
click to toggle source
# File lib/spark_api/authentication/oauth2.rb, line 35 def authenticate granter = OAuth2Impl::GrantTypeBase.create(@client, @provider, session) self.session = granter.authenticate session end
logout()
click to toggle source
# File lib/spark_api/authentication/oauth2.rb, line 64 def logout @provider.save_session(nil) end
request(method, path, body, options={})
click to toggle source
Perform an HTTP request (no data)
# File lib/spark_api/authentication/oauth2.rb, line 42 def request(method, path, body, options={}) escaped_path = Addressable::URI.escape(path) connection = @client.connection(true) # SSL Only! connection.headers.merge!(options.delete(:override_headers) || {}) connection.headers.merge!(self.auth_header) unless (@client.api_user.nil? || options[:ApiUser]) options.merge!(:ApiUser => "#{@client.api_user}") end parameter_string = options.size > 0 ? "?#{build_url_parameters(options)}" : "" request_path = "#{escaped_path}#{parameter_string}" SparkApi.logger.debug { "Request: #{request_path}" } if body.nil? response = connection.send(method, request_path) else SparkApi.logger.debug { "Data: #{body}" } response = connection.send(method, request_path, body) end response end
session()
click to toggle source
# File lib/spark_api/authentication/oauth2.rb, line 28 def session @provider.load_session() end
session=(s)
click to toggle source
# File lib/spark_api/authentication/oauth2.rb, line 31 def session=(s) @provider.save_session(s) end
sparkbar_token()
click to toggle source
Create a sparkbar token based on the current user's access token
# File lib/spark_api/authentication/oauth2.rb, line 78 def sparkbar_token() raise ClientError, "OAuth2Provider must configure the sparkbar_uri to use sparkbar tokens" if provider.sparkbar_uri.nil? SparkApi.logger.debug { "[sparkbar] create token to #{provider.sparkbar_uri}" } uri = URI.parse(provider.sparkbar_uri) request_path = "#{uri.path}" SparkApi.logger.info { "[sparkbar] create token to #{request_path}, #{session.access_token.inspect}" } response = sparkbar_connection("#{uri.scheme}://#{uri.host}").post(request_path, "access_token=#{session.access_token}").body token = response["token"] SparkApi.logger.debug { "[sparkbar] New token created #{token}" } token end
Protected Instance Methods
auth_header()
click to toggle source
# File lib/spark_api/authentication/oauth2.rb, line 95 def auth_header {"Authorization"=> "OAuth #{session.access_token}"} end
sparkbar_connection(endpoint)
click to toggle source
Faraday handle to the sparkbar
# File lib/spark_api/authentication/oauth2.rb, line 100 def sparkbar_connection(endpoint) opts = { :headers => client.headers } opts[:headers].delete(:content_type) opts[:ssl] = {:verify => false } unless @client.ssl_verify opts[:url] = endpoint conn = Faraday::Connection.new(opts) do |conn| conn.response :sparkbar_impl conn.adapter Faraday.default_adapter end end