Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Frontend tokens #1569

Merged
merged 24 commits into from
Aug 5, 2022
Merged

Frontend tokens #1569

merged 24 commits into from
Aug 5, 2022

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented Jul 15, 2022

Fixes

Fixes #1558 by @sarayourfriend

Description

This PR adds a mechanism to send authenticated requests when an API key has been provided (and unauthenticated ones) when not.

This means that API calls during SSR will be authenticated, and not throttled, but API calls from the client (subsequent calls) will be throttled as an authenticated user because they won't have any API key set.

Testing Instructions

  1. Check out the repo.
  2. Follow instructions in TESTING_GUIDELINES.md for setting up your local environment with API authentication tokens after registering an app on your local dev server or on staging.
    • Be sure to use the version of the document from this branch as I've introduced new documentation for how to do this!
  3. Visit the application as normal and confirm in the logs you can see the API token flow. Especially confirm that the first request does wait for the token to come back before continuing.

Furthermore, test using the docker image:

docker build . --build-arg RELEASE=v0-test --build-arg API_URL=https://api-dev.openverse.engineering/ -t openverse-frontend:latest
docker run --env-file .env -p 8443:8443/tcp openverse-frontend:latest

Note: Earlier this testing instructions read to use a locally running API with the docker container. This doesn't work due to CORS issues and having different URLs for the client and server to hit the API (due to the different network contexts they're running in). To test docker, please use a live API instance. To test against a local API, run the Nuxt app outside of docker using pnpm prod and an .env file set up to point to your local API.

Make sure to visit the docker-running service at localhost:8443 and not the IP address that nuxt spits out. That IP address is internal to the docker network. For some reason it still lets some requests through but most of them will fail (including static assets). If you stick to localhost:8443 it works fine though.

When making requests to the docker image, you will see more than one API token being generated now in the logs, depending on the number of CPUs docker has access to. That is because pm2 runs a worker per CPU it finds. If you're using docker desktop and it is not configured with multiple CPUs then you will not see this behavior. Please enable at least 2 CPUs so that you can test this!

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@github-actions
Copy link

github-actions bot commented Jul 15, 2022

Storybook and Tailwind configuration previews: Ready

Storybook: https://wordpress.github.io/openverse-frontend/_preview/1569
Tailwind: https://wordpress.github.io/openverse-frontend/_preview/1569/tailwind

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

@openverse-bot openverse-bot added 💻 aspect: code Concerns the software code in the repository 🟧 priority: high Stalls work on the project or its dependents 🧰 goal: internal improvement Improvement that benefits maintainers, not users labels Jul 15, 2022
@github-actions
Copy link

github-actions bot commented Jul 15, 2022

Size Change: -83.6 kB (-6%) ✅

Total Size: 1.35 MB

