Sha256: 20d55d81f0e61db05b54eedb2691d0df8613cafd069b0ee3488d093fce494bdd

Contents?: true

Size: 1.7 KB

Versions: 55

Compression:

Stored size: 1.7 KB

Contents

module SparkApi
  module Authentication
    module OAuth2Impl
      # OAuth2 authentication flow using username and password parameters for the user in the 
      # request.  This implementation is geared towards authentication styles for web applications
      # that have a OAuth flow for redirects.
      class GrantTypeCode < GrantTypeBase
        def initialize(client, provider, session)
          super(client, provider, session)
        end
        def authenticate
          if(provider.code.nil?)
            SparkApi.logger.debug { "[oauth2] No authoriztion code present. Redirecting to #{authorization_url}." }
            provider.redirect(authorization_url)
          end
          if needs_refreshing?
            new_session = refresh
          end
          return new_session unless new_session.nil?
          create_session(token_params)
        end
        
        def refresh()
          SparkApi.logger.debug { "[oauth2] Refresh oauth session." }
          refresher = GrantTypeRefresh.new(client,provider,session)
          refresher.params = {"redirect_uri" => @provider.redirect_uri}
          refresher.authenticate
        rescue ClientError => e
          SparkApi.logger.info { "[oauth2] Refreshing token failed, the library will try and authenticate from scratch: #{e.message}" }
          nil
        end

        private 
        def token_params
          hash = {
           "client_id" => @provider.client_id,
           "client_secret" => @provider.client_secret,
           "code" => @provider.code,
           "grant_type" => "authorization_code",
           "redirect_uri" => @provider.redirect_uri
         }
         MultiJson.dump(hash)
        end
        
      end
      
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
spark_api-2.0.0 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.6.3 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.6.2 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.6.1 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.5.7 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.6.0 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.5.6 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.5.5 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.5.4 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.5.3 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.5.2 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.5.1 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.5.0 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.4.34 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.4.32 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.4.31 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.4.29 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.4.28 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.4.27 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb
spark_api-1.4.26 lib/spark_api/authentication/oauth2_impl/grant_type_code.rb