<s> import logging <EOL> from django . conf import settings <EOL> from django . contrib import auth <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth import views as django_auth_views <EOL> from django . contrib import messages <EOL> from django import http as django_http <EOL> from django import shortcuts <EOL> from django . utils import functional <EOL> from django . utils import http <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . decorators . cache import never_cache <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from django . views . decorators . debug import sensitive_post_parameters <EOL> from keystoneauth1 import exceptions as keystone_exceptions <EOL> from keystoneauth1 import token_endpoint <EOL> import six <EOL> from openstack_auth import exceptions <EOL> from openstack_auth import forms <EOL> from openstack_auth . forms import Login <EOL> from openstack_auth import user as auth_user <EOL> from openstack_auth import utils <EOL> try : <EOL> is_safe_url = http . is_safe_url <EOL> except AttributeError : <EOL> is_safe_url = utils . is_safe_url <EOL> LOG = logging . getLogger ( __name__ ) <EOL> @ sensitive_post_parameters ( ) <EOL> @ csrf_protect <EOL> @ never_cache <EOL> def login ( request , template_name = None , extra_context = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:POST>' : <EOL> auth_type = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if utils . is_websso_enabled ( ) and auth_type != '<STR_LIT>' : <EOL> auth_url = request . POST . get ( '<STR_LIT>' ) <EOL> url = utils . get_websso_url ( request , auth_url , auth_type ) <EOL> return shortcuts . redirect ( url ) <EOL> if not request . is_ajax ( ) : <EOL> if ( request . user . is_authenticated ( ) and <EOL> auth . REDIRECT_FIELD_NAME not in request . GET and <EOL> auth . REDIRECT_FIELD_NAME not in request . POST ) : <EOL> return shortcuts . redirect ( settings . LOGIN_REDIRECT_URL ) <EOL> initial = { } <EOL> current_region = request . session . get ( '<STR_LIT>' , None ) <EOL> requested_region = request . GET . get ( '<STR_LIT>' , None ) <EOL> regions = dict ( getattr ( settings , "<STR_LIT>" , [ ] ) ) <EOL> if requested_region in regions and requested_region != current_region : <EOL> initial . update ( { '<STR_LIT>' : requested_region } ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = functional . curry ( forms . Login ) <EOL> else : <EOL> form = functional . curry ( forms . Login , initial = initial ) <EOL> if extra_context is None : <EOL> extra_context = { '<STR_LIT>' : auth . REDIRECT_FIELD_NAME } <EOL> if not template_name : <EOL> if request . is_ajax ( ) : <EOL> template_name = '<STR_LIT>' <EOL> extra_context [ '<STR_LIT>' ] = True <EOL> else : <EOL> template_name = '<STR_LIT>' <EOL> res = django_auth_views . login ( request , <EOL> template_name = template_name , <EOL> authentication_form = form , <EOL> extra_context = extra_context , <EOL> ** kwargs ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> utils . set_response_cookie ( res , '<STR_LIT>' , <EOL> request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> utils . set_response_cookie ( res , '<STR_LIT>' , <EOL> request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if request . user . is_authenticated ( ) : <EOL> auth_user . set_session_from_user ( request , request . user ) <EOL> regions = dict ( forms . Login . get_region_choices ( ) ) <EOL> region = request . user . endpoint <EOL> region_name = regions . get ( region ) <EOL> request . session [ '<STR_LIT>' ] = region <EOL> request . session [ '<STR_LIT>' ] = region_name <EOL> return res <EOL> @ sensitive_post_parameters ( ) <EOL> @ csrf_exempt <EOL> @ never_cache <EOL> def websso ( request ) : <EOL> """<STR_LIT>""" <EOL> referer = request . META . get ( '<STR_LIT>' , settings . OPENSTACK_KEYSTONE_URL ) <EOL> auth_url = utils . clean_up_auth_url ( referer ) <EOL> token = request . POST . get ( '<STR_LIT>' ) <EOL> try : <EOL> request . user = auth . authenticate ( request = request , auth_url = auth_url , <EOL> token = token ) <EOL> except exceptions . KeystoneAuthException as exc : <EOL> msg = '<STR_LIT>' % six . text_type ( exc ) <EOL> res = django_http . HttpResponseRedirect ( settings . LOGIN_URL ) <EOL> res . set_cookie ( '<STR_LIT>' , msg , max_age = <NUM_LIT:10> ) <EOL> return res <EOL> auth_user . set_session_from_user ( request , request . user ) <EOL> auth . login ( request , request . user ) <EOL> if request . session . test_cookie_worked ( ) : <EOL> request . session . delete_test_cookie ( ) <EOL> return django_http . HttpResponseRedirect ( settings . LOGIN_REDIRECT_URL ) <EOL> def logout ( request , login_url = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' % { '<STR_LIT:username>' : request . user . username } <EOL> LOG . info ( msg ) <EOL> endpoint = request . session . get ( '<STR_LIT>' ) <EOL> token = request . session . get ( '<STR_LIT>' ) <EOL> if token and endpoint : <EOL> delete_token ( endpoint = endpoint , token_id = token . id ) <EOL> domain_token = request . session . get ( '<STR_LIT>' ) <EOL> if domain_token and endpoint : <EOL> delete_token ( endpoint = endpoint , token_id = domain_token . auth_token ) <EOL> """<STR_LIT>""" <EOL> return django_auth_views . logout_then_login ( request , login_url = login_url , <EOL> ** kwargs ) <EOL> def delete_token ( endpoint , token_id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> endpoint = utils . fix_auth_url_version ( endpoint ) <EOL> session = utils . get_session ( ) <EOL> auth_plugin = token_endpoint . Token ( endpoint = endpoint , <EOL> token = token_id ) <EOL> client = utils . get_keystone_client ( ) . Client ( session = session , <EOL> auth = auth_plugin ) <EOL> if utils . get_keystone_version ( ) >= <NUM_LIT:3> : <EOL> client . tokens . revoke_token ( token = token_id ) <EOL> else : <EOL> client . tokens . delete ( token = token_id ) <EOL> LOG . info ( '<STR_LIT>' % token_id ) <EOL> except keystone_exceptions . ClientException : <EOL> LOG . info ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def switch ( request , tenant_id , redirect_field_name = auth . REDIRECT_FIELD_NAME ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' <EOL> % ( tenant_id , request . user . username ) ) <EOL> endpoint = utils . fix_auth_url_version ( request . user . endpoint ) <EOL> session = utils . get_session ( ) <EOL> unscoped_token = request . user . unscoped_token <EOL> auth = utils . get_token_auth_plugin ( auth_url = endpoint , <EOL> token = unscoped_token , <EOL> project_id = tenant_id ) <EOL> try : <EOL> auth_ref = auth . get_access ( session ) <EOL> msg = '<STR_LIT>' % { '<STR_LIT:username>' : request . user . username } <EOL> LOG . info ( msg ) <EOL> except keystone_exceptions . ClientException : <EOL> msg = ( <EOL> _ ( '<STR_LIT>' ) % <EOL> { '<STR_LIT:username>' : request . user . username } ) <EOL> messages . error ( request , msg ) <EOL> auth_ref = None <EOL> LOG . exception ( '<STR_LIT>' ) <EOL> redirect_to = request . GET . get ( redirect_field_name , '<STR_LIT>' ) <EOL> if not is_safe_url ( url = redirect_to , host = request . get_host ( ) ) : <EOL> redirect_to = settings . LOGIN_REDIRECT_URL <EOL> if auth_ref : <EOL> old_endpoint = request . session . get ( '<STR_LIT>' ) <EOL> old_token = request . session . get ( '<STR_LIT>' ) <EOL> if old_token and old_endpoint and old_token . id != auth_ref . auth_token : <EOL> delete_token ( endpoint = old_endpoint , token_id = old_token . id ) <EOL> user = auth_user . create_user_from_token ( <EOL> request , <EOL> auth_user . Token ( auth_ref , unscoped_token = unscoped_token ) , <EOL> endpoint ) <EOL> auth_user . set_session_from_user ( request , user ) <EOL> message = ( <EOL> _ ( '<STR_LIT>' ) % <EOL> { '<STR_LIT>' : request . user . project_name } ) <EOL> messages . success ( request , message ) <EOL> response = shortcuts . redirect ( redirect_to ) <EOL> utils . set_response_cookie ( response , '<STR_LIT>' , <EOL> request . user . project_id ) <EOL> return response <EOL> @ login_required <EOL> def switch_region ( request , region_name , <EOL> redirect_field_name = auth . REDIRECT_FIELD_NAME ) : <EOL> """<STR_LIT>""" <EOL> if region_name in request . user . available_services_regions : <EOL> request . session [ '<STR_LIT>' ] = region_name <EOL> LOG . debug ( '<STR_LIT>' <EOL> % ( region_name , request . user . username ) ) <EOL> redirect_to = request . GET . get ( redirect_field_name , '<STR_LIT>' ) <EOL> if not is_safe_url ( url = redirect_to , host = request . get_host ( ) ) : <EOL> redirect_to = settings . LOGIN_REDIRECT_URL <EOL> response = shortcuts . redirect ( redirect_to ) <EOL> utils . set_response_cookie ( response , '<STR_LIT>' , <EOL> request . session [ '<STR_LIT>' ] ) <EOL> return response </s>
