Class: CheckoutSdk::OAuthSdkCredentials

Inherits:
SdkCredentials show all
Defined in:
lib/checkout_sdk/oauth_sdk_credentials.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, scopes, http_client, environment, logger, auth_uri = nil) ⇒ OAuthSdkCredentials

Returns a new instance of OAuthSdkCredentials.

Parameters:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 34

def initialize(client_id, client_secret, scopes, http_client, environment, logger, auth_uri = nil)
  validate_arguments client_id, client_secret, environment, auth_uri
  @client_id = client_id
  @client_secret = client_secret
  @scopes = scopes
  @authorization_uri = auth_uri.nil? ? environment.authorization_uri : auth_uri
  @oauth_http_client = http_client.clone
  @oauth_http_client.url_prefix = @authorization_uri
  @log = logger
  build_access_token
end

Instance Attribute Details

#access_tokenCheckoutSdk::OAuthAccessToken



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 18

class OAuthSdkCredentials < SdkCredentials
  attr_accessor :access_token,
                :client_id,
                :client_secret,
                :scopes,
                :http_client,
                :environment,
                :authorization_uri,
                :log

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Array(CheckoutSdk::OAuthScopes)] scopes
  # @param [Faraday::Connection] http_client
  # @param [CheckoutSdk::Environment] environment
  # @param [String, nil] auth_uri
  def initialize(client_id, client_secret, scopes, http_client, environment, logger, auth_uri = nil)
    validate_arguments client_id, client_secret, environment, auth_uri
    @client_id = client_id
    @client_secret = client_secret
    @scopes = scopes
    @authorization_uri = auth_uri.nil? ? environment.authorization_uri : auth_uri
    @oauth_http_client = http_client.clone
    @oauth_http_client.url_prefix = @authorization_uri
    @log = logger
    build_access_token
  end

  # @param [String] authorization_type
  # @return [CheckoutSdk::SdkAuthorization]
  def get_authorization(authorization_type)
    case authorization_type
    when AuthorizationType::SECRET_KEY_OR_OAUTH, AuthorizationType::PUBLIC_KEY_OR_OAUTH, AuthorizationType::OAUTH
      SdkAuthorization.new(PlatformType::DEFAULT, build_access_token.token)
    else
      raise CheckoutAuthorizationException.invalid_authorization authorization_type
    end
  end

  # @return [CheckoutSdk::OAuthAccessToken]
  def build_access_token
    return @access_token unless @access_token.nil? || @access_token.token.nil? || !@access_token.valid?

    data = {
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials',
      scope: @scopes.join(' ')
    }

    headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
    body = URI.encode_www_form data

    begin
      @log.info "post: #{authorization_uri}"
      response = @oauth_http_client.run_request(:post, @authorization_uri, body, headers)
    rescue Faraday::ClientError => e
      raise CheckoutAuthorizationException, e.response
    rescue Faraday::ConnectionFailed => e
      raise CheckoutArgumentException, e.wrapped_exception
    end

    if !response.body.nil? && response.body != ''
      oauth_response = JSON.parse(response.body, object_class: OpenStruct)

      @access_token = OAuthAccessToken.new(oauth_response.access_token,
                                           Time.now + oauth_response.expires_in)
    end

    @access_token
  end

  private

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Environment] environment
  # @param [String, nil] authorization_uri
  def validate_arguments(client_id, client_secret, environment, authorization_uri)
    if client_id.nil? || client_secret.nil?
      raise CheckoutArgumentException, 'Invalid OAuth "client_id" or "client_secret"'
    end

    return unless environment.nil? && authorization_uri.nil?

    raise CheckoutArgumentException,
          'Invalid configuration. Please specify an "environment" or a specific OAuth "authorization_uri"'
  end
end

#authorization_uriString

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 18

