Created
April 6, 2014 06:42
-
-
Save j0lvera/10002305 to your computer and use it in GitHub Desktop.
auth.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| from bottle import route, run, template, static_file, request, parse_auth, auth_basic | |
| from redis import StrictRedis as Redis | |
| from cork import Cork | |
| from hashids import Hashids | |
| from passlib.hash import pbkdf2_sha256 | |
| incr1 = Hashids(salt="Eehu6laucelohh3b", min_length="16") | |
| incr2 = Hashids(salt="Phiejapie2ahr9wa", min_length="8") | |
| incr3 = Hashids(salt="fiefiesh8IWaibah", min_length="6") # uid | |
| redis = Redis() | |
| redis.set("incr1", 0) | |
| redis.set("incr2", 0) | |
| redis.set("incr3", 0) | |
| # Methods | |
| def get_get(name, default=""): | |
| """...""" | |
| return request.GET.get(name, default).strip() | |
| def post_get(name, default=""): | |
| """...""" | |
| return request.POST.get(name, default).strip() | |
| # def is_equal(a, b): | |
| # """...""" | |
| # if len(a) != len(b): | |
| # return False | |
| # result = 0 | |
| # for x, y in zip(a, b): | |
| # result |= ord(x) ^ ord(y) | |
| # return result == 0 | |
| def hash_pass(password): | |
| """...""" | |
| return pbkdf2_sha256.encrypt(password, rounds=8000, salt_size=16) | |
| def check_pass(email, password): | |
| """...""" | |
| # auth = request.headers.get('Authorization') | |
| # email, password = parse_auth(auth) | |
| hashed = ''.join(redis.hmget(email, 'password')) | |
| return pbkdf2_sha256.verify(password, hashed) | |
| @route('/') | |
| def index(): | |
| """...""" | |
| return static_file('index.html', root='./public/dev/') | |
| @route('/<filename:path>') | |
| def files(filename): | |
| """...""" | |
| return static_file(filename, root='./public/dev/') | |
| @route('/app', method="GET") | |
| @auth_basic(check_pass) | |
| def app(): | |
| """...""" | |
| return "GOOD" | |
| # if check_pass(): | |
| # return "GOOD" | |
| # return "BAD" | |
| @route('/login', method="POST") | |
| def login(): | |
| """...""" | |
| email = post_get('email') | |
| guessed_password = post_get('password') | |
| password = hash_pass(guessed_password) | |
| hashed = ''.join(redis.hmget(email, 'password')) | |
| print password | |
| print ' ' | |
| print hashed | |
| if pbkdf2_sha256.verify(guessed_password, hashed): | |
| return "GOOD" | |
| return "BAD" | |
| @route('/register', method="POST") | |
| def register(): | |
| """...""" | |
| email = post_get('email') | |
| hashed = hash_pass(post_get('password')) | |
| uid = incr3.encrypt(redis.incr('incr3')) | |
| redis.hmset(email, { 'password': hashed, 'uid': uid }) | |
| run(host='localhost', port=8080, debug=True, reloader=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment