Commit 97794f84 authored by impfundev's avatar impfundev

feat: bcrypt hasher

parent e5d825d5
import jwt import jwt
import bcrypt
from django.conf import settings from django.conf import settings
def create_auth_session(request, payload): def create_auth_session(request, payload):
token = jwt.encode(payload, settings.JWT_SECRET, algorithm="HS256") token = jwt.encode(payload, settings.JWT_SECRET, algorithm="HS256")
request.session["auth_session"] = token request.session["auth_session"] = token
class Hasher:
def encode(password: str):
hashed_password = bcrypt.hashpw(
password.encode("utf-8"), bcrypt.gensalt(rounds=8)
)
return hashed_password
def verify(password: str, encoded: str):
hashed_password = encoded[2:].replace("'", "").encode("utf-8")
password_encode = password.encode("utf-8")
print(hashed_password)
print(password_encode)
is_verified = bcrypt.checkpw(
password=password_encode, hashed_password=hashed_password
)
return is_verified
...@@ -5,7 +5,7 @@ from django.shortcuts import render ...@@ -5,7 +5,7 @@ from django.shortcuts import render
from authentications.forms import LoginForm, SignUpForm, ForgotPassword from authentications.forms import LoginForm, SignUpForm, ForgotPassword
from librarians.models import Librarians, LoginHistory from librarians.models import Librarians, LoginHistory
from authentications.utils import create_auth_session from authentications.utils import create_auth_session, Hasher
class AuthView(TemplateView): class AuthView(TemplateView):
...@@ -15,16 +15,21 @@ class AuthView(TemplateView): ...@@ -15,16 +15,21 @@ class AuthView(TemplateView):
if request.method == "POST": if request.method == "POST":
form = LoginForm(request.POST) form = LoginForm(request.POST)
if form.is_valid(): if form.is_valid():
account = librarian.filter( account = librarian.filter(email=form.data["email"])
email=form.data["email"], password=form.data["password"] password = form.data["password"]
)
if account.exists(): if account.exists():
librarian = librarian.get( librarian = librarian.get(email=form.data["email"])
email=form.data["email"],
password=form.data["password"], verified = Hasher.verify(
password=password, encoded=librarian.password
) )
if not verified:
context["error_message"] = (
"Password invalid, please enter valid data or Sign Up first"
)
else:
expiration_time = datetime.now() + timedelta(hours=2) expiration_time = datetime.now() + timedelta(hours=2)
payload = { payload = {
"exp": expiration_time.timestamp(), "exp": expiration_time.timestamp(),
...@@ -39,7 +44,7 @@ class AuthView(TemplateView): ...@@ -39,7 +44,7 @@ class AuthView(TemplateView):
return HttpResponseRedirect("/dashboard/") return HttpResponseRedirect("/dashboard/")
else: else:
context["error_message"] = ( context["error_message"] = (
"Email or Password invalid, please enter valid data or Sign Up first" "Email invalid, please enter valid data or Sign Up first"
) )
else: else:
form = LoginForm() form = LoginForm()
...@@ -59,15 +64,17 @@ class AuthView(TemplateView): ...@@ -59,15 +64,17 @@ class AuthView(TemplateView):
"Email was already exist, please use different email" "Email was already exist, please use different email"
) )
else: else:
password = form.data["password"]
hashed_password = Hasher.encode(password=password)
librarian.create( librarian.create(
name=form.data["name"], name=form.data["name"],
email=form.data["email"], email=form.data["email"],
password=form.data["password"], password=hashed_password,
) )
new_librarian = librarian.get( new_librarian = librarian.get(
name=form.data["name"], name=form.data["name"],
email=form.data["email"], email=form.data["email"],
password=form.data["password"],
) )
expiration_time = datetime.now() + timedelta(minutes=30) expiration_time = datetime.now() + timedelta(minutes=30)
......
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">Previous</a></li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item"><a class="page-link" href="#">Next</a></li>
</ul>
</nav>
from authentications.utils import create_auth_session, Hasher
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.core.cache import cache from django.core.cache import cache
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
...@@ -18,8 +19,9 @@ def index(request): ...@@ -18,8 +19,9 @@ def index(request):
name = form.data["name"] name = form.data["name"]
email = form.data["email"] email = form.data["email"]
password = form.data["password"] password = form.data["password"]
hashed_password = Hasher.encode(password=password)
Librarians.objects.create(name=name, email=email, password=password) Librarians.objects.create(name=name, email=email, password=hashed_password)
cache.clear() cache.clear()
if request.method == "GET": if request.method == "GET":
...@@ -52,7 +54,6 @@ def update(request, id): ...@@ -52,7 +54,6 @@ def update(request, id):
initial = { initial = {
"name": librarian.name, "name": librarian.name,
"email": librarian.email, "email": librarian.email,
"password": librarian.password,
} }
form = LibrarianForm(request.POST or None, initial=initial) form = LibrarianForm(request.POST or None, initial=initial)
...@@ -61,10 +62,14 @@ def update(request, id): ...@@ -61,10 +62,14 @@ def update(request, id):
name = form.data["name"] name = form.data["name"]
email = form.data["email"] email = form.data["email"]
password = form.data["password"] password = form.data["password"]
hashed_password = Hasher.encode(password=password)
librarian = Librarians.objects.filter(id=id) librarian = Librarians.objects.filter(id=id)
librarian.update( librarian.update(
name=name, email=email, password=password, updated_at=datetime.now() name=name,
email=email,
password=hashed_password,
updated_at=datetime.now(),
) )
cache.clear() cache.clear()
return HttpResponseRedirect("/dashboard/librarians") return HttpResponseRedirect("/dashboard/librarians")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment