Authentication
Repository Informations |
Open Tasks |
 |
 |
 |
 |
 |
 |
Copyright |
 |
 |
π Project created to train authentication issues, password recovery, login with third parties, permissions, etc.
π Table of Contents
===================
π±βπ Features
π» Dependencies and Environment
π Installing
π§Ή Formatting the Code
π§ͺ Testing
β Using
π License
π· Author
===================
π±βπ Features
π§Ύ Documentation
- Migrations
- BDD (Behavior Driven Development) to use cases
- Gin Swagger to routes
- Concept of semantic versioning with tags and releases
β General
-
CI/CD process with github actions to perform code formatting check (golangci-lint), build and run automated tests
-
Test setup with TestContainers:
1- For each test switch/file that uses the database, a Postgres container is created just for testing
2- Then all migrations are run in this container
3- Before each test, a script is run to clean all records from the tables
4- After executing the switch, the container is terminated
-
Common middlewares to routes: block inactives users, rate limiter, timeout, only https, jwt signature checker for some routes, admin only for some routes, check 2fa when user has the 2fa activated
π Use Cases
- login_2fa: With the normal JWT and the code generated by google authenticator it is possible to return the valid JWT for users who have 2FA activated
- active_2fa (need to be logged in): returns qrcode to synchronize with google authenticator
- desactive_2fa (need to be logged in)
- notify the user by email: when your password is changed and when your email is verified
- verify_change_email_code (need to be logged in): Verifies that the code is correct and not expired
- send_change_email_code (need to be logged in): Saves a code and an expiration time (5 minutes) in the database and sends an email with the code
- change_email (need to be logged in): It is necessary to use a unique code that is sent to the current email
- change_password_in_recovery: Verifies that the code is correct and not expired and change the password to the new password
- verify_password_recovery_code: Verifies that the code is correct and not expired
- send_password_recovery_code: Saves a code and an expiration time (5 minutes) in the database and sends an email with the code
- verify_email: Verifies that the code is correct and not expired and updates the email as verified
- send_email_verification_code: Saves a code and an expiration time (5 minutes) in the database and sends an email with the code
- admin elevation: you can promote anothers users to admin, delete users, inative user, find user information, list all users, list all logs, list all logs of a user
- log: all operations have log persistence with information such as: user id, operation code, method, route, success (true/false), ip and timestamp
- delete_user (need to be logged in): delete by id or e-mail
- find_user (need to be logged in): find by id or e-mail
- change_password (need to be logged in)
- login: With JWT
- create_user: Do not allow repeated emails and weak passwords
π‘ Technical Decisions
- Clean Code
- Scream Architecture
- Commit Lint
- SOLID
- Clean Architecture
π» Dependencies and Environment
My dependencies and versions
Go: go version go1.22.0 windows/amd64
Docker: Docker version 25.0.3, build 4debf41
docker-compose: Docker Compose version v2.24.5-desktop.1
π Installing
1- To install the dependencies you can run the following command in the root folder:
$ go mod tidy
$ go mod download
OBS: We have the development .env file committed to the project, but you can change it as you see fit
2- (If you already have a PostgresSQL instance, you can skip this part) You will need a postgresSQL instance, we have a docker-compose ready to create a container, you can run the following command in the root folder
$ docker-compose up -d
3- Up the migrations: Naturally, when running the server it will execute the migrations, but they can be executed by code with (change pg url to yours):
$ migrate -database postgres://myuser:mypassword@localhost:5432/mydatabase?sslmode=disable -path src/db/migrations up
To check the code format you will need instal golangci-lint and run the following command in the root folder:
$ golangci-lint run
π§ͺ Testing
To exec all the tests run the following command in the root folder:
$ go test -p 1 ./src/...
You can add the "-v" flag to see detailed output
$ go test -v -p 1 ./src/...
β Using
First, check the dependencies and the installation process:
Going to root folder and exec:
$ go run .\main.go
Now you can open http://localhost:8080 with your browser to see the result.
You can see the routes in Local Swagger Documentation or you can see the routes documentation in 'rest' folder, this files using de REST Client extension of VSCode, but you can export it any way you want
You can create new migrations using the command
migrate create -ext sql -dir src/db/migrations -seq MIGRATION_NAME
π License
Projeto contΓͺm GNU GENERAL PUBLIC LICENSE.
π· Author
Made by Glener Pizzolato! π
