diff --git a/mysecrets.py b/mysecrets.py index e39613d..cfe569e 100755 --- a/mysecrets.py +++ b/mysecrets.py @@ -6,11 +6,11 @@ import web from web import form db = web.database(dbn='mysql', user='mysecrets', pw='horsebatteries', - db='mysecrets') - + db='mysecrets') + urls = ( - '/secret/api/(.*)', 'api', - '/secret/(.*)', 'index' + '/secret/api/(.*)', 'api', + '/secret/(.*)', 'index' ) app = web.application(urls, globals()) @@ -18,144 +18,145 @@ app = web.application(urls, globals()) render = web.template.render('templates/') create = form.Form( - form.Textbox('base_url', description="domain"), - form.Textbox('username'), - form.Password('password'), + form.Textbox('base_url', description="domain"), + form.Textbox('username'), + form.Password('password'), ) def mkpass(size=10): - validChars = string.ascii_letters + string.digits - validChars = validChars.strip("oO01l") + validChars = string.ascii_letters + string.digits + validChars = validChars.strip("oO01l") - return string.join([random.choice(validChars) for x in range(size)],"") + return string.join([random.choice(validChars) for x in range(size)],"") def get_pair_from_url(base_url): - return db.select('passwords', where='base_url LIKE "%'+base_url+'%"', order='id DESC') + return db.select('passwords', where='base_url LIKE "%'+base_url+'%"', order='id DESC') def get_generated_from_url(base_url): - gen = db.select('generated', where='base_url LIKE "%'+base_url+'%"', order='id DESC') + gen = db.select('generated', where='base_url LIKE "%'+base_url+'%"', order='id DESC') - while not len(gen) > 0: - db.insert('generated', base_url = base_url, password = mkpass()) - gen = db.select('generated', where='base_url LIKE "%'+base_url+'%"') + while not len(gen) > 0: + db.insert('generated', base_url = base_url, password = mkpass()) + gen = db.select('generated', where='base_url LIKE "%'+base_url+'%"') - return gen + return gen -class index: - def GET(self, method): - if method == 'get': - if web.input(): - i = web.input() - selected = get_pair_from_url(i.base_url) +class index: + def GET(self, method): + if method == 'get': + if web.input(): + i = web.input() + selected = get_pair_from_url(i.base_url) - if len(selected) > 0: - pairs = '' + if len(selected) > 0: + pairs = '' - else: - pairs = "

Sorry buddy, you're outta luck

" + else: + pairs = "

Sorry buddy, you're outta luck

" - body = '

'+i.base_url+'

\n'+pairs + body = '

'+i.base_url+'

\n'+pairs - return render.page(i.base_url, body) - - else: - return render.page('get','

need input.

') - - if method == 'new': + return render.page(i.base_url, body) + + else: + return render.page('get','

need input.

') + + if method == 'new': - i = web.input() + i = web.input() - f = create() + f = create() - body = [] + body = [] - body.append('

'+i.base_url+'

\n') + body.append('

'+i.base_url+'

\n') - selected = get_pair_from_url(i.base_url) - generated = get_generated_from_url(i.base_url) + selected = get_pair_from_url(i.base_url) + generated = get_generated_from_url(i.base_url) - if selected: - body.append('

Existing: \n') - for pair in selected: - body.append('('+pair.username+', '+pair.password+')\n') - body.append('

') + if selected: + body.append('

Existing:

\n') - body.append('

Generated: ' + generated[0].password + '

') + body.append('

Generated:

\n

' + generated[0].password + '

\n') + body.append('

Create:

\n') - body.append('
\n'+ \ - f.render()+ '\n
') - - return render.page('create',''.join(body)) - - - if method == 'create': - i = web.input() + body.append('
\n'+ \ + f.render()+ '\n
') + + return render.page('create',''.join(body)) + + + if method == 'create': + i = web.input() - exists = db.select('passwords', where='username="'+i.username+'" and password="' +\ - i.password+'" and base_url="'+i.base_url+'"') + exists = db.select('passwords', where='username="'+i.username+'" and password="' +\ + i.password+'" and base_url="'+i.base_url+'"') - if not exists: - n = db.insert('passwords', username=i.username, password=i.password, \ - base_url=i.base_url) - - selected = get_pair_from_url(i.base_url) + if not exists: + n = db.insert('passwords', username=i.username, password=i.password, \ + base_url=i.base_url) + + selected = get_pair_from_url(i.base_url) - body = ['

'+i.base_url+'

',''] + body = ['

'+i.base_url+'

',''] - for pair in selected: - body.append('('+pair.username+', '+pair.password+')') + for pair in selected: + body.append('('+pair.username+', '+pair.password+')') - return render.page('Created', ''.join(body)) + return render.page('Created', ''.join(body)) - if method == 'generate': - i = web.input() - - body = '

'+i.base_url+'

\n

'+get_generated_from_url(i.base_url)[0].password+'

' + if method == 'generate': + i = web.input() + + body = '

'+i.base_url+'

\n

'+get_generated_from_url(i.base_url)[0].password+'

' - return render.page('Generated', body) + return render.page('Generated', body) - - else: - return render.page('mySecrets', '

These are mySecrets. There is nothing for you here.

') + + else: + return render.page('mySecrets', '

These are mySecrets. There is nothing for you here.

') class api: - def GET(self, method): - if method == "list": - tmp = '' - for pair in db.select('passwords'): - tmp += pair['base_url']+','+pair['username']+','+pair['password']+'\n' - - return tmp - - if method == "get": - i = web.input() - base_url = i.base_url - pairs = db.select('passwords', where='`base_url`="'+base_url+'"') - - tmp = '' - for pair in pairs: - tmp += pair.username+','+pair.password+'\n' - - return tmp - - else: - return 'mySecrets API ' + method - - def POST(self, method): - i = web.input() - n = db.insert('passwords', username=i.username, password=i.password, \ - base_url=i.base_url) - - body = '

'+i.base_url+'

'+i.username+', '+i.password+'

' - - return render.page('Created', body) + def GET(self, method): + if method == "list": + tmp = '' + for pair in db.select('passwords'): + tmp += pair['base_url']+','+pair['username']+','+pair['password']+'\n' + + return tmp + + if method == "get": + i = web.input() + base_url = i.base_url + pairs = db.select('passwords', where='`base_url`="'+base_url+'"') + + tmp = '' + for pair in pairs: + tmp += pair.username+','+pair.password+'\n' + + return tmp + + else: + return 'mySecrets API ' + method + + def POST(self, method): + i = web.input() + n = db.insert('passwords', username=i.username, password=i.password, \ + base_url=i.base_url) + + body = '

'+i.base_url+'

'+i.username+', '+i.password+'

' + + return render.page('Created', body) if __name__ == "__main__": - web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr) - app.run() + web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr) + app.run() diff --git a/templates/page.html b/templates/page.html index 3808862..3467db3 100644 --- a/templates/page.html +++ b/templates/page.html @@ -3,8 +3,90 @@ $def with (title, body) $title + + -$:body + + + + + $:body - \ No newline at end of file +