Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include XSRF token in userprofile request #1778

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)

Check warning on line 85 in src/utils/urls.js

View check run for this annotation

Codecov / codecov/patch

src/utils/urls.js#L85

Added line #L85 was not covered by tests
}
return cylcHeaders
}

export {
createUrl
createUrl,
getCylcHeaders,
}