-
Notifications
You must be signed in to change notification settings - Fork 8
feat: send auth credentials on all STAC API requests #71
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4539570
feat(client): inject bearer token into StacApiProvider and Api.fetch
alukach f104f07
refactor(client): address Task 1 review — drop cleanup, tighten URL s…
alukach 2a18a7e
test(api): cover bearer-token injection rules for Api.fetch
alukach a76a1cb
refactor(client): pass authed options into local useCollections' StacApi
alukach 0d4cb03
refactor(client): route collection mutations through Api.fetch auth i…
alukach 88c662d
Normalize STAC API URL
alukach 059b928
Simplify auth config
alukach 1afb98f
fix: Defer mounting StacApiProvider until OIDC has resolved.
alukach d91abf9
chore: cleanup
alukach 3cce30c
chore: lint & tests
alukach cfcea5c
chore: bump node types
alukach deb0290
refactor: update Api class to use constructor for auth token and base…
alukach f9a73e5
docs: add docs
alukach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| import '@testing-library/jest-dom'; | ||
|
|
||
| process.env.REACT_APP_STAC_API = 'https://fake-stac-api.net'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * @jest-environment node | ||
| */ | ||
| import Api from './index'; | ||
|
|
||
| const STAC_BASE = 'https://stac.example.com'; | ||
|
|
||
| describe('Api.fetch auth injection', () => { | ||
| let fetchSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| fetchSpy = jest | ||
| .spyOn(global, 'fetch') | ||
| .mockResolvedValue( | ||
| new Response(JSON.stringify({ ok: true }), { status: 200 }) | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fetchSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('adds Authorization when token is set and URL is under the STAC API base', async () => { | ||
| const api = new Api('abc123', STAC_BASE); | ||
|
|
||
| await api.fetch(`${STAC_BASE}/collections/foo`, { method: 'GET' }); | ||
|
|
||
| const init = fetchSpy.mock.calls[0][1] as RequestInit; | ||
| expect(init.headers).toMatchObject({ Authorization: 'Bearer abc123' }); | ||
| }); | ||
|
|
||
| it('omits Authorization when token is absent', async () => { | ||
| const api = new Api(undefined, STAC_BASE); | ||
|
|
||
| await api.fetch(`${STAC_BASE}/collections/foo`, { method: 'GET' }); | ||
|
|
||
| const init = fetchSpy.mock.calls[0][1] as RequestInit; | ||
| expect(init.headers || {}).not.toHaveProperty('Authorization'); | ||
| }); | ||
|
|
||
| it('omits Authorization for URLs outside the STAC API base', async () => { | ||
| const api = new Api('abc123', STAC_BASE); | ||
|
|
||
| await api.fetch('https://other.example.com/foo', { method: 'GET' }); | ||
|
|
||
| const init = fetchSpy.mock.calls[0][1] as RequestInit; | ||
| expect(init.headers || {}).not.toHaveProperty('Authorization'); | ||
| }); | ||
|
|
||
| it('lets caller headers override the injected Authorization', async () => { | ||
| const api = new Api('abc123', STAC_BASE); | ||
|
|
||
| await api.fetch(`${STAC_BASE}/collections/foo`, { | ||
| method: 'GET', | ||
| headers: { Authorization: 'Bearer override' } | ||
| }); | ||
|
|
||
| const init = fetchSpy.mock.calls[0][1] as RequestInit; | ||
| expect(init.headers).toMatchObject({ Authorization: 'Bearer override' }); | ||
| }); | ||
|
|
||
| it('omits Authorization when no STAC base URL is configured', async () => { | ||
| const api = new Api('abc123', undefined); | ||
|
|
||
| await api.fetch(`${STAC_BASE}/collections/foo`, { method: 'GET' }); | ||
|
|
||
| const init = fetchSpy.mock.calls[0][1] as RequestInit; | ||
| expect(init.headers || {}).not.toHaveProperty('Authorization'); | ||
| }); | ||
|
|
||
| it('does not match sibling paths (e.g. /stac-admin against /stac)', async () => { | ||
| const api = new Api('abc123', 'https://stac.example.com/stac'); | ||
|
|
||
| await api.fetch('https://stac.example.com/stac-admin/collections', { | ||
| method: 'GET' | ||
| }); | ||
|
|
||
| const init = fetchSpy.mock.calls[0][1] as RequestInit; | ||
| expect(init.headers || {}).not.toHaveProperty('Authorization'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.