Part 14: Tutorial Mengenkripsi Password dengan FastAPI dan Passlib – Belajar FastAPI untuk Pemula

Posted by

In this tutorial, we will learn how to encrypt passwords using Passlib in FastAPI. Passlib is a Python library that provides a secure way to hash passwords. By encrypting passwords, we can store them securely in our database and ensure that user passwords are not stored in plain text.

Step 1: Install Passlib

First, we need to install Passlib using pip. Open your terminal or command prompt and run the following command:

pip install passlib

Step 2: Create a Password Encryptor Service

Next, we will create a password encryptor service in FastAPI. Let’s create a new file called password_encryptor.py and add the following code:

from passlib.context import CryptContext

SECRET = "mysecret"

class PasswordEncryptor:
    def __init__(self):
        self.pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

    def encrypt_password(self, password: str) -> str:
        return self.pwd_context.hash(password)

    def verify_password(self, plain_password: str, hashed_password: str) -> bool:
        return self.pwd_context.verify(plain_password, hashed_password)

In this code, we create a PasswordEncryptor class with methods to encrypt and verify passwords using the bcrypt hashing algorithm. We also set a secret key for additional security.

Step 3: Test the Password Encryptor Service

Now, let’s test our password encryptor service. Create a new file called main.py and add the following code:

from fastapi import FastAPI
from password_encryptor import PasswordEncryptor

app = FastAPI()

pwd_encryptor = PasswordEncryptor()

@app.get("/encrypt_password")
def encrypt_password(password: str):
    encrypted_password = pwd_encryptor.encrypt_password(password)
    return {"encrypted_password": encrypted_password}

@app.get("/verify_password")
def verify_password(plain_password: str, hashed_password: str):
    is_valid = pwd_encryptor.verify_password(plain_password, hashed_password)
    return {"is_valid": is_valid}

In this code, we create two API endpoints: /encrypt_password to encrypt a password and /verify_password to verify a password. We use our PasswordEncryptor class to handle the encryption and verification of passwords.

Step 4: Run the FastAPI Server

To run the FastAPI server, open your terminal or command prompt and run the following command:

uvicorn main:app --reload

This will start the FastAPI server, and you can access the API endpoints at http://localhost:8000/encrypt_password and http://localhost:8000/verify_password.

Step 5: Test the Password Encryptor Service

Now, you can test the password encryptor service by making HTTP requests to the API endpoints. You can use tools like Postman or curl to send requests to the server.

For example, to encrypt a password, you can send a GET request to http://localhost:8000/encrypt_password?password=mypassword and the server will return the encrypted password. To verify a password, send a GET request to http://localhost:8000/verify_password?plain_password=mypassword&hashed_password=encrypted_password and the server will return whether the password is valid.

Congratulations! You have successfully learned how to encrypt passwords using Passlib in FastAPI. Passlib provides a secure way to hash passwords and protect user data. Feel free to explore more features of Passlib and FastAPI to enhance the security of your applications.