Class: Boldsign::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/boldsign/access_token.rb

Overview

Fetches and caches an OAuth 2.0 access token using the client credentials grant (server-to-server; no user interaction).

A single instance is shared by a Client and may be hit from multiple threads, so token reads/refreshes are guarded by a mutex. The token is cached until shortly before it expires (EXPIRY_LEEWAY_SECONDS), then a new one is requested on the next call. The client credentials grant does not issue refresh tokens — expiry is handled by re-requesting.

Constant Summary collapse

TOKEN_PATH =

Token endpoint path, appended to the region’s account host.

"/connect/token".freeze
DEFAULT_SCOPE =

Default scope requested for the access token. Covers the document / signature endpoints this gem is primarily used for. Pass a different ‘scope:` to request others (an empty scope grants all the app is allowed).

"BoldSign.Documents.All".freeze
EXPIRY_LEEWAY_SECONDS =

Refresh this many seconds before the token actually expires, so an in-flight request never carries a token that lapses server-side mid-call.

60
DEFAULT_EXPIRES_IN =

Fallback lifetime (seconds) when the token response omits ‘expires_in`.

3600

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, token_url:, scope: DEFAULT_SCOPE, adapter: Faraday.default_adapter) ⇒ AccessToken

Returns a new instance of AccessToken.

Parameters:

  • client_id (String)

    OAuth app client ID.

  • client_secret (String)

    OAuth app client secret.

  • token_url (String)

    Full token endpoint URL (host + TOKEN_PATH).

  • scope (String) (defaults to: DEFAULT_SCOPE)

    OAuth scope to request.

  • adapter (Symbol) (defaults to: Faraday.default_adapter)

    Faraday adapter.



33
34
35
36
37
38
39
40
41
# File 'lib/boldsign/access_token.rb', line 33

def initialize(client_id:, client_secret:, token_url:, scope: DEFAULT_SCOPE,
               adapter: Faraday.default_adapter)
  @client_id = client_id
  @client_secret = client_secret
  @token_url = token_url
  @scope = scope
  @adapter = adapter
  @mutex = Mutex.new
end

Instance Method Details

#valueString

Returns a currently-valid bearer token, fetching or refreshing one if the cached token is missing or near expiry.

Returns:

  • (String)

    a currently-valid bearer token, fetching or refreshing one if the cached token is missing or near expiry.



45
46
47
48
49
50
# File 'lib/boldsign/access_token.rb', line 45

def value
  @mutex.synchronize do
    refresh! if expired?
    @token
  end
end