This repository has been archived on 2023-01-03. You can view files and clone it, but cannot push or open issues or pull requests.
mysecrets/mysecrets.py

153 lines
3.6 KiB
Python
Executable file

#!/usr/bin/env python
import string
import random
import web
from web import form
db = web.database(dbn='mysql', user='mysecrets', pw='horsebatteries',
db='mysecrets')
urls = (
'/secret/api/(.*)', 'api',
'/secret/(.*)', 'index'
)
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'),
)
def mkpass(size=10):
validChars = string.ascii_letters + string.digits
validChars = validChars.strip("oO0")
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+'%"')
def get_generated_from_url(base_url):
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
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 = '<ul>'
for p in selected:
pairs += '<li>'+p.username+', '+p.password+'</li>'
pairs += '</ul>'
else:
pairs = "<p>Sorry buddy, you're outta luck</p>"
body = '<h1>'+i.base_url+'</h1>\n'+pairs
return render.page(i.base_url, body)
else:
return render.page('get','<p>need input.</p>')
if method == 'new':
i = web.input()
f = create()
body = []
body.append('<h1>'+i.base_url+'</h1>\n')
selected = get_pair_from_url(i.base_url)
generated = get_generated_from_url(i.base_url)
if selected:
body.append('<p>Existing: \n')
for pair in selected:
body.append('('+pair.username+', '+pair.password+')\n')
body.append('</p>')
body.append('<p>Generated: ' + generated[0].password + '</p>')
body.append('<form class="form" method="get" action="/secret/create">\n'+ \
f.render()+ '\n<input type="submit" name="submit" id="submit"/></form>')
return render.page('create',''.join(body))
if method == 'create':
i = web.input()
n = db.insert('passwords', username=i.username, password=i.password, \
base_url=i.base_url)
selected = get_pair_from_url(i.base_url)
body = '<h1>'+i.base_url+'</h1><p>'+selected[0].username+', '+selected[0].password+'</p>'
return render.page('Created', body)
if method == 'generate':
i = web.input()
body = '<h1>'+i.base_url+'</h1>\n<p> '+get_generated_from_url(i.base_url)[0].password+'</p>'
return render.page('Generated', body)
else:
return render.page('mySecrets', '<p>These are mySecrets. There is nothing for you here.</p>')
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 = '<h1>'+i.base_url+'</h1><p>'+i.username+', '+i.password+'</p>'
return render.page('Created', body)
if __name__ == "__main__":
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()