Class: SparkApi::Authentication::OAuth2

Inherits:
BaseAuth
  • Object
show all
Defined in:
lib/spark_api/authentication/oauth2.rb

Overview

OAuth2

Implementation the BaseAuth interface for API style authentication

Direct Known Subclasses

OpenIdOAuth2Hybrid

Instance Method Summary collapse

Methods inherited from BaseAuth

#authenticated?, #build_url_parameters

Constructor Details

#initialize(client) ⇒ OAuth2

Returns a new instance of OAuth2.



23
24
25
26
# File 'lib/spark_api/authentication/oauth2.rb', line 23

def initialize(client)
  super(client)
  @provider = client.oauth2_provider
end

Instance Method Details

#authenticateObject



35
36
37
38
39
# File 'lib/spark_api/authentication/oauth2.rb', line 35

def authenticate
  granter = OAuth2Impl::GrantTypeBase.create(@client, @provider, session)
  self.session = granter.authenticate
  session
end

#authorization_urlObject



68
69
70
71
72
73
74
75
# File 'lib/spark_api/authentication/oauth2.rb', line 68

def authorization_url()
  params = {
    "client_id" => @provider.client_id,
    "response_type" => "code",
    "redirect_uri" => @provider.redirect_uri
  }
  "#{@provider.authorization_uri}?#{build_url_parameters(params)}"
end

#logoutObject



64
65
66
# File 'lib/spark_api/authentication/oauth2.rb', line 64

def logout
  @provider.save_session(nil)
end

#request(method, path, body, options = {}) ⇒ Object

Perform an HTTP request (no data)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

#sessionObject



28
29
30
# File 'lib/spark_api/authentication/oauth2.rb', line 28

def session
  @provider.load_session()
end

#session=(s) ⇒ Object



31
32
33
# File 'lib/spark_api/authentication/oauth2.rb', line 31

def session=(s)
  @provider.save_session(s)
end

#sparkbar_tokenObject

Create a sparkbar token based on the current user’s access token

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
# 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