linkedout/core-parser/auth-manager.js

95 lines
2.4 KiB
JavaScript

/**
* Authentication Manager
*
* Handles login/authentication for different sites
*/
class AuthManager {
constructor(coreParser) {
this.coreParser = coreParser;
}
/**
* Authenticate to a specific site
*/
async authenticate(site, credentials, pageId = "default") {
const strategies = {
linkedin: this.authenticateLinkedIn.bind(this),
// Add more auth strategies as needed
};
const strategy = strategies[site.toLowerCase()];
if (!strategy) {
throw new Error(`No authentication strategy found for site: ${site}`);
}
return await strategy(credentials, pageId);
}
/**
* LinkedIn authentication strategy
*/
async authenticateLinkedIn(credentials, pageId = "default") {
const { username, password } = credentials;
if (!username || !password) {
throw new Error("LinkedIn authentication requires username and password");
}
const page = this.coreParser.getPage(pageId);
if (!page) {
throw new Error(`Page with ID '${pageId}' not found`);
}
try {
// Navigate to LinkedIn login
await this.coreParser.navigateTo("https://www.linkedin.com/login", {
pageId,
});
// Fill credentials
await page.fill('input[name="session_key"]', username);
await page.fill('input[name="session_password"]', password);
// Submit form
await page.click('button[type="submit"]');
// Wait for successful login (profile image appears)
await page.waitForSelector("img.global-nav__me-photo", {
timeout: 15000,
});
return true;
} catch (error) {
throw new Error(`LinkedIn authentication failed: ${error.message}`);
}
}
/**
* Check if currently authenticated to a site
*/
async isAuthenticated(site, pageId = "default") {
const page = this.coreParser.getPage(pageId);
if (!page) {
return false;
}
const checkers = {
linkedin: async () => {
try {
await page.waitForSelector("img.global-nav__me-photo", {
timeout: 2000,
});
return true;
} catch {
return false;
}
},
};
const checker = checkers[site.toLowerCase()];
return checker ? await checker() : false;
}
}
module.exports = AuthManager;