class OAuthSdkCredentials < SdkCredentials
  attr_accessor :access_token,
                :client_id,
                :client_secret,
                :scopes,
                :http_client,
                :environment,
                :authorization_uri,
                :log

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Array(CheckoutSdk::OAuthScopes)] scopes
  # @param [Faraday::Connection] http_client
  # @param [CheckoutSdk::Environment] environment
  # @param [String, nil] auth_uri
  def initialize(client_id, client_secret, scopes, http_client, environment, logger, auth_uri = nil)
    validate_arguments client_id, client_secret, environment, auth_uri
    @client_id = client_id
    @client_secret = client_secret
    @scopes = scopes
    @authorization_uri = auth_uri.nil? ? environment.authorization_uri : auth_uri
    @oauth_http_client = http_client.clone
    @oauth_http_client.url_prefix = @authorization_uri
    @log = logger
    build_access_token
  end

  # @param [String] authorization_type
  # @return [CheckoutSdk::SdkAuthorization]
  def get_authorization(authorization_type)
    case authorization_type
    when AuthorizationType::SECRET_KEY_OR_OAUTH, AuthorizationType::PUBLIC_KEY_OR_OAUTH, AuthorizationType::OAUTH
      SdkAuthorization.new(PlatformType::DEFAULT, build_access_token.token)
    else
      raise CheckoutAuthorizationException.invalid_authorization authorization_type
    end
  end

  # @return [CheckoutSdk::OAuthAccessToken]
  def build_access_token
    return @access_token unless @access_token.nil? || @access_token.token.nil? || !@access_token.valid?

    data = {
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials',
      scope: @scopes.join(' ')
    }

    headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
    body = URI.encode_www_form data

    begin
      @log.info "post: #{authorization_uri}"
      response = @oauth_http_client.run_request(:post, @authorization_uri, body, headers)
    rescue Faraday::ClientError => e
      raise CheckoutAuthorizationException, e.response
    rescue Faraday::ConnectionFailed => e
      raise CheckoutArgumentException, e.wrapped_exception
    end

    if !response.body.nil? && response.body != ''
      oauth_response = JSON.parse(response.body, object_class: OpenStruct)

      @access_token = OAuthAccessToken.new(oauth_response.access_token,
                                           Time.now + oauth_response.expires_in)
    end

    @access_token
  end

  private

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Environment] environment
  # @param [String, nil] authorization_uri
  def validate_arguments(client_id, client_secret, environment, authorization_uri)
    if client_id.nil? || client_secret.nil?
      raise CheckoutArgumentException, 'Invalid OAuth "client_id" or "client_secret"'
    end

    return unless environment.nil? && authorization_uri.nil?

    raise CheckoutArgumentException,
          'Invalid configuration. Please specify an "environment" or a specific OAuth "authorization_uri"'
  end
end

#client_idString

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 18

class OAuthSdkCredentials < SdkCredentials
  attr_accessor :access_token,
                :client_id,
                :client_secret,
                :scopes,
                :http_client,
                :environment,
                :authorization_uri,
                :log

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Array(CheckoutSdk::OAuthScopes)] scopes
  # @param [Faraday::Connection] http_client
  # @param [CheckoutSdk::Environment] environment
  # @param [String, nil] auth_uri
  def initialize(client_id, client_secret, scopes, http_client, environment, logger, auth_uri = nil)
    validate_arguments client_id, client_secret, environment, auth_uri
    @client_id = client_id
    @client_secret = client_secret
    @scopes = scopes
    @authorization_uri = auth_uri.nil? ? environment.authorization_uri : auth_uri
    @oauth_http_client = http_client.clone
    @oauth_http_client.url_prefix = @authorization_uri
    @log = logger
    build_access_token
  end

  # @param [String] authorization_type
  # @return [CheckoutSdk::SdkAuthorization]
  def get_authorization(authorization_type)
    case authorization_type
    when AuthorizationType::SECRET_KEY_OR_OAUTH, AuthorizationType::PUBLIC_KEY_OR_OAUTH, AuthorizationType::OAUTH
      SdkAuthorization.new(PlatformType::DEFAULT, build_access_token.token)
    else
      raise CheckoutAuthorizationException.invalid_authorization authorization_type
    end
  end

  # @return [CheckoutSdk::OAuthAccessToken]
  def build_access_token
    return @access_token unless @access_token.nil? || @access_token.token.nil? || !@access_token.valid?

    data = {
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials',
      scope: @scopes.join(' ')
    }

    headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
    body = URI.encode_www_form data

    begin
      @log.info "post: #{authorization_uri}"
      response = @oauth_http_client.run_request(:post, @authorization_uri, body, headers)
    rescue Faraday::ClientError => e
      raise CheckoutAuthorizationException, e.response
    rescue Faraday::ConnectionFailed => e
      raise CheckoutArgumentException, e.wrapped_exception
    end

    if !response.body.nil? && response.body != ''
      oauth_response = JSON.parse(response.body, object_class: OpenStruct)

      @access_token = OAuthAccessToken.new(oauth_response.access_token,
                                           Time.now + oauth_response.expires_in)
    end

    @access_token
  end

  private

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Environment] environment
  # @param [String, nil] authorization_uri
  def validate_arguments(client_id, client_secret, environment, authorization_uri)
    if client_id.nil? || client_secret.nil?
      raise CheckoutArgumentException, 'Invalid OAuth "client_id" or "client_secret"'
    end

    return unless environment.nil? && authorization_uri.nil?

    raise CheckoutArgumentException,
          'Invalid configuration. Please specify an "environment" or a specific OAuth "authorization_uri"'
  end
