<s> """<STR_LIT>""" <EOL> from yubiauth import YubiAuth <EOL> from u2flib_server . u2f_v2 import ( start_register , complete_register , <EOL> start_authenticate , verify_authenticate ) <EOL> from webob . dec import wsgify <EOL> from webob import exc <EOL> import json <EOL> import traceback <EOL> def get_origin ( environ ) : <EOL> if environ . get ( '<STR_LIT>' ) : <EOL> host = environ [ '<STR_LIT>' ] <EOL> else : <EOL> host = environ [ '<STR_LIT>' ] <EOL> if environ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if environ [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> host += '<STR_LIT::>' + environ [ '<STR_LIT>' ] <EOL> else : <EOL> if environ [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> host += '<STR_LIT::>' + environ [ '<STR_LIT>' ] <EOL> return '<STR_LIT>' % ( environ [ '<STR_LIT>' ] , host ) <EOL> class U2FServer ( object ) : <EOL> """<STR_LIT>""" <EOL> @ wsgify <EOL> def __call__ ( self , request ) : <EOL> self . origin = get_origin ( request . environ ) <EOL> self . app_id = self . origin <EOL> page = request . path_info_pop ( ) <EOL> if page == '<STR_LIT>' : <EOL> return self . origin <EOL> elif page is None : <EOL> return json . dumps ( [ self . origin ] ) <EOL> with YubiAuth ( ) as auth : <EOL> try : <EOL> username = '<STR_LIT>' + request . params [ '<STR_LIT:username>' ] <EOL> password = request . params [ '<STR_LIT:password>' ] <EOL> data = request . params . get ( '<STR_LIT:data>' , None ) <EOL> self . auth = auth <EOL> if page == '<STR_LIT>' : <EOL> return self . enroll ( username , password ) <EOL> elif page == '<STR_LIT>' : <EOL> return self . bind ( username , password , data ) <EOL> elif page == '<STR_LIT>' : <EOL> return self . sign ( username , password ) <EOL> elif page == '<STR_LIT>' : <EOL> return self . verify ( username , password , data ) <EOL> else : <EOL> raise exc . HTTPNotFound ( ) <EOL> except Exception : <EOL> return exc . HTTPBadRequest ( comment = traceback . format_exc ( ) ) <EOL> def enroll ( self , username , password ) : <EOL> try : <EOL> user = self . auth . get_user ( username ) <EOL> user . set_password ( password ) <EOL> except : <EOL> user = self . auth . create_user ( username , password ) <EOL> enroll = start_register ( self . app_id ) <EOL> user . attributes [ '<STR_LIT>' ] = enroll . json <EOL> return enroll . json <EOL> def bind ( self , username , password , data ) : <EOL> user = self . _get_user ( username , password ) <EOL> enroll = user . attributes [ '<STR_LIT>' ] <EOL> binding , cert = complete_register ( enroll , data , [ self . origin ] ) <EOL> user . attributes [ '<STR_LIT>' ] = binding . json <EOL> user . attributes [ '<STR_LIT>' ] = cert . as_pem ( ) <EOL> return json . dumps ( { <EOL> '<STR_LIT:username>' : username [ <NUM_LIT:4> : ] , <EOL> '<STR_LIT>' : self . origin , <EOL> '<STR_LIT>' : cert . as_pem ( ) <EOL> } ) <EOL> def sign ( self , username , password ) : <EOL> user = self . _get_user ( username , password ) <EOL> binding = user . attributes [ '<STR_LIT>' ] <EOL> challenge = start_authenticate ( binding ) <EOL> user . attributes [ '<STR_LIT>' ] = challenge . json <EOL> return challenge . json <EOL> def verify ( self , username , password , data ) : <EOL> user = self . _get_user ( username , password ) <EOL> binding = user . attributes [ '<STR_LIT>' ] <EOL> challenge = user . attributes [ '<STR_LIT>' ] <EOL> c , t = verify_authenticate ( binding , challenge , data , [ self . origin ] ) <EOL> return json . dumps ( { <EOL> '<STR_LIT>' : t , <EOL> '<STR_LIT>' : c <EOL> } ) <EOL> def _get_user ( self , username , password ) : <EOL> user = self . auth . get_user ( username ) <EOL> if not user . validate_password ( password ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return user <EOL> application = U2FServer ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from wsgiref . simple_server import make_server <EOL> httpd = make_server ( '<STR_LIT>' , <NUM_LIT> , application ) <EOL> httpd . serve_forever ( ) </s>
