Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@transferwise/approve-api-action-helpers

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@transferwise/approve-api-action-helpers - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

2

dist/main.js

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["@transferwise/approve-api-action-helpers"]=t():e["@transferwise/approve-api-action-helpers"]=t()}("undefined"!=typeof self?self:this,(function(){return(()=>{"use strict";var e={650:(e,t,n)=>{function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function c(e,t){return!t||"object"!==i(t)&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function u(e){var t="function"==typeof Map?new Map:void 0;return(u=function(e){if(null===e||(n=e,-1===Function.toString.call(n).indexOf("[native code]")))return e;var n;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return p(e,arguments,f(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),l(r,e)})(e)}function p(e,t,n){return(p=s()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&l(o,n.prototype),o}).apply(null,arguments)}function s(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function l(e,t){return(l=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function f(e){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}n.r(t),n.d(t,{create:()=>P,runFlow:()=>w});var d=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&l(e,t)}(o,e);var t,n,r=(t=o,n=s(),function(){var e,r=f(t);if(n){var o=f(this).constructor;e=Reflect.construct(r,arguments,o)}else e=r.apply(this,arguments);return c(this,e)});function o(e){var t;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,o),(t=r.call(this,e.statusText)).name="HTTPError",t.response=e,t}return o}(u(Error));function v(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return fetch(e,o(o({},t),{},{headers:o(o({},{"Content-Type":"application/json"}),t.headers)})).then((function(e){if(!e.ok)throw new d(e);return e}))}var y="https://transferwise.com/approve-api-action/",b="tw-approve-api-action-success",m="tw-approve-api-action-failed",h="tw-approve-api-action-closed";function w(e){var t=e.token,n=e.flow,r=void 0===n?"tab":n;return new Promise((function(e,n){"iframe"===r?function(e){var t,n,r=e.token,o=e.resolve,a=e.reject;document.getElementById("tw-approve-api-action-styles")||(t=document.head,(n=document.createElement("style")).id="tw-approve-api-action-styles",n.appendChild(document.createTextNode("\n .tw-approval-modal-overlay {\n position: fixed;\n bottom: 0;\n left: 0;\n right: 0;\n top: 0;\n height: 100%;\n width: 100%;\n background: rgba(55,81,126,0);\n transition: background-color .35s cubic-bezier(0,.94,.62,1);\n z-index: 1050;\n }\n .tw-approval-modal-overlay--open {\n background: rgba(55,81,126,0.8);\n }\n .tw-approval-modal-wrapper {\n height: 100%;\n width: 100vw;\n display: flex;\n justify-content: center;\n align-items: center;\n padding: 16px;\n }\n .tw-approval-modal {\n display: flex;\n max-height: 100%;\n height: 540px;\n width: 540px;\n background-color: #fff;\n border-radius: 3px;\n box-shadow: 0 5px 15px rgba(0,0,0,.2);\n overflow: auto;\n }\n")),t.appendChild(n));var i=document.createElement("div");i.classList.add("tw-approval-modal-overlay");var c=document.createElement("div");c.classList.add("tw-approval-modal-wrapper"),i.appendChild(c);var u=document.createElement("div");u.classList.add("tw-approval-modal"),c.appendChild(u);var p=document.createElement("iframe");p.name="tw-approve-api-action",p.width="100%",p.height="100%",p.style.border="0",u.appendChild(p);var s=document.createElement("form"),l=document.createElement("input");s.style.display="none",s.method="POST",s.action=y,l.value=r,l.name="ott",s.appendChild(l),u.appendChild(s),document.body.appendChild(i),i.classList.add("tw-approval-modal-overlay--open"),s.setAttribute("target","tw-approve-api-action"),s.submit();var f=function(e){switch(e.data){case b:return d(),void o();case m:return d(),void a();case h:d()}},d=function(){i.remove(),window.removeEventListener("message",f)};i.addEventListener("click",d,{once:!0}),window.addEventListener("message",f)}({token:t,resolve:e,reject:n}):function(e){var t=e.token,n=e.resolve,r=e.reject,o="tw-approve-api-action",a=function(e){var t=e.name,n=e.token,r=document.createElement("form"),o=document.createElement("input");return r.style.display="none",r.target=t,r.method="POST",r.action=y,o.value=n,o.name="ott",r.appendChild(o),r}({name:o,token:t});document.body.appendChild(a);var i=window.open(y,o);a.submit();var c=function(e){switch(e.data){case b:return u(),void n();case m:return u(),void r();case h:u()}},u=function(){i.close(),window.removeEventListener("message",c)};window.addEventListener("message",c)}({token:t,resolve:e,reject:n})}))}function g(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function O(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?g(Object(n),!0).forEach((function(t){j(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):g(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function j(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function P(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return v(t,n).then((function(e){return{response:e,metadata:{scaRequired:!1}}})).catch((function(r){if(x(r)){var o=S(r.response);return E(e.onSCARequired),w({language:e.language||"en",apiUrl:e.apiUrl,intent:e.intent,container:e.container,userEmail:e.userEmail,token:o,flow:e.flow}).then((function(){E(e.onSCACompleted);var r=O(O({},n.headers||{}),{},{"X-2FA-APPROVAL":o});return v(t,O(O({},n),{},{headers:r})).then((function(e){return{response:e,metadata:{scaRequired:!0}}}))}))}throw r})).then((function(e){return n&&n.withMetadata?e:e.response}))}}function E(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:function(){};e()}function x(e){if(!e||!e.response)return!1;var t=S(e.response),n=e.response.status;return(403===n||400===n)&&t}function S(e){return e&&e.headers&&e.headers.get?e.headers.get("X-2FA-APPROVAL"):null}}},t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}return n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n(650)})()}));
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["@transferwise/approve-api-action-helpers"]=t():e["@transferwise/approve-api-action-helpers"]=t()}("undefined"!=typeof self?self:this,(function(){return(()=>{"use strict";var e={650:(e,t,n)=>{function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function c(e,t){return!t||"object"!==i(t)&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function p(e){var t="function"==typeof Map?new Map:void 0;return(p=function(e){if(null===e||(n=e,-1===Function.toString.call(n).indexOf("[native code]")))return e;var n;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return u(e,arguments,f(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),s(r,e)})(e)}function u(e,t,n){return(u=l()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&s(o,n.prototype),o}).apply(null,arguments)}function l(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function s(e,t){return(s=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function f(e){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}n.r(t),n.d(t,{Flow:()=>h,Mode:()=>w,create:()=>E,runFlow:()=>O});var d=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&s(e,t)}(o,e);var t,n,r=(t=o,n=l(),function(){var e,r=f(t);if(n){var o=f(this).constructor;e=Reflect.construct(r,arguments,o)}else e=r.apply(this,arguments);return c(this,e)});function o(e){var t;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,o),(t=r.call(this,e.statusText)).name="HTTPError",t.response=e,t}return o}(p(Error));function v(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return fetch(e,o(o({},t),{},{headers:o(o({},{"Content-Type":"application/json"}),t.headers)})).then((function(e){if(!e.ok)throw new d(e);return e}))}var y="tw-approve-api-action-success",b="tw-approve-api-action-failed",m="tw-approve-api-action-closed";var h={IFRAME:"iframe",TAB:"tab"},w={PRODUCTION:"production",SANDBOX:"sandbox"},g=function(e){return e===w.SANDBOX?"https://sandbox.transferwise.tech/approve-api-action/":"https://transferwise.com/approve-api-action/"};function O(e){var t=e.token,n=e.flow,r=void 0===n?h.IFRAME:n,o=e.mode,a=void 0===o?w.PRODUCTION:o,i=e.approvalPageUrl;return new Promise((function(e,n){var o={token:t,approvalPageUrl:i||g(a),resolve:e,reject:n};r===h.TAB?function(e){var t=e.token,n=e.approvalPageUrl,r=e.resolve,o=e.reject,a="tw-approve-api-action",i=function(e){var t=e.name,n=e.token,r=e.approvalPageUrl,o=document.createElement("form"),a=document.createElement("input");return o.style.display="none",o.target=t,o.method="POST",o.action=r,a.value=n,a.name="ott",o.appendChild(a),o}({name:a,token:t,approvalPageUrl:n});document.body.appendChild(i);var c=window.open(n,a);i.submit();var p=function(e){switch(e.data){case y:return u(),void r();case b:return u(),void o();case m:u()}},u=function(){c.close(),window.removeEventListener("message",p)};window.addEventListener("message",p)}(o):function(e){var t,n,r=e.token,o=e.approvalPageUrl,a=e.resolve,i=e.reject;document.getElementById("tw-approve-api-action-styles")||(t=document.head,(n=document.createElement("style")).id="tw-approve-api-action-styles",n.appendChild(document.createTextNode("\n .tw-approval-modal-overlay {\n position: fixed;\n bottom: 0;\n left: 0;\n right: 0;\n top: 0;\n height: 100%;\n width: 100%;\n background: rgba(55,81,126,0);\n transition: background-color .35s cubic-bezier(0,.94,.62,1);\n z-index: 1050;\n }\n .tw-approval-modal-overlay--open {\n background: rgba(55,81,126,0.8);\n }\n .tw-approval-modal-wrapper {\n height: 100%;\n width: 100vw;\n display: flex;\n justify-content: center;\n align-items: center;\n padding: 16px;\n }\n .tw-approval-modal {\n display: flex;\n max-height: 100%;\n height: 540px;\n width: 540px;\n background-color: #fff;\n border-radius: 3px;\n box-shadow: 0 5px 15px rgba(0,0,0,.2);\n overflow: auto;\n }\n")),t.appendChild(n));var c=document.createElement("div");c.classList.add("tw-approval-modal-overlay");var p=document.createElement("div");p.classList.add("tw-approval-modal-wrapper"),c.appendChild(p);var u=document.createElement("div");u.classList.add("tw-approval-modal"),p.appendChild(u);var l=document.createElement("iframe");l.name="tw-approve-api-action",l.width="100%",l.height="100%",l.style.border="0",u.appendChild(l);var s=document.createElement("form"),f=document.createElement("input");s.style.display="none",s.method="POST",s.action=o,f.value=r,f.name="ott",s.appendChild(f),u.appendChild(s),document.body.appendChild(c),c.classList.add("tw-approval-modal-overlay--open"),s.setAttribute("target","tw-approve-api-action"),s.submit();var d=function(e){switch(e.data){case y:return v(),void a();case b:return v(),void i();case m:v()}},v=function(){c.remove(),window.removeEventListener("message",d)};c.addEventListener("click",v,{once:!0}),window.addEventListener("message",d)}(o)}))}function P(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function j(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?P(Object(n),!0).forEach((function(t){x(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):P(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function x(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function E(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return v(t,n).then((function(e){return{response:e,metadata:{scaRequired:!1}}})).catch((function(r){if(C(r)){var o=R(r.response);return S(e.onSCARequired),O({token:o,flow:e.flow,mode:e.mode,approvalPageUrl:e.approvalPageUrl}).then((function(){S(e.onSCACompleted);var r=j(j({},n.headers||{}),{},{"X-2FA-APPROVAL":o});return v(t,j(j({},n),{},{headers:r})).then((function(e){return{response:e,metadata:{scaRequired:!0}}}))}))}throw r})).then((function(e){return n&&n.withMetadata?e:e.response}))}}function S(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:function(){};e()}function C(e){if(!e||!e.response)return!1;var t=R(e.response),n=e.response.status;return(403===n||400===n)&&t}function R(e){return e&&e.headers&&e.headers.get?e.headers.get("X-2FA-APPROVAL"):null}}},t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}return n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n(650)})()}));
{
"name": "@transferwise/approve-api-action-helpers",
"version": "0.1.1",
"version": "0.2.0",
"description": "An http client that handles SCA required requests gracefully",

@@ -5,0 +5,0 @@ "main": "dist/main.js",

@@ -13,26 +13,26 @@ # Approve api action helpers

This library exports a single function, `create`, which creates an SCA request wrapper with a provided config (see available options below). Use it like so:
This library exports a `create` function for SCA protected requests. It returns a request wrapper that can be used like a regular [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) request. Use it as follows:
```javascript
import { create } from '@transferwise/approve-api-action-helpers';
import { create, Flow, Mode } from '@transferwise/approve-api-action-helpers';
const request = create({
flow: 'iframe',
mode: 'production',
flow: Flow.IFRAME,
mode: Mode.PRODUCTION,
});
const res = await request('https://my-backend-api.com/sca-protected-call', { method: 'GET', ... });
```
The request function it returns is essentially the same as [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch), except when the backend returns that SCA is required, it will run the user through an SCA flow and retry that request. Also, it will throw an error if you get a `4xx` or `5xx` response.
When the backend returns that SCA is required, it will run the user through an SCA flow and retry that request. Also, it will throw an error if you get a `4xx` or `5xx` response.
**NB! make sure that your backend proxies 'x-2fa-approval' response header to the frontend and forwards it back to TransferWise when this library passes it back. Also please reflect the response status (403) back to the frontend.**
```javascript
const res = await request('https://my-backend-api.com/sca-protected-call', { options }
```
#### Configuration options
#### Configuration options for `create`
| key | optional | default | alternatives |
|------|----------|----------------------|--------------|
| flow | yes | 'iframe' | 'tab' |
| mode | yes | 'production' | 'sandbox' |
| flow | yes | Flow.IFRAME | Flow.TAB |
| mode | yes | Mode.PRODUCTION | Mode.SANDBOX |

@@ -39,0 +39,0 @@ ## Example

import { iframeFlow } from './iframeFlow';
import { tabFlow } from './tabFlow';
import { APPROVAL_PAGE_URL_PRODUCTION, APPROVAL_PAGE_URL_SANDBOX } from './config';
export function runFlow({ token, flow = 'tab' }) {
export const Flow = {
IFRAME: 'iframe',
TAB: 'tab',
};
export const Mode = {
PRODUCTION: 'production',
SANDBOX: 'sandbox',
};
const getApprovalPageUrlFromMode = (mode) =>
mode === Mode.SANDBOX ? APPROVAL_PAGE_URL_SANDBOX : APPROVAL_PAGE_URL_PRODUCTION;
export function runFlow({ token, flow = Flow.IFRAME, mode = Mode.PRODUCTION, approvalPageUrl }) {
return new Promise((resolve, reject) => {
if (flow === 'iframe') {
iframeFlow({ token, resolve, reject });
const props = {
token,
approvalPageUrl: approvalPageUrl || getApprovalPageUrlFromMode(mode),
resolve,
reject,
};
if (flow === Flow.TAB) {
tabFlow(props);
} else {
tabFlow({ token, resolve, reject });
iframeFlow(props);
}
});
}

@@ -1,2 +0,3 @@

export const APPROVAL_PAGE_URL = 'https://transferwise.com/approve-api-action/';
export const APPROVAL_PAGE_URL_PRODUCTION = 'https://transferwise.com/approve-api-action/';
export const APPROVAL_PAGE_URL_SANDBOX = 'https://sandbox.transferwise.tech/approve-api-action/';

@@ -3,0 +4,0 @@ export const MESSAGE_SUCCESS = 'tw-approve-api-action-success';

/* eslint-disable fp/no-mutation */
import { APPROVAL_PAGE_URL, MESSAGE_SUCCESS, MESSAGE_FAILED, MESSAGE_CLOSED } from './config';
import { MESSAGE_SUCCESS, MESSAGE_FAILED, MESSAGE_CLOSED } from './config';

@@ -48,3 +48,3 @@ const css = `

export function iframeFlow({ token, resolve, reject }) {
export function iframeFlow({ token, approvalPageUrl, resolve, reject }) {
if (!document.getElementById('tw-approve-api-action-styles')) {

@@ -76,3 +76,3 @@ addStylesheet();

form.method = 'POST';
form.action = APPROVAL_PAGE_URL;
form.action = approvalPageUrl;
ottInput.value = token;

@@ -79,0 +79,0 @@ ottInput.name = 'ott';

/* eslint-disable fp/no-mutation */
import { iframeFlow } from './iframeFlow';
import * as config from './config';
jest.mock('./config', () => ({
APPROVAL_PAGE_URL: 'https://a.a/',
MESSAGE_SUCCESS: 'b',

@@ -20,2 +18,7 @@ MESSAGE_FAILED: 'c',

afterEach(() => {
// cleanup JSDOM document, because jest doesn't do it after each run. https://stackoverflow.com/a/50800473
document.getElementsByTagName('html')[0].innerHTML = '';
});
it('adds stylesheet', () => {

@@ -37,9 +40,9 @@ expect(document.getElementById('tw-approve-api-action-styles')).toBeFalsy();

it('posts to the iframe', () => {
iframeFlow({ token: 'a1b2' });
iframeFlow({ token: 'a1b2', approvalPageUrl: 'https://a.a/' });
const form = document.querySelector('form');
expect(form.target).toBe('tw-approve-api-action');
expect(form.method).toBe('post');
expect(form.action).toBe(config.APPROVAL_PAGE_URL);
expect(form.action).toBe('https://a.a/');
expect(submit).toHaveBeenCalled();
});
});
export { create } from './request';
export { runFlow } from './authenticationFlow';
export { runFlow, Flow, Mode } from './authenticationFlow';

@@ -14,9 +14,6 @@ import { http } from './http';

return runFlow({
language: config.language || 'en',
apiUrl: config.apiUrl,
intent: config.intent,
container: config.container,
userEmail: config.userEmail,
token,
flow: config.flow,
mode: config.mode,
approvalPageUrl: config.approvalPageUrl,
}).then(() => {

@@ -23,0 +20,0 @@ callback(config.onSCACompleted);

/* eslint-disable fp/no-mutation */
import { http } from './http';
import { runFlow } from './authenticationFlow';
import { Flow, Mode, runFlow } from './authenticationFlow';
import { create } from './request';

@@ -41,3 +41,3 @@

beforeEach(() => {
request = create({ language: 'en', apiUrl: 'an api url', intent: 'meh' });
request = create({ mode: Mode.SANDBOX, flow: Flow.IFRAME });
http.mockImplementation(() =>

@@ -59,17 +59,19 @@ Promise.reject({

token: 'a token',
language: 'en',
apiUrl: 'an api url',
intent: 'meh',
mode: Mode.SANDBOX,
flow: Flow.IFRAME,
approvalPageUrl: undefined,
});
});
it('runs the authentication flow with the language passed in', async () => {
request = create({ language: 'some language' });
it('runs the authentication flow with the approvalPageUrl passed in', async () => {
request = create({ approvalPageUrl: 'https://a.b/' });
expect(runFlow).not.toHaveBeenCalled();
await request('https://example.com', { language: 'some language' });
expect(runFlow).toHaveBeenCalledWith(expect.objectContaining({ language: 'some language' }));
await request('https://example.com');
expect(runFlow).toHaveBeenCalledWith(
expect.objectContaining({ approvalPageUrl: 'https://a.b/' }),
);
});
it('runs the original request with the 2fa token on success', async () => {
request = create({ language: 'en' });
request = create();
const result = await request('https://example.com');

@@ -76,0 +78,0 @@ expect(http).toHaveBeenNthCalledWith(

/* eslint-disable fp/no-mutation */
import { APPROVAL_PAGE_URL, MESSAGE_SUCCESS, MESSAGE_FAILED, MESSAGE_CLOSED } from './config';
import { MESSAGE_SUCCESS, MESSAGE_FAILED, MESSAGE_CLOSED } from './config';
const createForm = ({ name, token }) => {
const createForm = ({ name, token, approvalPageUrl }) => {
const form = document.createElement('form');

@@ -10,3 +10,3 @@ const ottInput = document.createElement('input');

form.method = 'POST';
form.action = APPROVAL_PAGE_URL;
form.action = approvalPageUrl;
ottInput.value = token;

@@ -18,10 +18,10 @@ ottInput.name = 'ott';

export function tabFlow({ token, resolve, reject }) {
export function tabFlow({ token, approvalPageUrl, resolve, reject }) {
// 1. creates a new form, but doesn't submit it yet
const name = 'tw-approve-api-action';
const form = createForm({ name, token });
const form = createForm({ name, token, approvalPageUrl });
document.body.appendChild(form);
// 2. opens the approval page in new tab
const approvalPage = window.open(APPROVAL_PAGE_URL, name);
const approvalPage = window.open(approvalPageUrl, name);

@@ -28,0 +28,0 @@ // 3. submit the form (will POST to this new tab)

/* eslint-disable fp/no-mutation */
import { tabFlow } from './tabFlow';
import * as config from './config';
jest.mock('./config', () => ({
APPROVAL_PAGE_URL: 'https://a.a/',
MESSAGE_SUCCESS: 'b',

@@ -22,14 +20,14 @@ MESSAGE_FAILED: 'c',

it('opens approval page', () => {
tabFlow({});
expect(global.open).toHaveBeenCalledWith(config.APPROVAL_PAGE_URL, 'tw-approve-api-action');
tabFlow({ approvalPageUrl: 'https://a.a/' });
expect(global.open).toHaveBeenCalledWith('https://a.a/', 'tw-approve-api-action');
});
it('posts to approval page', () => {
tabFlow({ token: 'a1b2' });
tabFlow({ token: 'a1b2', approvalPageUrl: 'https://a.a/' });
const form = document.querySelector('form');
expect(form.target).toBe('tw-approve-api-action');
expect(form.method).toBe('post');
expect(form.action).toBe(config.APPROVAL_PAGE_URL);
expect(form.action).toBe('https://a.a/');
expect(submit).toHaveBeenCalled();
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc