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

fix: api-key v2 data access #526

Merged
merged 2 commits into from
Jan 6, 2025
Merged
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
26 changes: 22 additions & 4 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ export default class ScopedApiKeyHandler extends AbstractHandler {
throw new Error('Data access is required');
}

const { ApiKey } = dataAccess;

const apiKeyFromHeader = headers['x-api-key'];
if (!hasText(apiKeyFromHeader)) {
return null;
}

// Keys are stored by their hash, so we need to hash the key to look it up
const hashedApiKey = hashWithSHA256(apiKeyFromHeader);
const apiKeyEntity = await dataAccess.getApiKeyByHashedApiKey(hashedApiKey);
const apiKeyEntity = await ApiKey.findByHashedApiKey(hashedApiKey);

if (!apiKeyEntity) {
this.log(`No API key entity found in the data layer for the provided API key: ${apiKeyFromHeader}`, 'error');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('auth wrapper', () => {
pathInfo: {
suffix: '',
},
dataAccess: {},
dataAccess: { ApiKey: { findByHashedApiKey: async () => mockApiKey } },
};
mockApiKey = createApiKey({
hashedApiKey: '372c6ba5a67b01a8d6c45e5ade6b41db9586ca06c77f0ef7795dfe895111fd0b',
Expand Down Expand Up @@ -118,7 +118,6 @@ describe('auth wrapper', () => {
const scopedAction = wrap(() => 42)
.with(authWrapper, { authHandlers: [ScopedApiKeyHandler] })
.with(enrichPathInfo);
context.dataAccess.getApiKeyByHashedApiKey = async () => mockApiKey;

const resp = await scopedAction(new Request('https://space.cat/', {
headers: { 'x-api-key': 'test-api-key' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('ScopedApiKeyHandler', () => {

mockContext = {
dataAccess: {
getApiKeyByHashedApiKey: sinon.stub().resolves(createApiKey(baseApiKeyData)),
ApiKey: { findByHashedApiKey: sinon.stub().resolves(createApiKey(baseApiKeyData)) },
},
pathInfo: {
headers: {
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('ScopedApiKeyHandler', () => {
const context = {
...mockContext,
dataAccess: {
getApiKeyByHashedApiKey: sinon.stub().resolves(null),
ApiKey: { findByHashedApiKey: sinon.stub().resolves(null) },
},
};

Expand All @@ -114,7 +114,7 @@ describe('ScopedApiKeyHandler', () => {
});

it('should return null if the API key has expired', async () => {
mockContext.dataAccess.getApiKeyByHashedApiKey = sinon.stub().resolves(createApiKey({
mockContext.dataAccess.ApiKey.findByHashedApiKey = sinon.stub().resolves(createApiKey({
...baseApiKeyData,
expiresAt: '2024-01-01T16:23:00.000Z',
}));
Expand All @@ -127,7 +127,7 @@ describe('ScopedApiKeyHandler', () => {
});

it('should return null if the API key has been revoked', async () => {
mockContext.dataAccess.getApiKeyByHashedApiKey = sinon.stub().resolves(createApiKey({
mockContext.dataAccess.ApiKey.findByHashedApiKey = sinon.stub().resolves(createApiKey({
...baseApiKeyData,
revokedAt: '2024-08-01T10:00:00.000Z',
}));
Expand Down
Loading