|
7 | 7 | import fs from 'node:fs'; |
8 | 8 | import path from 'node:path'; |
9 | 9 | import { Org, SfError } from '@salesforce/core'; |
| 10 | +import axios from 'axios'; |
10 | 11 |
|
11 | 12 | export type SiteMetadata = { |
12 | 13 | bundleName: string; |
@@ -225,6 +226,140 @@ export class ExperienceSite { |
225 | 226 |
|
226 | 227 | return resourcePath; |
227 | 228 | } |
| 229 | + |
| 230 | + // TODO cleanup |
| 231 | + public async getNetworkIdByName(): Promise<string> { |
| 232 | + const conn = this.org.getConnection(); |
| 233 | + // try { |
| 234 | + // Query the Network object for the network with the given site name |
| 235 | + const result = await conn.query<{ Id: string }>(`SELECT Id FROM Network WHERE Name = '${this.siteDisplayName}'`); |
| 236 | + |
| 237 | + const record = result.records[0]; |
| 238 | + if (record) { |
| 239 | + let networkId = record.Id; |
| 240 | + // Subtract the last three characters from the Network ID |
| 241 | + networkId = networkId.substring(0, networkId.length - 3); |
| 242 | + return networkId; |
| 243 | + } else { |
| 244 | + throw new Error(`Network with name '${this.siteDisplayName}' not found`); |
| 245 | + } |
| 246 | + // } catch (error) { |
| 247 | + // // console.error('Error fetching Network ID:', error); |
| 248 | + // throw error; |
| 249 | + // } |
| 250 | + } |
| 251 | + |
| 252 | + public async getNewSidToken(networkId: string): Promise<string> { |
| 253 | + // Get the connection and access token from the org |
| 254 | + const conn = this.org.getConnection(); |
| 255 | + const identity = await conn.identity(); |
| 256 | + if (identity.user_id) { |
| 257 | + // do something |
| 258 | + } |
| 259 | + const orgId = this.org.getOrgId(); |
| 260 | + const orgIdMinus3 = orgId.substring(0, orgId.length - 3); |
| 261 | + const accessToken = conn.accessToken; |
| 262 | + const instanceUrl = conn.instanceUrl; |
| 263 | + |
| 264 | + // Construct the switch URL |
| 265 | + const switchUrl = `${instanceUrl}/servlet/networks/switch?networkId=${networkId}`; |
| 266 | + |
| 267 | + // try { |
| 268 | + // Make the GET request without following redirects |
| 269 | + if (accessToken) { |
| 270 | + const cookies = [ |
| 271 | + `sid=${accessToken}`, |
| 272 | + `oid=${orgIdMinus3}`, |
| 273 | + // 'sid_Client=s000000uuCPw000000I7xV', |
| 274 | + // Include other essential cookies if necessary |
| 275 | + // For example: |
| 276 | + // `oid=${conn.getAuthInfoFields().orgId}`, |
| 277 | + // `sid_Client=${conn.userInfo.id}`, |
| 278 | + // Add any other cookies that might be required |
| 279 | + ] |
| 280 | + .join('; ') |
| 281 | + .trim(); |
| 282 | + let response = await axios.get(switchUrl, { |
| 283 | + headers: { |
| 284 | + Cookie: cookies, |
| 285 | + // Include other headers if necessary |
| 286 | + // 'User-Agent': 'Your User Agent String', |
| 287 | + // 'Referer': 'Referer URL if required', |
| 288 | + }, |
| 289 | + withCredentials: true, |
| 290 | + maxRedirects: 0, // Prevent axios from following redirects |
| 291 | + validateStatus: (status) => status >= 200 && status < 400, // Accept 3xx status codes |
| 292 | + }); |
| 293 | + |
| 294 | + // Extract the Location header |
| 295 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 296 | + const locationHeader = response.headers['location']; |
| 297 | + |
| 298 | + if (locationHeader) { |
| 299 | + // Parse the URL to extract the 'sid' parameter |
| 300 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument |
| 301 | + const urlObj = new URL(locationHeader); |
| 302 | + const sid = urlObj.searchParams.get('sid') ?? ''; |
| 303 | + const cookies2 = [ |
| 304 | + '__Secure-has-sid=1', |
| 305 | + `sid=${sid}`, |
| 306 | + `oid=${orgIdMinus3}`, |
| 307 | + // 'sid_Client=s000000uuCPw000000I7xV', |
| 308 | + // Include other essential cookies if necessary |
| 309 | + // For example: |
| 310 | + // `oid=${conn.getAuthInfoFields().orgId}`, |
| 311 | + // `sid_Client=${conn.userInfo.id}`, |
| 312 | + // Add any other cookies that might be required |
| 313 | + ] |
| 314 | + .join('; ') |
| 315 | + .trim(); |
| 316 | + |
| 317 | + response = await axios.get(urlObj.toString(), { |
| 318 | + headers: { |
| 319 | + Cookie: cookies2, |
| 320 | + // Include other headers if necessary |
| 321 | + // 'User-Agent': 'Your User Agent String', |
| 322 | + // 'Referer': 'Referer URL if required', |
| 323 | + }, |
| 324 | + withCredentials: true, |
| 325 | + maxRedirects: 0, // Prevent axios from following redirects |
| 326 | + validateStatus: (status) => status >= 200 && status < 400, // Accept 3xx status codes |
| 327 | + }); |
| 328 | + const setCookieHeader = response.headers['set-cookie']; |
| 329 | + if (setCookieHeader) { |
| 330 | + // 'set-cookie' can be an array if multiple cookies are set |
| 331 | + // Find the 'sid' cookie in the set-cookie header |
| 332 | + const sidCookie = setCookieHeader.find((cookieStr: string) => cookieStr.startsWith('sid=')); |
| 333 | + |
| 334 | + if (sidCookie) { |
| 335 | + // Extract the sid value from the cookie string |
| 336 | + const sidMatch = sidCookie.match(/sid=([^;]+)/); |
| 337 | + if (sidMatch?.[1]) { |
| 338 | + const sidToken = sidMatch[1]; |
| 339 | + return sidToken; |
| 340 | + } |
| 341 | + } |
| 342 | + } else { |
| 343 | + // eslint-disable-next-line no-console |
| 344 | + console.log('error couldnt find set-cookie header for sid token'); |
| 345 | + } |
| 346 | + |
| 347 | + if (sid) { |
| 348 | + return sid; |
| 349 | + } else { |
| 350 | + throw new Error('SID token not found in Location header'); |
| 351 | + } |
| 352 | + } else { |
| 353 | + throw new Error('Location header not found in response'); |
| 354 | + } |
| 355 | + } |
| 356 | + return ''; |
| 357 | + // } catch (error) { |
| 358 | + // // Handle errors (e.g., network issues, HTTP errors) |
| 359 | + // // console.error('Error obtaining new SID token:', error); |
| 360 | + // throw error; |
| 361 | + // } |
| 362 | + } |
228 | 363 | } |
229 | 364 |
|
230 | 365 | /** |
|
0 commit comments