#!/usr/bin/env python import string import random import web from web import form from urllib import urlopen from urlparse import urlparse from contextlib import closing 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 get_domain(base_url): with closing(urlopen('https://mxr.mozilla.org/mozilla/source/netwerk/dns/src/effective_tld_names.dat?raw=1')) as tldFile: tlds = [line.strip() for line in tldFile if line[0] not in "/\n"] urlElements = base_url.split('.') for i in range(-len(urlElements),0): lastIElements = urlElements[i:] # i=-3: ["abcde","co","uk"] # i=-2: ["co","uk"] # i=-1: ["uk"] etc candidate = ".".join(lastIElements) # abcde.co.uk, co.uk, uk wildcardCandidate = ".".join(["*"]+lastIElements[1:]) # *.co.uk, *.uk, * exceptionCandidate = "!"+candidate if (exceptionCandidate in tlds): return ".".join(urlElements[i:]) if (candidate in tlds or wildcardCandidate in tlds): return ".".join(urlElements[i-1:]) return base_url def mkpass(size=10): validChars = string.ascii_letters + string.digits validChars = validChars.strip("oO01l") return string.join([random.choice(validChars) for x in range(size)],"") def get_pair_from_url(domain): return db.select('passwords', where='base_url LIKE "%'+domain+'%"', order='id DESC') def get_generated_from_url(domain): gen = db.select('generated', where='base_url LIKE "%'+domain+'%"', order='id DESC') while not len(gen) > 0: db.insert('generated', base_url = domain, password = mkpass()) gen = db.select('generated', where='base_url LIKE "%'+domain+'%"') 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 = '' else: pairs = "

Sorry buddy, you're outta luck

" body = '

'+i.base_url+'

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

need input.

') if method == 'new': i = web.input() f = create() body = [] domain = get_domain(i.base_url) body.append('

'+domain+'

\n') selected = get_pair_from_url(domain) generated = get_generated_from_url(domain) if selected: body.append('

Existing:

\n') body.append('

Suggested:

\n

' + generated[0].password + '

\n') body.append('

Create:

\n') body.append('
\n'+ \ f.render()+ '\n
') return render.page('create',''.join(body)) if method == 'js-overlay': return '''\ (function() { function cleanHouse() { elements = document.querySelectorAll('.myS'); for (i=0; iThese 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) if __name__ == "__main__": web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr) app.run()