<s> import argparse <EOL> from functools import wraps , partial <EOL> import json <EOL> import mimetypes <EOL> import os <EOL> import urllib <EOL> import urlparse <EOL> import errno <EOL> import cherrypy <EOL> from cherrypy import wsgiserver <EOL> from cherrypy . wsgiserver import ssl_pyopenssl <EOL> from cherrypy . wsgiserver . wsgiserver2 import WSGIPathInfoDispatcher <EOL> from jinja2 . environment import Environment <EOL> from jinja2 . loaders import FileSystemLoader <EOL> import yaml <EOL> from oic . oauth2 import rndstr <EOL> from oic . oic . provider import Provider , AuthorizationEndpoint , TokenEndpoint , UserinfoEndpoint , RegistrationEndpoint , EndSessionEndpoint <EOL> from oic . utils . authn . authn_context import AuthnBroker <EOL> from oic . utils . authn . client import verify_client <EOL> from oic . utils . authz import AuthzHandling <EOL> from oic . utils . http_util import Redirect , SeeOther , Response , BadRequest , get_or_post , get_post , NotFound <EOL> from oic . utils . keyio import keyjar_init <EOL> from oic . utils . sdb import SessionDB <EOL> from oic . utils . userinfo import UserInfo <EOL> from oic . utils . webfinger import OIC_ISSUER , WebFinger <EOL> from provider . authn import make_cls_from_name <EOL> def VerifierMiddleware ( verifier ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( verifier . verify ) <EOL> def wrapper ( environ , start_response ) : <EOL> data = get_post ( environ ) <EOL> kwargs = dict ( urlparse . parse_qsl ( data ) ) <EOL> kwargs [ "<STR_LIT:state>" ] = json . loads ( urllib . unquote ( kwargs [ "<STR_LIT:state>" ] ) ) <EOL> val , completed = verifier . verify ( ** kwargs ) <EOL> if not completed : <EOL> return val ( environ , start_response ) <EOL> if val : <EOL> set_cookie , cookie_value = verifier . create_cookie ( val , "<STR_LIT>" ) <EOL> cookie_value += "<STR_LIT>" <EOL> url = "<STR_LIT>" . format ( <EOL> base_url = "<STR_LIT>" , <EOL> query_string = kwargs [ "<STR_LIT:state>" ] [ "<STR_LIT>" ] ) <EOL> response = SeeOther ( url , headers = [ ( set_cookie , cookie_value ) ] ) <EOL> return response ( environ , start_response ) <EOL> else : <EOL> url = "<STR_LIT>" . format ( <EOL> base_url = "<STR_LIT>" , <EOL> query_string = kwargs [ "<STR_LIT:state>" ] [ "<STR_LIT>" ] ) <EOL> response = SeeOther ( url ) <EOL> return response ( environ , start_response ) <EOL> return wrapper <EOL> def pyoidcMiddleware ( func ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( environ , start_response ) : <EOL> data = get_or_post ( environ ) <EOL> cookies = environ . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> resp = func ( request = data , cookie = cookies ) <EOL> return resp ( environ , start_response ) <EOL> return wrapper <EOL> def resp2flask ( resp ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( resp , Redirect ) or isinstance ( resp , SeeOther ) : <EOL> code = int ( resp . status . split ( ) [ <NUM_LIT:0> ] ) <EOL> raise cherrypy . HTTPRedirect ( resp . message , code ) <EOL> return resp . message , resp . status , resp . headers <EOL> def setup_authentication_methods ( authn_config , template_env ) : <EOL> """<STR_LIT>""" <EOL> routing = { } <EOL> ac = AuthnBroker ( ) <EOL> for authn_method in authn_config : <EOL> cls = make_cls_from_name ( authn_method [ "<STR_LIT:class>" ] ) <EOL> instance = cls ( template_env = template_env , ** authn_method [ "<STR_LIT>" ] ) <EOL> ac . add ( authn_method [ "<STR_LIT>" ] , instance ) <EOL> routing [ instance . url_endpoint ] = VerifierMiddleware ( instance ) <EOL> return ac , routing <EOL> def setup_endpoints ( provider ) : <EOL> """<STR_LIT>""" <EOL> app_routing = { } <EOL> endpoints = [ <EOL> AuthorizationEndpoint ( <EOL> pyoidcMiddleware ( provider . authorization_endpoint ) ) , <EOL> TokenEndpoint ( <EOL> pyoidcMiddleware ( provider . token_endpoint ) ) , <EOL> UserinfoEndpoint ( <EOL> pyoidcMiddleware ( provider . userinfo_endpoint ) ) , <EOL> RegistrationEndpoint ( <EOL> pyoidcMiddleware ( provider . registration_endpoint ) ) , <EOL> EndSessionEndpoint ( <EOL> pyoidcMiddleware ( provider . endsession_endpoint ) ) <EOL> ] <EOL> for ep in endpoints : <EOL> app_routing [ "<STR_LIT>" . format ( ep . etype ) ] = ep <EOL> return app_routing <EOL> def _webfinger ( provider , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> params = urlparse . parse_qs ( request ) <EOL> if params [ "<STR_LIT>" ] [ <NUM_LIT:0> ] == OIC_ISSUER : <EOL> wf = WebFinger ( ) <EOL> return Response ( wf . response ( params [ "<STR_LIT>" ] [ <NUM_LIT:0> ] , provider . baseurl ) , <EOL> headers = [ ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) ] ) <EOL> else : <EOL> return BadRequest ( "<STR_LIT>" ) <EOL> def make_static_handler ( static_dir ) : <EOL> def static ( environ , start_response ) : <EOL> path = environ [ '<STR_LIT>' ] <EOL> full_path = os . path . join ( static_dir , os . path . normpath ( path ) . lstrip ( "<STR_LIT:/>" ) ) <EOL> if os . path . exists ( full_path ) : <EOL> with open ( full_path , '<STR_LIT:rb>' ) as f : <EOL> content = f . read ( ) <EOL> content_type , encoding = mimetypes . guess_type ( full_path ) <EOL> headers = [ ( '<STR_LIT:Content-Type>' , content_type ) ] <EOL> start_response ( "<STR_LIT>" , headers ) <EOL> return [ content ] <EOL> else : <EOL> response = NotFound ( <EOL> "<STR_LIT>" . format ( environ [ '<STR_LIT>' ] ) ) <EOL> return response ( environ , start_response ) <EOL> return static <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = <NUM_LIT> , type = int ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" , type = str ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> parser . add_argument ( "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> with open ( args . settings , "<STR_LIT:r>" ) as f : <EOL> settings = yaml . load ( f ) <EOL> issuer = args . base . rstrip ( "<STR_LIT:/>" ) <EOL> template_dirs = settings [ "<STR_LIT>" ] . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> jinja_env = Environment ( loader = FileSystemLoader ( template_dirs ) ) <EOL> authn_broker , auth_routing = setup_authentication_methods ( settings [ "<STR_LIT>" ] , <EOL> jinja_env ) <EOL> userinfo_conf = settings [ "<STR_LIT>" ] <EOL> cls = make_cls_from_name ( userinfo_conf [ "<STR_LIT:class>" ] ) <EOL> i = cls ( ** userinfo_conf [ "<STR_LIT>" ] ) <EOL> userinfo = UserInfo ( i ) <EOL> client_db = { } <EOL> provider = Provider ( issuer , SessionDB ( issuer ) , client_db , authn_broker , <EOL> userinfo , AuthzHandling ( ) , verify_client , None ) <EOL> provider . baseurl = issuer <EOL> provider . symkey = rndstr ( <NUM_LIT:16> ) <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" ) <EOL> try : <EOL> os . makedirs ( path ) <EOL> except OSError , e : <EOL> if e . errno != errno . EEXIST : <EOL> raise e <EOL> pass <EOL> jwks = keyjar_init ( provider , settings [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> name = "<STR_LIT>" <EOL> with open ( os . path . join ( path , name ) , "<STR_LIT:w>" ) as f : <EOL> f . write ( json . dumps ( jwks ) ) <EOL> provider . jwks_uri . append ( <EOL> "<STR_LIT>" . format ( provider . baseurl , name ) ) <EOL> app_routing = setup_endpoints ( provider ) <EOL> app_routing [ "<STR_LIT>" ] = pyoidcMiddleware ( <EOL> provider . providerinfo_endpoint ) <EOL> app_routing [ "<STR_LIT>" ] = pyoidcMiddleware ( <EOL> partial ( _webfinger , provider ) ) <EOL> routing = dict ( auth_routing . items ( ) + app_routing . items ( ) ) <EOL> routing [ "<STR_LIT>" ] = make_static_handler ( path ) <EOL> dispatcher = WSGIPathInfoDispatcher ( routing ) <EOL> server = wsgiserver . CherryPyWSGIServer ( ( '<STR_LIT>' , args . port ) , dispatcher ) <EOL> if provider . baseurl . startswith ( "<STR_LIT>" ) : <EOL> server . ssl_adapter = ssl_pyopenssl . pyOpenSSLAdapter ( <EOL> settings [ "<STR_LIT>" ] [ "<STR_LIT>" ] , settings [ "<STR_LIT>" ] [ "<STR_LIT:key>" ] , <EOL> settings [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> try : <EOL> print ( "<STR_LIT>" . format ( issuer ) ) <EOL> server . start ( ) <EOL> except KeyboardInterrupt : <EOL> server . stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
