Skip to content

Commit

Permalink
Merge pull request #1778 from minrk/xsrf
Browse files Browse the repository at this point in the history
Include XSRF token in userprofile request
  • Loading branch information
oliver-sanders authored May 8, 2024
2 parents fff887e + 26a117d commit 44ad373
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ below.
- Jamie Allen
- Christopher Bennett
- Mark Dawson
- Min RK
<!-- end-shortlog -->

(All contributors are identifiable with email addresses in the git version
Expand Down
1 change: 1 addition & 0 deletions changes.d/1778.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Compatibility with JupyterHub 4.1 XSRF changes
3 changes: 2 additions & 1 deletion src/graphql/graphiql.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// Code related to GraphiQL

import { parse } from 'graphql'
import { createGraphQLUrls, getCylcHeaders } from '@/graphql/index'
import { createGraphQLUrls } from '@/graphql/index'
import { getCylcHeaders } from '@/utils/urls'

// TODO: https://github.com/apollographql/GraphiQL-Subscriptions-Fetcher/issues/16
// the functions hasSubscriptionOperation and graphQLFetcher are both from
Expand Down
17 changes: 1 addition & 16 deletions src/graphql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { WebSocketLink } from '@apollo/client/link/ws'
import { setContext } from '@apollo/client/link/context'
import { SubscriptionClient } from 'subscriptions-transport-ws'
import { store } from '@/store/index'
import { createUrl } from '@/utils/urls'
import { createUrl, getCylcHeaders } from '@/utils/urls'

/** @typedef {import('subscriptions-transport-ws').ClientOptions} ClientOptions */

Expand All @@ -46,21 +46,6 @@ export function createGraphQLUrls () {
}
}

/**
* Get request headers for use with UI Server requests.
*
* - Adds X-XSRFToken header for hubless token based auth.
*/
export function getCylcHeaders () {
const xsrfToken = document.cookie.match('\\b_xsrf=([^;]*)\\b')
const cylcHeaders = {}
if (Array.isArray(xsrfToken) && xsrfToken.length > 0) {
// pick the last match
cylcHeaders['X-XSRFToken'] = xsrfToken.splice(-1)
}
return cylcHeaders
}

/**
* Create a subscription client.
*
Expand Down
7 changes: 5 additions & 2 deletions src/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@

import axios from 'axios'
import User from '@/model/User.model'
import { createUrl } from '@/utils/urls'
import { createUrl, getCylcHeaders } from '@/utils/urls'

class UserService {
/**
* Gets the user profile from the backend server.
* @returns {Promise<*>} - a promise that dispatches Vuex action
*/
getUserProfile () {
return axios.get(createUrl('userprofile')).then(({ data }) => {
return axios.get(
createUrl('userprofile'),
{ headers: getCylcHeaders() },
).then(({ data }) => {
return new User(
data.name,
data.groups,
Expand Down
18 changes: 17 additions & 1 deletion src/utils/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ function createUrl (path, websockets = false, baseOnly = false) {
return normalize(url)
}

/**
* Get request headers for use with UI Server requests.
*
* - Adds X-XSRFToken header cookie-based auth.
*/
function getCylcHeaders () {
const xsrfToken = document.cookie.match('\\b_xsrf=([^;]*)\\b')
const cylcHeaders = {}
if (Array.isArray(xsrfToken) && xsrfToken.length > 0) {
// pick the last match
cylcHeaders['X-XSRFToken'] = xsrfToken.splice(-1)
}
return cylcHeaders
}

export {
createUrl
createUrl,
getCylcHeaders,
}

0 comments on commit 44ad373

Please sign in to comment.