<s> import json <EOL> import uuid <EOL> from tornado . web import HTTPError <EOL> from . util import CorsHandlerWithAuth <EOL> from . . keys import KeyConflict , KeyNotFound <EOL> class KeysHandler ( CorsHandlerWithAuth ) : <EOL> def get ( self , * args ) : <EOL> if not self . api_key . is_admin ( ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> if self . get_argument ( "<STR_LIT>" , "<STR_LIT:false>" ) . lower ( ) == "<STR_LIT:true>" : <EOL> include_key = True <EOL> else : <EOL> include_key = False <EOL> self . write ( { "<STR_LIT>" : self . key_mgr . all_keys ( include_key ) } ) <EOL> def post ( self , * args ) : <EOL> if not self . api_key . can_create_key ( ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> api_key , data , parent = self . fetch_key ( ) <EOL> except ValueError : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> permissions = data . get ( '<STR_LIT>' , { } ) <EOL> if permissions . get ( '<STR_LIT>' ) == True and not self . api_key . is_admin ( ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> self . key_mgr . set_key ( api_key , data , parent = parent ) <EOL> except KeyConflict : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> location = '<STR_LIT>' % ( self . request . protocol , <EOL> self . request . host , api_key ) <EOL> self . set_header ( "<STR_LIT:Content-Type>" , "<STR_LIT:application/json>" ) <EOL> self . set_header ( "<STR_LIT>" , api_key ) <EOL> self . set_header ( "<STR_LIT>" , location ) <EOL> self . write ( json . dumps ( { "<STR_LIT>" : True , "<STR_LIT>" : api_key } ) ) <EOL> def fetch_key ( self ) : <EOL> obj = json . loads ( self . request . body . decode ( '<STR_LIT:utf-8>' ) ) <EOL> key = uuid . uuid4 ( ) . hex <EOL> if "<STR_LIT:key>" in obj : <EOL> del obj [ '<STR_LIT:key>' ] <EOL> parent = None <EOL> if "<STR_LIT>" in obj : <EOL> parent = obj . pop ( '<STR_LIT>' ) <EOL> return key , obj , parent <EOL> class KeyHandler ( CorsHandlerWithAuth ) : <EOL> def head ( self , * args ) : <EOL> if ( not self . api_key . can_create_key ( ) and <EOL> self . api_key . api_key != args [ <NUM_LIT:0> ] ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> key_obj = self . key_mgr . has_key ( args [ <NUM_LIT:0> ] ) <EOL> except KeyNotFound : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> self . set_status ( <NUM_LIT:200> ) <EOL> def get ( self , * args ) : <EOL> if ( not self . api_key . can_create_key ( ) and <EOL> self . api_key . api_key != args [ <NUM_LIT:0> ] ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> key_obj = self . key_mgr . get_key ( args [ <NUM_LIT:0> ] ) <EOL> except KeyNotFound : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> if self . get_argument ( "<STR_LIT>" , "<STR_LIT:false>" ) . lower ( ) == "<STR_LIT:true>" : <EOL> subkeys = self . key_mgr . all_subkeys ( args [ <NUM_LIT:0> ] ) <EOL> key_obj [ '<STR_LIT>' ] = subkeys <EOL> self . write ( key_obj ) <EOL> def delete ( self , * args ) : <EOL> if ( not self . api_key . can_create_key ( ) and <EOL> self . api_key . api_key != args [ <NUM_LIT:0> ] ) : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> try : <EOL> key_obj = self . key_mgr . delete_key ( args [ <NUM_LIT:0> ] ) <EOL> except KeyNotFound : <EOL> raise HTTPError ( <NUM_LIT> ) <EOL> self . write ( { "<STR_LIT>" : True } ) </s>
