Permalink
Browse files

Add flask interface

  • Loading branch information...
1 parent 74cc8ee commit 57da0d095efaa949e5f29272ba5a61146c4c968b @bugsduggan bugsduggan committed Jan 18, 2014
View
@@ -1,4 +1,4 @@
access-control
==============
-Prototype code for the door access control system
+Code for the door access control system
View
@@ -0,0 +1,16 @@
+from flask import Flask
+from flask.ext.login import LoginManager
+from flask.ext.sqlalchemy import SQLAlchemy
+
+app = Flask(__name__)
+app.config.from_object('config')
+
+db = SQLAlchemy(app)
+
+login_manager = LoginManager()
+login_manager.init_app(app)
+login_manager.login_view = 'login'
+login_manager.login_message_category = 'warning'
+login_manager.needs_refresh_message_category = 'warning'
+
+from access import views
View
@@ -0,0 +1,2 @@
+ROLE_USER = 0
+ROLE_ADMIN = 1
View
@@ -0,0 +1,27 @@
+from flask.ext.wtf import Form
+from wtforms import TextField, BooleanField, PasswordField
+from wtforms.validators import Required, EqualTo
+
+
+class LoginForm(Form):
+ email = TextField('email', validators=[Required()])
+ password = PasswordField('password', validators=[Required()])
+ remember_me = BooleanField('remember_me', default=False)
+
+
+class NewAdminForm(Form):
+ password = PasswordField('password', validators=[
+ Required(),
+ EqualTo('confirm', message='Passwords must match')
+ ])
+ confirm = PasswordField('confirm_password', validators=[Required()])
+
+
+class NewKeyForm(Form):
+ key_id = TextField('key_id', validators=[Required()])
+
+
+class NewUserForm(Form):
+ name = TextField('name', validators=[Required()])
+ email = TextField('email', validators=[Required()])
+ key_id = TextField('key_id', validators=[Required()])
View
@@ -0,0 +1,55 @@
+import hashlib
+import uuid
+
+from access import db
+from access.constants import ROLE_USER, ROLE_ADMIN
+
+
+class User(db.Model):
+ id = db.Column(db.Integer, primary_key=True)
+ name = db.Column(db.String(128), unique=True, nullable=False)
+ email = db.Column(db.String(128), unique=True, nullable=False)
+ role = db.Column(db.SmallInteger, default=ROLE_USER)
+ key_id = db.Column(db.Integer, unique=True)
+ pw_hash = db.Column(db.String(128))
+ pw_salt = db.Column(db.String(32))
+
+ def __init__(self, name, email, key_id):
+ self.name = name
+ self.email = email
+ self.key_id = key_id
+
+ def make_admin(self, password):
+ self.role = ROLE_ADMIN
+ self.pw_salt = uuid.uuid4().hex
+ self.pw_hash = hashlib.sha512(password + self.pw_salt).hexdigest()
+
+ def make_user(self):
+ self.role = ROLE_USER
+ self.pw_salt = None
+ self.pw_hash = None
+
+ def check_password(self, password):
+ if self.role != ROLE_ADMIN:
+ return False
+ check_hash = hashlib.sha512(password + self.pw_salt).hexdigest()
+ return check_hash == self.pw_hash
+
+ def is_authenticated(self):
+ return True
+
+ def is_active(self):
+ """
+ We're only allowing admins to login, that is only
+ admins will be considered active.
+ """
+ return self.role == ROLE_ADMIN
+
+ def is_anonymous(self):
+ return False
+
+ def get_id(self):
+ return unicode(self.id)
+
+ def __repr__(self):
+ return '<User %r>' % (self.email)
Oops, something went wrong.

0 comments on commit 57da0d0

Please sign in to comment.