Filename Size Change
./.nuxt/dist/client/232.js 0 B -273 B (removed) 🏆
./.nuxt/dist/client/232.modern.js 0 B -277 B (removed) 🏆
./.nuxt/dist/client/233.js 0 B -1.85 kB (removed) 🏆
./.nuxt/dist/client/app.js 109 kB -2.2 kB (-2%)
./.nuxt/dist/client/app.modern.js 101 kB -2.2 kB (-2%)
./.nuxt/dist/client/commons/app.js 84.3 kB -22 B (0%)
./.nuxt/dist/client/commons/app.modern.js 73.2 kB -19 B (0%)
./.nuxt/dist/client/components/v-all-results-grid.js 4.13 kB +1 B (0%)
./.nuxt/dist/client/components/v-box-layout.js 1.2 kB -1 B (0%)
./.nuxt/dist/client/components/v-content-report-form.js 3.76 kB +3 B (0%)
./.nuxt/dist/client/components/v-content-report-form.modern.js 3.57 kB +3 B (0%)
./.nuxt/dist/client/components/v-content-report-popover.js 4.43 kB +1 B (0%)
./.nuxt/dist/client/components/v-content-report-popover.modern.js 4.24 kB +4 B (0%)
./.nuxt/dist/client/components/v-copy-license.js 2.95 kB +3 B (0%)
./.nuxt/dist/client/components/v-error-image.modern.js 1.68 kB +1 B (0%)
./.nuxt/dist/client/components/v-image-details.js 1.45 kB -1 B (0%)
./.nuxt/dist/client/components/v-media-license.js 805 B -2 B (0%)
./.nuxt/dist/client/components/v-media-reuse.js 1.03 kB -2 B (0%)
./.nuxt/dist/client/components/v-media-reuse/pages/image/_id.js 2.95 kB +3 B (0%)
./.nuxt/dist/client/components/v-meta-search-form.js 2.71 kB -1 B (0%)
./.nuxt/dist/client/components/v-meta-source-list.js 2.06 kB -1 B (0%)
./.nuxt/dist/client/components/v-modal.js 876 B -1 B (0%)
./.nuxt/dist/client/components/v-modal.modern.js 881 B -1 B (0%)
./.nuxt/dist/client/components/v-related-images.js 3.15 kB -1 B (0%)
./.nuxt/dist/client/components/v-search-results-title.js 676 B -1 B (0%)
./.nuxt/dist/client/components/v-search-type-radio.js 832 B -1 B (0%)
./.nuxt/dist/client/components/v-sidebar-content.js 445 B -1 B (0%)
./.nuxt/dist/client/components/v-sidebar-content.modern.js 449 B +1 B (0%)
./.nuxt/dist/client/components/v-tab.js 1.74 kB +1 B (0%)
./.nuxt/dist/client/components/v-tabs.js 785 B +1 B (0%)
./.nuxt/dist/client/components/v-tabs.modern.js 769 B -1 B (0%)
./.nuxt/dist/client/lang-ar.json.modern.js 255 B -1 B (0%)
./.nuxt/dist/client/lang-en-au.json.js 791 B +1 B (0%)
./.nuxt/dist/client/lang-en-au.json.modern.js 794 B -1 B (0%)
./.nuxt/dist/client/lang-en-ca.json.modern.js 8.56 kB +1 B (0%)
./.nuxt/dist/client/lang-en-gb.json.modern.js 8.73 kB +1 B (0%)
./.nuxt/dist/client/lang-es-ar.json.modern.js 9.61 kB -1 B (0%)
./.nuxt/dist/client/lang-es-cl.json.modern.js 156 B -1 B (-1%)
./.nuxt/dist/client/lang-es-cr.json.modern.js 156 B -1 B (-1%)
./.nuxt/dist/client/lang-es-do.json.js 4.86 kB -1 B (0%)
./.nuxt/dist/client/lang-es-do.json.modern.js 4.86 kB -1 B (0%)
./.nuxt/dist/client/lang-es-ec.json.js 9.33 kB -2 B (0%)
./.nuxt/dist/client/lang-es-ec.json.modern.js 9.33 kB +2 B (0%)
./.nuxt/dist/client/lang-es-mx.json.js 9.52 kB +1 B (0%)
./.nuxt/dist/client/lang-es-ve.json.js 9.33 kB +3 B (0%)
./.nuxt/dist/client/lang-es.json.js 9.51 kB -1 B (0%)
./.nuxt/dist/client/lang-fa.json.js 3.64 kB -1 B (0%)
./.nuxt/dist/client/lang-hi.json.js 0 B -203 B (removed) 🏆
./.nuxt/dist/client/lang-hi.json.modern.js 0 B -210 B (removed) 🏆
./.nuxt/dist/client/lang-hu.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-hu.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-id.json.modern.js 8.22 kB -1 B (0%)
./.nuxt/dist/client/lang-ja.json.js 4.49 kB -1 B (0%)
./.nuxt/dist/client/lang-ja.json.modern.js 4.5 kB +2 B (0%)
./.nuxt/dist/client/lang-kal.json.js 0 B -149 B (removed) 🏆
./.nuxt/dist/client/lang-kal.json.modern.js 0 B -156 B (removed) 🏆
./.nuxt/dist/client/lang-kk.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-kk.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-kn.json.js 0 B -3.28 kB (removed) 🏆
./.nuxt/dist/client/lang-kn.json.modern.js 0 B -3.28 kB (removed) 🏆
./.nuxt/dist/client/lang-ml.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-ml.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-ms.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-ms.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-ne.json.js 0 B -330 B (removed) 🏆
./.nuxt/dist/client/lang-ne.json.modern.js 0 B -335 B (removed) 🏆
./.nuxt/dist/client/lang-nl-be.json.js 0 B -9.54 kB (removed) 🏆
./.nuxt/dist/client/lang-nl-be.json.modern.js 0 B -9.54 kB (removed) 🏆
./.nuxt/dist/client/lang-nl.json.js 0 B -6.3 kB (removed) 🏆
./.nuxt/dist/client/lang-nl.json.modern.js 0 B -6.31 kB (removed) 🏆
./.nuxt/dist/client/lang-pl.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-pl.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-pt-ao.json.js 0 B -151 B (removed) 🏆
./.nuxt/dist/client/lang-pt-ao.json.modern.js 0 B -157 B (removed) 🏆
./.nuxt/dist/client/lang-pt.json.modern.js 416 B +1 B (0%)
./.nuxt/dist/client/lang-ro.json.modern.js 9.64 kB +1 B (0%)
./.nuxt/dist/client/lang-ru.json.js 4.2 kB -1 B (0%)
./.nuxt/dist/client/lang-sa-in.json.js 0 B -151 B (removed) 🏆
./.nuxt/dist/client/lang-sa-in.json.modern.js 0 B -157 B (removed) 🏆
./.nuxt/dist/client/lang-sl.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-sl.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-so.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-so.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-sq.json.js 0 B -9.95 kB (removed) 🏆
./.nuxt/dist/client/lang-sq.json.modern.js 0 B -9.95 kB (removed) 🏆
./.nuxt/dist/client/lang-su.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-su.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-sv.json.js 0 B -5.11 kB (removed) 🏆
./.nuxt/dist/client/lang-sv.json.modern.js 0 B -5.12 kB (removed) 🏆
./.nuxt/dist/client/lang-te.json.modern.js 3.48 kB -1 B (0%)
./.nuxt/dist/client/lang-tir.json.js 0 B -149 B (removed) 🏆
./.nuxt/dist/client/lang-tir.json.modern.js 0 B -156 B (removed) 🏆
./.nuxt/dist/client/lang-tr.json.js 0 B -2.48 kB (removed) 🏆
./.nuxt/dist/client/lang-tr.json.modern.js 0 B -2.48 kB (removed) 🏆
./.nuxt/dist/client/lang-ur.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-ur.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-uz.json.js 0 B -148 B (removed) 🏆
./.nuxt/dist/client/lang-uz.json.modern.js 0 B -155 B (removed) 🏆
./.nuxt/dist/client/lang-xho.json.js 0 B -149 B (removed) 🏆
./.nuxt/dist/client/lang-xho.json.modern.js 0 B -156 B (removed) 🏆
./.nuxt/dist/client/lang-zh-cn.json.modern.js 4.9 kB +1 B (0%)
./.nuxt/dist/client/lang-zh-tw.json.modern.js 4.24 kB +1 B (0%)
./.nuxt/dist/client/pages/about.js 1.05 kB +1 B (0%)
./.nuxt/dist/client/pages/about.modern.js 1.06 kB +1 B (0%)
./.nuxt/dist/client/pages/audio/_id.js 6.37 kB -1 B (0%)
./.nuxt/dist/client/pages/extension.js 2.19 kB +1 B (0%)
./.nuxt/dist/client/pages/extension.modern.js 2.2 kB -1 B (0%)
./.nuxt/dist/client/pages/feedback.js 3.23 kB -1 B (0%)
./.nuxt/dist/client/pages/image/_id.modern.js 6.74 kB -1 B (0%)
./.nuxt/dist/client/pages/index.js 5.08 kB -2 B (0%)
./.nuxt/dist/client/pages/index.modern.js 4.96 kB -1 B (0%)
./.nuxt/dist/client/pages/meta-search.js 1.68 kB +1 B (0%)
./.nuxt/dist/client/pages/meta-search.modern.js 1.68 kB -1 B (0%)
./.nuxt/dist/client/pages/preferences.js 1.26 kB +1 B (0%)
./.nuxt/dist/client/pages/search-help.modern.js 1.54 kB +1 B (0%)
./.nuxt/dist/client/pages/search.js 2.73 kB +31 B (+1%)
./.nuxt/dist/client/pages/search.modern.js 2.57 kB +24 B (+1%)
./.nuxt/dist/client/pages/search/index.js 821 B -1 B (0%)
./.nuxt/dist/client/pages/search/model-3d.js 259 B -2 B (-1%)
./.nuxt/dist/client/pages/search/video.modern.js 262 B +1 B (0%)
./.nuxt/dist/client/pages/sources.js 1.44 kB +1 B (0%)
./.nuxt/dist/client/pages/sources.modern.js 1.44 kB -1 B (0%)
./.nuxt/dist/client/runtime.js 2.6 kB -117 B (-4%)
./.nuxt/dist/client/runtime.modern.js 2.61 kB -117 B (-4%)
./.nuxt/dist/client/vendors/app.modern.js 48.5 kB +1 B (0%)
./.nuxt/dist/client/vendors/sentry.js 41.5 kB -4 B (0%)
./.nuxt/dist/client/209.js 273 B +273 B (new file) 🆕
./.nuxt/dist/client/209.modern.js 277 B +277 B (new file) 🆕
./.nuxt/dist/client/210.js 1.85 kB +1.85 kB (new file) 🆕
ℹ️ View Unchanged
Filename Size
./.nuxt/dist/client/components/extension-browsers.js 816 B
./.nuxt/dist/client/components/extension-browsers.modern.js 819 B
./.nuxt/dist/client/components/loading-icon.js 766 B
./.nuxt/dist/client/components/loading-icon.modern.js 769 B
./.nuxt/dist/client/components/table-sort-icon.js 515 B
./.nuxt/dist/client/components/table-sort-icon.modern.js 519 B
./.nuxt/dist/client/components/v-all-results-grid.modern.js 4.01 kB
./.nuxt/dist/client/components/v-audio-cell.js 348 B
./.nuxt/dist/client/components/v-audio-cell.modern.js 353 B
./.nuxt/dist/client/components/v-audio-details.js 1.59 kB
./.nuxt/dist/client/components/v-audio-details.modern.js 1.6 kB
./.nuxt/dist/client/components/v-audio-track-skeleton.js 1.01 kB
./.nuxt/dist/client/components/v-audio-track-skeleton.modern.js 1.02 kB
./.nuxt/dist/client/components/v-audio-track.js 4.55 kB
./.nuxt/dist/client/components/v-audio-track.modern.js 4.5 kB
./.nuxt/dist/client/components/v-back-to-search-results-link.js 576 B
./.nuxt/dist/client/components/v-back-to-search-results-link.modern.js 581 B
./.nuxt/dist/client/components/v-bone.js 694 B
./.nuxt/dist/client/components/v-bone.modern.js 699 B
./.nuxt/dist/client/components/v-box-layout.modern.js 1.2 kB
./.nuxt/dist/client/components/v-content-link.js 1.06 kB
./.nuxt/dist/client/components/v-content-link.modern.js 1.05 kB
./.nuxt/dist/client/components/v-content-page.js 463 B
./.nuxt/dist/client/components/v-content-page.modern.js 467 B
./.nuxt/dist/client/components/v-content-report-button.js 777 B
./.nuxt/dist/client/components/v-content-report-button.modern.js 779 B
./.nuxt/dist/client/components/v-copy-button.js 3.94 kB
./.nuxt/dist/client/components/v-copy-button.modern.js 3.95 kB
./.nuxt/dist/client/components/v-copy-license.modern.js 2.93 kB
./.nuxt/dist/client/components/v-copy-license/components/v-error-image/components/v-media-reuse/components/v-search-grid/09090664.js 9.69 kB
./.nuxt/dist/client/components/v-copy-license/components/v-error-image/components/v-media-reuse/components/v-search-grid/09090664.modern.js 9.67 kB
./.nuxt/dist/client/components/v-dmca-notice.js 754 B
./.nuxt/dist/client/components/v-dmca-notice.modern.js 759 B
./.nuxt/dist/client/components/v-error-image.js 1.69 kB
./.nuxt/dist/client/components/v-error-section.js 387 B
./.nuxt/dist/client/components/v-error-section.modern.js 391 B
./.nuxt/dist/client/components/v-full-layout.js 1.45 kB
./.nuxt/dist/client/components/v-full-layout.modern.js 1.45 kB
./.nuxt/dist/client/components/v-grid-skeleton.js 1.62 kB
./.nuxt/dist/client/components/v-grid-skeleton.modern.js 1.63 kB
./.nuxt/dist/client/components/v-image-cell-square.js 1.02 kB
./.nuxt/dist/client/components/v-image-cell-square.modern.js 1.02 kB
./.nuxt/dist/client/components/v-image-cell.js 1.43 kB
./.nuxt/dist/client/components/v-image-cell.modern.js 1.42 kB
./.nuxt/dist/client/components/v-image-details.modern.js 1.44 kB
./.nuxt/dist/client/components/v-image-grid.js 2.59 kB
./.nuxt/dist/client/components/v-image-grid.modern.js 2.47 kB
./.nuxt/dist/client/components/v-license-tab-panel.js 933 B
./.nuxt/dist/client/components/v-license-tab-panel.modern.js 926 B
./.nuxt/dist/client/components/v-load-more.js 816 B
./.nuxt/dist/client/components/v-load-more.modern.js 710 B
./.nuxt/dist/client/components/v-media-license.modern.js 813 B
./.nuxt/dist/client/components/v-media-reuse.modern.js 1.03 kB
./.nuxt/dist/client/components/v-media-reuse/pages/image/_id.modern.js 2.93 kB
./.nuxt/dist/client/components/v-media-tag.js 432 B
./.nuxt/dist/client/components/v-media-tag.modern.js 436 B
./.nuxt/dist/client/components/v-meta-search-form.modern.js 2.69 kB
./.nuxt/dist/client/components/v-meta-source-list.modern.js 2.03 kB
./.nuxt/dist/client/components/v-no-results.js 2.25 kB
./.nuxt/dist/client/components/v-no-results.modern.js 2.22 kB
./.nuxt/dist/client/components/v-radio.js 1.52 kB
./.nuxt/dist/client/components/v-radio.modern.js 1.48 kB
./.nuxt/dist/client/components/v-related-audio.js 1.25 kB
./.nuxt/dist/client/components/v-related-audio.modern.js 1.25 kB
./.nuxt/dist/client/components/v-related-images.modern.js 3.03 kB
./.nuxt/dist/client/components/v-report-desc-form.js 960 B
./.nuxt/dist/client/components/v-report-desc-form.modern.js 961 B
./.nuxt/dist/client/components/v-row-layout.js 1.81 kB
./.nuxt/dist/client/components/v-row-layout.modern.js 1.81 kB
./.nuxt/dist/client/components/v-scroll-button.js 807 B
./.nuxt/dist/client/components/v-scroll-button.modern.js 814 B
./.nuxt/dist/client/components/v-search-grid.js 5.06 kB
./.nuxt/dist/client/components/v-search-grid.modern.js 5.01 kB
./.nuxt/dist/client/components/v-search-results-title.modern.js 668 B
./.nuxt/dist/client/components/v-search-type-radio.modern.js 822 B
./.nuxt/dist/client/components/v-server-timeout.js 315 B
./.nuxt/dist/client/components/v-server-timeout.modern.js 320 B
./.nuxt/dist/client/components/v-sketch-fab-viewer.js 997 B
./.nuxt/dist/client/components/v-sketch-fab-viewer.modern.js 893 B
./.nuxt/dist/client/components/v-skip-to-content-container.js 903 B
./.nuxt/dist/client/components/v-skip-to-content-container.modern.js 905 B
./.nuxt/dist/client/components/v-sources-table.js 14.3 kB
./.nuxt/dist/client/components/v-sources-table.modern.js 14.4 kB
./.nuxt/dist/client/components/v-tab-panel.js 689 B
./.nuxt/dist/client/components/v-tab-panel.modern.js 684 B
./.nuxt/dist/client/components/v-tab.modern.js 1.72 kB
./.nuxt/dist/client/components/v-warning-suppressor.js 300 B
./.nuxt/dist/client/components/v-warning-suppressor.modern.js 305 B
./.nuxt/dist/client/lang-af.json.js 147 B
./.nuxt/dist/client/lang-af.json.modern.js 154 B
./.nuxt/dist/client/lang-am.json.js 147 B
./.nuxt/dist/client/lang-am.json.modern.js 154 B
./.nuxt/dist/client/lang-an.json.js 147 B
./.nuxt/dist/client/lang-an.json.modern.js 153 B
./.nuxt/dist/client/lang-ar.json.js 251 B
./.nuxt/dist/client/lang-arq.json.js 148 B
./.nuxt/dist/client/lang-arq.json.modern.js 155 B
./.nuxt/dist/client/lang-ary.json.js 148 B
./.nuxt/dist/client/lang-ary.json.modern.js 155 B
./.nuxt/dist/client/lang-as.json.js 147 B
./.nuxt/dist/client/lang-as.json.modern.js 154 B
./.nuxt/dist/client/lang-az-tr.json.js 150 B
./.nuxt/dist/client/lang-az-tr.json.modern.js 157 B
./.nuxt/dist/client/lang-az.json.js 147 B
./.nuxt/dist/client/lang-az.json.modern.js 154 B
./.nuxt/dist/client/lang-azb.json.js 148 B
./.nuxt/dist/client/lang-azb.json.modern.js 155 B
./.nuxt/dist/client/lang-ba.json.js 147 B
./.nuxt/dist/client/lang-ba.json.modern.js 154 B
./.nuxt/dist/client/lang-bel.json.js 148 B
./.nuxt/dist/client/lang-bel.json.modern.js 155 B
./.nuxt/dist/client/lang-bg.json.js 147 B
./.nuxt/dist/client/lang-bg.json.modern.js 154 B
./.nuxt/dist/client/lang-bn-in.json.js 150 B
./.nuxt/dist/client/lang-bn-in.json.modern.js 156 B
./.nuxt/dist/client/lang-bn.json.js 147 B
./.nuxt/dist/client/lang-bn.json.modern.js 153 B
./.nuxt/dist/client/lang-bo.json.js 147 B
./.nuxt/dist/client/lang-bo.json.modern.js 154 B
./.nuxt/dist/client/lang-br.json.js 147 B
./.nuxt/dist/client/lang-br.json.modern.js 154 B
./.nuxt/dist/client/lang-bs.json.js 147 B
./.nuxt/dist/client/lang-bs.json.modern.js 154 B
./.nuxt/dist/client/lang-ca.json.js 9.54 kB
./.nuxt/dist/client/lang-ca.json.modern.js 9.54 kB
./.nuxt/dist/client/lang-ckb.json.js 148 B
./.nuxt/dist/client/lang-ckb.json.modern.js 155 B
./.nuxt/dist/client/lang-co.json.js 147 B
./.nuxt/dist/client/lang-co.json.modern.js 154 B
./.nuxt/dist/client/lang-cor.json.js 148 B
./.nuxt/dist/client/lang-cor.json.modern.js 155 B
./.nuxt/dist/client/lang-cs.json.js 10 kB
./.nuxt/dist/client/lang-cs.json.modern.js 10 kB
./.nuxt/dist/client/lang-cy.json.js 9.55 kB
./.nuxt/dist/client/lang-cy.json.modern.js 9.55 kB
./.nuxt/dist/client/lang-da.json.js 9.17 kB
./.nuxt/dist/client/lang-da.json.modern.js 9.18 kB
./.nuxt/dist/client/lang-de-at.json.js 150 B
./.nuxt/dist/client/lang-de-at.json.modern.js 157 B
./.nuxt/dist/client/lang-de-ch.json.js 150 B
./.nuxt/dist/client/lang-de-ch.json.modern.js 157 B
./.nuxt/dist/client/lang-de.json.js 10.1 kB
./.nuxt/dist/client/lang-de.json.modern.js 10.1 kB
./.nuxt/dist/client/lang-dv.json.js 147 B
./.nuxt/dist/client/lang-dv.json.modern.js 154 B
./.nuxt/dist/client/lang-dzo.json.js 148 B
./.nuxt/dist/client/lang-dzo.json.modern.js 155 B
./.nuxt/dist/client/lang-el.json.js 11.9 kB
./.nuxt/dist/client/lang-el.json.modern.js 12 kB
./.nuxt/dist/client/lang-en-ca.json.js 8.56 kB
./.nuxt/dist/client/lang-en-gb.json.js 8.72 kB
./.nuxt/dist/client/lang-en-nz.json.js 150 B
./.nuxt/dist/client/lang-en-nz.json.modern.js 157 B
./.nuxt/dist/client/lang-en-za.json.js 5.08 kB
./.nuxt/dist/client/lang-en-za.json.modern.js 5.09 kB
./.nuxt/dist/client/lang-en.json.js 8.62 kB
./.nuxt/dist/client/lang-en.json.modern.js 8.63 kB
./.nuxt/dist/client/lang-eo.json.js 147 B
./.nuxt/dist/client/lang-eo.json.modern.js 154 B
./.nuxt/dist/client/lang-es-ar.json.js 9.61 kB
./.nuxt/dist/client/lang-es-cl.json.js 150 B
./.nuxt/dist/client/lang-es-co.json.js 9.32 kB
./.nuxt/dist/client/lang-es-co.json.modern.js 9.33 kB
./.nuxt/dist/client/lang-es-cr.json.js 150 B
./.nuxt/dist/client/lang-es-gt.json.js 151 B
./.nuxt/dist/client/lang-es-gt.json.modern.js 158 B
./.nuxt/dist/client/lang-es-hn.json.js 151 B
./.nuxt/dist/client/lang-es-hn.json.modern.js 157 B
./.nuxt/dist/client/lang-es-mx.json.modern.js 9.53 kB
./.nuxt/dist/client/lang-es-pe.json.js 151 B
./.nuxt/dist/client/lang-es-pe.json.modern.js 158 B
./.nuxt/dist/client/lang-es-pr.json.js 151 B
./.nuxt/dist/client/lang-es-pr.json.modern.js 158 B
./.nuxt/dist/client/lang-es-uy.json.js 151 B
./.nuxt/dist/client/lang-es-uy.json.modern.js 158 B
./.nuxt/dist/client/lang-es-ve.json.modern.js 9.33 kB
./.nuxt/dist/client/lang-es.json.modern.js 9.52 kB
./.nuxt/dist/client/lang-et.json.js 148 B
./.nuxt/dist/client/lang-et.json.modern.js 155 B
./.nuxt/dist/client/lang-eu.json.js 148 B
./.nuxt/dist/client/lang-eu.json.modern.js 155 B
./.nuxt/dist/client/lang-fa-af.json.js 151 B
./.nuxt/dist/client/lang-fa-af.json.modern.js 158 B
./.nuxt/dist/client/lang-fa.json.modern.js 3.64 kB
./.nuxt/dist/client/lang-fi.json.js 148 B
./.nuxt/dist/client/lang-fi.json.modern.js 155 B
./.nuxt/dist/client/lang-fo.json.js 148 B
./.nuxt/dist/client/lang-fo.json.modern.js 155 B
./.nuxt/dist/client/lang-fr-be.json.js 151 B
./.nuxt/dist/client/lang-fr-be.json.modern.js 158 B
./.nuxt/dist/client/lang-fr-ca.json.js 151 B
./.nuxt/dist/client/lang-fr-ca.json.modern.js 158 B
./.nuxt/dist/client/lang-fr.json.js 7.12 kB
./.nuxt/dist/client/lang-fr.json.modern.js 7.13 kB
./.nuxt/dist/client/lang-fuc.json.js 149 B
./.nuxt/dist/client/lang-fuc.json.modern.js 156 B
./.nuxt/dist/client/lang-fy.json.js 148 B
./.nuxt/dist/client/lang-fy.json.modern.js 155 B
./.nuxt/dist/client/lang-ga.json.js 148 B
./.nuxt/dist/client/lang-ga.json.modern.js 155 B
./.nuxt/dist/client/lang-gd.json.js 148 B
./.nuxt/dist/client/lang-gd.json.modern.js 155 B
./.nuxt/dist/client/lang-gl.json.js 9.38 kB
./.nuxt/dist/client/lang-gl.json.modern.js 9.38 kB
./.nuxt/dist/client/lang-gu.json.js 148 B
./.nuxt/dist/client/lang-gu.json.modern.js 155 B
./.nuxt/dist/client/lang-hat.json.js 149 B
./.nuxt/dist/client/lang-hat.json.modern.js 156 B
./.nuxt/dist/client/lang-hau.json.js 149 B
./.nuxt/dist/client/lang-hau.json.modern.js 156 B
./.nuxt/dist/client/lang-he.json.js 148 B
./.nuxt/dist/client/lang-he.json.modern.js 155 B
./.nuxt/dist/client/lang-hr.json.js 148 B
./.nuxt/dist/client/lang-hr.json.modern.js 155 B
./.nuxt/dist/client/lang-hy.json.js 148 B
./.nuxt/dist/client/lang-hy.json.modern.js 155 B
./.nuxt/dist/client/lang-id.json.js 8.22 kB
./.nuxt/dist/client/lang-ido.json.js 149 B
./.nuxt/dist/client/lang-ido.json.modern.js 156 B
./.nuxt/dist/client/lang-is.json.js 148 B
./.nuxt/dist/client/lang-is.json.modern.js 155 B
./.nuxt/dist/client/lang-it.json.js 9.5 kB
./.nuxt/dist/client/lang-it.json.modern.js 9.5 kB
./.nuxt/dist/client/lang-ka.json.js 148 B
./.nuxt/dist/client/lang-ka.json.modern.js 155 B
./.nuxt/dist/client/lang-km.json.js 148 B
./.nuxt/dist/client/lang-km.json.modern.js 155 B
./.nuxt/dist/client/lang-kmr.json.js 149 B
./.nuxt/dist/client/lang-kmr.json.modern.js 156 B
./.nuxt/dist/client/lang-ko.json.js 148 B
./.nuxt/dist/client/lang-ko.json.modern.js 155 B
./.nuxt/dist/client/lang-lb.json.js 148 B
./.nuxt/dist/client/lang-lb.json.modern.js 155 B
./.nuxt/dist/client/lang-li.json.js 148 B
./.nuxt/dist/client/lang-li.json.modern.js 155 B
./.nuxt/dist/client/lang-lin.json.js 149 B
./.nuxt/dist/client/lang-lin.json.modern.js 155 B
./.nuxt/dist/client/lang-lo.json.js 148 B
./.nuxt/dist/client/lang-lo.json.modern.js 155 B
./.nuxt/dist/client/lang-lt.json.js 148 B
./.nuxt/dist/client/lang-lt.json.modern.js 155 B
./.nuxt/dist/client/lang-lug.json.js 149 B
./.nuxt/dist/client/lang-lug.json.modern.js 156 B
./.nuxt/dist/client/lang-lv.json.js 6.52 kB
./.nuxt/dist/client/lang-lv.json.modern.js 6.52 kB
./.nuxt/dist/client/lang-mg.json.js 148 B
./.nuxt/dist/client/lang-mg.json.modern.js 155 B
./.nuxt/dist/client/lang-mk.json.js 148 B
./.nuxt/dist/client/lang-mk.json.modern.js 155 B
./.nuxt/dist/client/lang-mn.json.js 148 B
./.nuxt/dist/client/lang-mn.json.modern.js 154 B
./.nuxt/dist/client/lang-mr.json.js 148 B
./.nuxt/dist/client/lang-mr.json.modern.js 155 B
./.nuxt/dist/client/lang-mri.json.js 149 B
./.nuxt/dist/client/lang-mri.json.modern.js 156 B
./.nuxt/dist/client/lang-mya.json.js 149 B
./.nuxt/dist/client/lang-mya.json.modern.js 156 B
./.nuxt/dist/client/lang-nb.json.js 5.01 kB
./.nuxt/dist/client/lang-nb.json.modern.js 5.02 kB
./.nuxt/dist/client/lang-nn.json.js 148 B
./.nuxt/dist/client/lang-nn.json.modern.js 154 B
./.nuxt/dist/client/lang-oci.json.js 149 B
./.nuxt/dist/client/lang-oci.json.modern.js 156 B
./.nuxt/dist/client/lang-ory.json.js 149 B
./.nuxt/dist/client/lang-ory.json.modern.js 156 B
./.nuxt/dist/client/lang-os.json.js 148 B
./.nuxt/dist/client/lang-os.json.modern.js 155 B
./.nuxt/dist/client/lang-ps.json.js 148 B
./.nuxt/dist/client/lang-ps.json.modern.js 155 B
./.nuxt/dist/client/lang-pt.json.js 412 B
./.nuxt/dist/client/lang-ro.json.js 9.63 kB
./.nuxt/dist/client/lang-roh.json.js 149 B
./.nuxt/dist/client/lang-roh.json.modern.js 156 B
./.nuxt/dist/client/lang-ru.json.modern.js 4.21 kB
./.nuxt/dist/client/lang-si.json.js 148 B
./.nuxt/dist/client/lang-si.json.modern.js 155 B
./.nuxt/dist/client/lang-sk.json.js 268 B
./.nuxt/dist/client/lang-sk.json.modern.js 271 B
./.nuxt/dist/client/lang-sna.json.js 149 B
./.nuxt/dist/client/lang-sna.json.modern.js 156 B
./.nuxt/dist/client/lang-snd.json.js 149 B
./.nuxt/dist/client/lang-snd.json.modern.js 156 B
./.nuxt/dist/client/lang-sq-xk.json.js 151 B
./.nuxt/dist/client/lang-sq-xk.json.modern.js 158 B
./.nuxt/dist/client/lang-sr.json.js 148 B
./.nuxt/dist/client/lang-sr.json.modern.js 155 B
./.nuxt/dist/client/lang-srd.json.js 149 B
./.nuxt/dist/client/lang-srd.json.modern.js 156 B
./.nuxt/dist/client/lang-ssw.json.js 149 B
./.nuxt/dist/client/lang-ssw.json.modern.js 156 B
./.nuxt/dist/client/lang-sw.json.js 148 B
./.nuxt/dist/client/lang-sw.json.modern.js 155 B
./.nuxt/dist/client/lang-ta-lk.json.js 151 B
./.nuxt/dist/client/lang-ta-lk.json.modern.js 158 B
./.nuxt/dist/client/lang-ta.json.js 148 B
./.nuxt/dist/client/lang-ta.json.modern.js 155 B
./.nuxt/dist/client/lang-tah.json.js 149 B
./.nuxt/dist/client/lang-tah.json.modern.js 156 B
./.nuxt/dist/client/lang-te.json.js 3.48 kB
./.nuxt/dist/client/lang-tg.json.js 148 B
./.nuxt/dist/client/lang-tg.json.modern.js 155 B
./.nuxt/dist/client/lang-th.json.js 148 B
./.nuxt/dist/client/lang-th.json.modern.js 155 B
./.nuxt/dist/client/lang-tl.json.js 148 B
./.nuxt/dist/client/lang-tl.json.modern.js 155 B
./.nuxt/dist/client/lang-tt.json.js 148 B
./.nuxt/dist/client/lang-tt.json.modern.js 155 B
./.nuxt/dist/client/lang-tuk.json.js 149 B
./.nuxt/dist/client/lang-tuk.json.modern.js 156 B
./.nuxt/dist/client/lang-ug.json.js 148 B
./.nuxt/dist/client/lang-ug.json.modern.js 155 B
./.nuxt/dist/client/lang-uk.json.js 148 B
./.nuxt/dist/client/lang-uk.json.modern.js 155 B
./.nuxt/dist/client/lang-vi.json.js 148 B
./.nuxt/dist/client/lang-vi.json.modern.js 155 B
./.nuxt/dist/client/lang-wol.json.js 149 B
./.nuxt/dist/client/lang-wol.json.modern.js 156 B
./.nuxt/dist/client/lang-zh-cn.json.js 4.9 kB
./.nuxt/dist/client/lang-zh-hk.json.js 151 B
./.nuxt/dist/client/lang-zh-hk.json.modern.js 158 B
./.nuxt/dist/client/lang-zh-sg.json.js 151 B
./.nuxt/dist/client/lang-zh-sg.json.modern.js 158 B
./.nuxt/dist/client/lang-zh-tw.json.js 4.24 kB
./.nuxt/dist/client/lang-zul.json.js 149 B
./.nuxt/dist/client/lang-zul.json.modern.js 156 B
./.nuxt/dist/client/pages/audio/_id.modern.js 6.23 kB
./.nuxt/dist/client/pages/feedback.modern.js 3.2 kB
./.nuxt/dist/client/pages/image/_id.js 6.98 kB
./.nuxt/dist/client/pages/preferences.modern.js 1.26 kB
./.nuxt/dist/client/pages/search-help.js 1.53 kB
./.nuxt/dist/client/pages/search/audio.js 2.87 kB
./.nuxt/dist/client/pages/search/audio.modern.js 2.77 kB
./.nuxt/dist/client/pages/search/image.js 2.97 kB
./.nuxt/dist/client/pages/search/image.modern.js 2.84 kB
./.nuxt/dist/client/pages/search/index.modern.js 737 B
./.nuxt/dist/client/pages/search/model-3d.modern.js 263 B
./.nuxt/dist/client/pages/search/search-page.types.js 266 B
./.nuxt/dist/client/pages/search/search-page.types.modern.js 271 B
./.nuxt/dist/client/pages/search/video.js 257 B
./.nuxt/dist/client/vendors/app.js 48.9 kB
./.nuxt/dist/client/vendors/sentry.modern.js 41.5 kB

