-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: align auth failure error messages for oauth
In the Typescript CLI, when OAuth fails due to an invalid or expired token, throw the same "missing auth" error that one would get if not logged in (no auth token). The resulting error message will prompt the user to re-authenticate. This has to be mediated by the legacycli http proxy because the Typescript side of the CLI is not involved in the OAuth flow at all; it is handled transparently by the go-application-framework's networking middleware. A special HTTP request and response header is used by the legacycli proxy to indicate whether that middleware auth has failed. If it has, the functions which perform HTTP client requests with needle will throw an error that indicates "unauthenticated", resulting in an experience consistent with the legacy token auth flow in the CLI. CLI-392
- Loading branch information
Showing
9 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 @@ | ||
export const headerSnykAuthFailed = 'snyk-auth-failed'; |
This file contains 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 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
39 changes: 39 additions & 0 deletions
39
test/jest/unit/lib/ecosystems/resolve-test-facts.oauth.spec.ts
This file contains 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,39 @@ | ||
const mockMakeRequest = jest.fn(); | ||
|
||
import { Options } from '../../../../../src/lib/types'; | ||
import { MissingApiTokenError } from '../../../../../src/lib/errors'; | ||
|
||
import { resolveAndTestFacts } from '../../../../../src/lib/ecosystems/resolve-test-facts'; | ||
|
||
jest.mock('../../../../../src/lib/request/request', () => { | ||
return { | ||
makeRequest: mockMakeRequest, | ||
}; | ||
}); | ||
|
||
describe('oauth failure', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('rethrows same error for missing api token', async () => { | ||
mockMakeRequest.mockRejectedValue(new MissingApiTokenError()); | ||
await expect(resolveAndTestFacts('cpp', {}, {} as Options)).rejects.toThrow( | ||
expect.objectContaining({ | ||
message: | ||
'`snyk` requires an authenticated account. Please run `snyk auth` and try again.', | ||
}), | ||
); | ||
}); | ||
|
||
it('rethrows general error for other api auth failures', async () => { | ||
const err: any = new Error('nope'); | ||
err.code = 403; | ||
mockMakeRequest.mockRejectedValue(err); | ||
await expect(resolveAndTestFacts('cpp', {}, {} as Options)).rejects.toThrow( | ||
expect.objectContaining({ | ||
message: 'Unauthorized request to unmanaged service', | ||
}), | ||
); | ||
}); | ||
}); |
This file contains 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,30 @@ | ||
const mockNeedleRequest = jest.fn(); | ||
|
||
import { makeRequest } from '../../../../../src/lib/request/request'; | ||
import { Payload } from '../../../../../src/lib/request/types'; | ||
|
||
jest.mock('needle', () => { | ||
return { | ||
request: mockNeedleRequest, | ||
}; | ||
}); | ||
|
||
describe('needle header auth failed', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('throws missing api token on auth failed marker header', async () => { | ||
mockNeedleRequest.mockImplementation((method, url, data, options, fn) => { | ||
fn(null, { headers: { 'snyk-auth-failed': 'true' } }, {}); | ||
}); | ||
await expect( | ||
makeRequest({ url: 'https://example.com' } as Payload), | ||
).rejects.toThrow( | ||
expect.objectContaining({ | ||
message: | ||
'`snyk` requires an authenticated account. Please run `snyk auth` and try again.', | ||
}), | ||
); | ||
}); | ||
}); |
e3bfec3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementing an Information Retrieval System Using Inverted Index
Objective
The objective of this assignment is to develop a simple Information Retrieval (IR) system
that indexes a collection of documents using an inverted index and allows users to perform
basic search queries efficiently. Students will learn about text preprocessing, indexing, and
query retrieval.
Learning Outcomes
stemming.
Requirements
or Elasticsearch. (Basic libraries for file I/O, string operations, and data structures like
dictionaries in Python are allowed.)
Assignment Tasks
'run').
Construct an inverted index where:
each document.
Example Structure:
{
'term1': {'doc1': 3, 'doc3': 1},
'term2': {'doc2': 2, 'doc3': 4}
}
Allow users to input simple queries and implement the following retrieval models:
Index the document collection and provide a user interface (command-line or GUI) to:
Measure the accuracy of the retrieval system using a small test set with queries and relevant
documents (precision and recall metrics).
Prepare a report (3–5 pages) including:
Deliverables
group).
Grading Criteria
Component Weight
Inverted Index Implementation 30%
Query Processing 25%
System Features (Usability) 15%
Evaluation and Testing 10%
Report Quality 10%
Team Collaboration 10%
Suggested Tools
Deadline
24/12/2024
Additional Notes
Each group must ensure the originality of their work.
Use comments and modular programming practices to make the code easy to understand
and debug.
Feel free to consult with me for guidance on implementation challenges.