Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
course-discovery
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
course-discovery
Commits
97e4f73d
Commit
97e4f73d
authored
Jun 27, 2017
by
Simon Chen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add cache bust Querystring to get the CSRF token
parent
adc85c50
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
6 deletions
+21
-6
course_discovery/apps/course_metadata/utils.py
+21
-6
No files found.
course_discovery/apps/course_metadata/utils.py
View file @
97e4f73d
import
random
import
string
import
requests
import
requests
from
django.utils.functional
import
cached_property
from
django.utils.functional
import
cached_property
from
stdimage.models
import
StdImageFieldFile
from
stdimage.models
import
StdImageFieldFile
...
@@ -100,20 +103,32 @@ class MarketingSiteAPIClient(object):
...
@@ -100,20 +103,32 @@ class MarketingSiteAPIClient(object):
# This is not a RESTful API so checking the status code is not enough
# This is not a RESTful API so checking the status code is not enough
# We also check that we were redirected to the admin page
# We also check that we were redirected to the admin page
if
not
(
response
.
status_code
==
200
and
response
.
url
==
admin_url
):
if
not
(
response
.
status_code
==
200
and
response
.
url
==
admin_url
):
raise
MarketingSiteAPIClientException
(
'Marketing Site Login failed!'
)
raise
MarketingSiteAPIClientException
(
{
'message'
:
'Marketing Site Login failed!'
,
'status'
:
response
.
status_code
,
'url'
:
response
.
url
}
)
return
session
return
session
@
cached_
property
@property
def
api_session
(
self
):
def
api_session
(
self
):
self
.
init_session
.
headers
.
update
(
self
.
headers
)
self
.
init_session
.
headers
.
update
(
self
.
headers
)
return
self
.
init_session
return
self
.
init_session
@
cached_
property
@property
def
csrf_token
(
self
):
def
csrf_token
(
self
):
token_url
=
'{root}/restws/session/token'
.
format
(
root
=
self
.
api_url
)
# We need to make sure we can bypass the Varnish cache.
# So adding a random salt into the query string to cache bust
random_qs
=
''
.
join
(
random
.
choice
(
string
.
ascii_uppercase
+
string
.
digits
)
for
_
in
range
(
10
))
token_url
=
'{root}/restws/session/token?cachebust={qs}'
.
format
(
root
=
self
.
api_url
,
qs
=
random_qs
)
response
=
self
.
init_session
.
get
(
token_url
)
response
=
self
.
init_session
.
get
(
token_url
)
if
not
response
.
status_code
==
200
:
if
not
response
.
status_code
==
200
:
raise
MarketingSiteAPIClientException
(
'Failed to retrieve Marketing Site CSRF token!'
)
raise
MarketingSiteAPIClientException
({
'message'
:
'Failed to retrieve Marketing Site CSRF token!'
,
'status'
:
response
.
status_code
,
})
token
=
response
.
content
.
decode
(
'utf8'
)
token
=
response
.
content
.
decode
(
'utf8'
)
return
token
return
token
...
@@ -127,7 +142,7 @@ class MarketingSiteAPIClient(object):
...
@@ -127,7 +142,7 @@ class MarketingSiteAPIClient(object):
user_id
=
response
.
json
()[
'list'
][
0
][
'uid'
]
user_id
=
response
.
json
()[
'list'
][
0
][
'uid'
]
return
user_id
return
user_id
@
cached_
property
@property
def
headers
(
self
):
def
headers
(
self
):
return
{
return
{
'Content-Type'
:
'application/json'
,
'Content-Type'
:
'application/json'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment