Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
poc: remove auth
Browse files Browse the repository at this point in the history
  • Loading branch information
yyewolf committed Jul 13, 2024
1 parent c1c0d7f commit 6e05702
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 155 deletions.
269 changes: 133 additions & 136 deletions packages/server/src/app/api/challenge.js
Original file line number Diff line number Diff line change
@@ -1,153 +1,150 @@
import challengeResources from '../../k8s/resource.js'
import {
getInstance,
createInstance,
deleteInstance,
getInstance,
createInstance,
deleteInstance,
} from '../../k8s/instance.js'
import { InstanceCreationError, InstanceExistsError } from '../../error.js'

const routes = async (fastify, _options) => {
fastify.addHook('preHandler', async (req, res) => {
if (!challengeResources.has(req.params.challengeId)) {
res.notFound('Challenge does not exist')
}
})
fastify.addHook('preHandler', fastify.authenticate)
const routes = async(fastify, _options) => {
fastify.addHook('preHandler', async(req, res) => {
if (!challengeResources.has(req.params.challengeId)) {
res.notFound('Challenge does not exist')
}
})
fastify.addHook('preHandler', fastify.authenticate)

fastify.route({
method: 'GET',
url: '/:challengeId',
schema: {
response: {
200: {
type: 'object',
properties: {
name: { type: 'string' },
status: {
type: 'string',
enum: ['Stopped', 'Stopping', 'Unknown', 'Running', 'Starting'],
},
timeout: { type: 'integer' },
server: {
type: 'object',
properties: {
kind: { type: 'string' },
host: { type: 'string' },
port: { type: 'integer' },
},
required: ['kind', 'host'],
},
time: {
type: 'object',
properties: {
start: { type: 'integer' },
stop: { type: 'integer' },
remaining: { type: 'integer' },
},
required: ['start', 'stop', 'remaining'],
fastify.route({
method: 'GET',
url: '/:challengeId/:instanceId',
schema: {
response: {
200: {
type: 'object',
properties: {
name: { type: 'string' },
status: {
type: 'string',
enum: ['Stopped', 'Stopping', 'Unknown', 'Running', 'Starting'],
},
timeout: { type: 'integer' },
server: {
type: 'object',
properties: {
kind: { type: 'string' },
host: { type: 'string' },
port: { type: 'integer' },
},
required: ['kind', 'host'],
},
time: {
type: 'object',
properties: {
start: { type: 'integer' },
stop: { type: 'integer' },
remaining: { type: 'integer' },
},
required: ['start', 'stop', 'remaining'],
},
},
required: ['name', 'status', 'timeout'],
},
},
},
required: ['name', 'status', 'timeout'],
},
},
},
handler: async (req, _res) => {
const { challengeId } = req.params
const teamId = req.user.sub
return getInstance(challengeId, teamId)
},
})

fastify.route({
method: 'POST',
url: '/:challengeId/create',
schema: {
body: {
type: 'object',
properties: {
recaptcha: { type: 'string' },
handler: async(req, _res) => {
const { challengeId, instanceId } = req.params
return getInstance(challengeId, instanceId)
},
required: ['recaptcha'],
},
response: {
200: {
type: 'object',
properties: {
name: { type: 'string' },
status: { const: 'Starting' },
timeout: { type: 'integer' },
server: {
type: 'object',
properties: {
kind: { type: 'string' },
host: { type: 'string' },
port: { type: 'integer' },
},
required: ['kind', 'host'],
})

fastify.route({
method: 'POST',
url: '/:challengeId/:instanceId',
schema: {
body: {
type: 'object',
properties: {
recaptcha: { type: 'string' },
},
required: ['recaptcha'],
},
response: {
200: {
type: 'object',
properties: {
name: { type: 'string' },
status: { const: 'Starting' },
timeout: { type: 'integer' },
server: {
type: 'object',
properties: {
kind: { type: 'string' },
host: { type: 'string' },
port: { type: 'integer' },
},
required: ['kind', 'host'],
},
},
required: ['name', 'status', 'timeout', 'server'],
},
},
},
required: ['name', 'status', 'timeout', 'server'],
},
},
},
preHandler: [fastify.recaptcha],
handler: async (req, res) => {
const { challengeId } = req.params
const teamId = req.user.sub
preHandler: [fastify.recaptcha],
handler: async(req, res) => {
const { challengeId, instanceId } = req.params

try {
const instance = await createInstance(challengeId, teamId, req.log)
req.log.info('instance created')
return instance
} catch (err) {
if (err instanceof InstanceExistsError) {
req.log.debug(err)
req.log.debug(err.cause)
return res.conflict(err.message)
}
if (err instanceof InstanceCreationError) {
// this is likely a misconfiguration, so log it
req.log.error(err)
req.log.error(err.cause)
return res.conflict(err.message)
}
throw err
}
},
})
try {
const instance = await createInstance(challengeId, instanceId, req.log)
req.log.info('instance created')
return instance
} catch (err) {
if (err instanceof InstanceExistsError) {
req.log.debug(err)
req.log.debug(err.cause)
return res.conflict(err.message)
}
if (err instanceof InstanceCreationError) {
// this is likely a misconfiguration, so log it
req.log.error(err)
req.log.error(err.cause)
return res.conflict(err.message)
}
throw err
}
},
})

fastify.route({
method: 'POST',
url: '/:challengeId/delete',
schema: {
body: {
type: 'object',
properties: {
recaptcha: { type: 'string' },
fastify.route({
method: 'DELETE',
url: '/:challengeId/:instanceId',
schema: {
body: {
type: 'object',
properties: {
recaptcha: { type: 'string' },
},
required: ['recaptcha'],
},
response: {
200: {
type: 'object',
properties: {
name: { type: 'string' },
status: { const: 'Stopping' },
timeout: { type: 'integer' },
},
required: ['name', 'status', 'timeout'],
},
},
},
required: ['recaptcha'],
},
response: {
200: {
type: 'object',
properties: {
name: { type: 'string' },
status: { const: 'Stopping' },
timeout: { type: 'integer' },
},
required: ['name', 'status', 'timeout'],
preHandler: [fastify.recaptcha],
handler: async(req, _res) => {
const { challengeId, instanceId } = req.params
const instance = await deleteInstance(challengeId, instanceId, req.log)
req.log.info('instance deleted')
return instance
},
},
},
preHandler: [fastify.recaptcha],
handler: async (req, _res) => {
const { challengeId } = req.params
const teamId = req.user.sub
const instance = await deleteInstance(challengeId, teamId, req.log)
req.log.info('instance deleted')
return instance
},
})
})
}

export default routes
export default routes
12 changes: 6 additions & 6 deletions packages/server/src/app/api/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import auth from './auth.js'
import challenge from './challenge.js'

const routes = async (fastify, _options) => {
fastify.register(auth)
fastify.register(challenge, {
prefix: '/challenge',
})
const routes = async(fastify, _options) => {
fastify.register(auth)
fastify.register(challenge, {
prefix: '/v1',
})
}

export default routes
export default routes
26 changes: 13 additions & 13 deletions packages/server/src/app/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import jwt from '@fastify/jwt'

import config from '../config.js'

const auth = fp(async (fastify, _options) => {
fastify.register(jwt, {
secret: config.secretKey,
})
const auth = fp(async(fastify, _options) => {
fastify.register(jwt, {
secret: config.secretKey,
})

fastify.decorate('authenticate', async (req, res) => {
try {
await req.jwtVerify()
req.log.info({ user: req.user.sub }, 'user authenticated')
} catch (err) {
res.unauthorized('Invalid authorization token')
}
})
fastify.decorate('authenticate', async(req, res) => {
// try {
// await req.jwtVerify()
// req.log.info({ user: req.user.sub }, 'user authenticated')
// } catch (err) {
// res.unauthorized('Invalid authorization token')
// }
})
})

export default auth
export default auth

0 comments on commit 6e05702

Please sign in to comment.