import { ContextPlugin } from "@webiny/handler" ; import { SecurityContext } from "~/types" ; import { TenancyContext } from "@webiny/api-tenancy/types" ; type Context = TenancyContext & SecurityContext ; export interface Config { identityType ? : string ; } export default ( { identityType } ) => { return new ContextPlugin < Context > ( context => { context . security . addAuthenticator ( async ( token ) => { if ( typeof token !== "string" || ! token . startsWith ( "a" ) ) { return ; } const tenant = context . tenancy . getCurrentTenant ( ) ; const apiKey = await context . security . getStorageOperations ( ) . getApiKeyByToken ( { tenant : tenant . id , token } ) ; if ( ! apiKey ) { return ; } return { id : apiKey . id , displayName : apiKey . name , type : identityType || "api-key" , permissions : apiKey . permissions } ; } ) ; } ) ; } ;