@ckeditor/ckeditor5-cloud-services 47.6.1 → 48.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,23 +1,9 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-cloud-services",
3
- "version": "47.6.1",
3
+ "version": "48.0.0-alpha.0",
4
4
  "description": "CKEditor 5's Cloud Services integration layer.",
5
- "keywords": [
6
- "ckeditor",
7
- "ckeditor5",
8
- "ckeditor 5",
9
- "ckeditor5-lib",
10
- "ckeditor5-dll"
11
- ],
12
- "type": "module",
13
- "main": "src/index.js",
14
- "dependencies": {
15
- "@ckeditor/ckeditor5-core": "47.6.1",
16
- "@ckeditor/ckeditor5-utils": "47.6.1",
17
- "ckeditor5": "47.6.1"
18
- },
19
- "author": "CKSource (http://cksource.com/)",
20
5
  "license": "SEE LICENSE IN LICENSE.md",
6
+ "author": "CKSource (http://cksource.com/)",
21
7
  "homepage": "https://ckeditor.com/ckeditor-5",
22
8
  "bugs": "https://github.com/ckeditor/ckeditor5/issues",
23
9
  "repository": {
@@ -25,35 +11,28 @@
25
11
  "url": "https://github.com/ckeditor/ckeditor5.git",
26
12
  "directory": "packages/ckeditor5-cloud-services"
27
13
  },
14
+ "keywords": [
15
+ "ckeditor",
16
+ "ckeditor5",
17
+ "ckeditor 5",
18
+ "ckeditor5-lib"
19
+ ],
20
+ "type": "module",
21
+ "main": "./dist/index.js",
22
+ "exports": {
23
+ ".": "./dist/index.js",
24
+ "./dist/*": "./dist/*",
25
+ "./package.json": "./package.json"
26
+ },
27
+ "dependencies": {
28
+ "@ckeditor/ckeditor5-core": "48.0.0-alpha.0",
29
+ "@ckeditor/ckeditor5-upload": "48.0.0-alpha.0",
30
+ "@ckeditor/ckeditor5-utils": "48.0.0-alpha.0"
31
+ },
28
32
  "files": [
29
33
  "dist",
30
- "lang",
31
- "src/**/*.js",
32
- "src/**/*.d.ts",
33
- "theme",
34
- "build",
35
34
  "ckeditor5-metadata.json",
36
35
  "CHANGELOG.md"
37
36
  ],
38
- "types": "src/index.d.ts",
39
- "exports": {
40
- ".": {
41
- "types": "./src/index.d.ts",
42
- "import": "./src/index.js",
43
- "default": "./src/index.js"
44
- },
45
- "./dist/*": {
46
- "types": "./src/index.d.ts",
47
- "import": "./dist/*",
48
- "default": "./dist/*"
49
- },
50
- "./src/*": {
51
- "types": "./src/*.d.ts",
52
- "import": "./src/*",
53
- "default": "./src/*"
54
- },
55
- "./build/*": "./build/*",
56
- "./ckeditor5-metadata.json": "./ckeditor5-metadata.json",
57
- "./package.json": "./package.json"
58
- }
37
+ "types": "./dist/index.d.ts"
59
38
  }
