Sha256: 138de2dcef22e7779b6261e7e4f03551aecc521960afd6c341ae8abe1efbba95

Contents?: true

Size: 1.01 KB

Versions: 14

Compression:

Stored size: 1.01 KB

Contents

require 'net/http'
require 'json'

module Unimatrix::Authorization
  class ClientCredentialsGrant

    def initialize( args )
      @client_id = args[ :client_id ]
      @client_secret = args[ :client_secret ]
    end

    def request_token
      uri      = URI.parse( "#{ Unimatrix.configuration.authorization_url }/token" )
      params   = { "grant_type" => "client_credentials" }
      http     = Net::HTTP.new( uri.host, uri.port )
      request  = Net::HTTP::Post.new( uri.request_uri )

      http.use_ssl = true if uri.scheme == 'https'

      request.basic_auth( @client_id, @client_secret )
      request.set_form_data( params )

      begin
        response = http.request( request )

        if response.code == '200'
          body = JSON.parse( response.body )
          body = body[ 'token' ] if body[ 'token' ].present?

          body[ 'access_token' ] rescue nil
        else
          puts "ERROR: #{ response.body }"
        end
      rescue => e
        puts "REQUEST FAILED: #{ e }"
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
unimatrix-2.9.0 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.8.0 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.7.1 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.7.0 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.6.1 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.6.0 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.5.5 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.5.4 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.5.3 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.5.2 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.5.1 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.5.0 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.4.2 lib/unimatrix/authorization/client_credentials_grant.rb
unimatrix-2.3.1 lib/unimatrix/authorization/client_credentials_grant.rb