UNPKG

oauth-entra-id

Version:

🛡️ A Secure, Performant, and Feature-Rich OAuth 2.0 Integration for Microsoft Entra ID — Fully Abstracted and Production-Ready.

83 lines (81 loc) 2.72 kB
import { $sharedHandleAuthentication, $sharedHandleCallback, $sharedHandleLogout, $sharedHandleOnBehalfOf, $sharedMiddleware } from './chunk-IJAH4JBE.js'; import { OAuthProvider, OAuthError } from './chunk-VN6QIMDR.js'; // src/exports/express.ts var ERROR_MSG = "authConfig not initialized or incorrect usage of Express handlers"; var ERROR_DESC = "Ensure you have called `authConfig(config)` during app setup before endpoints, and are importing all functions from the Express-specific entry point."; var expressOAuthProvider = void 0; function authConfig(config) { return (req, _res, next) => { if (!expressOAuthProvider) { expressOAuthProvider = new OAuthProvider(config); } req.oauthProvider = expressOAuthProvider; req.serverType = "express"; next(); }; } function handleAuthentication() { return async (req, res, next) => { try { if (!req.oauthProvider || req.serverType !== "express") { throw new OAuthError({ msg: ERROR_MSG, desc: ERROR_DESC, status: 500 }); } await $sharedHandleAuthentication(req, res); } catch (err) { next(err); } }; } function handleCallback() { return async (req, res, next) => { try { if (!req.oauthProvider || req.serverType !== "express") { throw new OAuthError({ msg: ERROR_MSG, desc: ERROR_DESC, status: 500 }); } await $sharedHandleCallback(req, res); } catch (err) { next(err); } }; } function handleLogout() { return async (req, res, next) => { try { if (!req.oauthProvider || req.serverType !== "express") { throw new OAuthError({ msg: ERROR_MSG, desc: ERROR_DESC, status: 500 }); } await $sharedHandleLogout(req, res); } catch (err) { next(err); } }; } function handleOnBehalfOf() { return async (req, res, next) => { try { if (!req.oauthProvider || req.serverType !== "express") { throw new OAuthError({ msg: ERROR_MSG, desc: ERROR_DESC, status: 500 }); } await $sharedHandleOnBehalfOf(req, res); } catch (err) { next(err); } }; } function protectRoute(cb) { return async (req, res, next) => { try { if (!req.oauthProvider || req.serverType !== "express") { throw new OAuthError({ msg: ERROR_MSG, desc: ERROR_DESC, status: 500 }); } const { userInfo, tryInjectData } = await $sharedMiddleware(req, res); if (cb) await cb({ userInfo, tryInjectData }); next(); } catch (err) { next(err); } }; } export { authConfig, expressOAuthProvider, handleAuthentication, handleCallback, handleLogout, handleOnBehalfOf, protectRoute }; //# sourceMappingURL=express.js.map //# sourceMappingURL=express.js.map