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



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

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



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

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
# File 'lib/spark_api/authentication/oauth2.rb', line 42

def request(method, path, body, options={})
  escaped_path = URI.escape(path)
  connection = @client.connection(true)  # SSL Only!
  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:



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/spark_api/authentication/oauth2.rb', line 77

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