2021-04-16 02:56:33 -06:00
|
|
|
/*
|
|
|
|
Copyright 2021 Yarmo Mackenbach
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2023-07-08 00:17:13 -06:00
|
|
|
import { isNode } from 'browser-or-node'
|
|
|
|
import * as fetcher from './fetcher/index.js'
|
|
|
|
import { generateProxyURL } from './utils.js'
|
2023-07-09 04:03:25 -06:00
|
|
|
import { ProxyPolicy, ProofAccessRestriction } from './enums.js'
|
2021-04-16 02:56:33 -06:00
|
|
|
|
2021-04-22 07:14:21 -06:00
|
|
|
/**
|
2021-04-22 08:00:37 -06:00
|
|
|
* @module proofs
|
2021-04-22 07:14:21 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate the proof request to the correct fetcher.
|
|
|
|
* This method uses the current environment (browser/node), certain values from
|
|
|
|
* the `data` parameter and the proxy policy set in the `opts` parameter to
|
|
|
|
* choose the right approach to fetch the proof. An error will be thrown if no
|
|
|
|
* approach is possible.
|
|
|
|
* @async
|
2023-07-09 04:03:25 -06:00
|
|
|
* @param {import('./serviceProvider.js').ServiceProvider} data - Data from a claim definition
|
2021-04-22 07:14:21 -06:00
|
|
|
* @param {object} opts - Options to enable the request
|
|
|
|
* @returns {Promise<object|string>}
|
|
|
|
*/
|
2023-07-08 00:17:13 -06:00
|
|
|
export async function fetch (data, opts) {
|
|
|
|
if (isNode) {
|
2021-04-16 02:56:33 -06:00
|
|
|
return handleNodeRequests(data, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleBrowserRequests(data, opts)
|
|
|
|
}
|
|
|
|
|
2023-07-09 04:03:25 -06:00
|
|
|
/**
|
|
|
|
* @param {import('./serviceProvider.js').ServiceProvider} data - Data from a claim definition
|
|
|
|
* @param {object} opts - Options to enable the request
|
|
|
|
* @returns {Promise<object|string>}
|
|
|
|
*/
|
2021-04-16 02:56:33 -06:00
|
|
|
const handleBrowserRequests = (data, opts) => {
|
|
|
|
switch (opts.proxy.policy) {
|
2023-07-08 00:17:13 -06:00
|
|
|
case ProxyPolicy.ALWAYS:
|
2021-04-16 02:56:33 -06:00
|
|
|
return createProxyRequestPromise(data, opts)
|
2021-04-19 03:44:30 -06:00
|
|
|
|
2023-07-08 00:17:13 -06:00
|
|
|
case ProxyPolicy.NEVER:
|
2021-04-16 02:56:33 -06:00
|
|
|
switch (data.proof.request.access) {
|
2023-07-09 04:03:25 -06:00
|
|
|
case ProofAccessRestriction.NONE:
|
|
|
|
case ProofAccessRestriction.GRANTED:
|
2021-04-16 02:56:33 -06:00
|
|
|
return createDefaultRequestPromise(data, opts)
|
2023-07-09 04:03:25 -06:00
|
|
|
case ProofAccessRestriction.NOCORS:
|
|
|
|
case ProofAccessRestriction.SERVER:
|
2021-04-19 03:44:30 -06:00
|
|
|
throw new Error(
|
|
|
|
'Impossible to fetch proof (bad combination of service access and proxy policy)'
|
|
|
|
)
|
2021-04-16 02:56:33 -06:00
|
|
|
default:
|
|
|
|
throw new Error('Invalid proof access value')
|
|
|
|
}
|
2021-04-19 03:44:30 -06:00
|
|
|
|
2023-07-08 00:17:13 -06:00
|
|
|
case ProxyPolicy.ADAPTIVE:
|
2021-04-16 02:56:33 -06:00
|
|
|
switch (data.proof.request.access) {
|
2023-07-09 04:03:25 -06:00
|
|
|
case ProofAccessRestriction.NONE:
|
2021-04-30 07:37:13 -06:00
|
|
|
return createFallbackRequestPromise(data, opts)
|
2023-07-09 04:03:25 -06:00
|
|
|
case ProofAccessRestriction.NOCORS:
|
2021-04-16 02:56:33 -06:00
|
|
|
return createProxyRequestPromise(data, opts)
|
2023-07-09 04:03:25 -06:00
|
|
|
case ProofAccessRestriction.GRANTED:
|
2021-04-16 02:56:33 -06:00
|
|
|
return createFallbackRequestPromise(data, opts)
|
2023-07-09 04:03:25 -06:00
|
|
|
case ProofAccessRestriction.SERVER:
|
2021-04-26 03:38:35 -06:00
|
|
|
return createProxyRequestPromise(data, opts)
|
2021-04-16 02:56:33 -06:00
|
|
|
default:
|
|
|
|
throw new Error('Invalid proof access value')
|
|
|
|
}
|
2021-04-19 03:44:30 -06:00
|
|
|
|
2021-04-16 02:56:33 -06:00
|
|
|
default:
|
|
|
|
throw new Error('Invalid proxy policy')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 04:03:25 -06:00
|
|
|
/**
|
|
|
|
* @param {import('./serviceProvider.js').ServiceProvider} data - Data from a claim definition
|
|
|
|
* @param {object} opts - Options to enable the request
|
|
|
|
* @returns {Promise<object|string>}
|
|
|
|
*/
|
2021-04-16 02:56:33 -06:00
|
|
|
const handleNodeRequests = (data, opts) => {
|
|
|
|
switch (opts.proxy.policy) {
|
2023-07-08 00:17:13 -06:00
|
|
|
case ProxyPolicy.ALWAYS:
|
2021-04-16 02:56:33 -06:00
|
|
|
return createProxyRequestPromise(data, opts)
|
2021-04-19 03:44:30 -06:00
|
|
|
|
2023-07-08 00:17:13 -06:00
|
|
|
case ProxyPolicy.NEVER:
|
2021-04-16 02:56:33 -06:00
|
|
|
return createDefaultRequestPromise(data, opts)
|
2021-04-19 03:44:30 -06:00
|
|
|
|
2023-07-08 00:17:13 -06:00
|
|
|
case ProxyPolicy.ADAPTIVE:
|
2021-04-16 02:56:33 -06:00
|
|
|
return createFallbackRequestPromise(data, opts)
|
2021-04-19 03:44:30 -06:00
|
|
|
|
2021-04-16 02:56:33 -06:00
|
|
|
default:
|
|
|
|
throw new Error('Invalid proxy policy')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 04:03:25 -06:00
|
|
|
/**
|
|
|
|
* @param {import('./serviceProvider.js').ServiceProvider} data - Data from a claim definition
|
|
|
|
* @param {object} opts - Options to enable the request
|
|
|
|
* @returns {Promise<object|string>}
|
|
|
|
*/
|
2021-04-16 02:56:33 -06:00
|
|
|
const createDefaultRequestPromise = (data, opts) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2023-07-10 02:40:06 -06:00
|
|
|
if (!(data.proof.request.fetcher in fetcher)) {
|
|
|
|
reject(new Error(`fetcher for ${data.proof.request.fetcher} not found`))
|
2023-07-09 04:03:25 -06:00
|
|
|
}
|
2023-07-10 02:40:06 -06:00
|
|
|
fetcher[data.proof.request.fetcher]
|
2021-04-19 03:44:30 -06:00
|
|
|
.fn(data.proof.request.data, opts)
|
|
|
|
.then((res) => {
|
2021-04-16 02:56:33 -06:00
|
|
|
return resolve({
|
2023-07-10 02:40:06 -06:00
|
|
|
fetcher: data.proof.request.fetcher,
|
2023-06-16 07:38:47 -06:00
|
|
|
data,
|
2021-04-16 02:56:33 -06:00
|
|
|
viaProxy: false,
|
2021-07-09 15:44:52 -06:00
|
|
|
result: res
|
2021-04-16 02:56:33 -06:00
|
|
|
})
|
|
|
|
})
|
2021-04-19 03:44:30 -06:00
|
|
|
.catch((err) => {
|
2021-04-16 02:56:33 -06:00
|
|
|
return reject(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-09 04:03:25 -06:00
|
|
|
/**
|
|
|
|
* @param {import('./serviceProvider.js').ServiceProvider} data - Data from a claim definition
|
|
|
|
* @param {object} opts - Options to enable the request
|
|
|
|
* @returns {Promise<object|string>}
|
|
|
|
*/
|
2021-04-16 02:56:33 -06:00
|
|
|
const createProxyRequestPromise = (data, opts) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let proxyUrl
|
|
|
|
try {
|
2023-07-08 00:17:13 -06:00
|
|
|
proxyUrl = generateProxyURL(
|
2023-07-10 02:40:06 -06:00
|
|
|
data.proof.request.fetcher,
|
2021-04-19 03:44:30 -06:00
|
|
|
data.proof.request.data,
|
|
|
|
opts
|
|
|
|
)
|
2021-04-16 02:56:33 -06:00
|
|
|
} catch (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const requestData = {
|
|
|
|
url: proxyUrl,
|
|
|
|
format: data.proof.request.format,
|
2023-07-10 02:40:06 -06:00
|
|
|
fetcherTimeout: fetcher[data.proof.request.fetcher].timeout
|
2021-04-16 02:56:33 -06:00
|
|
|
}
|
2021-04-19 03:44:30 -06:00
|
|
|
fetcher.http
|
|
|
|
.fn(requestData, opts)
|
|
|
|
.then((res) => {
|
2021-04-16 02:56:33 -06:00
|
|
|
return resolve({
|
2023-07-10 02:40:06 -06:00
|
|
|
fetcher: 'http',
|
2023-06-16 07:38:47 -06:00
|
|
|
data,
|
2021-04-16 02:56:33 -06:00
|
|
|
viaProxy: true,
|
2021-07-09 15:44:52 -06:00
|
|
|
result: res
|
2021-04-16 02:56:33 -06:00
|
|
|
})
|
|
|
|
})
|
2021-04-19 03:44:30 -06:00
|
|
|
.catch((err) => {
|
2021-04-16 02:56:33 -06:00
|
|
|
return reject(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-09 04:03:25 -06:00
|
|
|
/**
|
|
|
|
* @param {import('./serviceProvider.js').ServiceProvider} data - Data from a claim definition
|
|
|
|
* @param {object} opts - Options to enable the request
|
|
|
|
* @returns {Promise<object|string>}
|
|
|
|
*/
|
2021-04-16 02:56:33 -06:00
|
|
|
const createFallbackRequestPromise = (data, opts) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
createDefaultRequestPromise(data, opts)
|
2021-04-19 03:44:30 -06:00
|
|
|
.then((res) => {
|
2021-04-16 02:56:33 -06:00
|
|
|
return resolve(res)
|
|
|
|
})
|
2021-04-19 03:44:30 -06:00
|
|
|
.catch((err1) => {
|
2021-04-16 02:56:33 -06:00
|
|
|
createProxyRequestPromise(data, opts)
|
2021-04-19 03:44:30 -06:00
|
|
|
.then((res) => {
|
2021-04-16 02:56:33 -06:00
|
|
|
return resolve(res)
|
|
|
|
})
|
2021-04-19 03:44:30 -06:00
|
|
|
.catch((err2) => {
|
2021-07-09 15:44:52 -06:00
|
|
|
return reject(err2)
|
2021-04-16 02:56:33 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|