|
6 | 6 | :plugincode: exportword |
7 | 7 |
|
8 | 8 |
|
9 | | -== Introduction |
| 9 | +include::partial$auth/document-converters/intro-and-prerequisites.adoc[] |
10 | 10 |
|
11 | | -{pluginname} requires setting up JSON Web Token (JWT) authentication to maintain control over file security. A JWT endpoint generates and provides authorization tokens that verify submitted content is sent by authorized users, preventing unauthorized access. As a standard web services authorization solution, JWT is documented extensively at link:https://jwt.io/[https://jwt.io/]. |
| 11 | +include::partial$auth/document-converters/initial-project-setup.adoc[] |
12 | 12 |
|
13 | | -This guide provides a comprehensive walkthrough for integrating {pluginname} with {productname}, including {pluginname} functionality, by using a Node.js server for JWT token generation. It covers project setup, server configuration, and {productname} customization. |
| 13 | +== Setup |
14 | 14 |
|
15 | | -== What You'll Build |
16 | | - |
17 | | -Before diving into the technical details, here's what you'll achieve with this guide: |
18 | | - |
19 | | -* A working {pluginname} system for your {productname} editor |
20 | | -* A secure authentication system using JWT tokens |
21 | | -* A simple Node.js server to handle the authentication |
22 | | - |
23 | | -[TIP] |
24 | | -==== |
25 | | -This guide is designed for developers new to JWT authentication and {productname} integration. |
26 | | -==== |
27 | | - |
28 | | -== Prerequisites |
29 | | - |
30 | | -Before starting, ensure you have: |
31 | | - |
32 | | -* Node.js installed on your computer |
33 | | -* A {productname} API key (get one from link:https://www.tiny.cloud/signup[TinyMCE's website]) |
34 | | -* Basic familiarity with the command line |
35 | | - |
36 | | -[IMPORTANT] |
37 | | -==== |
38 | | -Make sure you have your API key ready before starting. You'll need it for both the server and client configuration. |
39 | | -==== |
40 | | - |
41 | | -== Quick Start Guide |
42 | | - |
43 | | -=== Project Setup (5 minutes) |
44 | | - |
45 | | -[source,bash] |
46 | | ----- |
47 | | -# Create and enter project directory |
48 | | -mkdir tinymce-export-to-word |
49 | | -cd tinymce-export-to-word |
50 | | -
|
51 | | -# Initialize project |
52 | | -npm init -y |
53 | | -
|
54 | | -# Install required packages |
55 | | -npm install express cors jsonwebtoken |
56 | | ----- |
57 | | - |
58 | | -Verify that the `package.json` file includes the below required dependencies: |
59 | | - |
60 | | -[source,json] |
61 | | ----- |
62 | | -{ |
63 | | - "dependencies": { |
64 | | - "cors": "^2.8.5", |
65 | | - "express": "^4.21.0", |
66 | | - "jsonwebtoken": "^9.0.2" |
67 | | - } |
68 | | -} |
69 | | ----- |
70 | | - |
71 | | -=== Create Project Structure |
72 | | - |
73 | | -[source,bash] |
74 | | ----- |
75 | | -# Create the public folder for your web files |
76 | | -mkdir public |
77 | | -touch public/index.html |
78 | | -touch jwt.js |
79 | | ----- |
80 | | - |
81 | | -Your project should look like this: |
82 | | - |
83 | | -[source] |
84 | | ----- |
85 | | -/tinymce-export-to-word |
86 | | - /public |
87 | | - index.html (TinyMCE webpage) |
88 | | - jwt.js (Server code) |
89 | | - package.json (Project configuration) |
90 | | ----- |
91 | | - |
92 | | -. Inside the `public` folder where you created the `index.html` file add the HTML setup code (refer to the *Setting up {productname} HTML* section). |
93 | | - |
94 | | -==== Web Page Setup (public/index.html) |
| 15 | +=== Web Page (public/index.html) |
95 | 16 |
|
96 | 17 | [source,html] |
97 | 18 | ---- |
@@ -136,74 +57,6 @@ Your project should look like this: |
136 | 57 |
|
137 | 58 | . In the root directory, copy and paste the server setup code into the `jwt.js` file (refer to the *Configuring the Node.js Server for JWT Token Generation* section). |
138 | 59 |
|
139 | | -==== Server Setup (jwt.js) |
140 | | - |
141 | | -[source,javascript] |
142 | | ----- |
143 | | -const express = require('express'); // Sets up the web server. |
144 | | -const jwt = require('jsonwebtoken'); // Generates and signs JWTs. |
145 | | -const cors = require('cors'); // Allows cross-origin requests. |
146 | | -const path = require('path'); // Handles file paths. |
147 | | -
|
148 | | -const app = express(); |
149 | | -app.use(cors()); |
150 | | -
|
151 | | -// Your private key (Replace this with your actual key) |
152 | | -const privateKey = ` |
153 | | ------BEGIN PRIVATE KEY----- |
154 | | -{Your private PKCS8 key goes here} |
155 | | ------END PRIVATE KEY----- |
156 | | -`; |
157 | | -
|
158 | | -app.use(express.static(path.join(__dirname, 'public'))); |
159 | | -
|
160 | | -// JWT token generation endpoint |
161 | | -app.post('/jwt', (req, res) => { |
162 | | - const payload = { |
163 | | - aud: 'YOUR-API-KEY-HERE', // Replace with your actual API key |
164 | | - iat: Math.floor(Date.now() / 1000), // Issue timestamp |
165 | | - exp: Math.floor(Date.now() / 1000) + (60 * 10) // Expiration time (10 minutes) |
166 | | - }; |
167 | | -
|
168 | | - try { |
169 | | - // Tokens are signed with the RS256 algorithm using your private key |
170 | | - const token = jwt.sign(payload, privateKey, { algorithm: 'RS256' }); |
171 | | - res.json({ token }); |
172 | | - } catch (error) { |
173 | | - res.status(500).send('Failed to generate JWT token.'); |
174 | | - console.error(error.message); |
175 | | - } |
176 | | -}); |
177 | | -
|
178 | | -const PORT = 3000; |
179 | | -app.listen(PORT, () => { |
180 | | - console.log(`Server running at http://localhost:${PORT}`); |
181 | | -}); |
182 | | ----- |
183 | | - |
184 | | -=== Configuration Steps |
185 | | - |
186 | | -==== 1. Add Your API Key |
187 | | - |
188 | | -* Replace `YOUR-API-KEY` in both files with your actual {productname} API key |
189 | | -* The API key should be the same in both the HTML script source and the JWT payload |
190 | | - |
191 | | -==== 2. Add Your Private Key |
192 | | - |
193 | | -* Replace the private key placeholder in `jwt.js` with your actual private key |
194 | | -* Make sure it's in `PKCS8` format |
195 | | -* Keep this key secure and never share it publicly |
196 | | - |
197 | | -=== Running Your Project |
198 | | - |
199 | | -. Start the server: |
200 | | -+ |
201 | | -[source,bash] |
202 | | ----- |
203 | | -node jwt.js |
204 | | ----- |
| 60 | +include::partial$auth/document-converters/server-setup-jwt.adoc[] |
205 | 61 |
|
206 | | -. Open your browser to: `http://localhost:3000` |
207 | | -. You should see: |
208 | | -* The {productname} editor |
209 | | -* An "Export to Word" button in the toolbar |
| 62 | +include::partial$auth/document-converters/configuration-steps.adoc[] |
0 commit comments