<s> import logging <EOL> import requests <EOL> from flask import redirect , url_for , Blueprint , request <EOL> from redash . authentication . google_oauth import create_and_login_user <EOL> from redash . authentication . org_resolving import current_org <EOL> from redash import settings <EOL> from saml2 import BINDING_HTTP_POST , BINDING_HTTP_REDIRECT , entity <EOL> from saml2 . client import Saml2Client <EOL> from saml2 . config import Config as Saml2Config <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> blueprint = Blueprint ( '<STR_LIT>' , __name__ ) <EOL> def get_saml_client ( ) : <EOL> """<STR_LIT>""" <EOL> if settings . SAML_CALLBACK_SERVER_NAME : <EOL> acs_url = settings . SAML_CALLBACK_SERVER_NAME + url_for ( "<STR_LIT>" ) <EOL> else : <EOL> acs_url = url_for ( "<STR_LIT>" , _external = True ) <EOL> rv = requests . get ( settings . SAML_METADATA_URL ) <EOL> import tempfile <EOL> tmp = tempfile . NamedTemporaryFile ( ) <EOL> f = open ( tmp . name , '<STR_LIT:w>' ) <EOL> f . write ( rv . text ) <EOL> f . close ( ) <EOL> saml_settings = { <EOL> '<STR_LIT>' : { <EOL> "<STR_LIT>" : [ tmp . name ] <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ <EOL> ( acs_url , BINDING_HTTP_REDIRECT ) , <EOL> ( acs_url , BINDING_HTTP_POST ) <EOL> ] , <EOL> } , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> } , <EOL> } , <EOL> } <EOL> spConfig = Saml2Config ( ) <EOL> spConfig . load ( saml_settings ) <EOL> spConfig . allow_unknown_attributes = True <EOL> saml_client = Saml2Client ( config = spConfig ) <EOL> tmp . close ( ) <EOL> return saml_client <EOL> @ blueprint . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def idp_initiated ( ) : <EOL> saml_client = get_saml_client ( ) <EOL> authn_response = saml_client . parse_authn_request_response ( <EOL> request . form [ '<STR_LIT>' ] , <EOL> entity . BINDING_HTTP_POST ) <EOL> authn_response . get_identity ( ) <EOL> user_info = authn_response . get_subject ( ) <EOL> email = user_info . text <EOL> name = "<STR_LIT>" % ( authn_response . ava [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , authn_response . ava [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> create_and_login_user ( current_org , name , email ) <EOL> url = url_for ( '<STR_LIT>' ) <EOL> return redirect ( url ) <EOL> @ blueprint . route ( "<STR_LIT>" ) <EOL> def sp_initiated ( ) : <EOL> if not settings . SAML_METADATA_URL : <EOL> logger . error ( "<STR_LIT>" ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> saml_client = get_saml_client ( ) <EOL> reqid , info = saml_client . prepare_for_authenticate ( ) <EOL> redirect_url = None <EOL> for key , value in info [ '<STR_LIT>' ] : <EOL> if key is '<STR_LIT>' : <EOL> redirect_url = value <EOL> response = redirect ( redirect_url , code = <NUM_LIT> ) <EOL> response . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> response . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return response </s>
