Exemplo n.º 1
0
class Login:
    def __init__(self):
        self.datastore = Datastore()
        self.security = Security()
        self.home = Home()
        self.page_path = 'login.html'

    def render_page(self, error='', username=''):
        if 'user_session' in request.cookies:
            return redirect('/')
        else:
            return render_template(self.page_path,
                                   error=error,
                                   username=username)

    def do_login(self, username='', password=''):
        error = ''
        if username == '' or password == '':
            error = "Can not proceed without username or password"

        user = self.datastore.do_query('User', 'username', username)
        if user == 0 or not user or \
                not self.security.validate_password(
                    password, user[0].get('password')):
            error = "Invalid credentials"
        if error:
            return self.render_page(error, username)
        else:
            response = make_response(redirect('/'))
            self.security.set_cookie(response, username)
            return response
Exemplo n.º 2
0
class Logout:
    def __init__(self):
        self.data = []
        self.security = Security()

    def do_logout(self, redirect_path='/'):
        response = make_response(redirect(redirect_path))
        self.security.clear_cookie(response)
        return response
Exemplo n.º 3
0
class Signup:
    def __init__(self):
        self.datastore = Datastore()
        self.security = Security()
        self.home = Home()
        self.login = Login()
        self.page_path = 'signup.html'

    def render_page(self,
                    error='',
                    username='',
                    password='',
                    verified_password='',
                    email=''):
        return render_template(self.page_path,
                               error=error,
                               username_attempt=username,
                               password=password,
                               verify=verified_password,
                               email=email)

    def create_account(self,
                       username='',
                       password='',
                       verified_password='',
                       email=''):
        error = ''
        if username == '' or password == '' or email == '':
            error = "Can not proceed without username or password"
        elif password and len(password) < 6 or len(password) > 16:
            error = "Invalid password"
        elif password != verified_password:
            error = "Password dont match"
        elif len(self.datastore.do_query('User', 'username', username)) > 0:
            error = "User already exists"
        elif len(self.datastore.do_query('User', 'email', email)) > 0:
            error = "Email already in use"

        if error:
            return self.render_page(error, username, password,
                                    verified_password, email)
        else:
            user = self.datastore.create_entity('User', username)
            user['id'] = str(uuid.uuid4())
            user['username'] = username
            user['password'] = self.security.generate_hash(password)
            user['email'] = email
            self.datastore.save_object(user)
            return self.login.render_page('', username)
Exemplo n.º 4
0
 def __init__(self):
     self.data = []
     self.security = Security()
Exemplo n.º 5
0
from handlers.signup import Signup
from handlers.login import Login
from handlers.logout import Logout
from handlers.home import Home
from handlers.post import Post
from handlers.security import Security
import base64
import datetime

app = Flask(__name__)
signup = Signup()
login = Login()
logout = Logout()
home = Home()
post = Post()
security = Security()


@app.route("/login",
           methods=['GET', 'POST'])
def login_handler():
    if request.method == 'POST':
        return login.do_login(
            request.form.get('username'),
            request.form.get('password'))
    elif request.method == 'GET':
        return login.render_page()


@app.route("/signup",
           methods=['GET', 'POST'])
Exemplo n.º 6
0
 def __init__(self):
     self.datastore = Datastore()
     self.security = Security()
     self.home = Home()
     self.page_path = 'login.html'
Exemplo n.º 7
0
 def __init__(self):
     self.datastore = Datastore()
     self.security = Security()
     self.home = Home()
     self.login = Login()
     self.page_path = 'signup.html'