Skip to content

Commit

Permalink
abtract more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mldangelo committed Oct 3, 2023
1 parent 0b04b33 commit c87b5b5
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 257 deletions.
52 changes: 5 additions & 47 deletions src/js/basic-kyc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const validate = require("validate.js");
const { endpoints } = require("./common");
const { getEndpoint, parseJWT, postData } = require("./common");

var basicKyc = (function basicKyc() {
"use strict";
Expand All @@ -23,19 +23,6 @@ var basicKyc = (function basicKyc() {

var CloseIframeButtons = document.querySelectorAll('.close-iframe');

function postData(url = '', data = {}) {
return fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}

async function getProductConstraints() {
try {
const productsConfigPayload = {
Expand All @@ -44,9 +31,9 @@ var basicKyc = (function basicKyc() {
partner_params
}

const productsConfigUrl = `${endpoints[config.environment]}/v1/products_config`;
const productsConfigUrl = `${getEndpoint(config.environment)}/v1/products_config`;
const productsConfigPromise = postData(productsConfigUrl, productsConfigPayload);
const servicesPromise = fetch(`${endpoints[config.environment]}/v1/services`);
const servicesPromise = fetch(`${getEndpoint(config.environment)}/v1/services`);
const [productsConfigResponse, servicesResponse] = await Promise.all([
productsConfigPromise,
servicesPromise
Expand Down Expand Up @@ -317,7 +304,7 @@ var basicKyc = (function basicKyc() {
main.removeChild(EndUserConsent);
}
EndUserConsent = document.createElement("end-user-consent");
EndUserConsent.setAttribute('base-url', `${endpoints[config.environment] || config.environment}/v1`);
EndUserConsent.setAttribute('base-url', `${getEndpoint(config.environment) || config.environment}/v1`);
EndUserConsent.setAttribute('country', id_info.country);
EndUserConsent.setAttribute('id-regex', productConstraints[id_info.country]['id_types'][id_info.id_type]['id_number_regex']);
EndUserConsent.setAttribute('id-type', id_info.id_type);
Expand Down Expand Up @@ -449,36 +436,7 @@ var basicKyc = (function basicKyc() {
}

function getPartnerParams() {
function parseJWT(token) {
/**
* A JSON Web Token (JWT) uses a base64 URL encoded string in it's body.
*
* in order to get a regular JSON string, we would follow these steps:
*
* 1. get the body of a JWT string
* 2. replace the base64 URL delimiters ( - and _ ) with regular URL delimiters ( + and / )
* 3. convert the regular base64 string to a string
* 4. encode the string from above as a URIComponent,
* ref: just above this - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#examples
* 5. decode the URI Component to a JSON string
* 6. parse the JSON string to a javascript object
*/
var base64Url = token.split(".")[1];
var base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
var jsonPayload = decodeURIComponent(
atob(base64)
.split("")
.map(function (c) {
return "%" + c.charCodeAt(0).toString(16);
})
.join("")
);

return JSON.parse(jsonPayload);
}

const { partner_params: partnerParams } = parseJWT(config.token);

partner_params = { ...partnerParams, ...(config.partner_params || {}) };
}

Expand Down Expand Up @@ -671,7 +629,7 @@ var basicKyc = (function basicKyc() {
source_sdk_version: config.sdk_version || "v1.1.0"
};

const URL = `${endpoints[config.environment]}/v2/verify_async`;
const URL = `${getEndpoint(config.environment)}/v2/verify_async`;
const response = await postData(URL, payload);
const json = await response.json();

Expand Down
51 changes: 5 additions & 46 deletions src/js/biometric-kyc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const JSZip = require('jszip');
const validate = require("validate.js");
const { endpoints } = require("./common");
const { getEndpoint, parseJWT, postData } = require("./common");

var biometricKyc = function biometricKyc() {
'use strict';
Expand Down Expand Up @@ -32,19 +32,6 @@ var biometricKyc = function biometricKyc() {

var fileToUpload, uploadURL;

function postData(url = '', data = {}) {
return fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}

async function getProductConstraints() {
try {
const productsConfigPayload = {
Expand All @@ -53,9 +40,9 @@ var biometricKyc = function biometricKyc() {
partner_params
}

const productsConfigUrl = `${endpoints[config.environment]}/products_config`;
const productsConfigUrl = `${getEndpoint(config.environment)}/products_config`;
const productsConfigPromise = postData(productsConfigUrl, productsConfigPayload);
const servicesPromise = fetch(`${endpoints[config.environment]}/services`);
const servicesPromise = fetch(`${getEndpoint(config.environment)}/services`);
const [productsConfigResponse, servicesResponse] = await Promise.all([
productsConfigPromise,
servicesPromise
Expand Down Expand Up @@ -328,7 +315,7 @@ var biometricKyc = function biometricKyc() {
main.removeChild(EndUserConsent);
}
EndUserConsent = document.createElement('end-user-consent');
EndUserConsent.setAttribute('base-url', endpoints[config.environment] || config.environment);
EndUserConsent.setAttribute('base-url', getEndpoint(config.environment) || config.environment);
EndUserConsent.setAttribute('country', id_info.country);
EndUserConsent.setAttribute('id-regex', productConstraints[id_info.country]['id_types'][id_info.id_type]['id_number_regex']);
EndUserConsent.setAttribute('id-type', id_info.id_type);
Expand Down Expand Up @@ -426,35 +413,7 @@ var biometricKyc = function biometricKyc() {
}

function getPartnerParams() {
function parseJWT(token) {
/**
* A JSON Web Token (JWT) uses a base64 URL encoded string in it's body.
*
* in order to get a regular JSON string, we would follow these steps:
*
* 1. get the body of a JWT string
* 2. replace the base64 URL delimiters ( - and _ ) with regular URL delimiters ( + and / )
* 3. convert the regular base64 string to a string
* 4. encode the string from above as a URIComponent,
* ref: just above this - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#examples
* 5. decode the URI Component to a JSON string
* 6. parse the JSON string to a javascript object
*/
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(
atob(base64)
.split('')
.map(function(c) {
return '%' + (c.charCodeAt(0).toString(16));
}).join('')
);

return JSON.parse(jsonPayload);
};

const { partner_params: partnerParams } = parseJWT(config.token);

partner_params = { ...partnerParams, ...(config.partner_params || {}) };
}

Expand Down Expand Up @@ -648,7 +607,7 @@ var biometricKyc = function biometricKyc() {
}
}

const URL = `${endpoints[config.environment] || config.environment}/upload`;
const URL = `${getEndpoint(config.environment) || config.environment}/upload`;

try {
const response = await postData(URL, payload);
Expand Down
72 changes: 61 additions & 11 deletions src/js/common.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,68 @@
"use strict";
// NOTE: In order to support prior integrations, we have `live` and
// `production` pointing to the same URL
const endpoints = {
development: "https://devapi.smileidentity.com/v1",
sandbox: "https://testapi.smileidentity.com/v1",
live: "https://api.smileidentity.com/v1",
production: "https://api.smileidentity.com/v1",

/**
* Returns the appropriate endpoint for the specified environment.
*
* NOTE: In order to support prior integrations, we have `live` and
* `production` pointing to the same URL.
* @param {string} environment - see above for supported environments
* @returns {string} - the appropriate endpoint as a url.
*/
const getEndpoint = (environment) =>
({
development: "https://devapi.smileidentity.com/v1",
sandbox: "https://testapi.smileidentity.com/v1",
live: "https://api.smileidentity.com/v1",
production: "https://api.smileidentity.com/v1",
})[environment];

/**
* A JSON Web Token (JWT) uses a base64 URL encoded string in it's body.
*
* in order to get a regular JSON string, we would follow these steps:
*
* 1. get the body of a JWT string
* 2. replace the base64 URL delimiters ( - and _ ) with regular URL delimiters ( + and / )
* 3. convert the regular base64 string to a string
* 4. encode the string from above as a URIComponent,
* ref: just above this - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#examples
* 5. decode the URI Component to a JSON string
* 6. parse the JSON string to a javascript object
* @param {string} token - a JWT string
* @returns {Object} - the decoded JWT
*/
const parseJWT = (token) => {
const base64Url = token.split(".")[1];
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
const jsonPayload = decodeURIComponent(
atob(base64)
.split("")
.map(function (c) {
return "%" + c.charCodeAt(0).toString(16);
})
.join(""),
);

return JSON.parse(jsonPayload);
};

function toHRF(string) {
return string.replace(/\_/g, " ");
}
const postData = (url = "", data = {}) =>
fetch(url, {
method: "POST",
mode: "cors",
cache: "no-cache",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});

const toHRF = (string) => string.replace(/\_/g, " ");

module.exports = {
endpoints,
getEndpoint,
parseJWT,
postData,
toHRF,
};
34 changes: 3 additions & 31 deletions src/js/doc-verification.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const JSZip = require('jszip');
const { endpoints } = require("./common");
const { getEndpoint, parseJWT } = require("./common");

var documentVerification = function documentVerification() {
'use strict';
Expand All @@ -26,7 +26,7 @@ var documentVerification = function documentVerification() {

async function getProductConstraints() {
try {
const response = await fetch(`${endpoints[config.environment]}/services`);
const response = await fetch(`${getEndpoint(config.environment)}/services`);
const json = await response.json();

return json.hosted_web['doc_verification'];
Expand Down Expand Up @@ -230,35 +230,7 @@ var documentVerification = function documentVerification() {
});

function getPartnerParams() {
function parseJWT(token) {
/**
* A JSON Web Token (JWT) uses a base64 URL encoded string in it's body.
*
* in order to get a regular JSON string, we would follow these steps:
*
* 1. get the body of a JWT string
* 2. replace the base64 URL delimiters ( - and _ ) with regular URL delimiters ( + and / )
* 3. convert the regular base64 string to a string
* 4. encode the string from above as a URIComponent,
* ref: just above this - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#examples
* 5. decode the URI Component to a JSON string
* 6. parse the JSON string to a javascript object
*/
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(
atob(base64)
.split('')
.map(function(c) {
return '%' + (c.charCodeAt(0).toString(16));
}).join('')
);

return JSON.parse(jsonPayload);
};

const { partner_params: partnerParams } = parseJWT(config.token);

partner_params = { ...partnerParams, ...(config.partner_params || {}) }
}

Expand Down Expand Up @@ -348,7 +320,7 @@ var documentVerification = function documentVerification() {
body: JSON.stringify(payload),
};

const URL = `${endpoints[config.environment] || config.environment}/upload`;
const URL = `${getEndpoint(config.environment) || config.environment}/upload`;

try {
const response = await fetch(URL, fetchConfig);
Expand Down
Loading

0 comments on commit c87b5b5

Please sign in to comment.