Class: SparkApi::Authentication::OAuth2
- Defined in:
- lib/spark_api/authentication/oauth2.rb
Overview
OAuth2
Implementation the BaseAuth interface for API style authentication
Direct Known Subclasses
Instance Method Summary collapse
- #authenticate ⇒ Object
- #authorization_url ⇒ Object
-
#initialize(client) ⇒ OAuth2
constructor
A new instance of OAuth2.
- #logout ⇒ Object
-
#request(method, path, body, options = {}) ⇒ Object
Perform an HTTP request (no data).
- #session ⇒ Object
- #session=(s) ⇒ Object
-
#sparkbar_token ⇒ Object
Create a sparkbar token based on the current user’s access token.
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
#authenticate ⇒ Object
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_url ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/spark_api/authentication/oauth2.rb', line 68 def () params = { "client_id" => @provider.client_id, "response_type" => "code", "redirect_uri" => @provider.redirect_uri } "#{@provider.}?#{build_url_parameters(params)}" end |
#logout ⇒ Object
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, ={}) escaped_path = Addressable::URI.escape(path) connection = @client.connection(true) # SSL Only! connection.headers.merge!(.delete(:override_headers) || {}) connection.headers.merge!(self.auth_header) unless (@client.api_user.nil? || [:ApiUser]) .merge!(:ApiUser => "#{@client.api_user}") end parameter_string = .size > 0 ? "?#{build_url_parameters()}" : "" 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 ⇒ Object
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_token ⇒ Object
Create a sparkbar token based on the current user’s access token
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/spark_api/authentication/oauth2.rb', line 78 def () raise ClientError, "OAuth2Provider must configure the sparkbar_uri to use sparkbar tokens" if provider..nil? SparkApi.logger.debug { "[sparkbar] create token to #{provider.}" } uri = URI.parse(provider.) request_path = "#{uri.path}" SparkApi.logger.info { "[sparkbar] create token to #{request_path}, #{session.access_token.inspect}" } response = ("#{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 |