Dashboards & Visualizations

Django Web Framework: How to retrieve the current user session token with the Python SDK?

eirik_talberg
Explorer

Hi! I've been struggling with finding any proper documentation on this issue.

We are creating a Web Framework application to perform a certain kind of search and to display the results in a certain kind of way for the user. This requires us to do some fairly custom stuff that mixes Django Bindings/Web Framework and Splunk.

We are currently routing search parameters through a regular Django view, and the application itself is inside Splunk like any other app. To comply with legal requirements, our application has to implement extensive logging on who is using the application, and to do this I need to access the current user session information for the user that is logged in.

We use LDAP authentication and we have a series of groups in Splunk to determine access level and organization membership. Basically, what I need to do is retrieve the current session token (I'm using the Python SDK) and get the username and Splunk role from this so that it can be logged. I also need the session token so that I can use the current session to launch searches on behalf of that user.

Where is the current session token located? I've tried looking that the request.META information for the HTTP Request object, but I can't find a field value that seems appropriate.

1 Solution

eirik_talberg
Explorer

Yet again I solve my own problem through trial and error.

This whole issue is absurdly simple. My error was trying to establish a session or reestablish the current one manually by using the SDK. However, upon proper reading of this page: http://dev.splunk.com/view/SP-CAAAEMP, I learned that the service object I needed to use was already a part of the HTTP Request object, only it wasn't listed when I tried to print the whole thing.

Basically, to retrieve user info (roles given as an example) and access things like REST endpoints:

@login_required
def my_view(request):
    service = request.service

    # Get current user roles
    roles = service.roles

    # Print them
    for role in roles:
        print role.name

    #Do the stuff you want and return data to a template

You can find more info on how to use REST in the Python SDK documentation: http://dev.splunk.com/python

View solution in original post

0 Karma

eirik_talberg
Explorer

Yet again I solve my own problem through trial and error.

This whole issue is absurdly simple. My error was trying to establish a session or reestablish the current one manually by using the SDK. However, upon proper reading of this page: http://dev.splunk.com/view/SP-CAAAEMP, I learned that the service object I needed to use was already a part of the HTTP Request object, only it wasn't listed when I tried to print the whole thing.

Basically, to retrieve user info (roles given as an example) and access things like REST endpoints:

@login_required
def my_view(request):
    service = request.service

    # Get current user roles
    roles = service.roles

    # Print them
    for role in roles:
        print role.name

    #Do the stuff you want and return data to a template

You can find more info on how to use REST in the Python SDK documentation: http://dev.splunk.com/python

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...