Browse Source

Emit an event when the CSRF token changes

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Christoph Wurst 5 years ago
parent
commit
8c023eeabd
4 changed files with 31 additions and 11 deletions
  1. 9 3
      core/src/OC/index.js
  2. 6 8
      core/src/OC/requesttoken.js
  3. 15 0
      package-lock.json
  4. 1 0
      package.json

+ 9 - 3
core/src/OC/index.js

@@ -19,6 +19,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+import { subscribe } from '@nextcloud/event-bus'
+
 import { addScript, addStyle } from './legacy-loader'
 import {
 	ajaxConnectionLostHandler,
@@ -67,8 +69,7 @@ import {
 	getProtocol
 } from './host'
 import {
-	getToken as getRequestToken,
-	subscribe as subscribeToRequestTokenChange
+	getToken as getRequestToken
 } from './requesttoken'
 import {
 	hideMenus,
@@ -258,4 +259,9 @@ export default {
 }
 
 // Keep the request token prop in sync
-subscribeToRequestTokenChange(token => { OC.requestToken = token })
+subscribe('csrf-token-update', e => {
+	OC.requestToken = e.token
+
+	// Logging might help debug (Sentry) issues
+	console.info('OC.requestToken changed', e.token)
+})

+ 6 - 8
core/src/OC/requesttoken.js

@@ -19,24 +19,22 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+import { emit } from '@nextcloud/event-bus'
+
 let token = document.getElementsByTagName('head')[0].getAttribute('data-requesttoken')
-const observers = []
 
 /**
  * @returns {string}
  */
 export const getToken = () => token
 
-/**
- * @param {Function} observer observer
- * @returns {number}
- */
-export const subscribe = observer => observers.push(observer)
-
 /**
  * @param {String} newToken new token
  */
 export const setToken = newToken => {
 	token = newToken
-	observers.forEach(o => o(token))
+
+	emit('csrf-token-update', {
+		token
+	})
 }

+ 15 - 0
package-lock.json

@@ -851,6 +851,21 @@
         "qrcode": "^1.3.3"
       }
     },
+    "@nextcloud/event-bus": {
+      "version": "0.2.0",
+      "resolved": "https://registry.npmjs.org/@nextcloud/event-bus/-/event-bus-0.2.0.tgz",
+      "integrity": "sha512-G+YTHhkJFQgJ2126UIrm3ezv+iESDRR/blbaBAz1WoCRPUTHfQZfdoarv/hNbXJaJ7aFz6mLsNe1+oGHwCpstg==",
+      "requires": {
+        "core-js": "^3.1.4"
+      },
+      "dependencies": {
+        "core-js": {
+          "version": "3.2.1",
+          "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.2.1.tgz",
+          "integrity": "sha512-Qa5XSVefSVPRxy2XfUC13WbvqkxhkwB3ve+pgCQveNgYzbM/UxZeu1dcOX/xr4UmfUd+muuvsaxilQzCyUurMw=="
+        }
+      }
+    },
     "@nextcloud/router": {
       "version": "0.1.0",
       "resolved": "https://registry.npmjs.org/@nextcloud/router/-/router-0.1.0.tgz",

+ 1 - 0
package.json

@@ -25,6 +25,7 @@
   "dependencies": {
     "@babel/polyfill": "^7.6.0",
     "@chenfengyuan/vue-qrcode": "^1.0.1",
+    "@nextcloud/event-bus": "^0.2.0",
     "@nextcloud/router": "^0.1.0",
     "autosize": "^4.0.2",
     "backbone": "^1.4.0",