end

#client_secretString

Returns:

  • (String)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 18

class OAuthSdkCredentials < SdkCredentials
  attr_accessor :access_token,
                :client_id,
                :client_secret,
                :scopes,
                :http_client,
                :environment,
                :authorization_uri,
                :log

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Array(CheckoutSdk::OAuthScopes)] scopes
  # @param [Faraday::Connection] http_client
  # @param [CheckoutSdk::Environment] environment
  # @param [String, nil] auth_uri
  def initialize(client_id, client_secret, scopes, http_client, environment, logger, auth_uri = nil)
    validate_arguments client_id, client_secret, environment, auth_uri
    @client_id = client_id
    @client_secret = client_secret
    @scopes = scopes
    @authorization_uri = auth_uri.nil? ? environment.authorization_uri : auth_uri
    @oauth_http_client = http_client.clone
    @oauth_http_client.url_prefix = @authorization_uri
    @log = logger
    build_access_token
  end

  # @param [String] authorization_type
  # @return [CheckoutSdk::SdkAuthorization]
  def get_authorization(authorization_type)
    case authorization_type
    when AuthorizationType::SECRET_KEY_OR_OAUTH, AuthorizationType::PUBLIC_KEY_OR_OAUTH, AuthorizationType::OAUTH
      SdkAuthorization.new(PlatformType::DEFAULT, build_access_token.token)
    else
      raise CheckoutAuthorizationException.invalid_authorization authorization_type
    end
  end

  # @return [CheckoutSdk::OAuthAccessToken]
  def build_access_token
    return @access_token unless @access_token.nil? || @access_token.token.nil? || !@access_token.valid?

    data = {
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials',
      scope: @scopes.join(' ')
    }

    headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
    body = URI.encode_www_form data

    begin
      @log.info "post: #{authorization_uri}"
      response = @oauth_http_client.run_request(:post, @authorization_uri, body, headers)
    rescue Faraday::ClientError => e
      raise CheckoutAuthorizationException, e.response
    rescue Faraday::ConnectionFailed => e
      raise CheckoutArgumentException, e.wrapped_exception
    end

    if !response.body.nil? && response.body != ''
      oauth_response = JSON.parse(response.body, object_class: OpenStruct)

      @access_token = OAuthAccessToken.new(oauth_response.access_token,
                                           Time.now + oauth_response.expires_in)
    end

    @access_token
  end

  private

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Environment] environment
  # @param [String, nil] authorization_uri
  def validate_arguments(client_id, client_secret, environment, authorization_uri)
    if client_id.nil? || client_secret.nil?
      raise CheckoutArgumentException, 'Invalid OAuth "client_id" or "client_secret"'
    end

    return unless environment.nil? && authorization_uri.nil?

    raise CheckoutArgumentException,
          'Invalid configuration. Please specify an "environment" or a specific OAuth "authorization_uri"'
  end
end

#environmentCheckoutSdk::Environment



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 18

