Sha256: 4e7519d557c874df29f2581cb7497b7ed49f2b3fdff19ce5e84d45103868f313

Contents?: true

Size: 1015 Bytes

Versions: 3

Compression:

Stored size: 1015 Bytes

Contents

# frozen_string_literal: true

class FolioClient
  # Fetch a token from the Folio API using login_params
  class Authenticator
    def self.token
      new.token
    end

    # Request an access_token
    def token # rubocop:disable Metrics/AbcSize
      response = FolioClient.connection.post(login_endpoint, FolioClient.config.login_params.to_json)

      UnexpectedResponse.call(response) unless response.success?

      # remove legacy_auth once new tokens enabled on Poppy
      if FolioClient.config.legacy_auth
        JSON.parse(response.body)['okapiToken']
      else
        access_cookie = FolioClient.cookie_jar.cookies.find { |cookie| cookie.name == 'folioAccessToken' }

        raise StandardError, "Problem with folioAccessToken cookie: #{response.headers}, #{response.body}" unless access_cookie

        access_cookie.value
      end
    end

    private

    def login_endpoint
      return '/authn/login-with-expiry' unless FolioClient.config.legacy_auth

      '/authn/login'
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
folio_client-0.18.0 lib/folio_client/authenticator.rb
folio_client-0.17.0 lib/folio_client/authenticator.rb
folio_client-0.16.0 lib/folio_client/authenticator.rb