Class: DocusignDtr::Auth::Code

Inherits:
Base
  • Object
show all
Defined in:
lib/docusign_dtr/auth/code.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #token_response

Instance Method Summary collapse

Methods inherited from Base

#grant_url, #parse_url_response

Constructor Details

#initialize(integrator_key:, secret_key:, redirect_uri:, test_mode: true, state: nil, application: 'docusign_dtr') ⇒ Code

rubocop:disable Metrics/ParameterLists, Lint/MissingSuper



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/docusign_dtr/auth/code.rb', line 8

def initialize(integrator_key:,
               secret_key:,
               redirect_uri:,
               test_mode: true,
               state: nil,
               application: 'docusign_dtr')
  @config = DocusignDtr::Models::AuthConfig.new(
    application:,
    integrator_key:,
    redirect_uri:,
    secret_key:,
    state: state || SecureRandom.uuid,
    test_mode:
  )
end

Instance Attribute Details

#userinfoObject

Returns the value of attribute userinfo.



5
6
7
# File 'lib/docusign_dtr/auth/code.rb', line 5

def userinfo
  @userinfo
end

Instance Method Details

#refresh_tokenObject



34
35
36
37
38
39
40
41
# File 'lib/docusign_dtr/auth/code.rb', line 34

def refresh_token
  raise 'No token to refresh' unless @token_response&.refresh_token

  params = { grant_type: :refresh_token, refresh_token: @token_response.refresh_token }
  response = self.class.post("#{base_uri}oauth/token", query: params, headers:, timeout: 60)
  handle_error(response)
  @token_response = DocusignDtr::Models::AuthTokenResponse.new(response.parsed_response)
end

#request_token(code:, state: nil) ⇒ Object

rubocop:enable Metrics/ParameterLists, Lint/MissingSuper



25
26
27
28
29
30
31
32
# File 'lib/docusign_dtr/auth/code.rb', line 25

def request_token(code:, state: nil)
  raise 'State does not match. Possible CSRF!' if state && state != @config.state

  params = { grant_type: :authorization_code, code: }
  response = self.class.post(auth_uri, query: params, headers:, timeout: 60)
  handle_error(response)
  @token_response = DocusignDtr::Models::AuthTokenResponse.new(response.parsed_response)
end

#user_infoObject



43
44
45
46
47
48
49
# File 'lib/docusign_dtr/auth/code.rb', line 43

def 
  raise 'No token to obtain userinfo' unless @token_response&.access_token

  response = self.class.get("#{base_uri}oauth/userinfo", headers: , timeout: 60)
  handle_error(response)
  @userinfo = DocusignDtr::Models::Userinfo.new(response.parsed_response)
end