https://github.com/danindiana/bashcap
A bash cli telemetry capture client and server.
https://github.com/danindiana/bashcap
Last synced: 5 months ago
JSON representation
A bash cli telemetry capture client and server.
- Host: GitHub
- URL: https://github.com/danindiana/bashcap
- Owner: danindiana
- License: gpl-3.0
- Created: 2024-01-11T23:05:32.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-12T00:04:45.000Z (almost 2 years ago)
- Last Synced: 2025-01-09T06:37:45.880Z (12 months ago)
- Language: JavaScript
- Size: 266 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.txt
- License: LICENSE
Awesome Lists containing this project
README
```
# Bashcap Server
This is the main server file for the Bashcap application.
## Getting Started
To get started with this project, clone the repository and install the dependencies:
```
git clone
cd
npm install
```
## Running the Server
You can start the server with the following command:
```
node bashcap_server.js
```
The server will start and listen for requests.
## Contributing
If you want to contribute to this project, please create a new branch and submit a pull request.
## License
This project is licensed under the MIT License.
```
Please replace `` and `` with the actual URL of your repository and the directory where it's located.
This `README.txt` provides basic information about your project, including how to get started, how to run the server, how to contribute, and the license. You can add more sections as needed, like a section for the API endpoints, a section for the tests, or a section for the deployment.
This Node.js server is a basic implementation of a user authentication and email verification system. Here's a breakdown of what each part of the code does:
1. Importing Required Modules:
- The server uses the Express.js framework for handling HTTP requests.
- It also utilizes various libraries including `body-parser` for parsing JSON data in request bodies, `jsonwebtoken` for generating JSON Web Tokens (JWTs), `express-jwt` for JWT authentication middleware, `bcrypt` for password hashing and verification, `crypto` for generating random tokens, and `nodemailer` for sending emails.
2. Initializing the Express Application:
- An Express application is created by calling `express()`, and the server uses JSON body parsing middleware (`body-parser`) for handling incoming JSON data.
3. Defining Routes:
- There are several routes defined to handle various actions related to user registration, email verification, login, password reset, and basic server testing.
- The root route ("/") simply sends "Hello, world!" as a response.
4. Secret Key for JWT:
- A secret key (`SECRET_KEY`) is defined for JWT (JSON Web Token) generation and verification. It's used to sign and verify tokens.
5. Example User Database:
- An example user database (`users`) is stored in memory. It keeps track of user information, including their username, hashed password, email, and various tokens for verification and password reset.
6. Register Route ("/register"):
- This route handles user registration.
- It hashes the provided password using `bcrypt`, generates a random token, and creates a new user object with the provided data.
- The user is added to the `users` array.
- An email is sent to the user with a verification link.
7. Verify Email Route ("/verify-email/:token"):
- This route handles email verification.
- It searches for a user with a matching verification token in the `users` array and marks the user as verified if found.
8. Login Route ("/login"):
- This route handles user login.
- It checks if the provided username exists and verifies the password using `bcrypt`.
- If authentication is successful, it generates a JWT and sends it back as a JSON response.
9. Forgot Password Route ("/forgot"):
- This route handles password reset requests.
- It generates a random reset token, associates it with the user, and sends an email with a reset link.
10. Reset Password Route ("/reset/:token"):
- This route handles password reset.
- It validates the reset token and expiration time.
- If the token is valid, it updates the user's password with the new hashed password.
11. Server Listening:
- The server listens on port 3000 and logs a message when it starts.
This code provides a basic structure for user registration, email verification, login, and password reset functionalities. It's important to note that in a production environment, sensitive information such as secret keys and email credentials should be stored securely and not hard-coded in the source code as shown in this example. Additionally, security best practices should be followed to ensure the safety of user data and authentication processes.
To refactor the application to allow users to sign up/login and upload their Bash CLI telemetry while following best practices, you can follow these steps:
1. **Database Setup**:
- First, set up a database to store user information and telemetry data. You can use a database system like MongoDB, PostgreSQL, or MySQL for this purpose.
2. **User Model**:
- Create a user model that defines the structure of the user document in the database. This model should include fields like username, password (hashed), email, and any other relevant user data.
3. **Authentication Middleware**:
- Create authentication middleware that verifies JWT tokens for protected API routes. Use the `express-jwt` library or similar middleware for this purpose.
4. **User Registration (Sign Up)**:
- Implement a user registration endpoint, e.g., `/register`. The registration process should include:
- Validation of user input (username, password, email).
- Hashing the user's password securely using `bcrypt`.
- Storing the user's information (username, hashed password, email) in the database.
- Sending a verification email to the user's email address with a unique token (similar to the original code).
5. **Email Verification**:
- Implement an email verification endpoint, e.g., `/verify-email/:token`. When a user clicks the verification link from their email, this endpoint should:
- Verify the provided token.
- Mark the user's email as verified in the database.
- Redirect the user to a success page.
6. **User Login**:
- Implement a user login endpoint, e.g., `/login`. The login process should include:
- Validating user credentials (username and password).
- Checking if the user exists in the database and if the provided password matches the stored hashed password.
- If authentication is successful, generate a JWT token and return it to the user.
7. **Telemetry Data Upload**:
- Create an endpoint, e.g., `/upload-telemetry`, where authenticated users can upload their Bash CLI telemetry data.
- Protect this endpoint using the authentication middleware to ensure only authenticated users can access it.
- Implement logic to process and store the telemetry data in the database.
8. **JWT Token Generation and Refresh**:
- When a user logs in, generate a JWT token that includes user information (e.g., username) and set an expiration time.
- Optionally, implement token refresh functionality by issuing a new token with a new expiration time when the old token is about to expire.
9. **Password Reset**:
- Implement a password reset functionality similar to the original code. Allow users to request a password reset email with a unique token.
- Create an endpoint, e.g., `/reset-password/:token`, where users can reset their passwords by providing a new password and a valid reset token.
10. **Security Best Practices**:
- Implement security best practices such as salting and hashing passwords securely with `bcrypt`.
- Store sensitive information (e.g., secret keys, database credentials) in environment variables.
- Use HTTPS for secure communication.
- Implement rate limiting and authentication throttling to prevent abuse.
11. **Logging and Error Handling**:
- Implement proper logging and error handling throughout your application to track and handle errors gracefully.
12. **Testing**:
- Write unit tests and integration tests to ensure the security and functionality of your application.
13. **Deployment and Scaling**:
- Deploy your application to a production environment using a suitable hosting platform (e.g., AWS, Heroku, Azure).
- Consider scaling options as your user base grows.
By following these steps, you can refactor your application to provide a secure and reliable service for user signup/login and telemetry data upload while adhering to best practices in web application development.