Commit 2319b154 authored by Bertrand  NÉRON's avatar Bertrand NÉRON

redesign authentication

the user and the login are no longer option given on the commandline.
the program prompt for the logging and the password with no echoeing for the password.
the user have 3 attempt to give the right combination login/password
otherwise the program exit
parent b59b6548
./parser.py -S http://localhost:5984 -d secreton-dev -u bneron -p toto -r ../../data/info_replicon.dat -s ../../data/info_syst.dat -f
./parser.py -S http://localhost:5984 -d secreton-dev -r ../../data/info_replicon.dat -s ../../data/info_syst.dat -f
......@@ -7,6 +7,12 @@ Created on 27 dec. 2011
"""
from collections import namedtuple
from couchdbkit.client import Server
from couchdbkit.exceptions import ResourceNotFound
from couchdbkit.resource import CouchdbResource
from restkit import Resource, BasicAuth
import restkit.errors
def replicon_parser( replicon_data ):
"""
......@@ -113,23 +119,10 @@ def fill_db( server_uri, db_name, user, passwd, replicon_db , system_db , force_
@param force_update: if true force the entry to be updated even if the _rev number is not provided
@type force_update: boolean
"""
from couchdbkit.client import Server
from couchdbkit.exceptions import ResourceNotFound
from couchdbkit.resource import CouchdbResource
from restkit import Resource, BasicAuth
import restkit.errors
auth = BasicAuth(user , passwd )
resource = CouchdbResource( server_uri , filters=[auth])
server = Server( resource_instance= resource )
try:
secreton_db = server.get_or_create_db( db_name )
except restkit.errors.Unauthorized , err:
print "Bad Authentication"
sys.exit(1)
except Exception, err:
print >> sys.stderr , err
sys.exit(2)
secreton_db = server.get_or_create_db( db_name )
system_codes = system_db.keys()
system_codes.sort()
for syst_code in system_codes:
......@@ -160,10 +153,15 @@ def fill_db( server_uri, db_name, user, passwd, replicon_db , system_db , force_
secreton_db.save_doc( secretion_system , force_update= force_update )
if __name__ == '__main__':
from optparse import OptionParser , OptionGroup
import sys
import getpass
def get_credentials():
user = raw_input('login: ')
password = getpass.getpass('password: ')
return user, password
usage="""
%prog [options]
parse a file containing replicon informations and a file containing system informations
......@@ -181,16 +179,6 @@ if __name__ == '__main__':
type= "string" ,
dest = "db_name" ,
help = "the name of the data base")
server_opt.add_option( "-u" , "--user" ,
action = "store",
type= "string" ,
dest = "user" ,
help = "the login (the user must be a database admin)")
server_opt.add_option( "-p" , "--password" ,
action = "store",
type= "string" ,
dest = "passwd" ,
help = "the password of the user")
parser.add_option_group( server_opt )
parsing_opt = OptionGroup(parser, "Parsing Options")
......@@ -223,16 +211,6 @@ if __name__ == '__main__':
parser.print_help(sys.stderr)
sys.exit(1)
if not options.user:
print >> sys.stderr , "You must a user (which is data base admin)"
parser.print_help(sys.stderr)
sys.exit(1)
if not options.passwd:
print >> sys.stderr , "You must specify a password"
parser.print_help(sys.stderr)
sys.exit(1)
if not options.replicon_path:
print >> sys.stderr , "You must specify the path to the replicon information file"
parser.print_help(sys.stderr)
......@@ -246,5 +224,19 @@ if __name__ == '__main__':
replicon_db = replicon_parser( options.replicon_path )
system_db = system_parser( options.system_path )
fill_db( options.server_url , options.db_name , options.user , options.passwd , replicon_db , system_db , force_update = options.force_update )
\ No newline at end of file
try_again = 0
while True:
user, password = get_credentials()
try:
fill_db(options.server_url, options.db_name, user, password, replicon_db, system_db, force_update = options.force_update)
break
except restkit.errors.Unauthorized, err:
print >> sys.stderr, "Bad authentication, try again"
try_again += 1
if try_again > 2:
sys.exit("Authentication failure")
except Exception, err:
print >> sys.stderr, err
sys.exit(2)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment