UNPKG

@simpleapps-com/augur-api

Version:

TypeScript client library for Augur microservices API endpoints

107 lines 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.authenticateUserForSite = authenticateUserForSite; exports.createCrossSiteAuthenticator = createCrossSiteAuthenticator; const client_1 = require("../client"); /** * Authenticate a user against a target site using augur_info privileges * @description This utility function simplifies cross-site authentication by: * 1. Creating an augur_info API client with admin privileges * 2. Using verifyPassword with the target siteId in the request body * 3. Returning a ready-to-use API client for the target site on success * * @param params Cross-site authentication parameters * @returns Promise resolving to authentication result with optional target site API client * * @example * ```typescript * const result = await authenticateUserForSite({ * targetSiteId: 'tenant_site_1', * username: 'user@tenant.com', * password: 'userpassword', * augurInfoToken: 'admin-jwt-token' * }); * * if (result.success) { * // Use the pre-configured API client * const userData = await result.targetSiteAPI!.joomla.users.get(result.userId!); * console.log('User data:', userData); * } else { * console.error('Authentication failed:', result.error); * } * ``` */ async function authenticateUserForSite(params) { const { targetSiteId, username, password, augurInfoToken } = params; try { // Create augur_info API client with admin privileges const augurInfoAPI = new client_1.AugurAPI({ siteId: 'augur_info', bearerToken: augurInfoToken, }); // Perform cross-site authentication using existing verifyPassword method const authResponse = await augurInfoAPI.joomla.users.verifyPassword({ username, password, siteId: targetSiteId, // This tells the API to authenticate against the target site }); if (authResponse.data.isVerified) { // Create API client for target site with the returned token const targetSiteAPI = new client_1.AugurAPI({ siteId: targetSiteId, bearerToken: authResponse.data.token, }); return { success: true, userId: authResponse.data.id?.toString(), username: authResponse.data.username, email: authResponse.data.email, token: authResponse.data.token, targetSiteId, targetSiteAPI, }; } else { return { success: false, targetSiteId, error: 'Invalid username or password', }; } } catch (error) { return { success: false, targetSiteId, error: error instanceof Error ? error.message : 'Authentication failed', }; } } /** * Create a cross-site authentication function bound to specific augur_info credentials * @description Factory function for creating a reusable cross-site authenticator * * @param augurInfoToken JWT token with augur_info admin privileges * @returns Function that can authenticate users for any target site * * @example * ```typescript * // Create reusable authenticator * const crossSiteAuth = createCrossSiteAuthenticator('admin-jwt-token'); * * // Use it for multiple sites * const result1 = await crossSiteAuth('tenant_site_1', 'user1@tenant.com', 'pass1'); * const result2 = await crossSiteAuth('tenant_site_2', 'user2@tenant.com', 'pass2'); * ``` */ function createCrossSiteAuthenticator(augurInfoToken) { return async (targetSiteId, username, password) => { return authenticateUserForSite({ targetSiteId, username, password, augurInfoToken, }); }; } //# sourceMappingURL=cross-site-auth.js.map