Skip to content

Commit

Permalink
fix: api-key v2 data access
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 committed Jan 6, 2025
1 parent e3f9442 commit 03abf06
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
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

0 comments on commit 03abf06

Please sign in to comment.