class OAuthSdkCredentials < SdkCredentials
  attr_accessor :access_token,
                :client_id,
                :client_secret,
                :scopes,
                :http_client,
                :environment,
                :authorization_uri,
                :log

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Array(CheckoutSdk::OAuthScopes)] scopes
  # @param [Faraday::Connection] http_client
  # @param [CheckoutSdk::Environment] environment
  # @param [String, nil] auth_uri
  def initialize(client_id, client_secret, scopes, http_client, environment, logger, auth_uri = nil)
    validate_arguments client_id, client_secret, environment, auth_uri
    @client_id = client_id
    @client_secret = client_secret
    @scopes = scopes
    @authorization_uri = auth_uri.nil? ? environment.authorization_uri : auth_uri
    @oauth_http_client = http_client.clone
    @oauth_http_client.url_prefix = @authorization_uri
    @log = logger
    build_access_token
  end

  # @param [String] authorization_type
  # @return [CheckoutSdk::SdkAuthorization]
  def get_authorization(authorization_type)
    case authorization_type
    when AuthorizationType::SECRET_KEY_OR_OAUTH, AuthorizationType::PUBLIC_KEY_OR_OAUTH, AuthorizationType::OAUTH
      SdkAuthorization.new(PlatformType::DEFAULT, build_access_token.token)
    else
      raise CheckoutAuthorizationException.invalid_authorization authorization_type
    end
  end

  # @return [CheckoutSdk::OAuthAccessToken]
  def build_access_token
    return @access_token unless @access_token.nil? || @access_token.token.nil? || !@access_token.valid?

    data = {
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials',
      scope: @scopes.join(' ')
    }

    headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
    body = URI.encode_www_form data

    begin
      @log.info "post: #{authorization_uri}"
      response = @oauth_http_client.run_request(:post, @authorization_uri, body, headers)
    rescue Faraday::ClientError => e
      raise CheckoutAuthorizationException, e.response
    rescue Faraday::ConnectionFailed => e
      raise CheckoutArgumentException, e.wrapped_exception
    end

    if !response.body.nil? && response.body != ''
      oauth_response = JSON.parse(response.body, object_class: OpenStruct)

      @access_token = OAuthAccessToken.new(oauth_response.access_token,
                                           Time.now + oauth_response.expires_in)
    end

    @access_token
  end

  private

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Environment] environment
  # @param [String, nil] authorization_uri
  def validate_arguments(client_id, client_secret, environment, authorization_uri)
    if client_id.nil? || client_secret.nil?
      raise CheckoutArgumentException, 'Invalid OAuth "client_id" or "client_secret"'
    end

    return unless environment.nil? && authorization_uri.nil?

    raise CheckoutArgumentException,
          'Invalid configuration. Please specify an "environment" or a specific OAuth "authorization_uri"'
  end
end

#http_clientObject

Returns:

  • (Object)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 18

class OAuthSdkCredentials < SdkCredentials
  attr_accessor :access_token,
                :client_id,
                :client_secret,
                :scopes,
                :http_client,
                :environment,
                :authorization_uri,
                :log

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Array(CheckoutSdk::OAuthScopes)] scopes
  # @param [Faraday::Connection] http_client
  # @param [CheckoutSdk::Environment] environment
  # @param [String, nil] auth_uri
  def initialize(client_id, client_secret, scopes, http_client, environment, logger, auth_uri = nil)
    validate_arguments client_id, client_secret, environment, auth_uri
    @client_id = client_id
    @client_secret = client_secret
    @scopes = scopes
    @authorization_uri = auth_uri.nil? ? environment.authorization_uri : auth_uri
    @oauth_http_client = http_client.clone
    @oauth_http_client.url_prefix = @authorization_uri
    @log = logger
    build_access_token
  end

  # @param [String] authorization_type
  # @return [CheckoutSdk::SdkAuthorization]
  def get_authorization(authorization_type)
    case authorization_type
    when AuthorizationType::SECRET_KEY_OR_OAUTH, AuthorizationType::PUBLIC_KEY_OR_OAUTH, AuthorizationType::OAUTH
      SdkAuthorization.new(PlatformType::DEFAULT, build_access_token.token)
    else
      raise CheckoutAuthorizationException.invalid_authorization authorization_type
    end
  end

  # @return [CheckoutSdk::OAuthAccessToken]
  def build_access_token
    return @access_token unless @access_token.nil? || @access_token.token.nil? || !@access_token.valid?

    data = {
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials',
      scope: @scopes.join(' ')
    }

    headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
    body = URI.encode_www_form data

    begin
      @log.info "post: #{authorization_uri}"
      response = @oauth_http_client.run_request(:post, @authorization_uri, body, headers)
    rescue Faraday::ClientError => e
      raise CheckoutAuthorizationException, e.response
    rescue Faraday::ConnectionFailed => e
      raise CheckoutArgumentException, e.wrapped_exception
    end

    if !response.body.nil? && response.body != ''
      oauth_response = JSON.parse(response.body, object_class: OpenStruct)

      @access_token = OAuthAccessToken.new(oauth_response.access_token,
                                           Time.now + oauth_response.expires_in)
    end

    @access_token
  end

  private

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Environment] environment
  # @param [String, nil] authorization_uri
  def validate_arguments(client_id, client_secret, environment, authorization_uri)
    if client_id.nil? || client_secret.nil?
      raise CheckoutArgumentException, 'Invalid OAuth "client_id" or "client_secret"'
    end

    return unless environment.nil? && authorization_uri.nil?

    raise CheckoutArgumentException,
          'Invalid configuration. Please specify an "environment" or a specific OAuth "authorization_uri"'
  end
end

#logObject

Returns the value of attribute log.



19
20
21
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 19

def log
  @log
end

#scopesArray(CheckoutSdk::OAuthScopes)

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 18

