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

STCOR-671 handle access-control via cookies #1364

Merged
merged 4 commits into from
Nov 6, 2023
Merged

STCOR-671 handle access-control via cookies #1364

merged 4 commits into from
Nov 6, 2023

Conversation

zburke
Copy link
Member

@zburke zburke commented Nov 6, 2023

Handle access-control via HTTP-only cookies instead of storing the JWT in local storage and providing it in the X-Okapi-Token header of fetch requests, and proxy all requests through a service worker that performs Refresh Token Rotation as needed to make sure the access-token remains fresh.

Notable changes:

Note that this work DOES NOT address STCOR-758 (pages loaded with shift-reload have their ATs timeout after 10 minutes, causing one or more error alerts followed by auto-logout) or STCOR-759 (AT may timeout >= 10 minutes causing one or more error alerts followed by auto-logout).

Refs STCOR-671, STCOR-574, STCOR-756, FOLIO-3627

Handle access-control via HTTP-only cookies instead of storing the JWT
in local storage and providing it in the `X-Okapi-Token` header of fetch
requests, and proxy all requests through a service worker that performs
Refresh Token Rotation as needed to make sure the access-token remains
fresh.

Notable changes:

* Fetch requests are proxied by a Service Worker that intercepts them,
  validates that the Access Token is still valid, and performs token
  rotation (if it is not) before completing the original fetch.
* Sessions automatically end (i.e. the user is automatically logged out)
  when the Refresh Token expires.
* Access control is managed by including an HTTP-only cookie with all
  requests. This means the Access Token formerly available in the
  response-header as `X-Okapi-Token` is never accessible to JS code.

* Requires folio-org/stripes-connect/pull/223
* Requires folio-org/stripes-smart-components/pull/1397
* Requires folio-org/stripes-webpack/pull/125

Replaces #1340. It was gross and I really don't want to talk about it.
Let us never mention it again.

Refs STCOR-671, FOLIO-3627

(cherry picked from commit 27d2948)
Rotate tokens well before they expire. This solves a problem in
ui-data-import where every-five-second polling has caused some requests to
land in a gap of about three seconds between when the AT was actually
minted and when we stored it on the client side, which could cause the
UI to send an AT that mod-auth thinks is expired even though we thought
it was still valid.

i.e. it makes it much less likely that a token will expire in flight.

Refs STCOR-574

(cherry picked from commit dd71819)
The most important work here is fixing the bug from #1361 that
incorrectly evaluated whether a token was still valid. The original
implementation shrank the total lifespan of the token rather than
shrinking only the period of its lifespan in the future.

Additional improvements here include:

* evaluate `navigator.serviceWorker` more cautiously; some browsers (e.g.
  Firefox in Incognito) may deny access to service workers. See STCOR-757
  for additional details.
* use `{ source, type, value }` shaped messages consistently when exchanging
  messages with the service worker.
* shrink the tokens' validity windows when receiving the `TOKEN_EXPIRATION`
  message instead of calculating the shorter window each time the token is
  evaluated. This is more efficient.
* await the promise returned by `postTokenExpiration` from
  `navigator.serviceWorker.ready` in order to prevent requests from being
  sent when the service worker isn't ready for them.
* use the `new Response()` constructor correctly, i.e. stringify the JSON
  value; otherwise clients will receive the string `[object Object]` instead
  of the empty JSON object, `{}`.

Note: the login test is turned off here due to the fact that the login
flow invokes `navigator.serviceWorker.ready`, which returns a Promise that
only returns when the service worker enters a ready state, but the karma
build does not configure the service worker, hence this test times out
every time. This is not great, but resolving it is a non-trivial task.

Refs STCOR-756

(cherry picked from commit 607dc5c)
Copy link

github-actions bot commented Nov 6, 2023

Jest Unit Test Statistics

126 tests  +52   126 ✔️ +52   18s ⏱️ +2s
  15 suites +  3       0 💤 ±  0 
    1 files   ±  0       0 ±  0 

Results for commit bf96882. ± Comparison against base commit ef1e8c3.

Copy link

github-actions bot commented Nov 6, 2023

BigTest Unit Test Statistics

    1 files  ±0      1 suites  ±0   10s ⏱️ -1s
271 tests ±0  264 ✔️  - 2  7 💤 +2  0 ±0 
274 runs  ±0  267 ✔️  - 2  7 💤 +2  0 ±0 

Results for commit bf96882. ± Comparison against base commit ef1e8c3.

Copy link

sonarqubecloud bot commented Nov 6, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

73.1% 73.1% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@zburke zburke merged commit 93b35bc into b10.0 Nov 6, 2023
4 of 5 checks passed
@zburke zburke deleted the FOLIO-3627-poppy branch November 6, 2023 16:05
zburke added a commit that referenced this pull request Nov 9, 2023
This reverts commit 93b35bc.

Revert RTR until it's fully baked.
zburke added a commit that referenced this pull request Nov 9, 2023
This reverts commit 93b35bc.

Revert RTR until it's fully baked.
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.

1 participant