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

feature(auth): jwt as api key instead of ip addresses #7

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 101 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"express": "^4.19.2",
"express-rate-limit": "^7.3.1",
"helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2",
"openai": "^4.52.7",
"typescript": "^5.5.3"
},
Expand Down
46 changes: 44 additions & 2 deletions src/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const req = {} as Request;

const status = mock.fn<(status: number) => Response>(() => res);
const json = mock.fn<(body: any) => Response>(() => res);

Check warning on line 13 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type
const res = {
status,
json,
Expand All @@ -29,7 +29,11 @@

describe('getIndexHandler', () => {
it('should return the commit.sh file with the correct headers', async () => {
const req = {} as Request;
const req = {
headers: {
'user-agent': 'curl/8.6.0',
},
} as unknown as Request;

const readFileMock = mock.fn<(path: string, encoding: string) => Promise<string>>(() =>
Promise.resolve('echo "http://localhost"'),
Expand All @@ -51,7 +55,7 @@

const setHeaderMock = mock.fn<(name: string, value: string) => Response>(() => res);
const statusMock = mock.fn<(status: number) => Response>(() => res);
const sendMock = mock.fn<(body: any) => Response>(() => res);

Check warning on line 58 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type
const res = {
setHeader: setHeaderMock,
status: statusMock,
Expand Down Expand Up @@ -92,7 +96,11 @@
});

it('should return the cached commit.sh file if available', async () => {
const req = {} as Request;
const req = {
headers: {
'user-agent': 'curl/8.6.0',
},
} as unknown as Request;

const readFileMock = mock.fn<(path: string, encoding: string) => Promise<string>>();
const fs = {
Expand Down Expand Up @@ -149,6 +157,40 @@
assert.strictEqual(sendMock.mock.calls.length, 1);
assert.strictEqual(sendMock.mock.calls[0].arguments[0], cachedFile);
});

it('should return a message if the user-agent header does not include "curl"', async () => {
const req = {
headers: {
'user-agent': 'Mozilla/5.0',
},
} as unknown as Request;

const jsonMock = mock.fn<(body: any) => Response>(() => res);
const statusMock = mock.fn<(status: number) => Response>(() => res);
const res = {
status: statusMock,
json: jsonMock,
} as unknown as Response;

const extractDomainMock = mock.fn<(req: Request) => string>(() => 'http://example.com');

const handler = getIndexHandler(
{} as unknown as typeof import('node:fs/promises'),
{} as CacheType,
'commit.sh',
'/path/to/commit.sh',
extractDomainMock,
);
await handler(req, res);

assert.strictEqual(statusMock.mock.calls.length, 1);
assert.strictEqual(statusMock.mock.calls[0].arguments[0], 200);

assert.strictEqual(jsonMock.mock.calls.length, 1);
assert.deepStrictEqual(jsonMock.mock.calls[0].arguments[0], {
message: "Run this command from your terminal: 'curl -s http://example.com/ | sh'",
});
});
});

describe('postGenerateCommitMessageHandler', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export function getIndexHandler(
extractDomain: (req: Request) => string,
) {
return async (req: Request, res: Response) => {
if (!req.headers['user-agent']?.includes('curl')) {
const message = `Run this command from your terminal: 'curl -s ${extractDomain(req)}/ | sh'`;
return res.status(200).json({ message });
}

let file = cache.get(commitDotShPath);

if (!file) {
Expand Down
4 changes: 3 additions & 1 deletion src/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ describe('GET /healthz', () => {
describe('GET /', () => {
it('should return the script with domain replaced', async () => {
const readFileMock = mock.method(fs, 'readFile', async () => 'echo "http://localhost"');
const response = await fetch('http://localhost:3000/');
const response = await fetch('http://localhost:3000/', {
headers: { 'user-agent': 'curl/8.6.0' },
});
const body = await response.text();

assert.strictEqual(response.status, 200);
Expand Down
Loading