import{logger as e}from"../logger/index.mjs";import{ErrorHandler as t}from"../utils/errors/ErrorHandler.mjs";import{formatSession as n}from"../utils/auth/getAuth.mjs";import{authenticateOptions as r,getAuthModel as i}from"../utils/oAuth2.mjs";import{getOAuth2AccessTokenContext as a,validateOAuth2AccessToken as o}from"../services/oAuth2.service.mjs";import s,{Request as c,Response as l}from"oauth2-server";const u=new s({model:i(),accessTokenLifetime:3600,allowBearerTokensInQueryString:!0}),d=async(e,t)=>{e.oauth=u},f=async(i,s)=>{if(i.locals||={},i.locals?.authType===void 0)try{if(!i.headers.authorization)return;let t=new c({headers:i.headers,method:i.method,query:i.query,body:i.body}),u=new l(s.raw),d=n(await a(await o((await i.oauth.authenticate(t,u,r)).accessToken))),f=i.locals;if(f){f.authType=`session`;for(let[e,t]of Object.entries(d))f[e]=t}e.info(`OAuth2 bearer token authenticated`,d.user.email)}catch(e){t.handleAppErrorResponse(s,e);return}};export{d as attachOAuthInstance,f as oAuth2Middleware};
//# sourceMappingURL=oAuth2.middleware.mjs.map