Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
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
edx-platform
Commits
919b4338
Commit
919b4338
authored
May 30, 2014
by
Brian Wilson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Encrypt the session id before writing to tracking logs.
parent
721732bf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
12 deletions
+23
-12
common/djangoapps/track/middleware.py
+18
-2
common/djangoapps/track/tests/test_middleware.py
+2
-2
common/djangoapps/track/views.py
+3
-8
No files found.
common/djangoapps/track/middleware.py
View file @
919b4338
import
hmac
import
hashlib
import
json
import
re
import
logging
...
...
@@ -111,12 +113,26 @@ class TrackMiddleware(object):
)
def
get_session_key
(
self
,
request
):
"""
Gets the Django session key from the request or an empty string if it isn't found
"""
"""
Gets and encrypts the Django session key from the request or an empty string if it isn't found.
"""
try
:
return
request
.
session
.
session_key
return
self
.
encrypt_session_key
(
request
.
session
.
session_key
)
except
AttributeError
:
return
''
def
encrypt_session_key
(
self
,
session_key
):
"""Encrypts a Django session key to another 32-character hex value."""
if
not
session_key
:
return
''
# Follow the model of django.utils.crypto.salted_hmac() and
# django.contrib.sessions.backends.base._hash(), but use MD5
# so that the result has the same length (32) as the original
# session_key.
key_salt
=
"common.djangoapps.track"
+
self
.
__class__
.
__name__
key
=
hashlib
.
sha1
(
key_salt
+
settings
.
SECRET_KEY
)
.
digest
()
encrypted_session_key
=
hmac
.
new
(
key
,
msg
=
session_key
)
.
hexdigest
()
return
encrypted_session_key
def
get_user_primary_key
(
self
,
request
):
"""Gets the primary key of the logged in Django user"""
try
:
...
...
common/djangoapps/track/tests/test_middleware.py
View file @
919b4338
...
...
@@ -117,10 +117,10 @@ class TrackMiddlewareTestCase(TestCase):
SessionMiddleware
()
.
process_request
(
request
)
request
.
session
.
save
()
session_key
=
request
.
session
.
session_key
expected_session_key
=
self
.
track_middleware
.
encrypt_session_key
(
session_key
)
context
=
self
.
get_context_for_request
(
request
)
self
.
assert_dict_subset
(
context
,
{
'session'
:
session_key
,
'session'
:
expected_
session_key
,
})
def
test_request_headers
(
self
):
...
...
common/djangoapps/track/views.py
View file @
919b4338
...
...
@@ -41,18 +41,13 @@ def user_track(request):
except
:
username
=
"anonymous"
try
:
scookie
=
request
.
META
[
'HTTP_COOKIE'
]
# Get cookies
scookie
=
";"
.
join
([
c
.
split
(
'='
)[
1
]
for
c
in
scookie
.
split
(
";"
)
if
"sessionid"
in
c
])
.
strip
()
# Extract session ID
except
:
scookie
=
""
page
=
request
.
REQUEST
[
'page'
]
with
eventtracker
.
get_tracker
()
.
context
(
'edx.course.browser'
,
contexts
.
course_context_from_url
(
page
)):
context
=
eventtracker
.
get_tracker
()
.
resolve_context
()
event
=
{
"username"
:
username
,
"session"
:
scookie
,
"session"
:
context
.
get
(
'session'
,
''
)
,
"ip"
:
_get_request_header
(
request
,
'REMOTE_ADDR'
),
"event_source"
:
"browser"
,
"event_type"
:
request
.
REQUEST
[
'event_type'
],
...
...
@@ -61,7 +56,7 @@ def user_track(request):
"page"
:
page
,
"time"
:
datetime
.
datetime
.
utcnow
(),
"host"
:
_get_request_header
(
request
,
'SERVER_NAME'
),
"context"
:
eventtracker
.
get_tracker
()
.
resolve_context
()
,
"context"
:
context
,
}
log_event
(
event
)
...
...
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