UNPKG

@foxy.io/sdk

Version:

Universal SDK for a full server-side and a limited in-browser access to Foxy hAPI.

41 lines (40 loc) 1.41 kB
import * as crypto from 'crypto'; import { URL } from 'url'; import v8n from 'v8n'; const optionsV8N = v8n().schema({ customer: v8n().number(), domain: v8n().string(), secret: v8n().string(), session: v8n().optional(v8n().string()), timestamp: v8n().optional(v8n().number()), }); /** * Generates an SSO url for the given configuration. * * @example * * const url = FoxySDK.Backend.createSSOURL({ * customer: 123, * secret: "...", * domain: "https://yourdomain.foxycart.com" * }); * * @param options sso url configuration * @tutorial https://docs.foxycart.com/v/2.0/sso#the_details * @returns SSO URL as string. */ export function createSSOURL(options) { var _a; optionsV8N.check(options); const timestamp = (_a = options.timestamp) !== null && _a !== void 0 ? _a : Math.floor(Date.now() / 1000) + 3600; const decodedToken = `${options.customer}|${timestamp}|${options.secret}`; const encodedToken = crypto.createHash('sha1').update(decodedToken); const url = new URL('/checkout', options.domain); url.searchParams.append('fc_customer_id', options.customer.toString()); url.searchParams.append('fc_auth_token', encodedToken.digest('hex')); url.searchParams.append('timestamp', String(timestamp)); if (typeof options.session === 'string') { url.searchParams.append('fcsid', options.session); } return url.toString(); }