Update api key validation

This commit is contained in:
Hongbo Wu 2022-05-27 11:36:57 +08:00
parent a09588d3a7
commit 9051a3f43a
2 changed files with 33 additions and 2 deletions

View file

@ -24,6 +24,7 @@ import ScalarResolvers from './scalars'
import * as Sentry from '@sentry/node'
import { createPubSubClient } from './datalayer/pubsub'
import { initModels } from './server'
import { claimsFromApiKey } from './utils/auth'
const signToken = promisify(jwt.sign)
const logger = buildLogger('app.dispatch')
@ -47,8 +48,12 @@ const contextFunc: ContextFunction<ExpressContext, ResolverContext> = async ({
variables: req.body.variables,
})
if (token && jwt.verify(token, env.server.jwtSecret)) {
claims = jwt.decode(token) as Claims
if (token) {
jwt.verify(token, env.server.jwtSecret) &&
(claims = jwt.decode(token) as Claims)
if (!claims) {
claims = await claimsFromApiKey(token)
}
}
async function setClaims(

View file

@ -1,5 +1,8 @@
import * as bcrypt from 'bcryptjs'
import { v4 as uuidv4 } from 'uuid'
import { Claims } from '../resolvers/types'
import { getRepository } from '../entity/utils'
import { ApiKey } from '../entity/api_key'
export const hashKey = (key: string, salt = 10) => {
return bcrypt.hashSync(key, salt)
@ -13,3 +16,26 @@ export const generateApiKey = (): string => {
// TODO: generate random string key
return uuidv4()
}
export const claimsFromApiKey = async (
key: string
): Promise<Claims | undefined> => {
const hashedKey = hashKey(key)
const apiKey = await getRepository(ApiKey).findOne({
where: { key: hashedKey },
relations: ['user'],
})
if (!apiKey) {
console.error('api key not found')
return undefined
}
// update last used
await getRepository(ApiKey).update(apiKey.id, { usedAt: new Date() })
return {
uid: apiKey.user.id,
iat: new Date().getTime(),
exp: apiKey.expiresAt.getTime(),
}
}