class OAuthSdkCredentials < SdkCredentials
  attr_accessor :access_token,
                :client_id,
                :client_secret,
                :scopes,
                :http_client,
                :environment,
                :authorization_uri,
                :log

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Array(CheckoutSdk::OAuthScopes)] scopes
  # @param [Faraday::Connection] http_client
  # @param [CheckoutSdk::Environment] environment
  # @param [String, nil] auth_uri
  def initialize(client_id, client_secret, scopes, http_client, environment, logger, auth_uri = nil)
    validate_arguments client_id, client_secret, environment, auth_uri
    @client_id = client_id
    @client_secret = client_secret
    @scopes = scopes
    @authorization_uri = auth_uri.nil? ? environment.authorization_uri : auth_uri
    @oauth_http_client = http_client.clone
    @oauth_http_client.url_prefix = @authorization_uri
    @log = logger
    build_access_token
  end

  # @param [String] authorization_type
  # @return [CheckoutSdk::SdkAuthorization]
  def get_authorization(authorization_type)
    case authorization_type
    when AuthorizationType::SECRET_KEY_OR_OAUTH, AuthorizationType::PUBLIC_KEY_OR_OAUTH, AuthorizationType::OAUTH
      SdkAuthorization.new(PlatformType::DEFAULT, build_access_token.token)
    else
      raise CheckoutAuthorizationException.invalid_authorization authorization_type
    end
  end

  # @return [CheckoutSdk::OAuthAccessToken]
  def build_access_token
    return @access_token unless @access_token.nil? || @access_token.token.nil? || !@access_token.valid?

    data = {
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials',
      scope: @scopes.join(' ')
    }

    headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
    body = URI.encode_www_form data

    begin
      @log.info "post: #{authorization_uri}"
      response = @oauth_http_client.run_request(:post, @authorization_uri, body, headers)
    rescue Faraday::ClientError => e
      raise CheckoutAuthorizationException, e.response
    rescue Faraday::ConnectionFailed => e
      raise CheckoutArgumentException, e.wrapped_exception
    end

    if !response.body.nil? && response.body != ''
      oauth_response = JSON.parse(response.body, object_class: OpenStruct)

      @access_token = OAuthAccessToken.new(oauth_response.access_token,
                                           Time.now + oauth_response.expires_in)
    end

    @access_token
  end

  private

  # @param [String] client_id
  # @param [String] client_secret
  # @param [Environment] environment
  # @param [String, nil] authorization_uri
  def validate_arguments(client_id, client_secret, environment, authorization_uri)
    if client_id.nil? || client_secret.nil?
      raise CheckoutArgumentException, 'Invalid OAuth "client_id" or "client_secret"'
    end

    return unless environment.nil? && authorization_uri.nil?

    raise CheckoutArgumentException,
          'Invalid configuration. Please specify an "environment" or a specific OAuth "authorization_uri"'
  end
end

Instance Method Details

#build_access_tokenCheckoutSdk::OAuthAccessToken



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 58

def build_access_token
  return @access_token unless @access_token.nil? || @access_token.token.nil? || !@access_token.valid?

  data = {
    client_id: @client_id,
    client_secret: @client_secret,
    grant_type: 'client_credentials',
    scope: @scopes.join(' ')
  }

  headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
  body = URI.encode_www_form data

  begin
    @log.info "post: #{authorization_uri}"
    response = @oauth_http_client.run_request(:post, @authorization_uri, body, headers)
  rescue Faraday::ClientError => e
    raise CheckoutAuthorizationException, e.response
  rescue Faraday::ConnectionFailed => e
    raise CheckoutArgumentException, e.wrapped_exception
  end

  if !response.body.nil? && response.body != ''
    oauth_response = JSON.parse(response.body, object_class: OpenStruct)

    @access_token = OAuthAccessToken.new(oauth_response.access_token,
                                         Time.now + oauth_response.expires_in)
  end

  @access_token
end

#get_authorization(authorization_type) ⇒ CheckoutSdk::SdkAuthorization

Parameters:

  • authorization_type (String)

Returns:



48
49
50
51
52
53
54
55
# File 'lib/checkout_sdk/oauth_sdk_credentials.rb', line 48

def get_authorization(authorization_type)
  case authorization_type
  when AuthorizationType::SECRET_KEY_OR_OAUTH, AuthorizationType::PUBLIC_KEY_OR_OAUTH, AuthorizationType::OAUTH
    SdkAuthorization.new(PlatformType::DEFAULT, build_access_token.token)
  else
    raise CheckoutAuthorizationException.invalid_authorization authorization_type
  end
end