Class: FolioClient::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/folio_client/authenticator.rb

Overview

Fetch a token from the Folio API using login_params

Constant Summary collapse

LOGIN_ENDPOINT =
'/authn/login-with-expiry'

Class Method Summary collapse

Class Method Details

.refresh_token!String

Request an access_token

Returns:

  • (String)

    the access token

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/folio_client/authenticator.rb', line 12

def self.refresh_token!
  response = FolioClient.connection.post(LOGIN_ENDPOINT, FolioClient.config..to_json)

  UnexpectedResponse.call(response) unless response.success?

  access_cookie = FolioClient.cookie_jar.cookies.find { |cookie| cookie.name == 'folioAccessToken' }

  # NOTE: The client typically delegates raising exceptions (based on HTTP
  #       responses) to the UnexpectedResponse class, but this call in
  #       Authenticator is a one-off, unlike any other in the app, so we
  #       allow it to customize its exception handling.
  raise UnauthorizedError, "Problem with folioAccessToken cookie: #{response.headers}, #{response.body}" unless access_cookie

  access_cookie.value
end