UNPKG

oauth-entra-id

Version:

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

95 lines (92 loc) 4.01 kB
'use strict'; var chunk7MTPXR5V_cjs = require('./chunk-7MTPXR5V.cjs'); // src/lite.ts var LiteProvider = class { /** * @param configuration The OAuth configuration object: * - `azure`: clientId, tenantId, and optional clientSecret with B2B apps. * @throws {OAuthError} if the config fails validation or has duplicate service names */ constructor(configuration) { const result = chunk7MTPXR5V_cjs.$jwtClientConfig(configuration); if (result.error) throw new chunk7MTPXR5V_cjs.OAuthError(result.error); this.azure = result.azure; this.jwksClient = result.jwksClient; } /** * Verifies a JWT token and extracts its payload and metadata. * @param jwtToken - Microsoft Entra ID Access Token (JWT). * @returns A result containing the JWT payload and metadata. */ async $verifyJwt(jwtToken) { const { data: token, error } = chunk7MTPXR5V_cjs.zJwt.safeParse(jwtToken); if (error) return chunk7MTPXR5V_cjs.$err({ msg: "Unauthorized", desc: "Access token is required", status: 401 }); return await chunk7MTPXR5V_cjs.$verifyJwt({ jwtToken: token, azure: this.azure, jwksClient: this.jwksClient }); } async tryGetB2BToken(params) { if (!this.azure.b2bApps || !this.azure.cca) { return chunk7MTPXR5V_cjs.$err({ msg: "Invalid configuration", desc: "B2B apps are not configured", status: 500 }); } const { data: parsedParams, error: paramsError } = chunk7MTPXR5V_cjs.zMethods.tryGetB2BToken.safeParse(params); if (paramsError) return chunk7MTPXR5V_cjs.$err({ msg: "Invalid Params", desc: chunk7MTPXR5V_cjs.$stringErr(paramsError) }); const apps = parsedParams.apps.map((app) => this.azure.b2bApps?.get(app)).filter((app) => !!app); if (!apps || apps.length === 0) return chunk7MTPXR5V_cjs.$err({ msg: "Invalid Params", desc: "B2B app not found", status: 400 }); try { const results = await chunk7MTPXR5V_cjs.$mapAndFilter(apps, async (app) => { if (app.token && app.exp > Date.now() / 1e3) { return { clientId: this.azure.clientId, appName: app.appName, appId: app.aud, token: app.token, msalResponse: app.msalResponse, isCached: true, expiresAt: app.exp }; } const msalResponse = await this.azure.cca?.acquireTokenByClientCredential({ scopes: [app.scope], skipCache: true }); if (!msalResponse) return null; const { clientId, exp, error: audError } = chunk7MTPXR5V_cjs.$getExpiry(msalResponse.accessToken); if (audError) return null; this.azure.b2bApps?.set(app.appName, { appName: app.appName, scope: app.scope, token: msalResponse.accessToken, exp: exp - chunk7MTPXR5V_cjs.TIME_SKEW, aud: clientId, msalResponse }); return { clientId: this.azure.clientId, appName: app.appName, appId: clientId, token: msalResponse.accessToken, msalResponse, isCached: false, expiresAt: 0 }; }); if (!results || results.length === 0) { return chunk7MTPXR5V_cjs.$err({ msg: "Invalid Params", desc: "Failed to get B2B token" }); } return chunk7MTPXR5V_cjs.$ok("app" in params ? { result: results[0] } : { results }); } catch (error) { if (error instanceof chunk7MTPXR5V_cjs.OAuthError) return chunk7MTPXR5V_cjs.$err(error); return chunk7MTPXR5V_cjs.$err({ msg: "Invalid Params", desc: `Failed to get B2B token: ${chunk7MTPXR5V_cjs.$stringErr(error)}` }); } } }; Object.defineProperty(exports, "OAuthError", { enumerable: true, get: function () { return chunk7MTPXR5V_cjs.OAuthError; } }); Object.defineProperty(exports, "OAuthProvider", { enumerable: true, get: function () { return chunk7MTPXR5V_cjs.OAuthProvider; } }); exports.LiteProvider = LiteProvider; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map