Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #907 #914

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
users.db
venv
.idea
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn app:app
98 changes: 98 additions & 0 deletions Python/Hacking Scripts Website/Flask-Authentication/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from flask import Flask, render_template, request, url_for, redirect, flash, send_from_directory
from werkzeug.security import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_user, LoginManager, login_required, current_user, logout_user
import jinja2
import os

app = Flask(__name__)

app.config['SECRET_KEY'] = "secret"
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("DATABASE_URL", "sqlite:///users.db")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)


@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))


##Create table in data base
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
name = db.Column(db.String(1000))


# db.create_all only once
db.create_all()

@app.route('/')
def home():
return render_template("index.html", logged_in=current_user.is_authenticated)


@app.route('/register', methods=["GET", "POST"])
def register():
if request.method == "POST":
data = request.form
if User.query.filter_by(email=data["email"]).first():
flash("You are already registered,instead Login")
return redirect(url_for("login"))
# Using hashing and salting while creating password
hash_and_salted_password = generate_password_hash(
request.form.get('password'),
method='pbkdf2:sha256',
salt_length=8
)

new_user = User(
email=data["email"],
name=data["name"],
password=hash_and_salted_password
)

db.session.add(new_user)
db.session.commit()
login_user(new_user)
return redirect(url_for("secrets", name=new_user.name))

return render_template("register.html", logged_in=current_user.is_authenticated)


@app.route('/login', methods=["GET", "POST"])
def login():
if request.method == "POST":
data = request.form
email = data["email"]
password = data["password"]
user = User.query.filter_by(email=email).first()
# Using flask flash messages for errors
if not user:
flash('Please register first')
return redirect(url_for("login"))
elif not check_password_hash(user.password, password):
flash("Incorrect Password")
return redirect(url_for("login"))
else:
login_user(user)
return redirect(url_for("secrets", name=user.name))
return render_template("login.html", logged_in=current_user.is_authenticated)


@app.route('/secrets/<name>')
def secrets(name):
return render_template("secrets.html", name=name)


@app.route('/logout')
def logout():
return render_template("index.html")


if __name__ == "__main__":
app.run(debug=True)
4 changes: 4 additions & 0 deletions Python/Hacking Scripts Website/Flask-Authentication/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Flask Authentication Page
This app adds users to a website.It uses SQLAlchemy for managing the database(SQLite).
As it is deployed on heroku [here](https://flask-authenticate.herokuapp.com/) , it is using Postgres database.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
click==7.1.2
colorama==0.4.4
dominate==2.5.2
Flask==1.1.2
Flask-Bootstrap==3.3.7.1
Flask-Login==0.5.0
Flask-SQLAlchemy==2.4.4
Flask-WTF==0.14.3
greenlet==1.1.0
gunicorn==20.0.4
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
SQLAlchemy==1.3.20
visitor==0.1.3
Werkzeug==1.0.1
WTForms==2.3.3
click==7.1.2
colorama==0.4.4
dominate==2.5.2
Flask==1.1.2
Flask-Bootstrap==3.3.7.1
Flask-Login==0.5.0
Flask-SQLAlchemy==2.4.4
Flask-WTF==0.14.3
greenlet==1.1.0
gunicorn==20.0.4
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
psycopg2-binary==2.9.1
SQLAlchemy==1.3.20
visitor==0.1.3
Werkzeug==1.0.1
WTForms==2.3.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Flask Authentication</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css')}}">
</head>

<body>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/">IN-N-OUT</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="{{ url_for('home') }}">Home</a>
</li>
{% if not logged_in: %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('login') }}">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('register') }}">Register</a>
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('logout') }}">Logout</a>
</li>
</ul>


</div>
</nav>
{% block content %}
{% endblock %}
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}
{% block content %}

<div class="box">
<h1>Authentication Demo</h1>

<a href="{{ url_for('login') }}" class="btn btn-primary btn-sm btn-dark">Login</a>
<a href="{{ url_for('register') }}" class="btn btn-primary btn-sm btn-dark">Register</a>

</div>

{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends "base.html" %}
{% block content %}

<div class="box">
<h1>Login</h1>
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<p>{{ message }}</p>
{% endfor %}
{% endif %}
{% endwith %}
<form action="{{ url_for('login') }}" method="post">
<input type="text" name="email" placeholder="Email" required="required"/>
<input type="password" name="password" placeholder="Password" required="required"/>
<button type="submit" class="btn btn-primary btn-sm btn-dark" >Login</button>
</form>
</div>

{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base.html" %}
{% block content %}

<div class="box">
<h1>Register</h1>
<form action="{{ url_for('register') }}" method="post">
<input type="text" name="name" placeholder="Name" required="required" />
<input type="email" name="email" placeholder="Email" required="required" />
<input type="password" name="password" placeholder="Password" required="required" />
<button type="submit" class="btn btn-primary btn-sm btn-dark">Sign up</button>
</form>
</div>

{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "base.html" %}
{% block content %}

<div class="container">
<h1 class="title">Welcome, {{ name }}</h1>
</div>
{% endblock %}