🔒💡 Building a Password Generator Web Application with Flask: Secure Your Data! 🔐🚀
Tags: webdev, python, javascript, security
Published: 2023-05-30 11:42:51

In today's digital age, where our lives are intricately woven into the fabric of the internet, securing our personal information has become more crucial than ever. Yet, many of us still have the bad habit of using weak passwords that leave us vulnerable to cyber threats. From simple phrases and birthdates to easily guessable sequences, these weak passwords act as an open invitation for hackers to infiltrate our online accounts.

But fear not! In this post, we'll embark on a journey to break this bad habit by building a powerful and secure password generator web application using Flask. With Flask, a versatile Python web framework, we'll create an application that can generate strong passwords tailored to our preferences.

So, let's dive into the world of Flask and develop a password generator web application that will help us bid farewell to the era of weak and easily compromised passwords. Together, we'll take a step towards fortifying our online security and embracing a safer digital future.

## Flask Password Generator

I wanted to share with you a simple Flask code snippet that I wrote for generating passwords. It uses the Flask framework to create a web application with two routes: the home route ("/") and the password route ("/password"). Let's dive into the code:

```python
from flask import Flask, render_template, request, jsonify
from passwordGenerator import password_generator

app = Flask(__name__)

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

@app.route("/password", methods=["POST"])
def password():
    try:
        symbols = True if request.form["symbols"] == "true" else False
    except:
        symbols = False
    try:
        length = int(request.form["length"])
        if length > 20:
            length = 20
        elif length < 8:
            length = 8
    except:
        length = 8
    password = password_generator(length, symbols)
    data = {"password": password}
    return jsonify(data)

if __name__ == "__main__":
    app.run()


```
Let's break down the code to understand what it does.

First, we import the necessary modules: Flask for creating the web application, render_template for rendering HTML templates, request for handling incoming requests, and jsonify for converting Python objects to JSON format.

We then create an instance of the Flask class and assign it to the app variable.

The @app.route("/") decorator defines the home route, which renders an HTML template called "index.html" using the render_template function. You can customize this template to match your desired homepage.

The @app.route("/password", methods=["POST"]) decorator defines the password route, which accepts POST requests. Inside this route, we retrieve the form data sent by the client. If the "symbols" field is set to "true," we set the symbols variable to True; otherwise, we set it to False. If any error occurs during this process, we default symbols to False.

Next, we retrieve the value of the "length" field from the form data and convert it to an integer. We then perform some checks to ensure the password length is within a valid range (8 to 20 characters). If the length provided is outside this range, we adjust it accordingly.

After processing the form data, we generate a password using the password_generator function from a module called passwordGenerator.

And this is the code below:

```python
from string import ascii_letters
p_symbols = "#!\{}/+&@._§%?<>£;,:"
import random

def password_generator(length, symbols):
    if symbols:
        characters = ascii_letters + p_symbols
        password = "".join([random.choice(characters) for _ in range(length)])
        return password
    else:
        password = "".join([random.choice(ascii_letters) for _ in range(length)])
        return password


```
This code defines a function called password_generator that takes two parameters: length (the desired length of the password) and symbols (a boolean indicating whether to include symbols in the generated password).

Inside the function, if symbols is True, we concatenate the ascii_letters (which contains all uppercase and lowercase letters) with a string p_symbols that contains additional symbols. You can modify p_symbols to include the symbols you desire.

We then use a list comprehension and random.choice to select random characters from the combined set of characters (either including symbols or just letters) for the specified length. The resulting characters are joined together to form the password, which is then returned.

If symbols is False, we generate a password using only the ascii_letters characters.

That's it! This password generator module is used in the Flask code to generate passwords based on the provided length and symbol preference. Feel free to modify the symbols or add additional logic to suit your needs.

in The password route We create a dictionary called data and store the generated password inside it. We then return this dictionary as a JSON response using the jsonify function.

Finally, we have a conditional check if __name__ == "__main__": to ensure that the web application is run only when the script is executed directly (not imported as a module).

To run the Flask application, you can execute the script, and Flask will start a development server on your local machine.

source code [here](https://github.com/y3script/FlaskPasswordGenerator)

That's it! Feel free to modify and customize this code according to your requirements. Happy coding! 😄🚀