-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #659 from uploadcare/feat/new-resolvers
Add `secureDeliveryProxyUrlResolver` and `secureUploadsTokenResolver` options
- Loading branch information
Showing
17 changed files
with
395 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// @ts-check | ||
|
||
import { isSecureTokenExpired } from '../utils/isSecureTokenExpired.js'; | ||
|
||
export class SecureUploadsManager { | ||
/** | ||
* @private | ||
* @type {import('./UploaderBlock.js').UploaderBlock} | ||
*/ | ||
_block; | ||
/** | ||
* @private | ||
* @type {import('../types').SecureUploadsSignatureAndExpire | null} | ||
*/ | ||
_secureToken = null; | ||
|
||
/** @param {import('./UploaderBlock.js').UploaderBlock} block */ | ||
constructor(block) { | ||
this._block = block; | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {unknown[]} args | ||
*/ | ||
_debugPrint(...args) { | ||
this._block.debugPrint('[secure-uploads]', ...args); | ||
} | ||
|
||
/** @returns {Promise<import('../types').SecureUploadsSignatureAndExpire | null>} */ | ||
async getSecureToken() { | ||
const { secureSignature, secureExpire, secureUploadsSignatureResolver } = this._block.cfg; | ||
|
||
if ((secureSignature || secureExpire) && secureUploadsSignatureResolver) { | ||
console.warn( | ||
'Both secureSignature/secureExpire and secureUploadsSignatureResolver are set. secureUploadsSignatureResolver will be used.', | ||
); | ||
} | ||
|
||
if (secureUploadsSignatureResolver) { | ||
if ( | ||
!this._secureToken || | ||
isSecureTokenExpired(this._secureToken, { threshold: this._block.cfg.secureUploadsExpireThreshold }) | ||
) { | ||
if (!this._secureToken) { | ||
this._debugPrint('Secure signature is not set yet.'); | ||
} else { | ||
this._debugPrint('Secure signature is expired. Resolving a new one...'); | ||
} | ||
try { | ||
const result = await secureUploadsSignatureResolver(); | ||
if (!result) { | ||
this._debugPrint('Secure signature resolver returned nothing.'); | ||
this._secureToken = null; | ||
} else if (!result.secureSignature || !result.secureExpire) { | ||
console.error('Secure signature resolver returned an invalid result:', result); | ||
} else { | ||
this._debugPrint('Secure signature resolved:', result); | ||
this._debugPrint( | ||
'Secure signature will expire in', | ||
new Date(Number(result.secureExpire) * 1000).toISOString(), | ||
); | ||
this._secureToken = result; | ||
} | ||
} catch (err) { | ||
console.error('Secure signature resolving failed. Falling back to the previous one.', err); | ||
} | ||
} | ||
|
||
return this._secureToken; | ||
} | ||
|
||
if (secureSignature && secureExpire) { | ||
this._debugPrint('Secure signature and expire are set. Using them...', { | ||
secureSignature, | ||
secureExpire, | ||
}); | ||
|
||
return { | ||
secureSignature, | ||
secureExpire, | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
demo/preview-proxy/secure-delivery-proxy-url-resolver.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!doctype html> | ||
<base href="../../" /> | ||
|
||
<head> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="stylesheet" href="./solutions/file-uploader/regular/index.css" /> | ||
<script | ||
async="" | ||
src="https://cdn.skypack.dev/-/[email protected]/dist=es2020,mode=raw,min/dist/es-module-shims.js" | ||
></script> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"@symbiotejs/symbiote": "./node_modules/@symbiotejs/symbiote/build/symbiote.js", | ||
"@uploadcare/upload-client": "./node_modules/@uploadcare/upload-client/dist/esm/index.browser.mjs", | ||
"@uploadcare/image-shrink": "./node_modules/@uploadcare/image-shrink/dist/esm/index.browser.mjs" | ||
} | ||
} | ||
</script> | ||
<script src="./index.js" type="module"></script> | ||
<script type="module"> | ||
import * as LR from './index.js'; | ||
|
||
LR.registerBlocks(LR); | ||
|
||
const config = document.querySelector('lr-config'); | ||
config.secureDeliveryProxyUrlResolver = (previewUrl) => { | ||
return `http://localhost:3000/preview?url=${encodeURIComponent(previewUrl)}` | ||
}; | ||
</script> | ||
</head> | ||
|
||
<lr-file-uploader-regular ctx-name="my-uploader"></lr-file-uploader-regular> | ||
<lr-config ctx-name="my-uploader" pubkey="demopublickey"></lr-config> | ||
<lr-upload-ctx-provider ctx-name="my-uploader"></lr-upload-ctx-provider> |
34 changes: 34 additions & 0 deletions
34
demo/preview-proxy/secure-delivery-proxy-url-template.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!doctype html> | ||
<base href="../../" /> | ||
|
||
<head> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link rel="stylesheet" href="./solutions/file-uploader/regular/index.css" /> | ||
<script | ||
async="" | ||
src="https://cdn.skypack.dev/-/[email protected]/dist=es2020,mode=raw,min/dist/es-module-shims.js" | ||
></script> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"@symbiotejs/symbiote": "./node_modules/@symbiotejs/symbiote/build/symbiote.js", | ||
"@uploadcare/upload-client": "./node_modules/@uploadcare/upload-client/dist/esm/index.browser.mjs", | ||
"@uploadcare/image-shrink": "./node_modules/@uploadcare/image-shrink/dist/esm/index.browser.mjs" | ||
} | ||
} | ||
</script> | ||
<script src="./index.js" type="module"></script> | ||
<script type="module"> | ||
import * as LR from './index.js'; | ||
|
||
LR.registerBlocks(LR); | ||
</script> | ||
</head> | ||
|
||
<lr-file-uploader-regular ctx-name="my-uploader"></lr-file-uploader-regular> | ||
<lr-config | ||
ctx-name="my-uploader" | ||
pubkey="demopublickey" | ||
secure-delivery-proxy="http://localhost:3000/preview?url={{previewUrl}}" | ||
></lr-config> | ||
<lr-upload-ctx-provider ctx-name="my-uploader"></lr-upload-ctx-provider> |
File renamed without changes.
Oops, something went wrong.