Class: Echosign::Credentials

Inherits:
Object
  • Object
show all
Includes:
Validatable
Defined in:
lib/echosign/credentials.rb

Constant Summary collapse

OAUTH_SITE =
'https://secure.echosign.com'
AUTHORIZE_PATH =
'/public/oauth'
TOKEN_PATH =
'/oauth/token'
REFRESH_PATH =
'/oauth/refresh'
REVOKE_PATH =
'/oauth/revoke'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validatable

#require_exactly_one, #require_keys, #validate_field

Constructor Details

#initialize(client_id, client_secret) ⇒ Echosign::Credentials

Builds an Credentials object

Parameters:

  • client_id (String)

    Client ID

  • client_secret (String)

    Client secret


21
22
23
24
25
26
27
28
29
# File 'lib/echosign/credentials.rb', line 21

def initialize(client_id, client_secret)
  @client = OAuth2::Client.new(
    client_id,
    client_secret,
    site: OAUTH_SITE,
    authorize_url: AUTHORIZE_PATH,
    token_url: TOKEN_PATH
  )
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token


13
14
15
# File 'lib/echosign/credentials.rb', line 13

def access_token
  @access_token
end

#expires_atObject (readonly)

Returns the value of attribute expires_at


13
14
15
# File 'lib/echosign/credentials.rb', line 13

def expires_at
  @expires_at
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token


13
14
15
# File 'lib/echosign/credentials.rb', line 13

def refresh_token
  @refresh_token
end

Instance Method Details

#authorize_url(redirect_uri, scope, state = nil) ⇒ Echosign::Credentials

Build an authorization endpoint URL for EchoSign's OAuth2 provider

The redirect_uri must be specified on the app's OAuth Configuration page.

Parameters:

  • redirect_uri (String)

    A secure URL to redirect the user afterward

  • scope (String)

    Space delimited set of permissions to approve

  • state (String) (defaults to: nil)

    Any value; will be returned to the client afterward

Returns:

See Also:


41
42
43
44
45
46
47
# File 'lib/echosign/credentials.rb', line 41

def authorize_url(redirect_uri, scope, state = nil)
  return @client.auth_code.authorize_url(
    redirect_uri: redirect_uri,
    scope: scope,
    state: state
  )
end

#get_token(code, redirect_uri) ⇒ String

Make a request to the token endpoint and return an access token

Parameters:

  • code (String)

    The authorization code obtained after #authorize_url

  • redirect_uri (String)

    The redirect_url used during #authorize_url

Returns:

  • (String)

    An access token that can be used in the EchoSign API


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/echosign/credentials.rb', line 55

def get_token(code, redirect_uri)
  @client.options[:token_url] = TOKEN_PATH
  oauth_token = @client.get_token(code: code,
                                  redirect_uri: redirect_uri,
                                  grant_type: :authorization_code)

  @access_token = oauth_token.token
  @refresh_token = oauth_token.refresh_token
  @expires_at = oauth_token.expires_at

  return @access_token
end

#refresh_access_token(current_refresh_token = nil) ⇒ String

Update (refresh) an access token

This method should only be called after #get_token

Parameters:

  • current_refresh_token (String) (defaults to: nil)

    A previously obtained refresh_token from a get_token request

Returns:

  • (String)

    A new access token to be used in the EchoSign API


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/echosign/credentials.rb', line 75

def refresh_access_token(current_refresh_token = nil)
  @refresh_token = current_refresh_token if current_refresh_token != nil

  @client.options[:token_url] = REFRESH_PATH
  oauth_token = @client.get_token(grant_type: :refresh_token, refresh_token: @refresh_token)

  @access_token = oauth_token.token
  @expires_at = oauth_token.expires_at

  return @access_token
end

#revoke_token(which = :access) ⇒ void

This method returns an undefined value.

Revoke an access or refresh token, and any corresponding tokens

Parameters:

  • which (Symbol) (defaults to: :access)

    The token to revoke, either :access or :refresh


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/echosign/credentials.rb', line 92

def revoke_token(which = :access)
  if which == :access
    @client.request(:post, REVOKE_PATH, body: { token: @access_token })
  else
    @client.request(:post, REVOKE_PATH, body: { token: @refresh_token })
    @refresh_token = nil
  end

  @access_token = nil
  @expires_at = nil
end