Skip to content

Commit 44ad373

Browse files
Merge pull request cylc#1778 from minrk/xsrf
Include XSRF token in userprofile request
2 parents fff887e + 26a117d commit 44ad373

File tree

6 files changed

+27
-20
lines changed

6 files changed

+27
-20
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ below.
6060
- Jamie Allen
6161
- Christopher Bennett
6262
- Mark Dawson
63+
- Min RK
6364
<!-- end-shortlog -->
6465

6566
(All contributors are identifiable with email addresses in the git version

changes.d/1778.fix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Compatibility with JupyterHub 4.1 XSRF changes

src/graphql/graphiql.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// Code related to GraphiQL
1919

2020
import { parse } from 'graphql'
21-
import { createGraphQLUrls, getCylcHeaders } from '@/graphql/index'
21+
import { createGraphQLUrls } from '@/graphql/index'
22+
import { getCylcHeaders } from '@/utils/urls'
2223

2324
// TODO: https://github.com/apollographql/GraphiQL-Subscriptions-Fetcher/issues/16
2425
// the functions hasSubscriptionOperation and graphQLFetcher are both from

src/graphql/index.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { WebSocketLink } from '@apollo/client/link/ws'
2727
import { setContext } from '@apollo/client/link/context'
2828
import { SubscriptionClient } from 'subscriptions-transport-ws'
2929
import { store } from '@/store/index'
30-
import { createUrl } from '@/utils/urls'
30+
import { createUrl, getCylcHeaders } from '@/utils/urls'
3131

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

@@ -46,21 +46,6 @@ export function createGraphQLUrls () {
4646
}
4747
}
4848

49-
/**
50-
* Get request headers for use with UI Server requests.
51-
*
52-
* - Adds X-XSRFToken header for hubless token based auth.
53-
*/
54-
export function getCylcHeaders () {
55-
const xsrfToken = document.cookie.match('\\b_xsrf=([^;]*)\\b')
56-
const cylcHeaders = {}
57-
if (Array.isArray(xsrfToken) && xsrfToken.length > 0) {
58-
// pick the last match
59-
cylcHeaders['X-XSRFToken'] = xsrfToken.splice(-1)
60-
}
61-
return cylcHeaders
62-
}
63-
6449
/**
6550
* Create a subscription client.
6651
*

src/services/user.service.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717

1818
import axios from 'axios'
1919
import User from '@/model/User.model'
20-
import { createUrl } from '@/utils/urls'
20+
import { createUrl, getCylcHeaders } from '@/utils/urls'
2121

2222
class UserService {
2323
/**
2424
* Gets the user profile from the backend server.
2525
* @returns {Promise<*>} - a promise that dispatches Vuex action
2626
*/
2727
getUserProfile () {
28-
return axios.get(createUrl('userprofile')).then(({ data }) => {
28+
return axios.get(
29+
createUrl('userprofile'),
30+
{ headers: getCylcHeaders() },
31+
).then(({ data }) => {
2932
return new User(
3033
data.name,
3134
data.groups,

src/utils/urls.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ function createUrl (path, websockets = false, baseOnly = false) {
7272
return normalize(url)
7373
}
7474

75+
/**
76+
* Get request headers for use with UI Server requests.
77+
*
78+
* - Adds X-XSRFToken header cookie-based auth.
79+
*/
80+
function getCylcHeaders () {
81+
const xsrfToken = document.cookie.match('\\b_xsrf=([^;]*)\\b')
82+
const cylcHeaders = {}
83+
if (Array.isArray(xsrfToken) && xsrfToken.length > 0) {
84+
// pick the last match
85+
cylcHeaders['X-XSRFToken'] = xsrfToken.splice(-1)
86+
}
87+
return cylcHeaders
88+
}
89+
7590
export {
76-
createUrl
91+
createUrl,
92+
getCylcHeaders,
7793
}

0 commit comments

Comments
 (0)