Skip to content

Commit

Permalink
fix: api keys work after updating to null
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRibbens committed Apr 24, 2024
1 parent 3621d95 commit 9ae5808
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
11 changes: 7 additions & 4 deletions packages/payload/src/auth/baseFields/apiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ export default [
hidden: true,
hooks: {
beforeValidate: [
async ({ data, req, value }) => {
({ data, req, value }) => {
if (data.apiKey === false || data.apiKey === null) {
return null
}
if (data.enableAPIKey === false || data.enableAPIKey === null) {
return null
}
if (data.apiKey) {
return crypto
.createHmac('sha1', req.payload.secret)
.update(data.apiKey as string)
.digest('hex')
}
if (data.enableAPIKey === false) {
return null
}
return value
},
],
Expand Down
94 changes: 93 additions & 1 deletion test/auth/int.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { GraphQLClient } from 'graphql-request'
import jwtDecode from 'jwt-decode'
import { v4 as uuid } from 'uuid'

import type { User } from '../../packages/payload/src/auth'

import payload from '../../packages/payload/src'
import configPromise from '../collections-graphql/config'
import { devUser } from '../credentials'
import { initPayloadTest } from '../helpers/configHelpers'
import { namedSaveToJWTValue, saveToJWTKey, slug } from './shared'
import { apiKeysSlug, namedSaveToJWTValue, saveToJWTKey, slug } from './shared'

require('isomorphic-fetch')

Expand Down Expand Up @@ -680,5 +681,96 @@ describe('Auth', () => {

expect(fail.status).toStrictEqual(404)
})

it('should not remove an API key from a user when updating other fields', async () => {
const apiKey = uuid()
const user = await payload.create({
collection: 'api-keys',
data: {
apiKey,
enableAPIKey: true,
},
})

const updatedUser = await payload.update({
id: user.id,
collection: 'api-keys',
data: {
enableAPIKey: true,
},
})

const userResult = await payload.find({
collection: 'api-keys',
where: {
id: {
equals: user.id,
},
},
})

expect(updatedUser.apiKey).toStrictEqual(user.apiKey)
expect(userResult.docs[0].apiKey).toStrictEqual(user.apiKey)
})

it('should disable api key after updating apiKey: null', async () => {
const apiKey = uuid()
const user = await payload.create({
collection: apiKeysSlug,
data: {
apiKey,
enableAPIKey: true,
},
})

const updatedUser = await payload.update({
id: user.id,
collection: apiKeysSlug,
data: {
apiKey: null,
},
})

// use the api key in a fetch to assert that it is disabled
const response = await fetch(`${apiUrl}/${apiKeysSlug}/me`, {
headers: {
...headers,
Authorization: `${apiKeysSlug} API-Key ${apiKey}`,
},
}).then((res) => res.json())

expect(updatedUser.apiKey).toBeNull()
expect(response.user).toBeNull()
})

it('should disable api key after updating with enableAPIKey:false', async () => {
const apiKey = uuid()
const user = await payload.create({
collection: apiKeysSlug,
data: {
apiKey,
enableAPIKey: true,
},
})

const updatedUser = await payload.update({
id: user.id,
collection: apiKeysSlug,
data: {
enableAPIKey: false,
},
})

// use the api key in a fetch to assert that it is disabled
const response = await fetch(`${apiUrl}/${apiKeysSlug}/me`, {
headers: {
...headers,
Authorization: `${apiKeysSlug} API-Key ${apiKey}`,
},
}).then((res) => res.json())

expect(updatedUser.apiKey).toStrictEqual(apiKey)
expect(response.user).toBeNull()
})
})
})

0 comments on commit 9ae5808

Please sign in to comment.