Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro modify schema #666

Open
mdbetancourt opened this issue Jun 4, 2024 · 1 comment
Open

Macro modify schema #666

mdbetancourt opened this issue Jun 4, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request feature request

Comments

@mdbetancourt
Copy link

mdbetancourt commented Jun 4, 2024

What is the problem this feature would solve?

import { Elysia } from 'elysia'
import { auth } from './auth'

const app = new Elysia()
    .use(auth)
    .get('/', () => 'hi', {
        isAuth: true,
        role: 'admin'
    })

in this example how the macro included by auth plugin could add schemas for authentication
like

{
headers: 'auth.Header',
    detail: {
      security: [
        {
          bearerAuth: []
        }
      ],
      parameters: []
    },
    response: {
      401: 'errors.Unauthorized'
    }
}

What is the feature you are proposing to solve the problem?

import { Elysia } from 'elysia'
import { auth } from './auth'

const app = new Elysia()
    .use(auth)
    .macro(() => ({
      isAuth(requireAuth: boolean) {
        // all logic in beforeHandle
  
        return {
          headers: 'auth.Header',
          detail: {
            security: [
              {
                bearerAuth: []
              }
            ],
            parameters: []
          },
          response: {
            401: 'errors.Unauthorized'
          }
        }
      }
    }))
    .get('/', () => 'hi', {
        isAuth: true,
        role: 'admin'
    })

What alternatives have you considered?

export const Authentication = new Elysia({ name: 'Service.Authentication' })
  .use(HttpErrors)
  .use(Credential)
  .model('auth.Header', t.Object({
    authorization: t.TemplateLiteral('Bearer ${string}')
  }))
  .guard({
    headers: 'auth.Header',
    detail: {
      security: [
        {
          bearerAuth: []
        }
      ],
      parameters: []
    },
    response: {
      401: 'errors.Unauthorized'
    }
  })
  .resolve({ as: 'global' }, async ({ headers: { authorization }, error, jwt }) => {
    if(!authorization) {
      return error('Unauthorized', {
        type: 'authentication',
        errors: [
          { message: 'invalid token' }
        ]
      })
    }
    const [_, token] = authorization.split(' ', 1)
  
    const isValidToken = await jwt.verify(token)

    if(!isValidToken) return error('Unauthorized', {
      type: 'authentication',
      errors: [
        { message: 'invalid token' }
      ]
    })

    return {
      user: isValidToken.payload.user as User,
      authorizationToken: token
    }
  })
app
  // unprotected routes
  .get(...)
  .guard(app => app
     .use(Authentication)
     .get(....)
     // endpoint to protect

i considered this way too but i'am not able to make the thing works

export function secured<H>(guarded: H): <const T extends Elysia, const O extends Elysia>(app: T) => AnyElysia {
  return (app) => app
    .use(HttpErrors)
    .use(Credential)
    .model('auth.Header', t.Object({
      authorization: t.TemplateLiteral('Bearer ${string}')
    }))
    .guard({
      headers: 'auth.Header',
      detail: {
        security: [
          {
            bearerAuth: []
          }
        ],
        parameters: []
      },
      response: {
        401: 'errors.Unauthorized'
      },
    }, guarded)
}

used as

app
.use(secured(app => app.get(...)))
@mdbetancourt mdbetancourt added the enhancement New feature or request label Jun 4, 2024
@kravetsone
Copy link
Contributor

+1

@SaltyAom SaltyAom self-assigned this Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature request
Projects
None yet
Development

No branches or pull requests

3 participants