Sha256: 5a6ae90c5f7623946dce8c5819fec5a458015e0d7689ce613a2af608b26ebea3

Contents?: true

Size: 1.89 KB

Versions: 43

Compression:

Stored size: 1.89 KB

Contents

require 'rest_client'
require 'json'
require 'base64'

module PureCloud
  class << self
    # Authenticates to PureCloud using the client credientals OAuth grant.
    #
    # @param client_id OAuth client id
    # @param client_secret OAuth client secret
    # @param environment PureCloud environment (mypurecloud.com, mypurecloud.ie, mypurecloud.com.au, etc)
    def authenticate_with_client_credentials(client_id, client_secret, environment = nil)
      environment ||= "mypurecloud.com"
      self.configure.host = 'api.' + environment;

      basic = Base64.strict_encode64("#{client_id}:#{client_secret}")

      tokenData = JSON.parse RestClient.post("https://login.#{environment}/token",
                              {:grant_type => 'client_credentials'},
                              :Authorization => "Basic " + basic,
                              'content-type'=> 'application/x-www-form-urlencoded',
                              :accept => :json)
      self.configure.access_token = tokenData["access_token"]
    end

    # Retrieves an Access token given an authorization code (authorization code grant)
    #
    # @param auth_code Authorization code from the OAuth redirec
    # @param client_id OAuth client id
    # @param client_secret OAuth client secret
    # @param client_secret OAuth redirect URI
    # @param environment (Optional) PureCloud environment (mypurecloud.com, mypurecloud.ie, mypurecloud.com.au, etc)
    def get_access_token_from_auth_code(auth_code, client_id, client_secret, redirect_uri, environment = nil)
      environment ||= "mypurecloud.com"

      tokenFormData = {
        "grant_type" => "authorization_code",
        "code" => auth_code,
        "redirect_uri" => redirect_uri
      }

      tokenResponse =JSON.parse RestClient.post "https://#{client_id}:#{client_secret}@login.#{environment}/token", tokenFormData

      return tokenResponse['access_token'];
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
purecloud-0.70.1 extensions/auth.rb
purecloud-0.69.1 extensions/auth.rb
purecloud-0.68.1 extensions/auth.rb
purecloud-0.67.1 extensions/auth.rb
purecloud-0.66.1 extensions/auth.rb
purecloud-0.65.1 extensions/auth.rb
purecloud-0.64.1 extensions/auth.rb
purecloud-0.63.1 extensions/auth.rb
purecloud-0.62.1 extensions/auth.rb
purecloud-0.61.1 extensions/auth.rb
purecloud-0.60.1 extensions/auth.rb
purecloud-0.59.1 extensions/auth.rb
purecloud-0.58.1 extensions/auth.rb
purecloud-0.57.1 extensions/auth.rb
purecloud-0.56.1 extensions/auth.rb
purecloud-0.55.1 extensions/auth.rb
purecloud-0.54.1 extensions/auth.rb
purecloud-0.53.1 extensions/auth.rb
purecloud-0.52.1 extensions/auth.rb
purecloud-0.51.1 extensions/auth.rb