Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get token from cookie when using /login-with-expiry in Poppy #105

Merged
merged 1 commit into from
Jan 9, 2024

Conversation

lwrubel
Copy link
Contributor

@lwrubel lwrubel commented Jan 9, 2024

Why was this change made? 🤔

Resolves #92 to implement new process for getting an access token.

Currently, the token is in the body of the response from the /authn/login endpoint. From Poppy onwards the token will be obtained from a /authn/login-with-expiry endpoint and will be in a cookie. See the heading "A guide for non-module clients such as scripts or other integrations" on the Folio Wiki.

How the token is sent to FOLIO in a request isn't changing. If an expired access token is sent in a request, FOLIO will return a 403 rather than a 401 as it does in Nolana/Orchid.

If we try to use the login-with-expiry endpoint on Nolana, FOLIO returns a 404. The EBSCO FOLIO client sends everything to that endpoint and if it gets a 404, it then tries the login endpoint. I thought we might want more control over which endpoint we're sending requests to, especially since the docs say that both endpoints are supported in Poppy and Quesnalia (legacy tokens not removed until Ramsons). This has the downside that we are changing the FolioClient.configure method signature and would want to eventually update that to remove the legacy_auth param with that is no longer relevant (once we've been on Poppy in production for a bit?).

Will follow up with:

  • shared_configs updates for DSA, Argo, and Google Books
  • Update those consumers to pass in a legacy_auth param (rather than rely on default)
  • ticket updating the legacy_auth param when we go live with Poppy

How was this change tested? 🤨

Unit. Tested that the changes do not break current usage by using api_test.rb locally and running the sdr_deposit_spec.rb integration test and item_creation_with_folio_hrid_spec.rb when using this folio_client version on DSA and Argo QA.

I haven't been able to test this on a live Poppy instance, so that would be good to do as soon as a Poppy dev/test instance is available (Libsys estimates in mid-January).

@@ -151,7 +154,14 @@ def connection
url: config.url,
headers: DEFAULT_HEADERS.merge(config.okapi_headers || {}),
request: { timeout: config.timeout }
)
) do |faraday|
faraday.use :cookie_jar, jar: cookie_jar
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To access cookies directly, need to pass in a CookieJar.

@@ -276,8 +286,9 @@ def with_token_refresh_when_unauthorized
response = yield

# if unauthorized, token has likely expired. try to get a new token and then retry the same request(s).
if response.status == 401
config.token = Authenticator.token(config.login_params, connection)
if response.status == 401 || response.status == 403
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FOLIO docs say that with Poppy release onward, if you send an expired token, it will return a 403 rather than a 401.

@lwrubel lwrubel force-pushed the t92-refresh-token branch from 8a2c14d to 3abe420 Compare January 9, 2024 15:55
@lwrubel lwrubel marked this pull request as ready for review January 9, 2024 16:01
Comment on lines +28 to +29
access_cookies = cookie_jar.cookies.select { |cookie| cookie.name == 'folioAccessToken' }
access_cookies[0].value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we only care about the first value, we could consider Enumerable#find here too:

Suggested change
access_cookies = cookie_jar.cookies.select { |cookie| cookie.name == 'folioAccessToken' }
access_cookies[0].value
cookie_jar.cookies.find { |cookie| cookie.name == 'folioAccessToken' }.value

if legacy_auth
JSON.parse(response.body)['okapiToken']
else
access_cookies = cookie_jar.cookies.select { |cookie| cookie.name == 'folioAccessToken' }
Copy link
Member

@mjgiarlo mjgiarlo Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should handle the case where this line returns an empty array (in which case the #value call immediately below will fail), or is that an unlikely/corner case?

Copy link
Member

@mjgiarlo mjgiarlo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve pending consideration of minor enumerable questions

@mjgiarlo mjgiarlo merged commit 2f7725f into main Jan 9, 2024
@mjgiarlo mjgiarlo deleted the t92-refresh-token branch January 9, 2024 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use Refresh Token Rotation for authentication
2 participants