diff --git a/public/assets/js/ajax.utils.js b/public/assets/js/ajax.utils.js index 70bc53f38..a745e2635 100644 --- a/public/assets/js/ajax.utils.js +++ b/public/assets/js/ajax.utils.js @@ -1,3 +1,16 @@ +// Send the CSRF token on every same-origin state-changing jQuery request. +// The token is never attached to cross-origin requests. +if (window.jQuery) { + jQuery.ajaxPrefilter(function (options, originalOptions, jqXHR) { + var method = (options.type || options.method || 'GET').toUpperCase(); + if (options.crossDomain || method === 'GET' || method === 'HEAD' || method === 'OPTIONS') return; + var meta = document.head.querySelector('meta[name="csrf-token"]'); + if (meta && meta.content) { + jqXHR.setRequestHeader('X-CSRF-TOKEN', meta.content); + } + }); +} + function ajaxError(jqXHR, textStatus, errorThrown){ var HTTP_status = jqXHR.status; if(HTTP_status != 200){ diff --git a/resources/js/admin/edit_user/actions.js b/resources/js/admin/edit_user/actions.js index ca278dcd5..f023fee9a 100644 --- a/resources/js/admin/edit_user/actions.js +++ b/resources/js/admin/edit_user/actions.js @@ -76,7 +76,7 @@ export const save = async (entity, pic, token) => { return putRawRequest(window.SAVE_PROFILE_ENDPOINT)(normalizeEntity(entity), {} ,{'X-CSRF-TOKEN': token}) .then((res) => { if (pic) { - return putFile(window.SAVE_PIC_ENDPOINT)(pic, 'pic', {'X-CSRF-TOKEN': token}); + return putFile(window.SAVE_PIC_ENDPOINT)(pic, 'pic', {}, {'X-CSRF-TOKEN': token}); } return Promise.resolve() } diff --git a/resources/js/admin/edit_user/edit_user.js b/resources/js/admin/edit_user/edit_user.js index ca6adf34a..42861aed6 100644 --- a/resources/js/admin/edit_user/edit_user.js +++ b/resources/js/admin/edit_user/edit_user.js @@ -99,7 +99,7 @@ const EditUserPage = ({ confirmButtonText: 'Yes, revoke it!' }).then((result) => { if (result.value) { - revokeToken(value, 'access-token').then(() => { + revokeToken(value, 'access-token', csrfToken).then(() => { Swal(`Access token revoked`, `The access token has been revoked successfully`, "success"); setAccessTokensListRefresh(!accessTokensListRefresh); }).catch((err) => { diff --git a/resources/js/oauth2/profile/clients/actions.js b/resources/js/oauth2/profile/clients/actions.js index 43e1714eb..ecbda49dc 100644 --- a/resources/js/oauth2/profile/clients/actions.js +++ b/resources/js/oauth2/profile/clients/actions.js @@ -55,7 +55,7 @@ export const getClients = async (page = 1, order = 'updated_at', orderDir = 'des } export const addClient = async (entity, token) => { - return postRawRequest(window.ADD_CLIENT_ENDPOINT)(normalizeEntity(entity), {}, {'X-CSRF-TOKEN': token}); + return postRawRequest(window.ADD_CLIENT_ENDPOINT)(normalizeEntity(entity), {'X-CSRF-TOKEN': token}); } export const deleteClient = async (id, token) => { diff --git a/resources/js/oauth2/profile/edit_client/actions.js b/resources/js/oauth2/profile/edit_client/actions.js index 94de1dbef..12150f4aa 100644 --- a/resources/js/oauth2/profile/edit_client/actions.js +++ b/resources/js/oauth2/profile/edit_client/actions.js @@ -11,7 +11,7 @@ export const ClientEntitySection = { export const regenerateClientSecret = async (clientId) => { return putRawRequest(window.REGENERATE_CLIENT_SECRET_ENDPOINT - .replace('@client_id', clientId))({'X-CSRF-TOKEN': window.CSFR_TOKEN}); + .replace('@client_id', clientId))(null, {}, {'X-CSRF-TOKEN': window.CSFR_TOKEN}); } export const getAccessTokens = async (clientId, page = 1, perPage = PAGE_SIZE, order = 'created_at', orderDir = 'desc', filters = {}) => { @@ -72,7 +72,7 @@ export const removeScope = async (clientId, scopeId) => { export const addPublicKey = async (clientId, entity) => { return postRawRequest(window.ADD_PUBLIC_KEY_ENDPOINT - .replace('@client_id', clientId))(normalizeEntity(entity, ClientEntitySection.PUBLIC_KEYS), {}, {'X-CSRF-TOKEN': window.CSFR_TOKEN}); + .replace('@client_id', clientId))(normalizeEntity(entity, ClientEntitySection.PUBLIC_KEYS), {'X-CSRF-TOKEN': window.CSFR_TOKEN}); } export const getPublicKeys = async (clientId, page = 1, perPage = PAGE_SIZE) => { diff --git a/resources/js/profile/actions.js b/resources/js/profile/actions.js index c1c6c2f08..34bcfeb13 100644 --- a/resources/js/profile/actions.js +++ b/resources/js/profile/actions.js @@ -68,23 +68,23 @@ export const getUserAccessTokens = async (page = 1, order = 'created_at', orderD export const save = async (entity, pic, token) => { - return putRawRequest(window.SAVE_PROFILE_ENDPOINT)(normalizeEntity(entity), {'X-CSRF-TOKEN': token}).then(() => { + return putRawRequest(window.SAVE_PROFILE_ENDPOINT)(normalizeEntity(entity), {}, {'X-CSRF-TOKEN': token}).then(() => { if (pic) { - return putFile(window.SAVE_PIC_ENDPOINT)(pic, 'pic', {'X-CSRF-TOKEN': token}); + return putFile(window.SAVE_PIC_ENDPOINT)(pic, 'pic', {}, {'X-CSRF-TOKEN': token}); } return Promise.resolve(); }); } -export const revokeToken = async (value, hint) => { +export const revokeToken = async (value, hint, token) => { return deleteRawRequest(window.REVOKE_ACCESS_TOKENS_ENDPOINT .replace('@value', value) .replace('%40hint', hint) - )({'X-CSRF-TOKEN': window.CSFR_TOKEN}); + )({'X-CSRF-TOKEN': token}); } -export const revokeAllTokens = async () => { - return deleteRawRequest(window.REVOKE_ALL_TOKENS_ENDPOINT)({'X-CSRF-TOKEN': window.CSFR_TOKEN}); +export const revokeAllTokens = async (token) => { + return deleteRawRequest(window.REVOKE_ALL_TOKENS_ENDPOINT)({'X-CSRF-TOKEN': token}); } const normalizeEntity = (entity) => { diff --git a/resources/js/profile/profile.js b/resources/js/profile/profile.js index a2d2be411..383796efc 100644 --- a/resources/js/profile/profile.js +++ b/resources/js/profile/profile.js @@ -97,7 +97,7 @@ const ProfilePage = ({ confirmButtonText: 'Yes, sign out all devices!' }).then((result) => { if (result.value) { - revokeAllTokens().then(() => { + revokeAllTokens(csrfToken).then(() => { Swal('Signed out', 'All other sessions and tokens have been revoked.', 'success'); setAccessTokensListRefresh(!accessTokensListRefresh); }).catch((err) => { @@ -117,7 +117,7 @@ const ProfilePage = ({ confirmButtonText: 'Yes, revoke it!' }).then((result) => { if (result.value) { - revokeToken(value, 'access-token').then(() => { + revokeToken(value, 'access-token', csrfToken).then(() => { Swal(`Access token revoked`, `The access token has been revoked successfully`, "success"); setAccessTokensListRefresh(!accessTokensListRefresh); }).catch((err) => { diff --git a/resources/views/admin/server-config.blade.php b/resources/views/admin/server-config.blade.php index 9aab097af..349ce00b8 100644 --- a/resources/views/admin/server-config.blade.php +++ b/resources/views/admin/server-config.blade.php @@ -9,6 +9,7 @@