@@ -1,4 +0,0 @@
1
- /*!
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md.
4
- */(()=>{var e={237:e=>{"use strict";e.exports=CKEditor5.dll},584:(e,t,r)=>{e.exports=r(237)("./src/utils.js")},782:(e,t,r)=>{e.exports=r(237)("./src/core.js")}},t={};function r(s){var o=t[s];if(void 0!==o)return o.exports;var n=t[s]={exports:{}};return e[s](n,n.exports,r),n.exports}r.d=(e,t)=>{for(var s in t)r.o(t,s)&&!r.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:t[s]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var s={};(()=>{"use strict";r.r(s),r.d(s,{CloudServices:()=>d,CloudServicesCore:()=>l,Token:()=>i});var e=r(782),t=r(584);const o={autoRefresh:!0},n=36e5;class i extends((0,t.ObservableMixin)()){_refresh;_options;_tokenRefreshTimeout;_isDestroyed=!1;constructor(e,r={}){if(super(),!e)throw new t.CKEditorError("token-missing-token-url",this);r.initValue&&this._validateTokenValue(r.initValue),this.set("value",r.initValue),this._refresh="function"==typeof e?e:()=>{return r=e,new Promise((e,s)=>{const o=new XMLHttpRequest;o.open("GET",r),o.addEventListener("load",()=>{const r=o.status,n=o.response;return r<200||r>299?s(new t.CKEditorError("token-cannot-download-new-token",null)):e(n)}),o.addEventListener("error",()=>s(new Error("Network Error"))),o.addEventListener("abort",()=>s(new Error("Abort"))),o.send()});var r},this._options={...o,...r}}init(){return new Promise((e,t)=>{this.value?(this._options.autoRefresh&&this._registerRefreshTokenTimeout(),e(this)):this.refreshToken().then(e).catch(t)})}refreshToken(){const e=this._options.autoRefresh;return this._refresh().then(t=>(this._validateTokenValue(t),this.set("value",t),e&&this._registerRefreshTokenTimeout(),this)).catch(r=>{throw(0,t.logWarning)("token-refresh-failed",{autoRefresh:e}),e&&this._registerRefreshTokenTimeout(5e3),r})}destroy(){this._isDestroyed=!0,clearTimeout(this._tokenRefreshTimeout)}_validateTokenValue(e){const r="string"==typeof e,s=!/^".*"$/.test(e),o=r&&3===e.split(".").length;if(!s||!o)throw new t.CKEditorError("token-not-in-jwt-format",this)}_registerRefreshTokenTimeout(e){if(clearTimeout(this._tokenRefreshTimeout),this._isDestroyed)return;const t=e||this._getTokenRefreshTimeoutTime();this._tokenRefreshTimeout=setTimeout(()=>{this.refreshToken()},t)}_getTokenRefreshTimeoutTime(){try{const[,e]=this.value.split("."),{exp:t}=JSON.parse(atob(e));if(!t)return n;t>2147483647&&console.warn("Token expiration time exceeds 32-bit integer range. This might cause unpredictable token refresh timing. Token expiration time should always be provided in seconds.",{tokenExpireTime:t});return Math.floor((1e3*t-Date.now())/2)}catch{return n}}static create(e,t={}){return new i(e,t).init()}}const a=/^data:(\S*?);base64,/;class u extends((0,t.EmitterMixin)()){file;xhr;_token;_apiAddress;constructor(e,r,s){if(super(),!e)throw new t.CKEditorError("fileuploader-missing-file",null);if(!r)throw new t.CKEditorError("fileuploader-missing-token",null);if(!s)throw new t.CKEditorError("fileuploader-missing-api-address",null);this.file=function(e){if("string"!=typeof e)return!1;return!!e.match(a)?.length}(e)?function(e,r=512){try{const t=e.match(a)[1],s=atob(e.replace(a,"")),o=[];for(let e=0;e<s.length;e+=r){const t=s.slice(e,e+r),n=new Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);o.push(new Uint8Array(n))}return new Blob(o,{type:t})}catch{throw new t.CKEditorError("fileuploader-decoding-image-data-error",null)}}(e):e,this._token=r,this._apiAddress=s}onProgress(e){return this.on("progress",(t,r)=>e(r)),this}onError(e){return this.once("error",(t,r)=>e(r)),this}abort(){this.xhr.abort()}send(){return this._prepareRequest(),this._attachXHRListeners(),this._sendRequest()}_prepareRequest(){const e=new XMLHttpRequest;e.open("POST",this._apiAddress),e.setRequestHeader("Authorization",this._token.value),e.responseType="json",this.xhr=e}_attachXHRListeners(){const e=this.xhr,t=e=>()=>this.fire("error",e);e.addEventListener("error",t("Network Error")),e.addEventListener("abort",t("Abort")),e.upload&&e.upload.addEventListener("progress",e=>{e.lengthComputable&&this.fire("progress",{total:e.total,uploaded:e.loaded})}),e.addEventListener("load",()=>{const t=e.status,r=e.response;if(t<200||t>299)return this.fire("error",r.message||r.error)})}_sendRequest(){const e=new FormData,r=this.xhr;return e.append("file",this.file),new Promise((s,o)=>{r.addEventListener("load",()=>{const e=r.status,n=r.response;return e<200||e>299?n.message?o(new t.CKEditorError("fileuploader-uploading-data-failed",this,{message:n.message})):o(n.error):s(n)}),r.addEventListener("error",()=>o(new Error("Network Error"))),r.addEventListener("abort",()=>o(new Error("Abort"))),r.send(e)})}}class h{_token;_apiAddress;constructor(e,r){if(!e)throw new t.CKEditorError("uploadgateway-missing-token",null);if(!r)throw new t.CKEditorError("uploadgateway-missing-api-address",null);this._token=e,this._apiAddress=r}upload(e){return new u(e,this._token,this._apiAddress)}}class l extends e.ContextPlugin{static get pluginName(){return"CloudServicesCore"}static get isOfficialPlugin(){return!0}createToken(e,t){return new i(e,t)}createUploadGateway(e,t){return new h(e,t)}}class d extends e.ContextPlugin{tokenUrl;uploadUrl;webSocketUrl;bundleVersion;autoRefresh=!0;token=null;_tokens=new Map;static get pluginName(){return"CloudServices"}static get isOfficialPlugin(){return!0}static get requires(){return[l]}async init(){const e=this.context.config.get("cloudServices")||{};for(const[t,r]of Object.entries(e))this[t]=r;if(!this.tokenUrl)return void(this.token=null);const t=this.context.plugins.get("CloudServicesCore").createToken(this.tokenUrl,{autoRefresh:this.autoRefresh});try{this.token=await t.init(),this._tokens.set(this.tokenUrl,this.token)}catch(e){throw t.destroy(),e}}async registerTokenUrl(e){if(this._tokens.has(e))return this.getTokenFor(e);const t=this.context.plugins.get("CloudServicesCore"),r=await t.createToken(e,{autoRefresh:this.autoRefresh}).init();return this._tokens.set(e,r),r}getTokenFor(e){const r=this._tokens.get(e);if(!r)throw new t.CKEditorError("cloudservices-token-not-registered",this);return r}destroy(){super.destroy();for(const e of this._tokens.values())e.destroy()}}})(),(window.CKEditor5=window.CKEditor5||{}).cloudServices=s})();
@@ -1,5 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- export {};
@@ -1,147 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module cloud-services/cloudservices
7
- */
8
- import { ContextPlugin } from 'ckeditor5/src/core.js';
9
- import { CKEditorError } from 'ckeditor5/src/utils.js';
10
- import { CloudServicesCore } from './cloudservicescore.js';
11
- /**
12
- * Plugin introducing the integration between CKEditor 5 and CKEditor Cloud Services .
13
- *
14
- * It initializes the token provider based on
15
- * the {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig `config.cloudService`}.
16
- */
17
- export class CloudServices extends ContextPlugin {
18
- /**
19
- * The authentication token URL for CKEditor Cloud Services or a callback to the token value promise. See the
20
- * {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig#tokenUrl} for more details.
21
- */
22
- tokenUrl;
23
- /**
24
- * The URL to which the files should be uploaded.
25
- */
26
- uploadUrl;
27
- /**
28
- * The URL for web socket communication, used by the `RealTimeCollaborativeEditing` plugin. Every customer (organization in the CKEditor
29
- * Ecosystem dashboard) has their own, unique URLs to communicate with CKEditor Cloud Services. The URL can be found in the
30
- * CKEditor Ecosystem customer dashboard.
31
- *
32
- * Note: Unlike most plugins, `RealTimeCollaborativeEditing` is not included in any CKEditor 5 build and needs to be installed manually.
33
- * Check [Collaboration overview](https://ckeditor.com/docs/ckeditor5/latest/features/collaboration/overview.html) for more details.
34
- */
35
- webSocketUrl;
36
- /**
37
- * An optional parameter used for integration with CKEditor Cloud Services when uploading the editor build to cloud services.
38
- *
39
- * Whenever the editor build or the configuration changes, this parameter should be set to a new, unique value to differentiate
40
- * the new bundle (build + configuration) from the old ones.
41
- */
42
- bundleVersion;
43
- /**
44
- * Specifies whether the token should be automatically refreshed when it expires.
45
- */
46
- autoRefresh = true;
47
- /**
48
- * Other plugins use this token for the authorization process. It handles token requesting and refreshing.
49
- * Its value is `null` when {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig#tokenUrl} is not provided.
50
- *
51
- * @readonly
52
- */
53
- token = null;
54
- /**
55
- * A map of token object instances keyed by the token URLs.
56
- */
57
- _tokens = new Map();
58
- /**
59
- * @inheritDoc
60
- */
61
- static get pluginName() {
62
- return 'CloudServices';
63
- }
64
- /**
65
- * @inheritDoc
66
- */
67
- static get isOfficialPlugin() {
68
- return true;
69
- }
70
- /**
71
- * @inheritDoc
72
- */
73
- static get requires() {
74
- return [CloudServicesCore];
75
- }
76
- /**
77
- * @inheritDoc
78
- */
79
- async init() {
80
- const config = this.context.config;
81
- const options = config.get('cloudServices') || {};
82
- for (const [key, value] of Object.entries(options)) {
83
- this[key] = value;
84
- }
85
- if (!this.tokenUrl) {
86
- this.token = null;
87
- return;
88
- }
89
- // Initialization of the token may fail. By default, the token is being refreshed on the failure.
90
- // The problem is that if this happens here, then the token refresh interval will be executed even
91
- // after destroying the editor (as the exception was thrown from `init` method). To prevent that
92
- // behavior we need to catch the exception and destroy the uninitialized token instance.
93
- // See: https://github.com/ckeditor/ckeditor5/issues/17531
94
- const cloudServicesCore = this.context.plugins.get('CloudServicesCore');
95
- const uninitializedToken = cloudServicesCore.createToken(this.tokenUrl, { autoRefresh: this.autoRefresh });
96
- try {
97
- this.token = await uninitializedToken.init();
98
- this._tokens.set(this.tokenUrl, this.token);
99
- }
100
- catch (error) {
101
- uninitializedToken.destroy();
102
- throw error;
103
- }
104
- }
105
- /**
106
- * Registers an additional authentication token URL for CKEditor Cloud Services or a callback to the token value promise. See the
107
- * {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig#tokenUrl} for more details.
108
- *
109
- * @param tokenUrl The authentication token URL for CKEditor Cloud Services or a callback to the token value promise.
110
- */
111
- async registerTokenUrl(tokenUrl) {
112
- // Reuse the token instance in case of multiple features using the same token URL.
113
- if (this._tokens.has(tokenUrl)) {
114
- return this.getTokenFor(tokenUrl);
115
- }
116
- const cloudServicesCore = this.context.plugins.get('CloudServicesCore');
117
- const token = await cloudServicesCore.createToken(tokenUrl, { autoRefresh: this.autoRefresh }).init();
118
- this._tokens.set(tokenUrl, token);
119
- return token;
120
- }
121
- /**
122
- * Returns an authentication token provider previously registered by {@link #registerTokenUrl}.
123
- *
124
- * @param tokenUrl The authentication token URL for CKEditor Cloud Services or a callback to the token value promise.
125
- */
126
- getTokenFor(tokenUrl) {
127
- const token = this._tokens.get(tokenUrl);
128
- if (!token) {
129
- /**
130
- * The provided `tokenUrl` was not registered by {@link module:cloud-services/cloudservices~CloudServices#registerTokenUrl}.
131
- *
132
- * @error cloudservices-token-not-registered
133
- */
134
- throw new CKEditorError('cloudservices-token-not-registered', this);
135
- }
136
- return token;
137
- }
138
- /**
139
- * @inheritDoc
140
- */
141
- destroy() {
142
- super.destroy();
143
- for (const token of this._tokens.values()) {
144
- token.destroy();
145
- }
146
- }
147
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- export {};
@@ -1,47 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module cloud-services/cloudservicescore
7
- */
8
- import { ContextPlugin } from 'ckeditor5/src/core.js';
9
- import { Token } from './token/token.js';
10
- import { UploadGateway } from './uploadgateway/uploadgateway.js';
11
- /**
12
- * The `CloudServicesCore` plugin exposes the base API for communication with CKEditor Cloud Services.
13
- */
14
- export class CloudServicesCore extends ContextPlugin {
15
- /**
16
- * @inheritDoc
17
- */
18
- static get pluginName() {
19
- return 'CloudServicesCore';
20
- }
21
- /**
22
- * @inheritDoc
23
- */
24
- static get isOfficialPlugin() {
25
- return true;
26
- }
27
- /**
28
- * Creates the {@link module:cloud-services/token/token~Token} instance.
29
- *
30
- * @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
31
- * value is a function it has to match the {@link module:cloud-services/token/token~Token#refreshToken} interface.
32
- * @param options.initValue Initial value of the token.
33
- * @param options.autoRefresh Specifies whether to start the refresh automatically.
34
- */
35
- createToken(tokenUrlOrRefreshToken, options) {
36
- return new Token(tokenUrlOrRefreshToken, options);
37
- }
38
- /**
39
- * Creates the {@link module:cloud-services/uploadgateway/uploadgateway~UploadGateway} instance.
40
- *
41
- * @param token Token used for authentication.
42
- * @param apiAddress API address.
43
- */
44
- createUploadGateway(token, apiAddress) {
45
- return new UploadGateway(token, apiAddress);
46
- }
47
- }
package/src/index.js DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module cloud-services
7
- */
8
- export { CloudServices } from './cloudservices.js';
9
- export { CloudServicesCore } from './cloudservicescore.js';
10
- export { Token } from './token/token.js';
11
- import './augmentation.js';
@@ -1,232 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module cloud-services/token/token
7
- */
8
- import { ObservableMixin, CKEditorError, logWarning } from 'ckeditor5/src/utils.js';
9
- const DEFAULT_OPTIONS = { autoRefresh: true };
10
- const DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME = 3600000; // 1 hour
11
- const TOKEN_FAILED_REFRESH_TIMEOUT_TIME = 5000; // 5 seconds
12
- /**
13
- * The class representing the token used for communication with CKEditor Cloud Services.
14
- * The value of the token is retrieved from the specified URL and refreshed every 1 hour by default.
15
- * If the token retrieval fails, the token will automatically retry in 5 seconds intervals.
16
- */
17
- export class Token extends /* #__PURE__ */ ObservableMixin() {
18
- /**
19
- * Base refreshing function.
20
- */
21
- _refresh;
22
- /**
23
- * Cached token options.
24
- */
25
- _options;
26
- /**
27
- * `setTimeout()` id for a token refresh when {@link module:cloud-services/token/token~CloudServicesTokenOptions auto refresh}
28
- * is enabled.
29
- */
30
- _tokenRefreshTimeout;
31
- /**
32
- * Flag indicating whether the token has been destroyed.
33
- */
34
- _isDestroyed = false;
35
- /**
36
- * Creates `Token` instance.
37
- * Method `init` should be called after using the constructor or use `create` method instead.
38
- *
39
- * @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
40
- * value is a function it has to match the {@link module:cloud-services/token/token~Token#refreshToken} interface.
41
- */
42
- constructor(tokenUrlOrRefreshToken, options = {}) {
43
- super();
44
- if (!tokenUrlOrRefreshToken) {
45
- /**
46
- * A `tokenUrl` must be provided as the first constructor argument.
47
- *
48
- * @error token-missing-token-url
49
- */
50
- throw new CKEditorError('token-missing-token-url', this);
51
- }
52
- if (options.initValue) {
53
- this._validateTokenValue(options.initValue);
54
- }
55
- this.set('value', options.initValue);
56
- if (typeof tokenUrlOrRefreshToken === 'function') {
57
- this._refresh = tokenUrlOrRefreshToken;
58
- }
59
- else {
60
- this._refresh = () => defaultRefreshToken(tokenUrlOrRefreshToken);
61
- }
62
- this._options = { ...DEFAULT_OPTIONS, ...options };
63
- }
64
- /**
65
- * Initializes the token.
66
- */
67
- init() {
68
- return new Promise((resolve, reject) => {
69
- if (!this.value) {
70
- this.refreshToken()
71
- .then(resolve)
72
- .catch(reject);
73
- return;
74
- }
75
- if (this._options.autoRefresh) {
76
- this._registerRefreshTokenTimeout();
77
- }
78
- resolve(this);
79
- });
80
- }
81
- /**
82
- * Refresh token method. Useful in a method form as it can be overridden in tests.
83
- *
84
- * This method will be invoked periodically based on the token expiry date after first call to keep the token up-to-date
85
- * (requires {@link module:cloud-services/token/token~CloudServicesTokenOptions auto refresh option} to be set).
86
- *
87
- * If the token refresh fails, the method will retry in 5 seconds intervals until success or the token gets
88
- * {@link #destroy destroyed}.
89
- */
90
- refreshToken() {
91
- const autoRefresh = this._options.autoRefresh;
92
- return this._refresh()
93
- .then(value => {
94
- this._validateTokenValue(value);
95
- this.set('value', value);
96
- if (autoRefresh) {
97
- this._registerRefreshTokenTimeout();
98
- }
99
- return this;
100
- })
101
- .catch(err => {
102
- /**
103
- * You will see this warning when the CKEditor {@link module:cloud-services/token/token~Token token} could not be refreshed.
104
- * This may be a result of a network error, a token endpoint (server) error, or an invalid
105
- * {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig#tokenUrl token URL configuration}.
106
- *
107
- * If this warning repeats, please make sure that the configuration is correct and that the token
108
- * endpoint is up and running. {@link module:cloud-services/cloudservicesconfig~CloudServicesConfig#tokenUrl Learn more}
109
- * about token configuration.
110
- *
111
- * **Note:** If the token's {@link module:cloud-services/token/token~CloudServicesTokenOptions auto refresh option}
112
- * is enabled, attempts to refresh will be made until success or token's
113
- * {@link module:cloud-services/token/token~Token#destroy destruction}.
114
- *
115
- * @error token-refresh-failed
116
- * @param {boolean} autoRefresh Whether the token will keep auto refreshing.
117
- */
118
- logWarning('token-refresh-failed', { autoRefresh });
119
- // If the refresh failed, keep trying to refresh the token. Failing to do so will eventually
120
- // lead to the disconnection from the RTC service and the editing session (and potential data loss
121
- // if the user keeps editing).
122
- if (autoRefresh) {
123
- this._registerRefreshTokenTimeout(TOKEN_FAILED_REFRESH_TIMEOUT_TIME);
124
- }
125
- throw err;
126
- });
127
- }
128
- /**
129
- * Destroys token instance. Stops refreshing.
130
- */
131
- destroy() {
132
- this._isDestroyed = true;
133
- clearTimeout(this._tokenRefreshTimeout);
134
- }
135
- /**
136
- * Checks whether the provided token follows the JSON Web Tokens (JWT) format.
137
- *
138
- * @param tokenValue The token to validate.
139
- */
140
- _validateTokenValue(tokenValue) {
141
- // The token must be a string.
142
- const isString = typeof tokenValue === 'string';
143
- // The token must be a plain string without quotes ("").
144
- const isPlainString = !/^".*"$/.test(tokenValue);
145
- // JWT token contains 3 parts: header, payload, and signature.
146
- // Each part is separated by a dot.
147
- const isJWTFormat = isString && tokenValue.split('.').length === 3;
148
- if (!(isPlainString && isJWTFormat)) {
149
- /**
150
- * The provided token must follow the [JSON Web Tokens](https://jwt.io/introduction/) format.
151
- *
152
- * @error token-not-in-jwt-format
153
- */
154
- throw new CKEditorError('token-not-in-jwt-format', this);
155
- }
156
- }
157
- /**
158
- * Registers a refresh token timeout for the time taken from token.
159
- */
160
- _registerRefreshTokenTimeout(timeoutTime) {
161
- clearTimeout(this._tokenRefreshTimeout);
162
- if (this._isDestroyed) {
163
- return;
164
- }
165
- const tokenRefreshTimeoutTime = timeoutTime || this._getTokenRefreshTimeoutTime();
166
- this._tokenRefreshTimeout = setTimeout(() => {
167
- this.refreshToken();
168
- }, tokenRefreshTimeoutTime);
169
- }
170
- /**
171
- * Returns token refresh timeout time calculated from expire time in the token payload.
172
- *
173
- * If the token parse fails or the token payload doesn't contain, the default DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME is returned.
174
- */
175
- _getTokenRefreshTimeoutTime() {
176
- try {
177
- const [, binaryTokenPayload] = this.value.split('.');
178
- const { exp: tokenExpireTime } = JSON.parse(atob(binaryTokenPayload));
179
- if (!tokenExpireTime) {
180
- return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
181
- }
182
- // Check if the token expire time exceeds 32-bit integer range
183
- // It could happen if the token expire time is provided in milliseconds instead of seconds.
184
- if (tokenExpireTime > 2147483647) {
185
- console.warn('Token expiration time exceeds 32-bit integer range. This might cause unpredictable token refresh timing. ' +
186
- 'Token expiration time should always be provided in seconds.', { tokenExpireTime });
187
- }
188
- const tokenRefreshTimeoutTime = Math.floor(((tokenExpireTime * 1000) - Date.now()) / 2);
189
- return tokenRefreshTimeoutTime;
190
- }
191
- catch {
192
- return DEFAULT_TOKEN_REFRESH_TIMEOUT_TIME;
193
- }
194
- }
195
- /**
196
- * Creates a initialized {@link module:cloud-services/token/token~Token} instance.
197
- *
198
- * @param tokenUrlOrRefreshToken Endpoint address to download the token or a callback that provides the token. If the
199
- * value is a function it has to match the {@link module:cloud-services/token/token~Token#refreshToken} interface.
200
- */
201
- static create(tokenUrlOrRefreshToken, options = {}) {
202
- const token = new Token(tokenUrlOrRefreshToken, options);
203
- return token.init();
204
- }
205
- }
206
- /**
207
- * This function is called in a defined interval by the {@link ~Token} class. It also can be invoked manually.
208
- * It should return a promise, which resolves with the new token value.
209
- * If any error occurs it should return a rejected promise with an error message.
210
- */
211
- function defaultRefreshToken(tokenUrl) {
212
- return new Promise((resolve, reject) => {
213
- const xhr = new XMLHttpRequest();
214
- xhr.open('GET', tokenUrl);
215
- xhr.addEventListener('load', () => {
216
- const statusCode = xhr.status;
217
- const xhrResponse = xhr.response;
218
- if (statusCode < 200 || statusCode > 299) {
219
- /**
220
- * Cannot download new token from the provided url.
221
- *
222
- * @error token-cannot-download-new-token
223
- */
224
- return reject(new CKEditorError('token-cannot-download-new-token', null));
225
- }
226
- return resolve(xhrResponse);
227
- });
228
- xhr.addEventListener('error', () => reject(new Error('Network Error')));
229
- xhr.addEventListener('abort', () => reject(new Error('Abort')));
230
- xhr.send();
231
- });
232
- }