import { getInstance } from 'egg-aop' ; import { BadRequestError , ForbiddenError } from '../error' ; import { getParamDataObj } from '../param' ; import { getRoleInfo , BaseAuth } from '../auth/BaseAuth' ; import { Context , Application } from 'egg' ; import { RouteType } from '../type' ; export const SkipAuthSymbol = Symbol ( 'SkipAuthSymbol' ) ; export const authMiddleware = ( app , typeInfo ) => { if ( ! app . config . controller . auth ) { return ; } return async ( ctx , next ) => { if ( ( ctx as any ) [ SkipAuthSymbol ] ) { return next ( ) ; } const paramObj = await getParamDataObj ( ctx , typeInfo ) ; await Promise . all ( ( typeInfo . auth || [ ] ) . map ( async ( permRole ) => { const params = getRoleInfo ( permRole ) . params . map ( p => { if ( p . required && ! ( p . name in paramObj ) ) { throw new BadRequestError ( `Permission [${ permRole . name }] NEED Param [${ p . name }]` ) ; } return paramObj [ p . name ] ; } ) ; if ( ! ( await getInstance < BaseAuth > ( permRole , ctx . app , ctx ) . has ( ... params ) ) ) { throw new ForbiddenError ( `Forbidden, Need: [${ permRole . displayName || permRole . name }] Permission.` ) ; } } ) . filter ( r => r ) ) ; return next ( ) ; } ; } ;