<s> import sys <EOL> import hmac <EOL> import crypt <EOL> from lwp . utils import read_config_file <EOL> class htpasswd : <EOL> def __init__ ( self ) : <EOL> self . HTPASSWD_FILE = read_config_file ( ) . get ( '<STR_LIT>' , '<STR_LIT:file>' ) <EOL> def authenticate ( self , username , password ) : <EOL> user = None <EOL> if self . check_htpasswd ( self . HTPASSWD_FILE , username , password ) : <EOL> user = { <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT:name>' : username , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return user <EOL> def check_htpasswd ( self , htpasswd_file , username , password ) : <EOL> htuser = None <EOL> lines = open ( htpasswd_file , '<STR_LIT:r>' ) . readlines ( ) <EOL> for line in lines : <EOL> htuser , htpasswd = line . split ( '<STR_LIT::>' ) <EOL> htpasswd = htpasswd . rstrip ( '<STR_LIT:\n>' ) <EOL> if username == htuser : <EOL> break <EOL> if htuser is None : <EOL> return False <EOL> else : <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> return crypt . crypt ( password , htpasswd ) == htpasswd <EOL> else : <EOL> return hmac . compare_digest ( crypt . crypt ( password , htpasswd ) , htpasswd ) </s>
