@ckeditor/ckeditor-cloud-services-core
Advanced tools
Comparing version 22.0.0 to 23.0.0
{ | ||
"name": "@ckeditor/ckeditor-cloud-services-core", | ||
"version": "22.0.0", | ||
"version": "23.0.0", | ||
"description": "CKEditor Cloud Services Core API.", | ||
@@ -10,3 +10,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@ckeditor/ckeditor5-utils": "^22.0.0" | ||
"@ckeditor/ckeditor5-utils": "^23.0.0" | ||
}, | ||
@@ -13,0 +13,0 @@ "engines": { |
@@ -10,3 +10,3 @@ /** | ||
/* globals XMLHttpRequest, setInterval, clearInterval */ | ||
/* globals XMLHttpRequest, setTimeout, clearTimeout, atob */ | ||
@@ -17,3 +17,4 @@ import mix from '@ckeditor/ckeditor5-utils/src/mix'; | ||
const DEFAULT_OPTIONS = { refreshInterval: 3600000, autoRefresh: true }; | ||
const DEFAULT_OPTIONS = { autoRefresh: true }; | ||
const DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME = 3600000; | ||
@@ -35,3 +36,2 @@ /** | ||
* @param {String} [options.initValue] Initial value of the token. | ||
* @param {Number} [options.refreshInterval=3600000] Delay between refreshes. Default 1 hour. | ||
* @param {Boolean} [options.autoRefresh=true] Specifies whether to start the refresh automatically. | ||
@@ -47,3 +47,3 @@ */ | ||
throw new CKEditorError( | ||
'token-missing-token-url: A `tokenUrl` must be provided as the first constructor argument.', | ||
'token-missing-token-url', | ||
this | ||
@@ -53,2 +53,6 @@ ); | ||
if ( options.initValue ) { | ||
this._validateTokenValue( options.initValue ); | ||
} | ||
/** | ||
@@ -92,6 +96,2 @@ * Value of the token. | ||
return new Promise( ( resolve, reject ) => { | ||
if ( this._options.autoRefresh ) { | ||
this._startRefreshing(); | ||
} | ||
if ( !this.value ) { | ||
@@ -105,2 +105,6 @@ this.refreshToken() | ||
if ( this._options.autoRefresh ) { | ||
this._registerRefreshTokenTimeout(); | ||
} | ||
resolve( this ); | ||
@@ -116,3 +120,10 @@ } ); | ||
return this._refresh() | ||
.then( value => this.set( 'value', value ) ) | ||
.then( value => { | ||
this._validateTokenValue( value ); | ||
this.set( 'value', value ); | ||
if ( this._options.autoRefresh ) { | ||
this._registerRefreshTokenTimeout(); | ||
} | ||
} ) | ||
.then( () => this ); | ||
@@ -125,24 +136,73 @@ } | ||
destroy() { | ||
this._stopRefreshing(); | ||
clearTimeout( this._tokenRefreshTimeout ); | ||
} | ||
/** | ||
* Starts value refreshing every `refreshInterval` time. | ||
* Checks whether the provided token follows the JSON Web Tokens (JWT) format. | ||
* | ||
* @protected | ||
* @param {String} tokenValue The token to validate. | ||
*/ | ||
_startRefreshing() { | ||
this._refreshInterval = setInterval( () => this.refreshToken(), this._options.refreshInterval ); | ||
_validateTokenValue( tokenValue ) { | ||
// The token must be a string. | ||
const isString = typeof tokenValue === 'string'; | ||
// The token must be a plain string without quotes (""). | ||
const isPlainString = !/^".*"$/.test( tokenValue ); | ||
// JWT token contains 3 parts: header, payload, and signature. | ||
// Each part is separated by a dot. | ||
const isJWTFormat = isString && tokenValue.split( '.' ).length === 3; | ||
if ( !( isPlainString && isJWTFormat ) ) { | ||
/** | ||
* The provided token must follow the [JSON Web Tokens](https://jwt.io/introduction/) format. | ||
* | ||
* @error token-not-in-jwt-format | ||
*/ | ||
throw new CKEditorError( 'token-not-in-jwt-format', this ); | ||
} | ||
} | ||
/** | ||
* Stops value refreshing. | ||
* Registers a refresh token timeout for the time taken from token. | ||
* | ||
* @protected | ||
*/ | ||
_stopRefreshing() { | ||
clearInterval( this._refreshInterval ); | ||
_registerRefreshTokenTimeout() { | ||
const tokenRefreshTimeoutTime = this._getTokenRefreshTimeoutTime(); | ||
clearTimeout( this._tokenRefreshTimeout ); | ||
this._tokenRefreshTimeout = setTimeout( () => { | ||
this.refreshToken(); | ||
}, tokenRefreshTimeoutTime ); | ||
} | ||
/** | ||
* Returns token refresh timeout time calculated from expire time in the token payload. | ||
* | ||
* If the token parse fails or the token payload doesn't contain, the default DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME is returned. | ||
* | ||
* @protected | ||
* @returns {Number} | ||
*/ | ||
_getTokenRefreshTimeoutTime() { | ||
try { | ||
const [ , binaryTokenPayload ] = this.value.split( '.' ); | ||
const { exp: tokenExpireTime } = JSON.parse( atob( binaryTokenPayload ) ); | ||
if ( !tokenExpireTime ) { | ||
return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME; | ||
} | ||
const tokenRefreshTimeoutTime = Math.floor( ( ( tokenExpireTime * 1000 ) - Date.now() ) / 2 ); | ||
return tokenRefreshTimeoutTime; | ||
} catch ( err ) { | ||
return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME; | ||
} | ||
} | ||
/** | ||
* Creates a initialized {@link module:cloud-services-core/token~Token} instance. | ||
@@ -154,3 +214,2 @@ * | ||
* @param {String} [options.initValue] Initial value of the token. | ||
* @param {Number} [options.refreshInterval=3600000] Delay between refreshes. Default 1 hour. | ||
* @param {Boolean} [options.autoRefresh=true] Specifies whether to start the refresh automatically. | ||
@@ -198,3 +257,3 @@ * @returns {Promise.<module:cloud-services-core/token~Token>} | ||
return reject( | ||
new CKEditorError( 'token-cannot-download-new-token: Cannot download new token from the provided url.', null ) | ||
new CKEditorError( 'token-cannot-download-new-token', null ) | ||
); | ||
@@ -201,0 +260,0 @@ } |
@@ -36,3 +36,3 @@ /** | ||
*/ | ||
throw new CKEditorError( 'fileuploader-missing-file: File must be provided as the first argument', null ); | ||
throw new CKEditorError( 'fileuploader-missing-file', null ); | ||
} | ||
@@ -46,3 +46,3 @@ | ||
*/ | ||
throw new CKEditorError( 'fileuploader-missing-token: Token must be provided as the second argument.', null ); | ||
throw new CKEditorError( 'fileuploader-missing-token', null ); | ||
} | ||
@@ -56,3 +56,3 @@ | ||
*/ | ||
throw new CKEditorError( 'fileuploader-missing-api-address: Api address must be provided as the third argument.', null ); | ||
throw new CKEditorError( 'fileuploader-missing-api-address', null ); | ||
} | ||
@@ -207,3 +207,3 @@ | ||
return reject( new CKEditorError( | ||
'fileuploader-uploading-data-failed: Uploading file failed.', | ||
'fileuploader-uploading-data-failed', | ||
this, | ||
@@ -277,3 +277,3 @@ { message: xhrResponse.message } | ||
*/ | ||
throw new CKEditorError( 'fileuploader-decoding-image-data-error: Problem with decoding Base64 image data.', null ); | ||
throw new CKEditorError( 'fileuploader-decoding-image-data-error', null ); | ||
} | ||
@@ -280,0 +280,0 @@ } |
@@ -30,3 +30,3 @@ /** | ||
*/ | ||
throw new CKEditorError( 'uploadgateway-missing-token: Token must be provided.', null ); | ||
throw new CKEditorError( 'uploadgateway-missing-token', null ); | ||
} | ||
@@ -40,3 +40,3 @@ | ||
*/ | ||
throw new CKEditorError( 'uploadgateway-missing-api-address: Api address must be provided.', null ); | ||
throw new CKEditorError( 'uploadgateway-missing-api-address', null ); | ||
} | ||
@@ -43,0 +43,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20126
538
+ Added@ckeditor/ckeditor5-utils@23.1.0(transitive)
- Removed@ckeditor/ckeditor5-utils@22.0.0(transitive)