Class: Echosign::Credentials
- Inherits:
-
Object
- Object
- Echosign::Credentials
- 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
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
Instance Method Summary collapse
-
#authorize_url(redirect_uri, scope, state = nil) ⇒ Echosign::Credentials
Build an authorization endpoint URL for EchoSign’s OAuth2 provider.
-
#get_token(code, redirect_uri) ⇒ String
Make a request to the token endpoint and return an access token.
-
#initialize(client_id, client_secret) ⇒ Echosign::Credentials
constructor
Builds an Credentials object.
-
#refresh_access_token(current_refresh_token = nil) ⇒ String
Update (refresh) an access token.
-
#revoke_token(which = :access) ⇒ void
Revoke an access or refresh token, and any corresponding tokens.
Methods included from Validatable
#require_exactly_one, #require_keys, #validate_field
Constructor Details
#initialize(client_id, client_secret) ⇒ Echosign::Credentials
Builds an Credentials object
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_token ⇒ Object (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_at ⇒ Object (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_token ⇒ Object (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.
41 42 43 44 45 46 47 |
# File 'lib/echosign/credentials.rb', line 41 def (redirect_uri, scope, state = nil) return @client.auth_code.( 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
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.[: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
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.[: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
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 |