Skip to content

Commit

Permalink
wip(idp): fixing various issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba committed Dec 14, 2024
1 parent 184ef65 commit 03df7fa
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
8 changes: 1 addition & 7 deletions idp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

## Getting Started

Run for development:

```shell
deno task dev
```

Run for production:
Run:

```shell
deno task start
Expand Down
1 change: 0 additions & 1 deletion idp/deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"tasks": {
"start": "deno run --allow-read --allow-write --allow-env --allow-sys --allow-net src/app.ts",
"dev": "deno run --allow-read --allow-write --allow-env --allow-sys --allow-net --reload src/app.ts",
"compile": "deno compile --allow-read --allow-write --allow-env --allow-sys --allow-net --output voltaserve-idp src/app.ts",
"check": "deno check src/app.ts"
},
Expand Down
36 changes: 24 additions & 12 deletions idp/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,26 @@ import { User } from '@/user/model.ts'
const app = new Hono()

app.onError((error, c) => {
if (error.name === 'UnauthorizedError') {
if (error.message === 'Unauthorized') {
return c.json(
newResponse(newError({ code: ErrorCode.InvalidCredentials })),
401,
)
} else {
const genericError = error as any
if (
genericError.code && Object.values(ErrorCode).includes(genericError.code)
) {
const data = genericError as ErrorData
console.error(error)
return c.json(
newResponse(newError({ code: ErrorCode.InternalServerError })),
500,
)
}
})

app.use('*', async (c, next) => {
try {
return await next()
} catch (error: any) {
if (error.code && Object.values(ErrorCode).includes(error.code)) {
const data = error as ErrorData
if (data.error) {
console.error(data.error)
}
Expand All @@ -55,16 +64,17 @@ app.onError((error, c) => {
}
})

app.route('/version', versionRouter)
app.route('/v3/accounts', accountRouter)

app.use('/v3/*', (c, next) => {
app.use('/v3/*', async (c, next) => {
const jwtMiddleware = jwt({ secret: getConfig().token.jwtSigningKey })
switch (c.req.path) {
case '/v3/token':
case '/v3/users/me/picture:extension':
return next()
case '/v3/health':
case '/v3/accounts':
case '/version':
return await next()
default:
return jwtMiddleware(c, next)
return await jwtMiddleware(c, next)
}
})

Expand All @@ -91,7 +101,9 @@ app.use('/v3/*', async (c, next) => {

app.route('/v3/health', healthRouter)
app.route('/v3/users', userRouter)
app.route('/v3/accounts', accountRouter)
app.route('/v3/token', tokenRouter)
app.route('/version', versionRouter)

postgres
.connect()
Expand Down
24 changes: 20 additions & 4 deletions idp/src/token/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@
// by the GNU Affero General Public License v3.0 only, included in the file
// AGPL-3.0-only in the root of this repository.
import { Hono } from 'hono'
import { z } from 'zod'
import { zValidator } from '@hono/zod-validator'
import { exchange, TokenExchangeOptions } from '@/token/service.ts'
import { handleValidationError } from '@/infra/error/validation.ts'

const router = new Hono()

router.post('/', async (c) => {
const options = (await c.req.json()) as TokenExchangeOptions
return c.json(await exchange(options))
})
router.post(
'/',
zValidator(
'form',
z.object({
grant_type: z.union([z.literal('password'), z.literal('refresh_token')]),
username: z.string().optional(),
password: z.string().optional(),
refresh_token: z.string().optional(),
}),
handleValidationError,
),
async (c) => {
const options = (await c.req.valid('form')) as TokenExchangeOptions
return c.json(await exchange(options))
},
)

export default router
8 changes: 6 additions & 2 deletions idp/src/user/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,12 @@ router.get(
'query',
z.object({
query: z.string().optional(),
page: z.number().int(),
size: z.number().int(),
page: z.string().regex(/^\d+$/, 'Must be a numeric value.').transform(
Number,
),
size: z.string().regex(/^\d+$/, 'Must be a numeric value.').transform(
Number,
),
}),
handleValidationError,
),
Expand Down

0 comments on commit 03df7fa

Please sign in to comment.