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
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...