Skip to content

Commit

Permalink
Merge pull request #108 from sul-dlss/token-refresh-interface
Browse files Browse the repository at this point in the history
Prefer exposing an interface for token refresh over leaking implementation details
  • Loading branch information
lwrubel authored Jan 10, 2024
2 parents ce684d5 + 8e666ca commit fcd89de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ client = FolioClient.configure(
The client is smart enough to automatically request a new token if it detects the one it is using has expired. If for some reason, you want to immediately request a new token, you can do this:

```ruby
client.config.token = FolioClient::Authenticator.token
client.force_token_refresh! # or `FolioClient.force_token_refresh!` as they are identical
```

## API Coverage
Expand Down
16 changes: 10 additions & 6 deletions lib/folio_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ def configure(url:, login_params:, okapi_headers:, timeout: default_timeout, leg
end

delegate :config, :connection, :cookie_jar, :data_import, :default_timeout,
:edit_marc_json, :fetch_external_id, :fetch_hrid, :fetch_instance_info,
:fetch_marc_hash, :fetch_marc_xml, :get, :has_instance_status?,
:edit_marc_json, :fetch_external_id, :fetch_hrid,
:fetch_instance_info, :fetch_marc_hash, :fetch_marc_xml,
:force_token_refresh!, :get, :has_instance_status?,
:http_get_headers, :http_post_and_put_headers, :interface_details,
:job_profiles, :organization_interfaces, :organizations, :users, :user_details,
:post, :put, to: :instance
:job_profiles, :organization_interfaces, :organizations, :users,
:user_details, :post, :put, to: :instance
end

attr_accessor :config
Expand Down Expand Up @@ -268,6 +269,10 @@ def default_timeout
120
end

def force_token_refresh!
config.token = Authenticator.token
end

private

# Wraps API operations to request new access token if expired.
Expand All @@ -287,8 +292,7 @@ def with_token_refresh_when_unauthorized

# if unauthorized, token has likely expired. try to get a new token and then retry the same request(s).
if response.status == 401 || response.status == 403

config.token = Authenticator.token
force_token_refresh!
response = yield
end

Expand Down
16 changes: 16 additions & 0 deletions spec/folio_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@
end
end

describe '#force_token_refresh!' do
let(:refreshed_token) { 'another dummy token value' }

before do
stub_request(:post, "#{url}/authn/login")
.to_return(status: 200, body: "{\"okapiToken\" : \"#{refreshed_token}\"}")
end

it 'forces a token refresh' do
expect { client.force_token_refresh! }
.to change(client.config, :token)
.from(token)
.to(refreshed_token)
end
end

describe '#get' do
let(:path) { 'some_path' }
let(:response) { { some: 'response' }.to_json }
Expand Down

0 comments on commit fcd89de

Please sign in to comment.