@ckeditor/ckeditor5-adapter-ckfinder 35.4.0 → 36.0.1
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/LICENSE.md +1 -1
- package/build/adapter-ckfinder.js +2 -2
- package/package.json +22 -18
- package/src/index.js +1 -3
- package/src/uploadadapter.js +111 -163
- package/src/utils.js +32 -57
package/LICENSE.md
CHANGED
|
@@ -2,7 +2,7 @@ Software License Agreement
|
|
|
2
2
|
==========================
|
|
3
3
|
|
|
4
4
|
**CKEditor 5 CKFinder adapter** – https://github.com/ckeditor/ckeditor5-adapter-ckfinder <br>
|
|
5
|
-
Copyright (c) 2003-
|
|
5
|
+
Copyright (c) 2003-2023, [CKSource Holding sp. z o.o.](https://cksource.com) All rights reserved.
|
|
6
6
|
|
|
7
7
|
Licensed under the terms of [GNU General Public License Version 2 or later](http://www.gnu.org/licenses/gpl.html).
|
|
8
8
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md.
|
|
4
|
-
*/(()=>{var e={704:(e,t,r)=>{e.exports=r(79)("./src/core.js")},448:(e,t,r)=>{e.exports=r(79)("./src/upload.js")},79:e=>{"use strict";e.exports=CKEditor5.dll}},t={};function r(o){var n=t[o];if(void 0!==n)return n.exports;var s=t[o]={exports:{}};return e[o](s,s.exports,r),s.exports}r.d=(e,t)=>{for(var o in t)r.o(t,o)&&!r.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},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 o={};(()=>{"use strict";r.r(o),r.d(o,{UploadAdapter:()=>
|
|
4
|
+
*/(()=>{var e={704:(e,t,r)=>{e.exports=r(79)("./src/core.js")},448:(e,t,r)=>{e.exports=r(79)("./src/upload.js")},79:e=>{"use strict";e.exports=CKEditor5.dll}},t={};function r(o){var n=t[o];if(void 0!==n)return n.exports;var s=t[o]={exports:{}};return e[o](s,s.exports,r),s.exports}r.d=(e,t)=>{for(var o in t)r.o(t,o)&&!r.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},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 o={};(()=>{"use strict";r.r(o),r.d(o,{UploadAdapter:()=>a});var e=r(704),t=r(448);const n="ckCsrfToken",s=40,i="abcdefghijklmnopqrstuvwxyz0123456789";function d(){let e=function(e){e=e.toLowerCase();const t=document.cookie.split(";");for(const r of t){const t=r.split("=");if(decodeURIComponent(t[0].trim().toLowerCase())===e)return decodeURIComponent(t[1])}return null}(n);var t,r;return e&&e.length==s||(e=function(e){let t="";const r=new Uint8Array(e);window.crypto.getRandomValues(r);for(let e=0;e<r.length;e++){const o=i.charAt(r[e]%i.length);t+=Math.random()>.5?o.toUpperCase():o}return t}(s),t=n,r=e,document.cookie=encodeURIComponent(t)+"="+encodeURIComponent(r)+";path=/"),e}class a extends e.Plugin{static get requires(){return[t.FileRepository]}static get pluginName(){return"CKFinderUploadAdapter"}init(){const e=this.editor.config.get("ckfinder.uploadUrl");e&&(this.editor.plugins.get(t.FileRepository).createUploadAdapter=t=>new l(t,e,this.editor.t))}}class l{constructor(e,t,r){this.loader=e,this.url=t,this.t=r}upload(){return this.loader.file.then((e=>new Promise(((t,r)=>{this._initRequest(),this._initListeners(t,r,e),this._sendRequest(e)}))))}abort(){this.xhr&&this.xhr.abort()}_initRequest(){const e=this.xhr=new XMLHttpRequest;e.open("POST",this.url,!0),e.responseType="json"}_initListeners(e,t,r){const o=this.xhr,n=this.loader,s=(0,this.t)("Cannot upload file:")+` ${r.name}.`;o.addEventListener("error",(()=>t(s))),o.addEventListener("abort",(()=>t())),o.addEventListener("load",(()=>{const r=o.response;if(!r||!r.uploaded)return t(r&&r.error&&r.error.message?r.error.message:s);e({default:r.url})})),o.upload&&o.upload.addEventListener("progress",(e=>{e.lengthComputable&&(n.uploadTotal=e.total,n.uploaded=e.loaded)}))}_sendRequest(e){const t=new FormData;t.append("upload",e),t.append("ckCsrfToken",d()),this.xhr.send(t)}}})(),(window.CKEditor5=window.CKEditor5||{}).adapterCkfinder=o})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckeditor/ckeditor5-adapter-ckfinder",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "36.0.1",
|
|
4
4
|
"description": "CKFinder adapter for CKEditor 5.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ckeditor",
|
|
@@ -12,23 +12,24 @@
|
|
|
12
12
|
],
|
|
13
13
|
"main": "src/index.js",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"ckeditor5": "^
|
|
15
|
+
"ckeditor5": "^36.0.1"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@ckeditor/ckeditor5-basic-styles": "^
|
|
19
|
-
"@ckeditor/ckeditor5-core": "^
|
|
20
|
-
"@ckeditor/ckeditor5-clipboard": "^
|
|
21
|
-
"@ckeditor/ckeditor5-dev-utils": "^
|
|
22
|
-
"@ckeditor/ckeditor5-editor-classic": "^
|
|
23
|
-
"@ckeditor/ckeditor5-enter": "^
|
|
24
|
-
"@ckeditor/ckeditor5-heading": "^
|
|
25
|
-
"@ckeditor/ckeditor5-image": "^
|
|
26
|
-
"@ckeditor/ckeditor5-list": "^
|
|
27
|
-
"@ckeditor/ckeditor5-paragraph": "^
|
|
28
|
-
"@ckeditor/ckeditor5-theme-lark": "^
|
|
29
|
-
"@ckeditor/ckeditor5-typing": "^
|
|
30
|
-
"@ckeditor/ckeditor5-undo": "^
|
|
31
|
-
"@ckeditor/ckeditor5-upload": "^
|
|
18
|
+
"@ckeditor/ckeditor5-basic-styles": "^36.0.1",
|
|
19
|
+
"@ckeditor/ckeditor5-core": "^36.0.1",
|
|
20
|
+
"@ckeditor/ckeditor5-clipboard": "^36.0.1",
|
|
21
|
+
"@ckeditor/ckeditor5-dev-utils": "^32.0.0",
|
|
22
|
+
"@ckeditor/ckeditor5-editor-classic": "^36.0.1",
|
|
23
|
+
"@ckeditor/ckeditor5-enter": "^36.0.1",
|
|
24
|
+
"@ckeditor/ckeditor5-heading": "^36.0.1",
|
|
25
|
+
"@ckeditor/ckeditor5-image": "^36.0.1",
|
|
26
|
+
"@ckeditor/ckeditor5-list": "^36.0.1",
|
|
27
|
+
"@ckeditor/ckeditor5-paragraph": "^36.0.1",
|
|
28
|
+
"@ckeditor/ckeditor5-theme-lark": "^36.0.1",
|
|
29
|
+
"@ckeditor/ckeditor5-typing": "^36.0.1",
|
|
30
|
+
"@ckeditor/ckeditor5-undo": "^36.0.1",
|
|
31
|
+
"@ckeditor/ckeditor5-upload": "^36.0.1",
|
|
32
|
+
"typescript": "^4.8.4",
|
|
32
33
|
"webpack": "^5.58.1",
|
|
33
34
|
"webpack-cli": "^4.9.0"
|
|
34
35
|
},
|
|
@@ -47,13 +48,16 @@
|
|
|
47
48
|
},
|
|
48
49
|
"files": [
|
|
49
50
|
"lang",
|
|
50
|
-
"src",
|
|
51
|
+
"src/**/*.js",
|
|
52
|
+
"src/**/*.d.ts",
|
|
51
53
|
"theme",
|
|
52
54
|
"build",
|
|
53
55
|
"ckeditor5-metadata.json",
|
|
54
56
|
"CHANGELOG.md"
|
|
55
57
|
],
|
|
56
58
|
"scripts": {
|
|
57
|
-
"dll:build": "webpack"
|
|
59
|
+
"dll:build": "webpack",
|
|
60
|
+
"build": "tsc -p ./tsconfig.release.json",
|
|
61
|
+
"postversion": "npm run build"
|
|
58
62
|
}
|
|
59
63
|
}
|
package/src/index.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* @module adapter-ckfinder
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
8
|
export { default as UploadAdapter } from './uploadadapter';
|
package/src/uploadadapter.js
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
5
|
/* globals XMLHttpRequest, FormData */
|
|
7
|
-
|
|
8
6
|
/**
|
|
9
7
|
* @module adapter-ckfinder/uploadadapter
|
|
10
8
|
*/
|
|
11
|
-
|
|
12
9
|
import { Plugin } from 'ckeditor5/src/core';
|
|
13
10
|
import { FileRepository } from 'ckeditor5/src/upload';
|
|
14
|
-
|
|
15
11
|
import { getCsrfToken } from './utils';
|
|
16
|
-
|
|
17
12
|
/**
|
|
18
13
|
* A plugin that enables file uploads in CKEditor 5 using the CKFinder server–side connector.
|
|
19
14
|
*
|
|
@@ -23,168 +18,121 @@ import { getCsrfToken } from './utils';
|
|
|
23
18
|
*
|
|
24
19
|
* Check out the {@glink features/images/image-upload/image-upload comprehensive "Image upload overview"} to learn about
|
|
25
20
|
* other ways to upload images into CKEditor 5.
|
|
26
|
-
*
|
|
27
|
-
* @extends module:core/plugin~Plugin
|
|
28
21
|
*/
|
|
29
22
|
export default class CKFinderUploadAdapter extends Plugin {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// Register CKFinderAdapter
|
|
55
|
-
this.editor.plugins.get( FileRepository ).createUploadAdapter = loader => new UploadAdapter( loader, url, this.editor.t );
|
|
56
|
-
}
|
|
23
|
+
/**
|
|
24
|
+
* @inheritDoc
|
|
25
|
+
*/
|
|
26
|
+
static get requires() {
|
|
27
|
+
return [FileRepository];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @inheritDoc
|
|
31
|
+
*/
|
|
32
|
+
static get pluginName() {
|
|
33
|
+
return 'CKFinderUploadAdapter';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @inheritDoc
|
|
37
|
+
*/
|
|
38
|
+
init() {
|
|
39
|
+
const url = this.editor.config.get('ckfinder.uploadUrl');
|
|
40
|
+
if (!url) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
// Register CKFinderAdapter
|
|
44
|
+
this.editor.plugins.get(FileRepository).createUploadAdapter = loader => new UploadAdapter(loader, url, this.editor.t);
|
|
45
|
+
}
|
|
57
46
|
}
|
|
58
|
-
|
|
59
47
|
/**
|
|
60
48
|
* Upload adapter for CKFinder.
|
|
61
|
-
*
|
|
62
|
-
* @private
|
|
63
|
-
* @implements module:upload/filerepository~UploadAdapter
|
|
64
49
|
*/
|
|
65
50
|
class UploadAdapter {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if ( !response || !response.uploaded ) {
|
|
155
|
-
return reject( response && response.error && response.error.message ? response.error.message : genericError );
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
resolve( {
|
|
159
|
-
default: response.url
|
|
160
|
-
} );
|
|
161
|
-
} );
|
|
162
|
-
|
|
163
|
-
// Upload progress when it's supported.
|
|
164
|
-
/* istanbul ignore else */
|
|
165
|
-
if ( xhr.upload ) {
|
|
166
|
-
xhr.upload.addEventListener( 'progress', evt => {
|
|
167
|
-
if ( evt.lengthComputable ) {
|
|
168
|
-
loader.uploadTotal = evt.total;
|
|
169
|
-
loader.uploaded = evt.loaded;
|
|
170
|
-
}
|
|
171
|
-
} );
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Prepares the data and sends the request.
|
|
177
|
-
*
|
|
178
|
-
* @private
|
|
179
|
-
* @param {File} file File instance to be uploaded.
|
|
180
|
-
*/
|
|
181
|
-
_sendRequest( file ) {
|
|
182
|
-
// Prepare form data.
|
|
183
|
-
const data = new FormData();
|
|
184
|
-
data.append( 'upload', file );
|
|
185
|
-
data.append( 'ckCsrfToken', getCsrfToken() );
|
|
186
|
-
|
|
187
|
-
// Send request.
|
|
188
|
-
this.xhr.send( data );
|
|
189
|
-
}
|
|
51
|
+
/**
|
|
52
|
+
* Creates a new adapter instance.
|
|
53
|
+
*/
|
|
54
|
+
constructor(loader, url, t) {
|
|
55
|
+
this.loader = loader;
|
|
56
|
+
this.url = url;
|
|
57
|
+
this.t = t;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Starts the upload process.
|
|
61
|
+
*
|
|
62
|
+
* @see module:upload/filerepository~UploadAdapter#upload
|
|
63
|
+
*/
|
|
64
|
+
upload() {
|
|
65
|
+
return this.loader.file.then(file => {
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
this._initRequest();
|
|
68
|
+
this._initListeners(resolve, reject, file);
|
|
69
|
+
this._sendRequest(file);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Aborts the upload process.
|
|
75
|
+
*
|
|
76
|
+
* @see module:upload/filerepository~UploadAdapter#abort
|
|
77
|
+
*/
|
|
78
|
+
abort() {
|
|
79
|
+
if (this.xhr) {
|
|
80
|
+
this.xhr.abort();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Initializes the XMLHttpRequest object.
|
|
85
|
+
*/
|
|
86
|
+
_initRequest() {
|
|
87
|
+
const xhr = this.xhr = new XMLHttpRequest();
|
|
88
|
+
xhr.open('POST', this.url, true);
|
|
89
|
+
xhr.responseType = 'json';
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Initializes XMLHttpRequest listeners.
|
|
93
|
+
*
|
|
94
|
+
* @param resolve Callback function to be called when the request is successful.
|
|
95
|
+
* @param reject Callback function to be called when the request cannot be completed.
|
|
96
|
+
* @param file File instance to be uploaded.
|
|
97
|
+
*/
|
|
98
|
+
_initListeners(resolve, reject, file) {
|
|
99
|
+
const xhr = this.xhr;
|
|
100
|
+
const loader = this.loader;
|
|
101
|
+
const t = this.t;
|
|
102
|
+
const genericError = t('Cannot upload file:') + ` ${file.name}.`;
|
|
103
|
+
xhr.addEventListener('error', () => reject(genericError));
|
|
104
|
+
xhr.addEventListener('abort', () => reject());
|
|
105
|
+
xhr.addEventListener('load', () => {
|
|
106
|
+
const response = xhr.response;
|
|
107
|
+
if (!response || !response.uploaded) {
|
|
108
|
+
return reject(response && response.error && response.error.message ? response.error.message : genericError);
|
|
109
|
+
}
|
|
110
|
+
resolve({
|
|
111
|
+
default: response.url
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
// Upload progress when it's supported.
|
|
115
|
+
/* istanbul ignore else */
|
|
116
|
+
if (xhr.upload) {
|
|
117
|
+
xhr.upload.addEventListener('progress', evt => {
|
|
118
|
+
if (evt.lengthComputable) {
|
|
119
|
+
loader.uploadTotal = evt.total;
|
|
120
|
+
loader.uploaded = evt.loaded;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Prepares the data and sends the request.
|
|
127
|
+
*
|
|
128
|
+
* @param file File instance to be uploaded.
|
|
129
|
+
*/
|
|
130
|
+
_sendRequest(file) {
|
|
131
|
+
// Prepare form data.
|
|
132
|
+
const data = new FormData();
|
|
133
|
+
data.append('upload', file);
|
|
134
|
+
data.append('ckCsrfToken', getCsrfToken());
|
|
135
|
+
// Send request.
|
|
136
|
+
this.xhr.send(data);
|
|
137
|
+
}
|
|
190
138
|
}
|
package/src/utils.js
CHANGED
|
@@ -1,83 +1,58 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
5
|
/* globals window, document */
|
|
7
|
-
|
|
8
6
|
/**
|
|
9
7
|
* @module adapter-ckfinder/utils
|
|
10
8
|
*/
|
|
11
|
-
|
|
12
9
|
const TOKEN_COOKIE_NAME = 'ckCsrfToken';
|
|
13
10
|
const TOKEN_LENGTH = 40;
|
|
14
11
|
const tokenCharset = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
15
|
-
|
|
16
12
|
/**
|
|
17
13
|
* Returns the CSRF token value. The value is a hash stored in `document.cookie`
|
|
18
14
|
* under the `ckCsrfToken` key. The CSRF token can be used to secure the communication
|
|
19
15
|
* between the web browser and the CKFinder server.
|
|
20
|
-
*
|
|
21
|
-
* @returns {String}
|
|
22
16
|
*/
|
|
23
17
|
export function getCsrfToken() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return token;
|
|
18
|
+
let token = getCookie(TOKEN_COOKIE_NAME);
|
|
19
|
+
if (!token || token.length != TOKEN_LENGTH) {
|
|
20
|
+
token = generateToken(TOKEN_LENGTH);
|
|
21
|
+
setCookie(TOKEN_COOKIE_NAME, token);
|
|
22
|
+
}
|
|
23
|
+
return token;
|
|
32
24
|
}
|
|
33
|
-
|
|
34
25
|
/**
|
|
35
26
|
* Returns the value of the cookie with a given name or `null` if the cookie is not found.
|
|
36
|
-
*
|
|
37
|
-
* @param {String} name
|
|
38
|
-
* @returns {String|null}
|
|
39
27
|
*/
|
|
40
|
-
export function getCookie(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return null;
|
|
28
|
+
export function getCookie(name) {
|
|
29
|
+
name = name.toLowerCase();
|
|
30
|
+
const parts = document.cookie.split(';');
|
|
31
|
+
for (const part of parts) {
|
|
32
|
+
const pair = part.split('=');
|
|
33
|
+
const key = decodeURIComponent(pair[0].trim().toLowerCase());
|
|
34
|
+
if (key === name) {
|
|
35
|
+
return decodeURIComponent(pair[1]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
54
39
|
}
|
|
55
|
-
|
|
56
40
|
/**
|
|
57
41
|
* Sets the value of the cookie with a given name.
|
|
58
|
-
*
|
|
59
|
-
* @param {String} name
|
|
60
|
-
* @param {String} value
|
|
61
42
|
*/
|
|
62
|
-
export function setCookie(
|
|
63
|
-
|
|
43
|
+
export function setCookie(name, value) {
|
|
44
|
+
document.cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value) + ';path=/';
|
|
64
45
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
for ( let j = 0; j < randValues.length; j++ ) {
|
|
78
|
-
const character = tokenCharset.charAt( randValues[ j ] % tokenCharset.length );
|
|
79
|
-
result += Math.random() > 0.5 ? character.toUpperCase() : character;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return result;
|
|
46
|
+
/**
|
|
47
|
+
* Generates the CSRF token with the given length.
|
|
48
|
+
*/
|
|
49
|
+
function generateToken(length) {
|
|
50
|
+
let result = '';
|
|
51
|
+
const randValues = new Uint8Array(length);
|
|
52
|
+
window.crypto.getRandomValues(randValues);
|
|
53
|
+
for (let j = 0; j < randValues.length; j++) {
|
|
54
|
+
const character = tokenCharset.charAt(randValues[j] % tokenCharset.length);
|
|
55
|
+
result += Math.random() > 0.5 ? character.toUpperCase() : character;
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
83
58
|
}
|