<s> from django . contrib . auth . models import AbstractUser <EOL> from splunklib . client import connect , Service <EOL> import requests <EOL> import os , time <EOL> from django . conf import settings <EOL> from encryption import SimplerAES <EOL> import logging <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> aes = SimplerAES ( settings . SECRET_KEY ) <EOL> def get_splunkweb_url ( path ) : <EOL> splunkweb_mount = "<STR_LIT>" <EOL> if settings . SPLUNK_WEB_MOUNT : <EOL> splunkweb_mount = "<STR_LIT>" % settings . SPLUNK_WEB_MOUNT <EOL> return "<STR_LIT>" % ( settings . SPLUNK_WEB_SCHEME , settings . SPLUNK_WEB_HOST , settings . SPLUNK_WEB_PORT , splunkweb_mount , path ) <EOL> def logout_user ( cookies , username ) : <EOL> try : <EOL> logger . info ( "<STR_LIT>" % username ) <EOL> r1 = requests . get ( <EOL> get_splunkweb_url ( "<STR_LIT>" ) , <EOL> allow_redirects = True , <EOL> cookies = cookies ) <EOL> except Exception , e : <EOL> logger . exception ( e ) <EOL> pass <EOL> class SplunkUser ( AbstractUser ) : <EOL> def __init__ ( self , id = None , splunkweb = None , service = None , tz = None , realname = "<STR_LIT>" , is_free = False , * args , ** kwargs ) : <EOL> super ( SplunkUser , self ) . __init__ ( * args , ** kwargs ) <EOL> self . id = id <EOL> self . splunkweb = splunkweb <EOL> self . realname = realname <EOL> self . service = service <EOL> self . tz = tz <EOL> self . is_free = is_free <EOL> def save ( self , * args , ** kwargs ) : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> pass <EOL> class SplunkWebInfo ( object ) : <EOL> def __init__ ( self , cookies ) : <EOL> self . session_id = cookies [ '<STR_LIT>' % settings . SPLUNK_WEB_PORT ] <EOL> self . cval = cookies [ '<STR_LIT>' ] <EOL> self . uid = cookies [ '<STR_LIT>' ] <EOL> def get_user ( username , token ) : <EOL> if not username or not token : <EOL> return None <EOL> user = None <EOL> properties = { } <EOL> is_free = False <EOL> try : <EOL> service = Service ( <EOL> username = username , <EOL> token = token , <EOL> scheme = settings . SPLUNKD_SCHEME , <EOL> host = settings . SPLUNKD_HOST , <EOL> port = settings . SPLUNKD_PORT <EOL> ) <EOL> server_info = service . info <EOL> if ( server_info . get ( '<STR_LIT>' , '<STR_LIT:0>' ) == '<STR_LIT:0>' ) : <EOL> user = service . users [ username ] <EOL> properties = user . content ( ) <EOL> else : <EOL> is_free = True <EOL> properties = { <EOL> "<STR_LIT:email>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : None <EOL> } <EOL> except Exception , e : <EOL> if hasattr ( e , '<STR_LIT:status>' ) and e . status == <NUM_LIT> : <EOL> logger . error ( "<STR_LIT>" % username ) <EOL> return None <EOL> elif settings . DEBUG : <EOL> logger . exception ( e ) <EOL> raise <EOL> else : <EOL> logger . exception ( e ) <EOL> return None <EOL> user_id = "<STR_LIT>" % ( username , token ) <EOL> user_id = aes . encrypt ( str ( user_id ) ) <EOL> return SplunkUser ( <EOL> id = user_id , <EOL> service = service , <EOL> username = username , <EOL> email = properties [ "<STR_LIT:email>" ] , <EOL> is_superuser = "<STR_LIT>" in properties [ "<STR_LIT>" ] , <EOL> is_staff = "<STR_LIT>" in properties [ "<STR_LIT>" ] , <EOL> is_active = True , <EOL> is_free = is_free , <EOL> realname = properties [ "<STR_LIT>" ] , <EOL> tz = properties [ '<STR_LIT>' ] <EOL> ) <EOL> class SplunkAuthenticationBackend ( object ) : <EOL> supports_inactive_user = False <EOL> def authenticate ( self , username = None , password = None , * args , ** kwargs ) : <EOL> try : <EOL> logger . info ( "<STR_LIT>" % <EOL> ( username , settings . SPLUNKD_HOST , settings . SPLUNKD_PORT ) ) <EOL> service = connect ( <EOL> username = username , <EOL> password = password , <EOL> scheme = settings . SPLUNKD_SCHEME , <EOL> host = settings . SPLUNKD_HOST , <EOL> port = settings . SPLUNKD_PORT <EOL> ) <EOL> except Exception , e : <EOL> if hasattr ( e , '<STR_LIT:status>' ) and e . status == <NUM_LIT> : <EOL> logger . error ( "<STR_LIT>" % username ) <EOL> return None <EOL> else : <EOL> logger . exception ( e ) <EOL> raise <EOL> user = service . users [ username ] <EOL> properties = user . content ( ) <EOL> user_id = "<STR_LIT>" % ( username , service . token ) <EOL> user_id = aes . encrypt ( str ( user_id ) ) <EOL> splunkweb_info = None <EOL> try : <EOL> r1 = requests . get ( <EOL> get_splunkweb_url ( "<STR_LIT>" ) , <EOL> allow_redirects = True ) <EOL> cval = r1 . cookies [ '<STR_LIT>' ] <EOL> r = requests . post ( <EOL> r1 . url , <EOL> cookies = r1 . cookies , <EOL> data = { "<STR_LIT:username>" : username , "<STR_LIT:password>" : password , "<STR_LIT>" : cval } ) <EOL> splunkweb_info = SplunkWebInfo ( r . cookies ) <EOL> except Exception , e : <EOL> logger . exception ( e ) <EOL> pass <EOL> return SplunkUser ( <EOL> id = user_id , <EOL> splunkweb = splunkweb_info , <EOL> service = service , <EOL> username = username , <EOL> password = password , <EOL> email = properties [ "<STR_LIT:email>" ] , <EOL> is_superuser = "<STR_LIT>" in properties [ "<STR_LIT>" ] , <EOL> is_staff = "<STR_LIT>" in properties [ "<STR_LIT>" ] , <EOL> is_active = True , <EOL> realname = properties [ "<STR_LIT>" ] , <EOL> tz = properties [ '<STR_LIT>' ] <EOL> ) <EOL> def get_user ( self , user_id , * args , ** kwargs ) : <EOL> username = None <EOL> token = None <EOL> try : <EOL> user_id = aes . decrypt ( user_id ) <EOL> parts = user_id . split ( "<STR_LIT::>" ) <EOL> username = parts [ <NUM_LIT:0> ] <EOL> token = parts [ <NUM_LIT:1> ] <EOL> except Exception , e : <EOL> logger . exception ( e ) <EOL> return None <EOL> return get_user ( username , token ) </s>