compressed-size-action

Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

Interesting approach! I kind of wish it was possible to do this without relying on Nuxt context. Is there a reason we need to use it? I have to be honest I still don't really understand the point of Vue and Nuxt context stuff. Especially with how hacky and awkward it is to use them from the composition API, which we'll hopefully be able to move away from.

The other question I have is how the token will get refreshed when it expires?

@dhruvkb
Copy link
Member Author

dhruvkb commented Jul 15, 2022

  1. Nuxt exposes the values in privateRuntimeConfig (which I understand is the "blessed" way to keep secrets like API keys) through context.$config so I don't suppose it's possible to access it in any other way and we'll be quite locked into the Nuxt context. I agree that Nuxt's context is a massive hack (so huge that their docs can't even explain it properly) but that's what we have at the moment.

  2. That's the roadblock I'm stuck at, the access token is read once when the server starts and isn't refreshed. I'm trying to see if the $config variable is mutable somehow (and if it is, where to mutate it?) but I'm afraid it'll break reactivity or something (needs further experimentation).

@sarayourfriend
Copy link
Contributor

sarayourfriend commented Jul 15, 2022

If we put the client key and secret into the privateRuntimeConfig, could we put a function into context instead of the actual API key? Otherwise, we could add a server-only middleware that checks a module-level variable that stores the API key and expiration time.

// ~/plugins/api-authentication.server.ts

import type { Plugin, Context } from '@nuxt/types'

interface State {
  apiKey: string
  expiration: Date
}

const local: { state: State | null } = {
  state: null,
}

const getApiKey = async (ctx: Context): State => {
  /* ... */
}

const expiresSoon = (s: State): boolean => {
  /* ... */
}

const apiAuthentication: Plugin = async (ctx, inject) => {
  if (!local.state || expiresSoon(local.state)) {
    local.state = await getApiKey(ctx)
  }

  inject('openverseApiKey', local.state.apiKey)
}

@sarayourfriend
Copy link
Contributor

It just sucks to have to have the apiService wired up and dependent on Nuxt state. It's suck a hack, like you said, and it infects everything.

@sarayourfriend
Copy link
Contributor

sarayourfriend commented Jul 15, 2022

With the code above I suggested I think getApiKey would need an async lock on it as well to prevent multiple requests in the same worker from trying to get a token and creating a race condition on the local.state property 🤔

https://www.npmjs.com/package/async-lock or the like might be helpful for that, basically for the same use-case they mention in the docs, just not with Redis. https://www.npmjs.com/package/async-mutex might be simpler and appears to be more widely used.

@dhruvkb
Copy link
Member Author

dhruvkb commented Jul 22, 2022

@sarayourfriend I tried this with a server-side plugin and it was refreshing the access token on every request. It seems to me that plugins are reinitialised on every request and so their state always starts as a blank one.

We will need to store this key outside of Nuxt (in Redis, memcached or Node-Cache etc.) so that it can be persisted on the server outside of the Nuxt lifecycle.

@sarayourfriend
Copy link
Contributor

it was refreshing the access token on every request

This is odd. I can reproduce it in the dev build but in the production build it works as expected (like a normal node module resolution).

I'll have some changes up soon.

@sarayourfriend
Copy link
Contributor

Changes pushed. They work if you do the following:

  1. Create a .env file with API_URL pointing to your local API
  2. Add API_CLIENT_ID and API_CLIENT_SECRET with client credentials for a local API exempted token
  3. Build and run the server: env $(cat .env) pnpm build && env $(cat .env) pnpm start

You can try the /secret-test page but also just check out the console as I've added additional logging to make it easy to track the request through the plugin.

@sarayourfriend
Copy link
Contributor

sarayourfriend commented Jul 22, 2022

Unit tests are currently failing due to missing API token so we'll have to sort out a way around that. Pushing the token into context makes the component code that relies on it a lot simpler. No need to do conditional checks or handle asynchrony. I'll come back and continue working on this later today.

@sarayourfriend sarayourfriend marked this pull request as ready for review July 25, 2022 17:19
@sarayourfriend sarayourfriend requested a review from a team as a code owner July 25, 2022 17:19
@sarayourfriend sarayourfriend requested review from krysal and obulat July 25, 2022 17:19
@sarayourfriend
Copy link
Contributor

This PR is ready for review now and the description has been updated with up-to-date testing instructions. You can also visit the /secret-test page, but I will remove that before merging the PR.

/**
* the schema of the API service
*/
export interface ApiService {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

TESTING_GUIDELINES.md Outdated Show resolved Hide resolved
Copy link
Member Author

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

I ran this locally and the "happy flow" seems to be working pretty well. I'm currently trying to understand how we could test these events:

  • access tokens expiring
  • token fetch failing (perhaps invalid credentials)
  • requests being blocked till the token is refreshed

src/plugins/api-token.server.ts Show resolved Hide resolved
@sarayourfriend
Copy link
Contributor

I think we could test all of those using the axios mock adapter. Thanks for pointing them out. I will write those tests.

Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

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

This works well on my end, even the docker build on mac with some extra packages. I have some questions, but nothing worth blocking, I believe 👏

Perhaps when the token expires we can force a hard refresh at the next request, so the request is done server side, and then the token gets refreshed? It's not ideal but could be a solution in the interim.

],
css: ['~/styles/tailwind.css', '~/assets/fonts.css', '~/styles/accent.css'],
head,
env,
env, // TODO: Replace with `publicRuntimeConfig`
Copy link
Member

Choose a reason for hiding this comment

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

Is this comment valid? When will we have to replace it?

Copy link
Contributor

@sarayourfriend sarayourfriend Jul 29, 2022

Choose a reason for hiding this comment

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

I don't know when we would change this. @dhruvkb what did you have in mind? I think it should be a separate issue. Can you cut an issue for this if so?

src/data/api-service.ts Show resolved Hide resolved
Comment on lines +146 to +155
handleTab(event: KeyboardEvent, element: string) {
if (this.showScrollButton.value && element !== 'scroll-button') {
return
}
focusIn(document.getElementById('__layout'), Focus.First)
},
fetchMedia(...args: unknown[]) {
const mediaStore = useMediaStore(this.$pinia)
return mediaStore.fetchMedia(...args)
},
Copy link
Member

Choose a reason for hiding this comment

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

Why did these functions change to the Option API?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because you can't return functions in setup and also use Options API for some things or else Nuxt will raise the "cannot stringify unbound function" error in the console. See #1237 for where we've fixed this before.

@sarayourfriend sarayourfriend requested a review from krysal July 29, 2022 14:12
@sarayourfriend
Copy link
Contributor

Perhaps when the token expires we can force a hard refresh at the next request, so the request is done server side, and then the token gets refreshed? It's not ideal but could be a solution in the interim.

I don't I understand the suggestion or what problem it is trying to solve. The token requests always happen on the server side and the token is never sent to the client (this would be unsafe). When the token is about to expire, we pre-emptively request a new one and all other requests that come in will wait until the new token is retrieved.

@krysal I've also dismissed your review because I made some non-trivial changes to the logic and I want to make sure the additional documentation in the code is reviewed and comprehensible. Can you re-review with an eye towards that?

I've also added extensive unit tests, some of which are rather complicated due to juggling asynchrony in the tests themselves. @dhruvkb please review the tests and let me know if they are sufficient.

If anyone can think of additional tests to write, please let me know.

@krysal krysal requested a review from obulat July 29, 2022 14:30
@sarayourfriend sarayourfriend merged commit b863e3e into main Aug 5, 2022
@sarayourfriend sarayourfriend deleted the frontend_tokens branch August 5, 2022 12:47
github-actions bot pushed a commit that referenced this pull request Aug 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository 🧰 goal: internal improvement Improvement that benefits maintainers, not users 🟧 priority: high Stalls work on the project or its dependents
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow configuring an API key to use for server-side Openverse API requests
6 participants