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, application: 'docusign_dtr') ⇒ Code

Returns a new instance of Code.



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

def initialize(integrator_key:,
               secret_key:,
               redirect_uri:,
               test_mode: true,
               application: 'docusign_dtr')
  @config = DocusignDtr::Models::AuthConfig.new(
    application: application,
    integrator_key: integrator_key,
    redirect_uri: redirect_uri,
    secret_key: secret_key,
    state: SecureRandom.uuid,
    test_mode: 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



30
31
32
33
34
35
36
37
# File 'lib/docusign_dtr/auth/code.rb', line 30

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: headers, timeout: 60)
  handle_error(response.code) if response.code != 200
  @token_response = DocusignDtr::Models::AuthTokenResponse.new(response.parsed_response)
end

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



21
22
23
24
25
26
27
28
# File 'lib/docusign_dtr/auth/code.rb', line 21

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

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

#user_infoObject



39
40
41
42
43
44
45
# File 'lib/docusign_dtr/auth/code.rb', line 39

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.code) if response.code != 200
  @userinfo = DocusignDtr::Models::Userinfo.new(response.parsed_response)
end