├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.yml │ └── feature_request.yml ├── PULL_REQUEST_TEMPLATE │ └── pull_request_template.md └── workflows │ └── CI.yaml ├── .gitignore ├── README.md ├── cmd └── http │ └── main.go ├── config ├── env.example.json └── env.json ├── deploy └── Dockerfile ├── docs ├── docs.go ├── swagger.json └── swagger.yaml ├── go.mod ├── go.sum ├── hooks └── pre-commit.sh ├── internal └── user │ ├── application │ ├── application.go │ ├── create.go │ ├── create_test.go │ ├── login.go │ └── login_test.go │ ├── domain │ ├── models │ │ ├── auth_request.go │ │ ├── auth_response.go │ │ ├── register_request.go │ │ └── user.go │ └── ports │ │ ├── user_application.go │ │ ├── user_handlers.go │ │ └── user_repository.go │ └── infrastructure │ ├── handlers │ ├── fiber.go │ ├── login.go │ ├── login_test.go │ ├── register.go │ └── register_test.go │ └── repositories │ ├── find_by_credentials.go │ ├── find_by_credentials_test.go │ ├── mongo.go │ ├── save.go │ └── save_test.go ├── logs └── log.csv ├── main.go ├── makefile ├── pkg ├── config │ ├── application │ │ ├── application.go │ │ ├── config.go │ │ └── get_config.go │ ├── domain │ │ ├── models │ │ │ ├── app.go │ │ │ ├── config.go │ │ │ ├── database.go │ │ │ ├── jwt.go │ │ │ └── server.go │ │ └── ports │ │ │ └── config_ports.go │ └── infrastructure │ │ └── repositories │ │ └── json.go ├── custom-errors │ └── errors.go ├── factories │ └── factories.go ├── logger │ ├── application │ │ └── application.go │ ├── domain │ │ ├── models │ │ │ └── log.go │ │ └── ports │ │ │ ├── logger_application.go │ │ │ └── logger_repository.go │ └── infrastructure │ │ └── repositories │ │ └── file.go ├── middleware │ ├── application │ │ └── application.go │ ├── domain │ │ └── ports │ │ │ ├── mdl_application.go │ │ │ ├── mdl_handlers.go │ │ │ └── mdl_repository.go │ └── infrastructure │ │ ├── handlers │ │ └── fiber.go │ │ └── repositories │ │ └── mongo.go ├── mocks │ ├── mock_config_application.go │ ├── mock_config_repository.go │ ├── mock_logger_application.go │ ├── mock_logger_repository.go │ ├── mock_mdl_application.go │ ├── mock_mdl_handlers.go │ ├── mock_mdl_repository.go │ ├── mock_user_application.go │ ├── mock_user_handlers.go │ ├── mock_user_repository.go │ ├── mock_validator_application.go │ └── mock_validator_evaluable.go ├── server │ ├── server.go │ └── server_test.go ├── utils │ ├── password_validation.go │ └── password_validation_test.go └── validator │ ├── application │ ├── validator.go │ └── validator_test.go │ └── domain │ └── ports │ └── validator_ports.go └── scripts ├── build-container.sh ├── generate-coverage-report.sh ├── generate-docs.sh ├── generate-mocks.sh ├── run-container.sh ├── run-lint.sh ├── run-test.sh └── run.sh /.github/ISSUE_TEMPLATE/bug_report.yml: -------------------------------------------------------------------------------- 1 | name: 🐛Bug Report 2 | description: File a bug report here 3 | title: "[BUG]: " 4 | labels: ["bug"] 5 | assignees: ["solrac97gr"] 6 | body: 7 | - type: markdown 8 | attributes: 9 | value: | 10 | Thanks for taking the time to fill out this bug report 🤗 11 | Make sure there aren't any open/closed issues for this topic 😃 12 | 13 | - type: textarea 14 | id: bug-description 15 | attributes: 16 | label: Description of the bug 17 | description: Give us a brief description of what happened and what should have happened 18 | validations: 19 | required: true 20 | 21 | - type: textarea 22 | id: steps-to-reproduce 23 | attributes: 24 | label: Steps To Reproduce 25 | description: Steps to reproduce the behavior. 26 | placeholder: | 27 | 1. Go to '...' 28 | 2. Click on '...' 29 | 3. Scroll down to '...' 30 | 4. See error 31 | validations: 32 | required: true 33 | - type: textarea 34 | id: additional-information 35 | attributes: 36 | label: Additional Information 37 | description: | 38 | Provide any additional information such as logs, screenshots, likes, scenarios in which the bug occurs so that it facilitates resolving the issue. -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.yml: -------------------------------------------------------------------------------- 1 | name: ✨Feature Request 2 | description: Request a new feature or enhancement 3 | labels: ["enhancement"] 4 | title: "[FEAT]: " 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | Please make sure this feature request hasn't been already submitted by someone by looking through other open/closed issues 10 | 11 | - type: textarea 12 | id: description 13 | attributes: 14 | label: Description 15 | description: Give us a brief description of the feature or enhancement you would like 16 | validations: 17 | required: true 18 | 19 | - type: textarea 20 | id: additional-information 21 | attributes: 22 | label: Additional Information 23 | description: Give us some additional information on the feature request like proposed solutions, links, screenshots, etc. -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | 3 | Describe the changes related with this PR 4 | 5 | 6 | ## Type of change 7 | 8 | - [ ] Bug fix (non-breaking change which fixes an issue) 9 | - [x] New feature (non-breaking change which adds functionality) 10 | - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) 11 | - [ ] This change requires a documentation update 12 | 13 | 14 | ## How Has This Been Tested? 15 | 16 | Describe how you test this new changes 17 | 18 | 19 | ## A self-code review has been performed. 20 | 21 | Checklist: 22 | 23 | - [ ] My code follows the style guidelines of this project 24 | - [ ] I have performed a self-review of my own code 25 | - [ ] I have commented my code, particularly in hard-to-understand areas 26 | - [ ] I have made corresponding changes to the documentation 27 | - [ ] My changes generate no new warnings 28 | - [ ] I have added tests that prove my fix is effective or that my feature works 29 | - [ ] New and existing unit tests pass locally with my changes 30 | - [ ] Any dependent changes have been merged and published in downstream modules 31 | -------------------------------------------------------------------------------- /.github/workflows/CI.yaml: -------------------------------------------------------------------------------- 1 | name: Go 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | 11 | build: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - name: Set up Go 17 | uses: actions/setup-go@v2 18 | with: 19 | go-version: 1.18 20 | 21 | - name: Run golangci-lint 22 | uses: golangci/golangci-lint-action@v3.2.0 23 | 24 | - name: Build 25 | run: go build -v ./... 26 | 27 | - name: Test 28 | run: go test -v ./... -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | .vscode 4 | ./config/env.json 5 | main -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go JWT Auth (Fiber & Mongo) 2 | 3 | Buy Me A Coffee 4 | 5 | Template create for use as CookieCutter for my Golang projects. 6 | The hardest part for start a project for me was to chose Stack and create the initial login and integrations 7 | like logger, database, etc. So I decided to create a template with everything already working. 8 | 9 | All the project is based in interfaces that mean you can implement your own logic and use it in the project. 10 | example: you can use different database like Postgres, MySQL, etc. Just implement the interface and use it. 11 | 12 | ## Stack 13 | - Router: [Fiber 🚀](https://gofiber.io) 14 | - Database: [Mongo 💾](https://www.mongodb.com/docs/drivers/go/current/) 15 | - Doc: [Swagger 📄](https://github.com/swaggo/swag) 16 | - Logger: [Zap ⚡](https://github.com/uber-go/zap) 17 | - Mocks: [gomock 💀](https://github.com/uber-go/mock) 18 | - Deploy: [Docker 🐳](https://www.docker.com) 19 | - CI: [Github Actions 🐙](https://docs.github.com/en/actions) 20 | 21 | ## Before the execution 22 | - Modify the file `./config/env.json` with your parameters 23 | - Install gomock `go install go.uber.org/mock/mockgen@latest` 24 | - Install swag `go install github.com/swaggo/swag/cmd/swag@latest` 25 | 26 | ## Routes 27 | You can also check in the route /swagger/index.html after run the project 🤩. 28 | 29 | Note 📝: For add a private route you need to create it in the private router `v1Private` 30 | inside the pkg/server/server.go file. 31 | 32 | | Name | Path | Method | Description | Request | Response | 33 | |---------------|------------------|--------|----------------------|----------------|----------| 34 | | Register | /api/v1/register | POST | Create a new user | email,password | | 35 | | Login | /api/v1/login | POST | Login a user | email,password | token | 36 | | Metrics | /metrics | GET | Monitor for your API | | html | 37 | | Documentation | /docs | GET | Documentation | | html | 38 | 39 | ## How to use 40 | For this example we will make suppose that you want to create endpoints for Blog Posts. 41 | 1. Create a new folder inside the internal folder with the name of your entity. In this case `post`. 42 | 2. Create tree folders inside the entity folder: `application`, `domain` and `infrastructure`. 43 | 3. Create two folders inside the domain folder: `ports` and `model`. 44 | 4. Create a file inside the model folder with the name of your entity. In this case `post.go` and define your struct `Post`. 45 | ```go 46 | package model 47 | 48 | import "time" 49 | 50 | type Post struct { 51 | ID string `json:"id" bson:"_id"` 52 | Title string `json:"title" bson:"title"` 53 | Content string `json:"content" bson:"content"` 54 | CreatedAt time.Time `json:"created_at" bson:"created_at"` 55 | UpdatedAt time.Time `json:"updated_at" bson:"updated_at"` 56 | } 57 | ``` 58 | 59 | 5. Create 3 files inside the ports folder: `repository.go`, `handlers.go` and `application.go`. 60 | 6. Define your interfaces inside the `repository.go`,`handlers.go` and `application.go` file. 61 | ```go 62 | package ports 63 | 64 | import "github.com/your_user/your_project/internal/post/domain/model" 65 | 66 | type PostRepository interface { 67 | Create(post *model.Post) error 68 | FindAll() ([]*model.Post, error) 69 | FindByID(id string) (*model.Post, error) 70 | Update(post *model.Post) error 71 | Delete(id string) error 72 | } 73 | ``` 74 | 7. Modify the `scripts/generate-mocks.sh` file and add your three new interfaces. 75 | ```sh 76 | mockgen -destination=pkg/mocks/mock_post_application.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/internal/post/domain/ports PostApplication && 77 | mockgen -destination=pkg/mocks/mock_post_repository.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/internal/post/domain/ports PostRepository && 78 | mockgen -destination=pkg/mocks/mock_post_handlers.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/internal/post/domain/ports PostHandlers 79 | ``` 80 | 8. Run the `scripts/generate-mocks.sh` file. 81 | 9. Now is time for implement your interfaces. Create two folders inside the `infrastructure` folder with the name of `repositories` and `handlers`. 82 | 10. Create a file inside the `repositories` folder with the name of your interface implementation. In this case `mongo.go` and implement the `PostRepository` interface. 83 | ```go 84 | package repositories 85 | 86 | type MongoPostRepository struct { 87 | db *mongo.Database 88 | logger logger.LoggerApplication 89 | configurator config.ConfigApplication 90 | } 91 | 92 | func NewMongoPostRepository(db *mongo.Database) *MongoPostRepository { 93 | return &MongoPostRepository{db: db} 94 | } 95 | 96 | func (m *MongoPostRepository) Create(post *model.Post) error { 97 | _, err := m.db.Collection("posts").InsertOne(context.Background(), post) 98 | if err != nil { 99 | m.logger.Error("Error creating post", err) 100 | return err 101 | } 102 | return nil 103 | } 104 | . 105 | . 106 | . 107 | ``` 108 | 11. Create a file inside the `handlers` folder with the name of your interface implementation. In this case `http.go` and implement the `PostHandler` interface. 109 | ```go 110 | package handlers 111 | 112 | type HTTPPostHandler struct { 113 | postApplication ports.PostApplication 114 | logger logger.LoggerApplication 115 | validator validator.ValidatorApplication 116 | } 117 | 118 | func NewHTTPPostHandler(postApplication ports.PostApplication) *HTTPPostHandler { 119 | return &HTTPPostHandler{postApplication: postApplication} 120 | } 121 | 122 | func (h *HTTPPostHandler) CreatePost(c *fiber.Ctx) error { 123 | post := &model.Post{} 124 | if err := c.BodyParser(post); err != nil { 125 | h.logger.Error("Error parsing post", err) 126 | return c.Status(http.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) 127 | } 128 | if err := h.postApplication.Create(post); err != nil { 129 | h.logger.Error("Error creating post", err) 130 | return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()}) 131 | } 132 | return c.Status(http.StatusCreated).JSON(fiber.Map{"message": "Post created successfully"}) 133 | } 134 | . 135 | . 136 | . 137 | ``` 138 | 12. Add your handlers to new routes depends on if it's public or private. In this case we will add it to the private routes in file `pkg/server/server.go` . 139 | ```go 140 | v1Private.Post("/posts", h.postHandler.CreatePost) 141 | ``` 142 | 13. If you want to add it to the swagger documentation view use comment with special annotation for your handlers. 143 | ```go 144 | // @Summary Create a new post 145 | // @Description Create a new post 146 | // @Tags posts 147 | // @Accept json 148 | // @Produce json 149 | // @Param post body model.Post true "Post" 150 | // @Success 201 {object} fiber.Map 151 | // @Failure 400 {object} fiber.Map 152 | // @Failure 500 {object} fiber.Map 153 | // @Router /posts [post] 154 | func (h *HTTPPostHandler) CreatePost(c *fiber.Ctx) error { 155 | . 156 | . 157 | . 158 | } 159 | ``` 160 | 14. Generate the swagger documentation with the command `/scripts/generate-docs.sh`. 161 | 15. Run the project with the command `go run cmd/http/main.go` or with the script `scripts/run.sh`. 162 | 163 | ## Considerations 164 | - The `scripts/generate-mocks.sh` file is used to generate the mocks of the interfaces. 165 | - The `scripts/generate-docs.sh` file is used to generate the swagger documentation. 166 | - The `scripts/run.sh` file is used to run the project. 167 | - The `scripts/run-tests.sh` file is used to run the tests. 168 | - The `scripts/run-tests-coverage.sh` file is used to run the tests with coverage. 169 | - For avoid create users with same mail, make the mail field unique in the database (mongo index in this case). 170 | 171 | ## License 172 | [Apache License 2.0]() 173 | -------------------------------------------------------------------------------- /cmd/http/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/docs" 5 | "github.com/solrac97gr/go-jwt-auth/pkg/factories" 6 | "github.com/solrac97gr/go-jwt-auth/pkg/server" 7 | ) 8 | 9 | // @contact.name Carlos García 10 | // @contact.email cgarciarosales97@gmail.com 11 | // @license.name Apache 2.0 12 | // @license.url http://www.apache.org/licenses/LICENSE-2.0.html 13 | func main() { 14 | factory := factories.NewFactory( 15 | "logs/log.csv", 16 | "./config/env.json", 17 | ) 18 | 19 | // Create and Share the dependencies throw the factory [Only one instance] 20 | factory.InitializeValidator() 21 | configurator := factory.InitializeConfigurator() 22 | factory.InitializeLogger() 23 | factory.InitializeMongoDB() 24 | 25 | middlewares := factory.BuildMiddlewaresHandlers() 26 | usersHandlers := factory.BuildUserHandlers() 27 | 28 | server := server.NewServer(middlewares, usersHandlers, configurator) 29 | 30 | config, err := configurator.GetConfig() 31 | if err != nil { 32 | panic(err) 33 | } 34 | 35 | docs.SwaggerInfo.Title = config.App.Name 36 | docs.SwaggerInfo.Description = config.App.Description 37 | docs.SwaggerInfo.Version = config.App.Version 38 | docs.SwaggerInfo.Host = config.App.Host 39 | docs.SwaggerInfo.BasePath = config.App.BaseURL 40 | 41 | err = server.Run(config.Server.Port) 42 | if err != nil { 43 | panic(err) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /config/env.example.json: -------------------------------------------------------------------------------- 1 | { 2 | "database":{ 3 | "url": "mongodb://localhost:27017", 4 | "name": "go-jwt-db" 5 | }, 6 | "server":{ 7 | "port": "5050" 8 | }, 9 | "app": { 10 | "name": "My App", 11 | "description": "My App Description", 12 | "host": "localhost:5050", 13 | "base_url": "/api/v1", 14 | "version": "0.0.1" 15 | }, 16 | "jwt": { 17 | "secret": "secret", 18 | "days_of_expiration": 1 19 | } 20 | } -------------------------------------------------------------------------------- /config/env.json: -------------------------------------------------------------------------------- 1 | { 2 | "database":{ 3 | "url": "mongodb://localhost:27017", 4 | "name": "go-jwt-db" 5 | }, 6 | "server":{ 7 | "port": "5050" 8 | }, 9 | "app": { 10 | "name": "My App", 11 | "description": "My App Description", 12 | "host": "localhost:5050", 13 | "base_url": "/api/v1", 14 | "version": "0.0.1" 15 | }, 16 | "jwt": { 17 | "secret": "secret", 18 | "days_of_expiration": 1 19 | } 20 | } -------------------------------------------------------------------------------- /deploy/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.18-alpine as build 2 | RUN mkdir /app 3 | ADD . /app/ 4 | WORKDIR /app 5 | COPY ./go.mod . 6 | COPY ./go.sum . 7 | ENV GOPROXY https://proxy.golang.org,direct 8 | RUN go mod download 9 | ENV CGO_ENABLED=0 10 | RUN GOOS=linux go build -o main ./cmd/http/main.go 11 | 12 | FROM scratch as serve 13 | WORKDIR /app 14 | COPY --from=build /app/config/env.json ./config/env.json 15 | COPY --from=build /app/main . 16 | CMD ["/app/main"] -------------------------------------------------------------------------------- /docs/docs.go: -------------------------------------------------------------------------------- 1 | // Code generated by swaggo/swag. DO NOT EDIT. 2 | 3 | package docs 4 | 5 | import "github.com/swaggo/swag" 6 | 7 | const docTemplate = `{ 8 | "schemes": {{ marshal .Schemes }}, 9 | "swagger": "2.0", 10 | "info": { 11 | "description": "{{escape .Description}}", 12 | "title": "{{.Title}}", 13 | "contact": { 14 | "name": "Carlos García", 15 | "email": "cgarciarosales97@gmail.com" 16 | }, 17 | "license": { 18 | "name": "Apache 2.0", 19 | "url": "http://www.apache.org/licenses/LICENSE-2.0.html" 20 | }, 21 | "version": "{{.Version}}" 22 | }, 23 | "host": "{{.Host}}", 24 | "basePath": "{{.BasePath}}", 25 | "paths": { 26 | "/login": { 27 | "post": { 28 | "description": "Login a user", 29 | "consumes": [ 30 | "application/json" 31 | ], 32 | "produces": [ 33 | "application/json" 34 | ], 35 | "tags": [ 36 | "User" 37 | ], 38 | "summary": "Login a user", 39 | "parameters": [ 40 | { 41 | "description": "Register", 42 | "name": "Body", 43 | "in": "body", 44 | "required": true, 45 | "schema": { 46 | "$ref": "#/definitions/models.AuthRequest" 47 | } 48 | } 49 | ], 50 | "responses": { 51 | "200": { 52 | "description": "OK", 53 | "schema": { 54 | "$ref": "#/definitions/models.AuthResponse" 55 | } 56 | }, 57 | "400": { 58 | "description": "Bad Request", 59 | "schema": { 60 | "type": "string" 61 | } 62 | }, 63 | "500": { 64 | "description": "Internal Server Error", 65 | "schema": { 66 | "type": "string" 67 | } 68 | } 69 | } 70 | } 71 | }, 72 | "/register": { 73 | "post": { 74 | "description": "Register a new user", 75 | "consumes": [ 76 | "application/json" 77 | ], 78 | "produces": [ 79 | "application/json" 80 | ], 81 | "tags": [ 82 | "User" 83 | ], 84 | "summary": "Register a new user", 85 | "parameters": [ 86 | { 87 | "description": "Register", 88 | "name": "Body", 89 | "in": "body", 90 | "required": true, 91 | "schema": { 92 | "$ref": "#/definitions/models.RegisterRequest" 93 | } 94 | } 95 | ], 96 | "responses": { 97 | "200": { 98 | "description": "OK", 99 | "schema": { 100 | "type": "string" 101 | } 102 | }, 103 | "400": { 104 | "description": "Bad Request", 105 | "schema": { 106 | "type": "string" 107 | } 108 | }, 109 | "500": { 110 | "description": "Internal Server Error", 111 | "schema": { 112 | "type": "string" 113 | } 114 | } 115 | } 116 | } 117 | } 118 | }, 119 | "definitions": { 120 | "models.AuthRequest": { 121 | "type": "object", 122 | "properties": { 123 | "email": { 124 | "type": "string" 125 | }, 126 | "password": { 127 | "type": "string" 128 | } 129 | } 130 | }, 131 | "models.AuthResponse": { 132 | "type": "object", 133 | "properties": { 134 | "token": { 135 | "type": "string" 136 | } 137 | } 138 | }, 139 | "models.RegisterRequest": { 140 | "type": "object", 141 | "properties": { 142 | "email": { 143 | "type": "string" 144 | }, 145 | "password": { 146 | "type": "string" 147 | }, 148 | "password_confirmation": { 149 | "type": "string" 150 | } 151 | } 152 | } 153 | } 154 | }` 155 | 156 | // SwaggerInfo holds exported Swagger Info so clients can modify it 157 | var SwaggerInfo = &swag.Spec{ 158 | Version: "", 159 | Host: "", 160 | BasePath: "", 161 | Schemes: []string{}, 162 | Title: "", 163 | Description: "", 164 | InfoInstanceName: "swagger", 165 | SwaggerTemplate: docTemplate, 166 | LeftDelim: "{{", 167 | RightDelim: "}}", 168 | } 169 | 170 | func init() { 171 | swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) 172 | } 173 | -------------------------------------------------------------------------------- /docs/swagger.json: -------------------------------------------------------------------------------- 1 | { 2 | "swagger": "2.0", 3 | "info": { 4 | "contact": { 5 | "name": "Carlos García", 6 | "email": "cgarciarosales97@gmail.com" 7 | }, 8 | "license": { 9 | "name": "Apache 2.0", 10 | "url": "http://www.apache.org/licenses/LICENSE-2.0.html" 11 | } 12 | }, 13 | "paths": { 14 | "/login": { 15 | "post": { 16 | "description": "Login a user", 17 | "consumes": [ 18 | "application/json" 19 | ], 20 | "produces": [ 21 | "application/json" 22 | ], 23 | "tags": [ 24 | "User" 25 | ], 26 | "summary": "Login a user", 27 | "parameters": [ 28 | { 29 | "description": "Register", 30 | "name": "Body", 31 | "in": "body", 32 | "required": true, 33 | "schema": { 34 | "$ref": "#/definitions/models.AuthRequest" 35 | } 36 | } 37 | ], 38 | "responses": { 39 | "200": { 40 | "description": "OK", 41 | "schema": { 42 | "$ref": "#/definitions/models.AuthResponse" 43 | } 44 | }, 45 | "400": { 46 | "description": "Bad Request", 47 | "schema": { 48 | "type": "string" 49 | } 50 | }, 51 | "500": { 52 | "description": "Internal Server Error", 53 | "schema": { 54 | "type": "string" 55 | } 56 | } 57 | } 58 | } 59 | }, 60 | "/register": { 61 | "post": { 62 | "description": "Register a new user", 63 | "consumes": [ 64 | "application/json" 65 | ], 66 | "produces": [ 67 | "application/json" 68 | ], 69 | "tags": [ 70 | "User" 71 | ], 72 | "summary": "Register a new user", 73 | "parameters": [ 74 | { 75 | "description": "Register", 76 | "name": "Body", 77 | "in": "body", 78 | "required": true, 79 | "schema": { 80 | "$ref": "#/definitions/models.RegisterRequest" 81 | } 82 | } 83 | ], 84 | "responses": { 85 | "200": { 86 | "description": "OK", 87 | "schema": { 88 | "type": "string" 89 | } 90 | }, 91 | "400": { 92 | "description": "Bad Request", 93 | "schema": { 94 | "type": "string" 95 | } 96 | }, 97 | "500": { 98 | "description": "Internal Server Error", 99 | "schema": { 100 | "type": "string" 101 | } 102 | } 103 | } 104 | } 105 | } 106 | }, 107 | "definitions": { 108 | "models.AuthRequest": { 109 | "type": "object", 110 | "properties": { 111 | "email": { 112 | "type": "string" 113 | }, 114 | "password": { 115 | "type": "string" 116 | } 117 | } 118 | }, 119 | "models.AuthResponse": { 120 | "type": "object", 121 | "properties": { 122 | "token": { 123 | "type": "string" 124 | } 125 | } 126 | }, 127 | "models.RegisterRequest": { 128 | "type": "object", 129 | "properties": { 130 | "email": { 131 | "type": "string" 132 | }, 133 | "password": { 134 | "type": "string" 135 | }, 136 | "password_confirmation": { 137 | "type": "string" 138 | } 139 | } 140 | } 141 | } 142 | } -------------------------------------------------------------------------------- /docs/swagger.yaml: -------------------------------------------------------------------------------- 1 | definitions: 2 | models.AuthRequest: 3 | properties: 4 | email: 5 | type: string 6 | password: 7 | type: string 8 | type: object 9 | models.AuthResponse: 10 | properties: 11 | token: 12 | type: string 13 | type: object 14 | models.RegisterRequest: 15 | properties: 16 | email: 17 | type: string 18 | password: 19 | type: string 20 | password_confirmation: 21 | type: string 22 | type: object 23 | info: 24 | contact: 25 | email: cgarciarosales97@gmail.com 26 | name: Carlos García 27 | license: 28 | name: Apache 2.0 29 | url: http://www.apache.org/licenses/LICENSE-2.0.html 30 | paths: 31 | /login: 32 | post: 33 | consumes: 34 | - application/json 35 | description: Login a user 36 | parameters: 37 | - description: Register 38 | in: body 39 | name: Body 40 | required: true 41 | schema: 42 | $ref: '#/definitions/models.AuthRequest' 43 | produces: 44 | - application/json 45 | responses: 46 | "200": 47 | description: OK 48 | schema: 49 | $ref: '#/definitions/models.AuthResponse' 50 | "400": 51 | description: Bad Request 52 | schema: 53 | type: string 54 | "500": 55 | description: Internal Server Error 56 | schema: 57 | type: string 58 | summary: Login a user 59 | tags: 60 | - User 61 | /register: 62 | post: 63 | consumes: 64 | - application/json 65 | description: Register a new user 66 | parameters: 67 | - description: Register 68 | in: body 69 | name: Body 70 | required: true 71 | schema: 72 | $ref: '#/definitions/models.RegisterRequest' 73 | produces: 74 | - application/json 75 | responses: 76 | "200": 77 | description: OK 78 | schema: 79 | type: string 80 | "400": 81 | description: Bad Request 82 | schema: 83 | type: string 84 | "500": 85 | description: Internal Server Error 86 | schema: 87 | type: string 88 | summary: Register a new user 89 | tags: 90 | - User 91 | swagger: "2.0" 92 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/solrac97gr/go-jwt-auth 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/gofiber/fiber/v2 v2.39.0 7 | github.com/gofiber/jwt/v3 v3.3.3 8 | github.com/gofiber/swagger v0.1.7 9 | github.com/golang-jwt/jwt/v4 v4.4.2 10 | github.com/swaggo/swag v1.16.1 11 | go.mongodb.org/mongo-driver v1.12.1 12 | go.uber.org/mock v0.2.0 13 | go.uber.org/zap v1.23.0 14 | golang.org/x/crypto v0.12.0 15 | ) 16 | 17 | require ( 18 | github.com/KyleBanks/depth v1.2.1 // indirect 19 | github.com/andybalholm/brotli v1.0.4 // indirect 20 | github.com/go-openapi/jsonpointer v0.20.0 // indirect 21 | github.com/go-openapi/jsonreference v0.20.2 // indirect 22 | github.com/go-openapi/spec v0.20.9 // indirect 23 | github.com/go-openapi/swag v0.22.4 // indirect 24 | github.com/golang/snappy v0.0.1 // indirect 25 | github.com/josharian/intern v1.0.0 // indirect 26 | github.com/klauspost/compress v1.15.12 // indirect 27 | github.com/mailru/easyjson v0.7.7 // indirect 28 | github.com/mattn/go-colorable v0.1.13 // indirect 29 | github.com/mattn/go-isatty v0.0.16 // indirect 30 | github.com/mattn/go-runewidth v0.0.14 // indirect 31 | github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect 32 | github.com/pkg/errors v0.9.1 // indirect 33 | github.com/rivo/uniseg v0.4.2 // indirect 34 | github.com/rogpeppe/go-internal v1.11.0 // indirect 35 | github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a // indirect 36 | github.com/valyala/bytebufferpool v1.0.0 // indirect 37 | github.com/valyala/fasthttp v1.41.0 // indirect 38 | github.com/valyala/tcplisten v1.0.0 // indirect 39 | github.com/xdg-go/pbkdf2 v1.0.0 // indirect 40 | github.com/xdg-go/scram v1.1.2 // indirect 41 | github.com/xdg-go/stringprep v1.0.4 // indirect 42 | github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect 43 | go.uber.org/atomic v1.10.0 // indirect 44 | go.uber.org/multierr v1.8.0 // indirect 45 | golang.org/x/net v0.14.0 // indirect 46 | golang.org/x/sync v0.3.0 // indirect 47 | golang.org/x/sys v0.11.0 // indirect 48 | golang.org/x/text v0.12.0 // indirect 49 | golang.org/x/tools v0.12.0 // indirect 50 | gopkg.in/yaml.v3 v3.0.1 // indirect 51 | ) 52 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 | github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= 3 | github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= 4 | github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= 5 | github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= 6 | github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= 7 | github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= 8 | github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= 9 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 10 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 11 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 12 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 13 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 14 | github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= 15 | github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= 16 | github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= 17 | github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= 18 | github.com/go-openapi/jsonpointer v0.20.0 h1:ESKJdU9ASRfaPNOPRx12IUyA1vn3R9GiE3KYD14BXdQ= 19 | github.com/go-openapi/jsonpointer v0.20.0/go.mod h1:6PGzBjjIIumbLYysB73Klnms1mwnU4G3YHOECG3CedA= 20 | github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns= 21 | github.com/go-openapi/jsonreference v0.20.0/go.mod h1:Ag74Ico3lPc+zR+qjn4XBUmXymS4zJbYVCZmcgkasdo= 22 | github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE= 23 | github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= 24 | github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I= 25 | github.com/go-openapi/spec v0.20.9 h1:xnlYNQAwKd2VQRRfwTEI0DcK+2cbuvI/0c7jx3gA8/8= 26 | github.com/go-openapi/spec v0.20.9/go.mod h1:2OpW+JddWPrpXSCIX8eOx7lZ5iyuWj3RYR6VaaBKcWA= 27 | github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= 28 | github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= 29 | github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= 30 | github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU= 31 | github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= 32 | github.com/gofiber/fiber/v2 v2.39.0 h1:uhWpYQ6EHN8J7FOPYbI2hrdBD/KNZBC5CjbuOd4QUt4= 33 | github.com/gofiber/fiber/v2 v2.39.0/go.mod h1:Cmuu+elPYGqlvQvdKyjtYsjGMi69PDp8a1AY2I5B2gM= 34 | github.com/gofiber/jwt/v3 v3.3.3 h1:mius3VXxmrdfF8apJH7M+rcfV8cKN7U0wLMHeCug7V4= 35 | github.com/gofiber/jwt/v3 v3.3.3/go.mod h1:g3gwVidveB0wHsNGX3qn5FVHaiEml2HEvs4aKZqtSww= 36 | github.com/gofiber/swagger v0.1.7 h1:+uVrHH/Lrv5z0pK0ELZj458WDq6MtKoLGrNfgK/dYKg= 37 | github.com/gofiber/swagger v0.1.7/go.mod h1:faFaKjGFpuNskc8lmOTuue1RGnmLpz+Ze+nuJnTreTs= 38 | github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs= 39 | github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= 40 | github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= 41 | github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= 42 | github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= 43 | github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 44 | github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= 45 | github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= 46 | github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= 47 | github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= 48 | github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= 49 | github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM= 50 | github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM= 51 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 52 | github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= 53 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 54 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 55 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 56 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 57 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 58 | github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= 59 | github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= 60 | github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= 61 | github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= 62 | github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= 63 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 64 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 65 | github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= 66 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 67 | github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= 68 | github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 69 | github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= 70 | github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= 71 | github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= 72 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 73 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 74 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 75 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 76 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 77 | github.com/rivo/uniseg v0.4.2 h1:YwD0ulJSJytLpiaWua0sBDusfsCZohxjxzVTYjwxfV8= 78 | github.com/rivo/uniseg v0.4.2/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= 79 | github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= 80 | github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= 81 | github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 82 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 83 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 84 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 85 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 86 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 87 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 88 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 89 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 90 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 91 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 92 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 93 | github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a h1:kAe4YSu0O0UFn1DowNo2MY5p6xzqtJ/wQ7LZynSvGaY= 94 | github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w= 95 | github.com/swaggo/swag v1.8.7/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9enSk= 96 | github.com/swaggo/swag v1.16.1 h1:fTNRhKstPKxcnoKsytm4sahr8FaYzUcT7i1/3nd/fBg= 97 | github.com/swaggo/swag v1.16.1/go.mod h1:9/LMvHycG3NFHfR6LwvikHv5iFvmPADQ359cKikGxto= 98 | github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= 99 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 100 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 101 | github.com/valyala/fasthttp v1.40.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I= 102 | github.com/valyala/fasthttp v1.41.0 h1:zeR0Z1my1wDHTRiamBCXVglQdbUwgb9uWG3k1HQz6jY= 103 | github.com/valyala/fasthttp v1.41.0/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY= 104 | github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= 105 | github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= 106 | github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= 107 | github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= 108 | github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= 109 | github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= 110 | github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= 111 | github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= 112 | github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= 113 | github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= 114 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 115 | go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE= 116 | go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= 117 | go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= 118 | go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= 119 | go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= 120 | go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= 121 | go.uber.org/mock v0.2.0 h1:TaP3xedm7JaAgScZO7tlvlKrqT0p7I6OsdGB5YNSMDU= 122 | go.uber.org/mock v0.2.0/go.mod h1:J0y0rp9L3xiff1+ZBfKxlC1fz2+aO16tw0tsDOixfuM= 123 | go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8= 124 | go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= 125 | go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY= 126 | go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY= 127 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 128 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 129 | golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 130 | golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 131 | golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= 132 | golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= 133 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 134 | golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= 135 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 136 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 137 | golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= 138 | golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 139 | golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 140 | golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= 141 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 142 | golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= 143 | golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= 144 | golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= 145 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 146 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 147 | golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= 148 | golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= 149 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 150 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 151 | golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 152 | golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 153 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 154 | golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 155 | golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 156 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 157 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 158 | golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 159 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 160 | golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= 161 | golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 162 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 163 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 164 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 165 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 166 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 167 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 168 | golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= 169 | golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 170 | golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= 171 | golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= 172 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 173 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 174 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 175 | golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss= 176 | golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= 177 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 178 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 179 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= 180 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 181 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 182 | gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 183 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 184 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 185 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 186 | gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 187 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= 188 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 189 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 190 | gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 191 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 192 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 193 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 194 | -------------------------------------------------------------------------------- /hooks/pre-commit.sh: -------------------------------------------------------------------------------- 1 | ./scripts/run-lint.sh && ./scripts/run-test.sh -------------------------------------------------------------------------------- /internal/user/application/application.go: -------------------------------------------------------------------------------- 1 | package application 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports" 5 | configurator "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports" 6 | logger "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports" 7 | val "github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports" 8 | ) 9 | 10 | type UserApp struct { 11 | repo ports.UserRepository 12 | validator val.ValidatorApplication 13 | logger logger.LoggerApplication 14 | configurator configurator.ConfigApplication 15 | } 16 | 17 | var _ ports.UserApplication = (*UserApp)(nil) 18 | 19 | func NewUserApp( 20 | repo ports.UserRepository, 21 | validator val.ValidatorApplication, 22 | logger logger.LoggerApplication, 23 | configurator configurator.ConfigApplication, 24 | ) *UserApp { 25 | return &UserApp{ 26 | repo: repo, 27 | validator: validator, 28 | logger: logger, 29 | configurator: configurator, 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /internal/user/application/create.go: -------------------------------------------------------------------------------- 1 | package application 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 5 | customErr "github.com/solrac97gr/go-jwt-auth/pkg/custom-errors" 6 | "time" 7 | ) 8 | 9 | func (app *UserApp) Create(registerReq *models.RegisterRequest) error { 10 | if registerReq.Password != registerReq.PasswordConfirmation { 11 | return customErr.ErrPasswordConfirmation 12 | } 13 | user := new(models.User) 14 | user.Email = registerReq.Email 15 | user.Password = registerReq.Password 16 | user.CreatedAt = time.Now() 17 | 18 | if err := app.validator.Struct(user); err != nil { 19 | app.logger.Error("Error validating user", err) 20 | return err 21 | } 22 | // Hash password after the validation 23 | user.Password = user.Hash256Password(registerReq.Password) 24 | 25 | err := app.repo.Save(user) 26 | if err != nil { 27 | app.logger.Error("Error creating user", err) 28 | return err 29 | } 30 | 31 | app.logger.Info("User created", user) 32 | return nil 33 | } 34 | -------------------------------------------------------------------------------- /internal/user/application/create_test.go: -------------------------------------------------------------------------------- 1 | package application_test 2 | 3 | import "testing" 4 | 5 | func TestCreate(t *testing.T) { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /internal/user/application/login.go: -------------------------------------------------------------------------------- 1 | package application 2 | 3 | import ( 4 | "github.com/golang-jwt/jwt/v4" 5 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 6 | "time" 7 | ) 8 | 9 | func (app *UserApp) Login(credentials *models.AuthRequest) (authToken *models.AuthResponse, err error) { 10 | user, err := app.repo.FindByCredentials(credentials) 11 | if err != nil { 12 | app.logger.Error("Error finding user", err) 13 | return nil, err 14 | } 15 | 16 | config, err := app.configurator.GetConfig() 17 | if err != nil { 18 | app.logger.Error("Error getting config", err) 19 | return nil, err 20 | } 21 | 22 | day := time.Hour * 24 23 | 24 | claims := jwt.MapClaims{ 25 | "ID": user.ID, 26 | "email": user.Email, 27 | "created_at": user.CreatedAt, 28 | "exp": time.Now().Add(day * time.Duration(config.JWT.ExpiresIn)).Unix(), 29 | } 30 | 31 | // Create token 32 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) 33 | 34 | // Generate encoded token and send it as response. 35 | t, err := token.SignedString([]byte(config.JWT.Secret)) 36 | if err != nil { 37 | return nil, err 38 | } 39 | return &models.AuthResponse{Token: t}, nil 40 | } 41 | -------------------------------------------------------------------------------- /internal/user/application/login_test.go: -------------------------------------------------------------------------------- 1 | package application_test 2 | 3 | import "testing" 4 | 5 | func TestLogin(t *testing.T) { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /internal/user/domain/models/auth_request.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import customErr "github.com/solrac97gr/go-jwt-auth/pkg/custom-errors" 4 | 5 | type AuthRequest struct { 6 | Email string `json:"email"` 7 | Password string `json:"password"` 8 | } 9 | 10 | func (a *AuthRequest) Validate() error { 11 | if a.Email == "" { 12 | return customErr.ErrEmailRequired 13 | } 14 | if a.Password == "" { 15 | return customErr.ErrPasswordRequired 16 | } 17 | return nil 18 | } 19 | -------------------------------------------------------------------------------- /internal/user/domain/models/auth_response.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import customErr "github.com/solrac97gr/go-jwt-auth/pkg/custom-errors" 4 | 5 | type AuthResponse struct { 6 | Token string `json:"token"` 7 | } 8 | 9 | func (a *AuthResponse) Validate() error { 10 | if a.Token == "" { 11 | return customErr.ErrTokenRequired 12 | } 13 | return nil 14 | } 15 | -------------------------------------------------------------------------------- /internal/user/domain/models/register_request.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | type RegisterRequest struct { 4 | Email string `json:"email"` 5 | Password string `json:"password"` 6 | PasswordConfirmation string `json:"password_confirmation"` 7 | } 8 | -------------------------------------------------------------------------------- /internal/user/domain/models/user.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | "encoding/hex" 5 | customErr "github.com/solrac97gr/go-jwt-auth/pkg/custom-errors" 6 | "github.com/solrac97gr/go-jwt-auth/pkg/utils" 7 | "go.mongodb.org/mongo-driver/bson/primitive" 8 | "golang.org/x/crypto/sha3" 9 | "net/mail" 10 | "time" 11 | ) 12 | 13 | type User struct { 14 | ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` 15 | Email string `json:"email" bson:"email"` 16 | Password string `json:"password" bson:"password"` 17 | CreatedAt time.Time `json:"created_at" bson:"created_at"` 18 | } 19 | 20 | func (u *User) Validate() error { 21 | if u.Email == "" { 22 | return customErr.ErrEmailRequired 23 | } 24 | if u.Password == "" { 25 | return customErr.ErrPasswordRequired 26 | } 27 | if !utils.IsValid(u.Password) { 28 | return customErr.ErrPasswordFormat 29 | } 30 | if u.CreatedAt.IsZero() { 31 | return customErr.ErrCreateAtRequired 32 | } 33 | _, err := mail.ParseAddress(u.Email) 34 | if err != nil { 35 | return customErr.ErrInvalidEmail 36 | } 37 | return nil 38 | } 39 | 40 | func (u *User) Hash256Password(password string) string { 41 | buf := []byte(password) 42 | pwd := sha3.New256() 43 | pwd.Write(buf) 44 | return hex.EncodeToString(pwd.Sum(nil)) 45 | } 46 | -------------------------------------------------------------------------------- /internal/user/domain/ports/user_application.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | import "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 4 | 5 | type UserApplication interface { 6 | Create(registerRequest *models.RegisterRequest) error 7 | Login(credentials *models.AuthRequest) (authToken *models.AuthResponse, err error) 8 | } 9 | -------------------------------------------------------------------------------- /internal/user/domain/ports/user_handlers.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | import "github.com/gofiber/fiber/v2" 4 | 5 | type UserHandlers interface { 6 | Register(ctx *fiber.Ctx) error 7 | Login(ctx *fiber.Ctx) error 8 | } 9 | -------------------------------------------------------------------------------- /internal/user/domain/ports/user_repository.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | import "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 4 | 5 | type UserRepository interface { 6 | Save(user *models.User) error 7 | FindByCredentials(credentials *models.AuthRequest) (*models.User, error) 8 | } 9 | -------------------------------------------------------------------------------- /internal/user/infrastructure/handlers/fiber.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports" 5 | logger "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports" 6 | ) 7 | 8 | type UserHdl struct { 9 | app ports.UserApplication 10 | logger logger.LoggerApplication 11 | } 12 | 13 | var _ ports.UserHandlers = (*UserHdl)(nil) 14 | 15 | func NewUserHdl(app ports.UserApplication, logger logger.LoggerApplication) *UserHdl { 16 | return &UserHdl{app: app, logger: logger} 17 | } 18 | -------------------------------------------------------------------------------- /internal/user/infrastructure/handlers/login.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "github.com/gofiber/fiber/v2" 5 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 6 | ) 7 | 8 | // Login godoc 9 | // @Summary Login a user 10 | // @Description Login a user 11 | // @Accept json 12 | // @Produce json 13 | // @Tags User 14 | // @Param Body body models.AuthRequest true "Register" 15 | // @Success 200 {object} models.AuthResponse 16 | // @Failure 400 {object} string 17 | // @Failure 500 {object} string 18 | // @Router /login [post] 19 | func (hdl *UserHdl) Login(ctx *fiber.Ctx) error { 20 | reqBody := new(models.AuthRequest) 21 | if err := ctx.BodyParser(reqBody); err != nil { 22 | hdl.logger.Error(err.Error()) 23 | return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{ 24 | "error": err.Error(), 25 | }) 26 | } 27 | authResponse, err := hdl.app.Login(reqBody) 28 | if err != nil { 29 | hdl.logger.Error(err.Error()) 30 | return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ 31 | "error": err.Error(), 32 | }) 33 | } 34 | return ctx.Status(fiber.StatusOK).JSON(authResponse) 35 | } 36 | -------------------------------------------------------------------------------- /internal/user/infrastructure/handlers/login_test.go: -------------------------------------------------------------------------------- 1 | package handlers_test 2 | 3 | import "testing" 4 | 5 | func TestLogin(t *testing.T) { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /internal/user/infrastructure/handlers/register.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "github.com/gofiber/fiber/v2" 5 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 6 | ) 7 | 8 | // Register godoc 9 | // @Summary Register a new user 10 | // @Description Register a new user 11 | // @Accept json 12 | // @Produce json 13 | // @Tags User 14 | // @Param Body body models.RegisterRequest true "Register" 15 | // @Success 200 {object} string 16 | // @Failure 400 {object} string 17 | // @Failure 500 {object} string 18 | // @Router /register [post] 19 | func (hdl *UserHdl) Register(ctx *fiber.Ctx) error { 20 | authReq := new(models.RegisterRequest) 21 | if err := ctx.BodyParser(authReq); err != nil { 22 | hdl.logger.Error(err.Error()) 23 | return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{ 24 | "error": err.Error(), 25 | }) 26 | } 27 | 28 | err := hdl.app.Create(authReq) 29 | if err != nil { 30 | hdl.logger.Error(err.Error()) 31 | return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ 32 | "error": err.Error(), 33 | }) 34 | } 35 | return ctx.Status(fiber.StatusOK).JSON(fiber.Map{ 36 | "message": "User created successfully", 37 | }) 38 | } 39 | -------------------------------------------------------------------------------- /internal/user/infrastructure/handlers/register_test.go: -------------------------------------------------------------------------------- 1 | package handlers_test 2 | 3 | import "testing" 4 | 5 | func TestRegister(t *testing.T) { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /internal/user/infrastructure/repositories/find_by_credentials.go: -------------------------------------------------------------------------------- 1 | package repositories 2 | 3 | import ( 4 | "context" 5 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 6 | "go.mongodb.org/mongo-driver/bson" 7 | ) 8 | 9 | func (repo *UserMongoDB) FindByCredentials(credentials *models.AuthRequest) (*models.User, error) { 10 | user := new(models.User) 11 | user.Email = credentials.Email 12 | user.Password = user.Hash256Password(credentials.Password) 13 | 14 | result := repo.database.Collection("users").FindOne(context.Background(), bson.D{ 15 | {Key: "email", Value: user.Email}, 16 | {Key: "password", Value: user.Password}, 17 | }) 18 | 19 | if result.Err() != nil { 20 | repo.logger.Error("Error finding user", result.Err()) 21 | return nil, result.Err() 22 | } 23 | 24 | err := result.Decode(user) 25 | if err != nil { 26 | repo.logger.Error("Error decoding user", err) 27 | return nil, err 28 | } 29 | user.Password = "" 30 | return user, nil 31 | } 32 | -------------------------------------------------------------------------------- /internal/user/infrastructure/repositories/find_by_credentials_test.go: -------------------------------------------------------------------------------- 1 | package repositories_test 2 | 3 | import "testing" 4 | 5 | func TestFindByCredentials(t *testing.T) { 6 | } 7 | -------------------------------------------------------------------------------- /internal/user/infrastructure/repositories/mongo.go: -------------------------------------------------------------------------------- 1 | package repositories 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports" 5 | config "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports" 6 | logger "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports" 7 | "go.mongodb.org/mongo-driver/mongo" 8 | ) 9 | 10 | type UserMongoDB struct { 11 | client *mongo.Client 12 | database *mongo.Database 13 | collection *mongo.Collection 14 | configurator config.ConfigApplication 15 | logger logger.LoggerApplication 16 | } 17 | 18 | var _ ports.UserRepository = (*UserMongoDB)(nil) 19 | 20 | func NewUserMongoDB(config config.ConfigApplication, logger logger.LoggerApplication, client *mongo.Client) *UserMongoDB { 21 | cfg, err := config.GetConfig() 22 | if err != nil { 23 | logger.Error("Error getting configurator", err) 24 | return nil 25 | } 26 | return &UserMongoDB{ 27 | configurator: config, 28 | logger: logger, 29 | client: client, 30 | database: client.Database(cfg.Database.Name), 31 | collection: client.Database(cfg.Database.Name).Collection("users"), 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /internal/user/infrastructure/repositories/save.go: -------------------------------------------------------------------------------- 1 | package repositories 2 | 3 | import ( 4 | "context" 5 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 6 | ) 7 | 8 | func (repo *UserMongoDB) Save(user *models.User) error { 9 | _, err := repo.collection.InsertOne(context.Background(), user) 10 | if err != nil { 11 | repo.logger.Error("Error saving user", err) 12 | return err 13 | } 14 | return nil 15 | } 16 | -------------------------------------------------------------------------------- /internal/user/infrastructure/repositories/save_test.go: -------------------------------------------------------------------------------- 1 | package repositories_test 2 | 3 | import "testing" 4 | 5 | func TestSave(t *testing.T) { 6 | } 7 | -------------------------------------------------------------------------------- /logs/log.csv: -------------------------------------------------------------------------------- 1 | ,info,User created,2022-11-09 23:15:58.85116 +0300 MSK m=+84.029638251 2 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package go_jwt_auth 2 | 3 | // Not delete 4 | // Path: ./main.go 5 | // It's necessary for Swagger to work 6 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | # Go parameters 2 | GOCMD=go 3 | GOBUILD=$(GOCMD) build 4 | GOCLEAN=$(GOCMD) clean 5 | GOTEST=$(GOCMD) test 6 | GOTOOL=$(GOCMD) tool 7 | GOGET=$(GOCMD) get 8 | GOMOD=$(GOCMD) mod 9 | GOINST=$(GOCMD) install 10 | 11 | MODULE_NAME=github.com/solrac97gr/go-jwt-auth 12 | 13 | # Binary name 14 | BINARY_NAME=main 15 | 16 | # Set environment file 17 | set-env: 18 | @cp ./config/env.example.json ./config/env.json 19 | @echo "📡 config file generated: ./config/env.json" 20 | 21 | # Build 22 | build: 23 | @$(GOBUILD) -o $(BINARY_NAME) ./cmd/http 24 | @echo "📦 Build Done" 25 | 26 | # Clean 27 | clean: 28 | @$(GOCLEAN) 29 | @rm -f $(BINARY_NAME) 30 | @rm -f test.out 31 | @echo "🧹 Program removed" 32 | 33 | # Test 34 | test:mocks 35 | @$(GOTEST) -v ./... >> test.out 36 | @echo "🧪 Test Completed" 37 | 38 | # Lint 39 | lint: 40 | @golangci-lint run 41 | @echo "🔦 Code Linted" 42 | 43 | # Download dependencies 44 | deps: 45 | @$(GOINST) github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.3 46 | @$(GOINST) go.uber.org/mock/gomock@latest 47 | @$(GOINST) github.com/swaggo/swag/cmd/swag@latest 48 | @$(GOMOD) download 49 | @echo "⬇️ Dependencies downloaded" 50 | 51 | # Build and install 52 | install: deps build 53 | @echo "🛡️ Successfully Installed" 54 | 55 | # Generate the doc 56 | doc: 57 | @$(GOINST) github.com/swaggo/swag/cmd/swag@latest 58 | @swag init --parseDependency=true -g cmd/http/main.go >> output.out 59 | @rm output.out 60 | @echo "📓 Docs Generated" 61 | 62 | 63 | # Generate Coverage Report 64 | cover: mocks 65 | @$(GOTEST) -coverprofile=coverage.out ./... 66 | @$(GOTOOL) cover -html=coverage.out 67 | @rm coverage.out 68 | @echo "🎯 Cover profile generated" 69 | 70 | # Build and run 71 | run: doc build 72 | @echo "🚀 Running App" 73 | @./$(BINARY_NAME) 74 | 75 | mocks: 76 | @echo "🔧 Generating mocks" 77 | @./scripts/generate-mocks.sh -------------------------------------------------------------------------------- /pkg/config/application/application.go: -------------------------------------------------------------------------------- 1 | package application 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/models" 5 | "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports" 6 | logger "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports" 7 | validator "github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports" 8 | ) 9 | 10 | type ConfigService struct { 11 | repository ports.ConfigRepository 12 | configuration *models.Config 13 | validator validator.ValidatorApplication 14 | logger logger.LoggerApplication 15 | } 16 | 17 | var _ ports.ConfigApplication = (*ConfigService)(nil) 18 | 19 | func NewConfigService( 20 | repository ports.ConfigRepository, 21 | val validator.ValidatorApplication, 22 | logger logger.LoggerApplication, 23 | ) *ConfigService { 24 | return &ConfigService{ 25 | repository: repository, 26 | validator: val, 27 | logger: logger, 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /pkg/config/application/config.go: -------------------------------------------------------------------------------- 1 | package application 2 | 3 | import ( 4 | "encoding/json" 5 | "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/models" 6 | ) 7 | 8 | func (c *ConfigService) Config() error { 9 | configuration := new(models.Config) 10 | file, err := c.repository.GetConfigFile() 11 | if err != nil { 12 | return err 13 | } 14 | defer file.Close() 15 | jsonParser := json.NewDecoder(file) 16 | if err = jsonParser.Decode(&configuration); err != nil { 17 | return err 18 | } 19 | c.configuration = configuration 20 | return nil 21 | } 22 | -------------------------------------------------------------------------------- /pkg/config/application/get_config.go: -------------------------------------------------------------------------------- 1 | package application 2 | 3 | import "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/models" 4 | 5 | func (c *ConfigService) GetConfig() (*models.Config, error) { 6 | if c.configuration == nil { 7 | if err := c.Config(); err != nil { 8 | return nil, err 9 | } 10 | } 11 | if err := c.validator.Struct(c.configuration); err != nil { 12 | return nil, err 13 | } 14 | return c.configuration, nil 15 | } 16 | -------------------------------------------------------------------------------- /pkg/config/domain/models/app.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import customErr "github.com/solrac97gr/go-jwt-auth/pkg/custom-errors" 4 | 5 | type App struct { 6 | Name string `json:"name"` 7 | Description string `json:"description"` 8 | Host string `json:"host"` 9 | BaseURL string `json:"base_url"` 10 | Version string `json:"version"` 11 | } 12 | 13 | func (a *App) Validate() error { 14 | if a.Name == "" { 15 | return customErr.ErrAppNameRequired 16 | } 17 | if a.Description == "" { 18 | return customErr.ErrAppDescriptionRequired 19 | } 20 | if a.Host == "" { 21 | return customErr.ErrAppHostRequired 22 | } 23 | if a.BaseURL == "" { 24 | return customErr.ErrAppBaseURLRequired 25 | } 26 | if a.Version == "" { 27 | return customErr.ErrAppVersionRequired 28 | } 29 | return nil 30 | } 31 | -------------------------------------------------------------------------------- /pkg/config/domain/models/config.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | type Config struct { 4 | App *App `json:"app"` 5 | Database *Database `json:"database"` 6 | Server *Server `json:"server"` 7 | JWT *JWT `json:"jwt"` 8 | } 9 | 10 | func (c *Config) Validate() error { 11 | if err := c.Database.Validate(); err != nil { 12 | return err 13 | } 14 | 15 | if err := c.App.Validate(); err != nil { 16 | return err 17 | } 18 | 19 | if err := c.Server.Validate(); err != nil { 20 | return err 21 | } 22 | 23 | if err := c.JWT.Validate(); err != nil { 24 | return err 25 | } 26 | 27 | return nil 28 | } 29 | -------------------------------------------------------------------------------- /pkg/config/domain/models/database.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import customErr "github.com/solrac97gr/go-jwt-auth/pkg/custom-errors" 4 | 5 | type Database struct { 6 | Name string `json:"name"` 7 | URL string `json:"url"` 8 | } 9 | 10 | func (d *Database) Validate() error { 11 | if d.Name == "" { 12 | return customErr.ErrDatabaseNameRequired 13 | } 14 | if d.URL == "" { 15 | return customErr.ErrDatabaseURLRequired 16 | } 17 | return nil 18 | } 19 | -------------------------------------------------------------------------------- /pkg/config/domain/models/jwt.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | customErr "github.com/solrac97gr/go-jwt-auth/pkg/custom-errors" 5 | ) 6 | 7 | type JWT struct { 8 | Secret string `json:"secret"` 9 | ExpiresIn int `json:"days_of_expiration"` 10 | } 11 | 12 | func (j *JWT) Validate() error { 13 | if j.Secret == "" { 14 | return customErr.ErrSecretRequired 15 | } 16 | if j.ExpiresIn == 0 { 17 | return customErr.ErrExpiresInRequired 18 | } 19 | return nil 20 | } 21 | -------------------------------------------------------------------------------- /pkg/config/domain/models/server.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import customErr "github.com/solrac97gr/go-jwt-auth/pkg/custom-errors" 4 | 5 | type Server struct { 6 | Port string `json:"port"` 7 | } 8 | 9 | func (s *Server) Validate() error { 10 | if s.Port == "" { 11 | return customErr.ErrServerPortRequired 12 | } 13 | return nil 14 | } 15 | -------------------------------------------------------------------------------- /pkg/config/domain/ports/config_ports.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/models" 5 | "os" 6 | ) 7 | 8 | type ConfigApplication interface { 9 | Config() error 10 | GetConfig() (*models.Config, error) 11 | } 12 | 13 | type ConfigRepository interface { 14 | GetConfigFile() (*os.File, error) 15 | } 16 | -------------------------------------------------------------------------------- /pkg/config/infrastructure/repositories/json.go: -------------------------------------------------------------------------------- 1 | package repositories 2 | 3 | import "os" 4 | 5 | type JSONRepository struct { 6 | path string 7 | } 8 | 9 | func NewJSONRepository(path string) *JSONRepository { 10 | return &JSONRepository{ 11 | path: path, 12 | } 13 | } 14 | 15 | func (j *JSONRepository) GetConfigFile() (*os.File, error) { 16 | return os.Open(j.path) 17 | } 18 | -------------------------------------------------------------------------------- /pkg/custom-errors/errors.go: -------------------------------------------------------------------------------- 1 | package customErr 2 | 3 | import "errors" 4 | 5 | var ( 6 | // ErrEmailRequired is returned when an email address is not provided 7 | ErrEmailRequired = errors.New("email address is required") 8 | // ErrPasswordRequired is returned when a password is not provided 9 | ErrPasswordRequired = errors.New("password is required") 10 | // ErrInvalidCredentials is returned when the provided email address/password combination is invalid 11 | ErrInvalidCredentials = errors.New("invalid credentials") 12 | // ErrCreateAtRequired is returned when a create_at is not provided 13 | ErrCreateAtRequired = errors.New("create_at is required") 14 | // ErrTokenRequired is returned when a token is not provided 15 | ErrTokenRequired = errors.New("token is required") 16 | // ErrDatabaseNameRequired is returned when a database name is not provided 17 | ErrDatabaseNameRequired = errors.New("database name is required") 18 | // ErrDatabaseURLRequired is returned when a database url is not provided 19 | ErrDatabaseURLRequired = errors.New("database url is required") 20 | // ErrServerPortRequired is returned when a port is not provided 21 | ErrServerPortRequired = errors.New("port is required") 22 | // ErrAppNameRequired is returned when a app name is not provided 23 | ErrAppNameRequired = errors.New("app name is required") 24 | // ErrAppDescriptionRequired is returned when an app description is not provided 25 | ErrAppDescriptionRequired = errors.New("app description is required") 26 | // ErrAppBaseURLRequired is returned when an app base url is not provided 27 | ErrAppBaseURLRequired = errors.New("app base url is required") 28 | // ErrAppVersionRequired is returned when an app version is not provided 29 | ErrAppVersionRequired = errors.New("app version is required") 30 | // ErrLogLevelRequired is returned when a log level is not provided 31 | ErrLogLevelRequired = errors.New("log level is required") 32 | // ErrLogMessageRequired is returned when a log message is not provided 33 | ErrLogMessageRequired = errors.New("log message is required") 34 | // ErrAppHostRequired is returned when an app host is not provided 35 | ErrAppHostRequired = errors.New("app host is required") 36 | // ErrSecretRequired is returned when a secret is not provided 37 | ErrSecretRequired = errors.New("secret is required") 38 | // ErrExpiresInRequired is returned when an expires_in is not provided 39 | ErrExpiresInRequired = errors.New("days of expiration is required") 40 | // ErrPasswordConfirmation is returned when a password confirmation is not same as password 41 | ErrPasswordConfirmation = errors.New("password confirmation is not same as password") 42 | // ErrInvalidEmail is returned when an email is not valid 43 | ErrInvalidEmail = errors.New("email is not valid") 44 | // ErrPasswordLength is returned when a password is not valid 45 | ErrPasswordLength = errors.New("password must be at least 8 characters") 46 | // ErrPasswordFormat is returned when a password is not valid 47 | ErrPasswordFormat = errors.New("password must contain at least one uppercase letter, one lowercase letter, one number and one special character") 48 | ) 49 | -------------------------------------------------------------------------------- /pkg/factories/factories.go: -------------------------------------------------------------------------------- 1 | package factories 2 | 3 | import ( 4 | "context" 5 | userApp "github.com/solrac97gr/go-jwt-auth/internal/user/application" 6 | userHdl "github.com/solrac97gr/go-jwt-auth/internal/user/infrastructure/handlers" 7 | userRepo "github.com/solrac97gr/go-jwt-auth/internal/user/infrastructure/repositories" 8 | configApp "github.com/solrac97gr/go-jwt-auth/pkg/config/application" 9 | configRepo "github.com/solrac97gr/go-jwt-auth/pkg/config/infrastructure/repositories" 10 | loggerApp "github.com/solrac97gr/go-jwt-auth/pkg/logger/application" 11 | loggerRepo "github.com/solrac97gr/go-jwt-auth/pkg/logger/infrastructure/repositories" 12 | mdlApp "github.com/solrac97gr/go-jwt-auth/pkg/middleware/application" 13 | mdlHdl "github.com/solrac97gr/go-jwt-auth/pkg/middleware/infrastructure/handlers" 14 | mdlRepo "github.com/solrac97gr/go-jwt-auth/pkg/middleware/infrastructure/repositories" 15 | valApp "github.com/solrac97gr/go-jwt-auth/pkg/validator/application" 16 | "go.mongodb.org/mongo-driver/mongo" 17 | "go.mongodb.org/mongo-driver/mongo/options" 18 | "go.mongodb.org/mongo-driver/mongo/readpref" 19 | "log" 20 | "time" 21 | ) 22 | 23 | const MongoClientTimeout = 10 24 | 25 | type Factory struct { 26 | //Variables 27 | logFilePath string 28 | configFilePath string 29 | //Packages 30 | validator *valApp.Validator 31 | configurator *configApp.ConfigService 32 | logger *loggerApp.Logger 33 | dbClient *mongo.Client 34 | } 35 | 36 | func NewFactory(logFilePath string, configFilePath string) *Factory { 37 | return &Factory{ 38 | logFilePath: logFilePath, 39 | configFilePath: configFilePath, 40 | } 41 | } 42 | 43 | func (f *Factory) InitializeMongoDB() *mongo.Client { 44 | if f.dbClient != nil { 45 | return f.dbClient 46 | } 47 | configurator := f.InitializeConfigurator() 48 | cfg, err := configurator.GetConfig() 49 | if err != nil { 50 | panic(err) 51 | } 52 | 53 | ctx, cancelFunc := context.WithTimeout(context.Background(), MongoClientTimeout*time.Second) 54 | defer cancelFunc() 55 | 56 | client, err := mongo.Connect(ctx, options.Client().ApplyURI( 57 | cfg.Database.URL, 58 | )) 59 | if err != nil { 60 | log.Fatal(err) 61 | } 62 | err = client.Ping(ctx, readpref.Primary()) 63 | if err != nil { 64 | log.Fatal(err) 65 | } 66 | f.dbClient = client 67 | return client 68 | } 69 | 70 | func (f *Factory) InitializeValidator() *valApp.Validator { 71 | if f.validator == nil { 72 | app := valApp.NewValidator() 73 | f.validator = app 74 | return app 75 | } 76 | return f.validator 77 | } 78 | 79 | func (f *Factory) InitializeLogger() *loggerApp.Logger { 80 | if f.configurator == nil { 81 | validator := f.InitializeValidator() 82 | path := f.logFilePath 83 | 84 | repo := loggerRepo.NewCSVFile(path) 85 | app := loggerApp.NewLogger(repo, validator) 86 | f.logger = app 87 | return app 88 | } 89 | return f.logger 90 | } 91 | 92 | func (f *Factory) InitializeConfigurator() *configApp.ConfigService { 93 | if f.configurator == nil { 94 | validator := f.InitializeValidator() 95 | logger := f.InitializeLogger() 96 | path := f.configFilePath 97 | 98 | repo := configRepo.NewJSONRepository(path) 99 | app := configApp.NewConfigService(repo, validator, logger) 100 | err := app.Config() 101 | if err != nil { 102 | panic(err) 103 | } 104 | f.configurator = app 105 | return app 106 | } 107 | return f.configurator 108 | } 109 | 110 | func (f *Factory) BuildMiddlewaresHandlers() *mdlHdl.FiberMdlHandlers { 111 | configurator := f.InitializeConfigurator() 112 | logger := f.InitializeLogger() 113 | validator := f.InitializeValidator() 114 | 115 | repo := mdlRepo.NewMongoMdlRepository(configurator, logger) 116 | app := mdlApp.NewFiberMiddlewares(repo, validator, logger) 117 | return mdlHdl.NewFiberMdlHandlers(app, logger, configurator) 118 | } 119 | 120 | func (f *Factory) BuildUserHandlers() *userHdl.UserHdl { 121 | configurator := f.InitializeConfigurator() 122 | logger := f.InitializeLogger() 123 | validator := f.InitializeValidator() 124 | dbClient := f.InitializeMongoDB() 125 | 126 | repo := userRepo.NewUserMongoDB(configurator, logger, dbClient) 127 | app := userApp.NewUserApp(repo, validator, logger, configurator) 128 | return userHdl.NewUserHdl(app, logger) 129 | } 130 | -------------------------------------------------------------------------------- /pkg/logger/application/application.go: -------------------------------------------------------------------------------- 1 | package application 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/models" 5 | "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports" 6 | validator "github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports" 7 | "go.uber.org/zap" 8 | "time" 9 | ) 10 | 11 | type Logger struct { 12 | repo ports.LoggerRepository 13 | val validator.ValidatorApplication 14 | zap *zap.SugaredLogger 15 | } 16 | 17 | var _ ports.LoggerApplication = (*Logger)(nil) 18 | 19 | func NewLogger(repo ports.LoggerRepository, val validator.ValidatorApplication) *Logger { 20 | logger, _ := zap.NewProduction() 21 | sugar := logger.Sugar() 22 | 23 | return &Logger{ 24 | repo: repo, 25 | val: val, 26 | zap: sugar, 27 | } 28 | } 29 | 30 | func (l Logger) Close() error { 31 | return l.zap.Sync() 32 | } 33 | 34 | func (l Logger) Debug(msg string, args ...interface{}) { 35 | l.zap.Debug(msg, args) 36 | log := new(models.Log) 37 | log.Level = "debug" 38 | log.Message = msg 39 | log.CreatedAt = time.Now() 40 | 41 | err := l.repo.Save(log) 42 | if err != nil { 43 | l.zap.Error(err.Error()) 44 | } 45 | } 46 | 47 | func (l Logger) Info(msg string, args ...interface{}) { 48 | l.zap.Info(msg, args) 49 | log := new(models.Log) 50 | log.Level = "info" 51 | log.Message = msg 52 | log.CreatedAt = time.Now() 53 | 54 | err := l.repo.Save(log) 55 | if err != nil { 56 | l.zap.Error(err.Error()) 57 | } 58 | } 59 | 60 | func (l Logger) Warn(msg string, args ...interface{}) { 61 | l.zap.Warn(msg, args) 62 | log := new(models.Log) 63 | log.Level = "warn" 64 | log.Message = msg 65 | log.CreatedAt = time.Now() 66 | 67 | err := l.repo.Save(log) 68 | if err != nil { 69 | l.zap.Error(err.Error()) 70 | } 71 | } 72 | 73 | func (l Logger) Error(msg string, args ...interface{}) { 74 | l.zap.Error(msg, args) 75 | log := new(models.Log) 76 | log.Level = "error" 77 | log.Message = msg 78 | log.CreatedAt = time.Now() 79 | 80 | err := l.repo.Save(log) 81 | if err != nil { 82 | l.zap.Error(err.Error()) 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /pkg/logger/domain/models/log.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | customErr "github.com/solrac97gr/go-jwt-auth/pkg/custom-errors" 5 | "time" 6 | ) 7 | 8 | type Log struct { 9 | ID string `json:"id"` 10 | Level string `json:"level"` 11 | Message string `json:"message"` 12 | CreatedAt time.Time `json:"created_at"` 13 | } 14 | 15 | func (l Log) Validate() error { 16 | if l.Level == "" { 17 | return customErr.ErrLogLevelRequired 18 | } 19 | if l.Message == "" { 20 | return customErr.ErrLogMessageRequired 21 | } 22 | if l.CreatedAt.IsZero() { 23 | return customErr.ErrCreateAtRequired 24 | } 25 | return nil 26 | } 27 | -------------------------------------------------------------------------------- /pkg/logger/domain/ports/logger_application.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | type LoggerApplication interface { 4 | Close() error 5 | Debug(msg string, args ...interface{}) 6 | Info(msg string, args ...interface{}) 7 | Warn(msg string, args ...interface{}) 8 | Error(msg string, args ...interface{}) 9 | } 10 | -------------------------------------------------------------------------------- /pkg/logger/domain/ports/logger_repository.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | import "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/models" 4 | 5 | type LoggerRepository interface { 6 | Save(log *models.Log) error 7 | } 8 | -------------------------------------------------------------------------------- /pkg/logger/infrastructure/repositories/file.go: -------------------------------------------------------------------------------- 1 | package repositories 2 | 3 | import ( 4 | "encoding/csv" 5 | "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/models" 6 | "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports" 7 | "os" 8 | ) 9 | 10 | type CSVFile struct { 11 | path string 12 | } 13 | 14 | var _ ports.LoggerRepository = (*CSVFile)(nil) 15 | 16 | func NewCSVFile(path string) *CSVFile { 17 | return &CSVFile{path: path} 18 | } 19 | 20 | func (c *CSVFile) Save(log *models.Log) error { 21 | file, err := os.Create(c.path) 22 | if err != nil { 23 | return err 24 | } 25 | 26 | defer func(file *os.File) { 27 | err := file.Close() 28 | if err != nil { 29 | panic(err) 30 | } 31 | }(file) 32 | 33 | csvWriter := csv.NewWriter(file) 34 | defer csvWriter.Flush() 35 | 36 | // Write data to csv file 37 | err = csvWriter.Write([]string{log.ID, log.Level, log.Message, log.CreatedAt.String()}) 38 | if err != nil { 39 | return err 40 | } 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /pkg/middleware/application/application.go: -------------------------------------------------------------------------------- 1 | package application 2 | 3 | import ( 4 | logger "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports" 5 | mdl "github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports" 6 | validator "github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports" 7 | ) 8 | 9 | type FiberMiddlewares struct { 10 | repo mdl.MiddlewareRepository 11 | validator validator.ValidatorApplication 12 | logger logger.LoggerApplication 13 | } 14 | 15 | var _ mdl.MiddlewareApplication = (*FiberMiddlewares)(nil) 16 | 17 | func NewFiberMiddlewares(repo mdl.MiddlewareRepository, val validator.ValidatorApplication, logger logger.LoggerApplication) *FiberMiddlewares { 18 | return &FiberMiddlewares{ 19 | repo: repo, 20 | validator: val, 21 | logger: logger, 22 | } 23 | } 24 | 25 | func (f *FiberMiddlewares) Authenticate(token string) (code int, err error) { 26 | //TODO implement me 27 | panic("implement me") 28 | } 29 | -------------------------------------------------------------------------------- /pkg/middleware/domain/ports/mdl_application.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | type MiddlewareApplication interface { 4 | Authenticate(token string) (code int, err error) 5 | } 6 | -------------------------------------------------------------------------------- /pkg/middleware/domain/ports/mdl_handlers.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | import ( 4 | "github.com/gofiber/fiber/v2" 5 | ) 6 | 7 | type MiddlewareHandlers interface { 8 | Authenticate() fiber.Handler 9 | } 10 | -------------------------------------------------------------------------------- /pkg/middleware/domain/ports/mdl_repository.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | type MiddlewareRepository interface { 4 | Authenticate(token string) error 5 | } 6 | -------------------------------------------------------------------------------- /pkg/middleware/infrastructure/handlers/fiber.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "github.com/gofiber/fiber/v2" 5 | jwtware "github.com/gofiber/jwt/v3" 6 | configurator "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports" 7 | logger "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports" 8 | "github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports" 9 | ) 10 | 11 | type FiberMdlHandlers struct { 12 | service ports.MiddlewareApplication 13 | logger logger.LoggerApplication 14 | configurator configurator.ConfigApplication 15 | } 16 | 17 | var _ ports.MiddlewareHandlers = (*FiberMdlHandlers)(nil) 18 | 19 | func NewFiberMdlHandlers(service ports.MiddlewareApplication, logger logger.LoggerApplication, config configurator.ConfigApplication) *FiberMdlHandlers { 20 | return &FiberMdlHandlers{ 21 | service: service, 22 | logger: logger, 23 | configurator: config, 24 | } 25 | } 26 | 27 | func (f *FiberMdlHandlers) Authenticate() fiber.Handler { 28 | config, err := f.configurator.GetConfig() 29 | if err != nil { 30 | f.logger.Error("Error getting config", err) 31 | return func(ctx *fiber.Ctx) error { 32 | return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ 33 | "message": err.Error(), 34 | }) 35 | } 36 | } 37 | return jwtware.New(jwtware.Config{ 38 | SigningKey: []byte(config.JWT.Secret), 39 | }) 40 | } 41 | -------------------------------------------------------------------------------- /pkg/middleware/infrastructure/repositories/mongo.go: -------------------------------------------------------------------------------- 1 | package repositories 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports" 5 | logger "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports" 6 | mdl "github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports" 7 | ) 8 | 9 | type MongoMdlRepository struct { 10 | config ports.ConfigApplication 11 | logger logger.LoggerApplication 12 | } 13 | 14 | var _ mdl.MiddlewareRepository = (*MongoMdlRepository)(nil) 15 | 16 | func NewMongoMdlRepository(config ports.ConfigApplication, logger logger.LoggerApplication) *MongoMdlRepository { 17 | return &MongoMdlRepository{ 18 | config: config, 19 | logger: logger, 20 | } 21 | } 22 | 23 | func (m *MongoMdlRepository) Authenticate(token string) (err error) { 24 | //TODO implement me 25 | panic("implement me") 26 | } 27 | -------------------------------------------------------------------------------- /pkg/mocks/mock_config_application.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports (interfaces: ConfigApplication) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | models "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/models" 11 | gomock "go.uber.org/mock/gomock" 12 | ) 13 | 14 | // MockConfigApplication is a mock of ConfigApplication interface. 15 | type MockConfigApplication struct { 16 | ctrl *gomock.Controller 17 | recorder *MockConfigApplicationMockRecorder 18 | } 19 | 20 | // MockConfigApplicationMockRecorder is the mock recorder for MockConfigApplication. 21 | type MockConfigApplicationMockRecorder struct { 22 | mock *MockConfigApplication 23 | } 24 | 25 | // NewMockConfigApplication creates a new mock instance. 26 | func NewMockConfigApplication(ctrl *gomock.Controller) *MockConfigApplication { 27 | mock := &MockConfigApplication{ctrl: ctrl} 28 | mock.recorder = &MockConfigApplicationMockRecorder{mock} 29 | return mock 30 | } 31 | 32 | // EXPECT returns an object that allows the caller to indicate expected use. 33 | func (m *MockConfigApplication) EXPECT() *MockConfigApplicationMockRecorder { 34 | return m.recorder 35 | } 36 | 37 | // Config mocks base method. 38 | func (m *MockConfigApplication) Config() error { 39 | m.ctrl.T.Helper() 40 | ret := m.ctrl.Call(m, "Config") 41 | ret0, _ := ret[0].(error) 42 | return ret0 43 | } 44 | 45 | // Config indicates an expected call of Config. 46 | func (mr *MockConfigApplicationMockRecorder) Config() *gomock.Call { 47 | mr.mock.ctrl.T.Helper() 48 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Config", reflect.TypeOf((*MockConfigApplication)(nil).Config)) 49 | } 50 | 51 | // GetConfig mocks base method. 52 | func (m *MockConfigApplication) GetConfig() (*models.Config, error) { 53 | m.ctrl.T.Helper() 54 | ret := m.ctrl.Call(m, "GetConfig") 55 | ret0, _ := ret[0].(*models.Config) 56 | ret1, _ := ret[1].(error) 57 | return ret0, ret1 58 | } 59 | 60 | // GetConfig indicates an expected call of GetConfig. 61 | func (mr *MockConfigApplicationMockRecorder) GetConfig() *gomock.Call { 62 | mr.mock.ctrl.T.Helper() 63 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", reflect.TypeOf((*MockConfigApplication)(nil).GetConfig)) 64 | } 65 | -------------------------------------------------------------------------------- /pkg/mocks/mock_config_repository.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports (interfaces: ConfigRepository) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | os "os" 9 | reflect "reflect" 10 | 11 | gomock "go.uber.org/mock/gomock" 12 | ) 13 | 14 | // MockConfigRepository is a mock of ConfigRepository interface. 15 | type MockConfigRepository struct { 16 | ctrl *gomock.Controller 17 | recorder *MockConfigRepositoryMockRecorder 18 | } 19 | 20 | // MockConfigRepositoryMockRecorder is the mock recorder for MockConfigRepository. 21 | type MockConfigRepositoryMockRecorder struct { 22 | mock *MockConfigRepository 23 | } 24 | 25 | // NewMockConfigRepository creates a new mock instance. 26 | func NewMockConfigRepository(ctrl *gomock.Controller) *MockConfigRepository { 27 | mock := &MockConfigRepository{ctrl: ctrl} 28 | mock.recorder = &MockConfigRepositoryMockRecorder{mock} 29 | return mock 30 | } 31 | 32 | // EXPECT returns an object that allows the caller to indicate expected use. 33 | func (m *MockConfigRepository) EXPECT() *MockConfigRepositoryMockRecorder { 34 | return m.recorder 35 | } 36 | 37 | // GetConfigFile mocks base method. 38 | func (m *MockConfigRepository) GetConfigFile() (*os.File, error) { 39 | m.ctrl.T.Helper() 40 | ret := m.ctrl.Call(m, "GetConfigFile") 41 | ret0, _ := ret[0].(*os.File) 42 | ret1, _ := ret[1].(error) 43 | return ret0, ret1 44 | } 45 | 46 | // GetConfigFile indicates an expected call of GetConfigFile. 47 | func (mr *MockConfigRepositoryMockRecorder) GetConfigFile() *gomock.Call { 48 | mr.mock.ctrl.T.Helper() 49 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfigFile", reflect.TypeOf((*MockConfigRepository)(nil).GetConfigFile)) 50 | } 51 | -------------------------------------------------------------------------------- /pkg/mocks/mock_logger_application.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports (interfaces: LoggerApplication) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | gomock "go.uber.org/mock/gomock" 11 | ) 12 | 13 | // MockLoggerApplication is a mock of LoggerApplication interface. 14 | type MockLoggerApplication struct { 15 | ctrl *gomock.Controller 16 | recorder *MockLoggerApplicationMockRecorder 17 | } 18 | 19 | // MockLoggerApplicationMockRecorder is the mock recorder for MockLoggerApplication. 20 | type MockLoggerApplicationMockRecorder struct { 21 | mock *MockLoggerApplication 22 | } 23 | 24 | // NewMockLoggerApplication creates a new mock instance. 25 | func NewMockLoggerApplication(ctrl *gomock.Controller) *MockLoggerApplication { 26 | mock := &MockLoggerApplication{ctrl: ctrl} 27 | mock.recorder = &MockLoggerApplicationMockRecorder{mock} 28 | return mock 29 | } 30 | 31 | // EXPECT returns an object that allows the caller to indicate expected use. 32 | func (m *MockLoggerApplication) EXPECT() *MockLoggerApplicationMockRecorder { 33 | return m.recorder 34 | } 35 | 36 | // Close mocks base method. 37 | func (m *MockLoggerApplication) Close() error { 38 | m.ctrl.T.Helper() 39 | ret := m.ctrl.Call(m, "Close") 40 | ret0, _ := ret[0].(error) 41 | return ret0 42 | } 43 | 44 | // Close indicates an expected call of Close. 45 | func (mr *MockLoggerApplicationMockRecorder) Close() *gomock.Call { 46 | mr.mock.ctrl.T.Helper() 47 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockLoggerApplication)(nil).Close)) 48 | } 49 | 50 | // Debug mocks base method. 51 | func (m *MockLoggerApplication) Debug(arg0 string, arg1 ...interface{}) { 52 | m.ctrl.T.Helper() 53 | varargs := []interface{}{arg0} 54 | for _, a := range arg1 { 55 | varargs = append(varargs, a) 56 | } 57 | m.ctrl.Call(m, "Debug", varargs...) 58 | } 59 | 60 | // Debug indicates an expected call of Debug. 61 | func (mr *MockLoggerApplicationMockRecorder) Debug(arg0 interface{}, arg1 ...interface{}) *gomock.Call { 62 | mr.mock.ctrl.T.Helper() 63 | varargs := append([]interface{}{arg0}, arg1...) 64 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Debug", reflect.TypeOf((*MockLoggerApplication)(nil).Debug), varargs...) 65 | } 66 | 67 | // Error mocks base method. 68 | func (m *MockLoggerApplication) Error(arg0 string, arg1 ...interface{}) { 69 | m.ctrl.T.Helper() 70 | varargs := []interface{}{arg0} 71 | for _, a := range arg1 { 72 | varargs = append(varargs, a) 73 | } 74 | m.ctrl.Call(m, "Error", varargs...) 75 | } 76 | 77 | // Error indicates an expected call of Error. 78 | func (mr *MockLoggerApplicationMockRecorder) Error(arg0 interface{}, arg1 ...interface{}) *gomock.Call { 79 | mr.mock.ctrl.T.Helper() 80 | varargs := append([]interface{}{arg0}, arg1...) 81 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockLoggerApplication)(nil).Error), varargs...) 82 | } 83 | 84 | // Info mocks base method. 85 | func (m *MockLoggerApplication) Info(arg0 string, arg1 ...interface{}) { 86 | m.ctrl.T.Helper() 87 | varargs := []interface{}{arg0} 88 | for _, a := range arg1 { 89 | varargs = append(varargs, a) 90 | } 91 | m.ctrl.Call(m, "Info", varargs...) 92 | } 93 | 94 | // Info indicates an expected call of Info. 95 | func (mr *MockLoggerApplicationMockRecorder) Info(arg0 interface{}, arg1 ...interface{}) *gomock.Call { 96 | mr.mock.ctrl.T.Helper() 97 | varargs := append([]interface{}{arg0}, arg1...) 98 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockLoggerApplication)(nil).Info), varargs...) 99 | } 100 | 101 | // Warn mocks base method. 102 | func (m *MockLoggerApplication) Warn(arg0 string, arg1 ...interface{}) { 103 | m.ctrl.T.Helper() 104 | varargs := []interface{}{arg0} 105 | for _, a := range arg1 { 106 | varargs = append(varargs, a) 107 | } 108 | m.ctrl.Call(m, "Warn", varargs...) 109 | } 110 | 111 | // Warn indicates an expected call of Warn. 112 | func (mr *MockLoggerApplicationMockRecorder) Warn(arg0 interface{}, arg1 ...interface{}) *gomock.Call { 113 | mr.mock.ctrl.T.Helper() 114 | varargs := append([]interface{}{arg0}, arg1...) 115 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warn", reflect.TypeOf((*MockLoggerApplication)(nil).Warn), varargs...) 116 | } 117 | -------------------------------------------------------------------------------- /pkg/mocks/mock_logger_repository.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports (interfaces: LoggerRepository) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | models "github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/models" 11 | gomock "go.uber.org/mock/gomock" 12 | ) 13 | 14 | // MockLoggerRepository is a mock of LoggerRepository interface. 15 | type MockLoggerRepository struct { 16 | ctrl *gomock.Controller 17 | recorder *MockLoggerRepositoryMockRecorder 18 | } 19 | 20 | // MockLoggerRepositoryMockRecorder is the mock recorder for MockLoggerRepository. 21 | type MockLoggerRepositoryMockRecorder struct { 22 | mock *MockLoggerRepository 23 | } 24 | 25 | // NewMockLoggerRepository creates a new mock instance. 26 | func NewMockLoggerRepository(ctrl *gomock.Controller) *MockLoggerRepository { 27 | mock := &MockLoggerRepository{ctrl: ctrl} 28 | mock.recorder = &MockLoggerRepositoryMockRecorder{mock} 29 | return mock 30 | } 31 | 32 | // EXPECT returns an object that allows the caller to indicate expected use. 33 | func (m *MockLoggerRepository) EXPECT() *MockLoggerRepositoryMockRecorder { 34 | return m.recorder 35 | } 36 | 37 | // Save mocks base method. 38 | func (m *MockLoggerRepository) Save(arg0 *models.Log) error { 39 | m.ctrl.T.Helper() 40 | ret := m.ctrl.Call(m, "Save", arg0) 41 | ret0, _ := ret[0].(error) 42 | return ret0 43 | } 44 | 45 | // Save indicates an expected call of Save. 46 | func (mr *MockLoggerRepositoryMockRecorder) Save(arg0 interface{}) *gomock.Call { 47 | mr.mock.ctrl.T.Helper() 48 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Save", reflect.TypeOf((*MockLoggerRepository)(nil).Save), arg0) 49 | } 50 | -------------------------------------------------------------------------------- /pkg/mocks/mock_mdl_application.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports (interfaces: MiddlewareApplication) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | gomock "go.uber.org/mock/gomock" 11 | ) 12 | 13 | // MockMiddlewareApplication is a mock of MiddlewareApplication interface. 14 | type MockMiddlewareApplication struct { 15 | ctrl *gomock.Controller 16 | recorder *MockMiddlewareApplicationMockRecorder 17 | } 18 | 19 | // MockMiddlewareApplicationMockRecorder is the mock recorder for MockMiddlewareApplication. 20 | type MockMiddlewareApplicationMockRecorder struct { 21 | mock *MockMiddlewareApplication 22 | } 23 | 24 | // NewMockMiddlewareApplication creates a new mock instance. 25 | func NewMockMiddlewareApplication(ctrl *gomock.Controller) *MockMiddlewareApplication { 26 | mock := &MockMiddlewareApplication{ctrl: ctrl} 27 | mock.recorder = &MockMiddlewareApplicationMockRecorder{mock} 28 | return mock 29 | } 30 | 31 | // EXPECT returns an object that allows the caller to indicate expected use. 32 | func (m *MockMiddlewareApplication) EXPECT() *MockMiddlewareApplicationMockRecorder { 33 | return m.recorder 34 | } 35 | 36 | // Authenticate mocks base method. 37 | func (m *MockMiddlewareApplication) Authenticate(arg0 string) (int, error) { 38 | m.ctrl.T.Helper() 39 | ret := m.ctrl.Call(m, "Authenticate", arg0) 40 | ret0, _ := ret[0].(int) 41 | ret1, _ := ret[1].(error) 42 | return ret0, ret1 43 | } 44 | 45 | // Authenticate indicates an expected call of Authenticate. 46 | func (mr *MockMiddlewareApplicationMockRecorder) Authenticate(arg0 interface{}) *gomock.Call { 47 | mr.mock.ctrl.T.Helper() 48 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Authenticate", reflect.TypeOf((*MockMiddlewareApplication)(nil).Authenticate), arg0) 49 | } 50 | -------------------------------------------------------------------------------- /pkg/mocks/mock_mdl_handlers.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports (interfaces: MiddlewareHandlers) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | fiber "github.com/gofiber/fiber/v2" 11 | gomock "go.uber.org/mock/gomock" 12 | ) 13 | 14 | // MockMiddlewareHandlers is a mock of MiddlewareHandlers interface. 15 | type MockMiddlewareHandlers struct { 16 | ctrl *gomock.Controller 17 | recorder *MockMiddlewareHandlersMockRecorder 18 | } 19 | 20 | // MockMiddlewareHandlersMockRecorder is the mock recorder for MockMiddlewareHandlers. 21 | type MockMiddlewareHandlersMockRecorder struct { 22 | mock *MockMiddlewareHandlers 23 | } 24 | 25 | // NewMockMiddlewareHandlers creates a new mock instance. 26 | func NewMockMiddlewareHandlers(ctrl *gomock.Controller) *MockMiddlewareHandlers { 27 | mock := &MockMiddlewareHandlers{ctrl: ctrl} 28 | mock.recorder = &MockMiddlewareHandlersMockRecorder{mock} 29 | return mock 30 | } 31 | 32 | // EXPECT returns an object that allows the caller to indicate expected use. 33 | func (m *MockMiddlewareHandlers) EXPECT() *MockMiddlewareHandlersMockRecorder { 34 | return m.recorder 35 | } 36 | 37 | // Authenticate mocks base method. 38 | func (m *MockMiddlewareHandlers) Authenticate() func(*fiber.Ctx) error { 39 | m.ctrl.T.Helper() 40 | ret := m.ctrl.Call(m, "Authenticate") 41 | ret0, _ := ret[0].(func(*fiber.Ctx) error) 42 | return ret0 43 | } 44 | 45 | // Authenticate indicates an expected call of Authenticate. 46 | func (mr *MockMiddlewareHandlersMockRecorder) Authenticate() *gomock.Call { 47 | mr.mock.ctrl.T.Helper() 48 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Authenticate", reflect.TypeOf((*MockMiddlewareHandlers)(nil).Authenticate)) 49 | } 50 | -------------------------------------------------------------------------------- /pkg/mocks/mock_mdl_repository.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports (interfaces: MiddlewareRepository) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | gomock "go.uber.org/mock/gomock" 11 | ) 12 | 13 | // MockMiddlewareRepository is a mock of MiddlewareRepository interface. 14 | type MockMiddlewareRepository struct { 15 | ctrl *gomock.Controller 16 | recorder *MockMiddlewareRepositoryMockRecorder 17 | } 18 | 19 | // MockMiddlewareRepositoryMockRecorder is the mock recorder for MockMiddlewareRepository. 20 | type MockMiddlewareRepositoryMockRecorder struct { 21 | mock *MockMiddlewareRepository 22 | } 23 | 24 | // NewMockMiddlewareRepository creates a new mock instance. 25 | func NewMockMiddlewareRepository(ctrl *gomock.Controller) *MockMiddlewareRepository { 26 | mock := &MockMiddlewareRepository{ctrl: ctrl} 27 | mock.recorder = &MockMiddlewareRepositoryMockRecorder{mock} 28 | return mock 29 | } 30 | 31 | // EXPECT returns an object that allows the caller to indicate expected use. 32 | func (m *MockMiddlewareRepository) EXPECT() *MockMiddlewareRepositoryMockRecorder { 33 | return m.recorder 34 | } 35 | 36 | // Authenticate mocks base method. 37 | func (m *MockMiddlewareRepository) Authenticate(arg0 string) error { 38 | m.ctrl.T.Helper() 39 | ret := m.ctrl.Call(m, "Authenticate", arg0) 40 | ret0, _ := ret[0].(error) 41 | return ret0 42 | } 43 | 44 | // Authenticate indicates an expected call of Authenticate. 45 | func (mr *MockMiddlewareRepositoryMockRecorder) Authenticate(arg0 interface{}) *gomock.Call { 46 | mr.mock.ctrl.T.Helper() 47 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Authenticate", reflect.TypeOf((*MockMiddlewareRepository)(nil).Authenticate), arg0) 48 | } 49 | -------------------------------------------------------------------------------- /pkg/mocks/mock_user_application.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports (interfaces: UserApplication) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | models "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 11 | gomock "go.uber.org/mock/gomock" 12 | ) 13 | 14 | // MockUserApplication is a mock of UserApplication interface. 15 | type MockUserApplication struct { 16 | ctrl *gomock.Controller 17 | recorder *MockUserApplicationMockRecorder 18 | } 19 | 20 | // MockUserApplicationMockRecorder is the mock recorder for MockUserApplication. 21 | type MockUserApplicationMockRecorder struct { 22 | mock *MockUserApplication 23 | } 24 | 25 | // NewMockUserApplication creates a new mock instance. 26 | func NewMockUserApplication(ctrl *gomock.Controller) *MockUserApplication { 27 | mock := &MockUserApplication{ctrl: ctrl} 28 | mock.recorder = &MockUserApplicationMockRecorder{mock} 29 | return mock 30 | } 31 | 32 | // EXPECT returns an object that allows the caller to indicate expected use. 33 | func (m *MockUserApplication) EXPECT() *MockUserApplicationMockRecorder { 34 | return m.recorder 35 | } 36 | 37 | // Create mocks base method. 38 | func (m *MockUserApplication) Create(arg0 *models.RegisterRequest) error { 39 | m.ctrl.T.Helper() 40 | ret := m.ctrl.Call(m, "Create", arg0) 41 | ret0, _ := ret[0].(error) 42 | return ret0 43 | } 44 | 45 | // Create indicates an expected call of Create. 46 | func (mr *MockUserApplicationMockRecorder) Create(arg0 interface{}) *gomock.Call { 47 | mr.mock.ctrl.T.Helper() 48 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockUserApplication)(nil).Create), arg0) 49 | } 50 | 51 | // Login mocks base method. 52 | func (m *MockUserApplication) Login(arg0 *models.AuthRequest) (*models.AuthResponse, error) { 53 | m.ctrl.T.Helper() 54 | ret := m.ctrl.Call(m, "Login", arg0) 55 | ret0, _ := ret[0].(*models.AuthResponse) 56 | ret1, _ := ret[1].(error) 57 | return ret0, ret1 58 | } 59 | 60 | // Login indicates an expected call of Login. 61 | func (mr *MockUserApplicationMockRecorder) Login(arg0 interface{}) *gomock.Call { 62 | mr.mock.ctrl.T.Helper() 63 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockUserApplication)(nil).Login), arg0) 64 | } 65 | -------------------------------------------------------------------------------- /pkg/mocks/mock_user_handlers.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports (interfaces: UserHandlers) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | fiber "github.com/gofiber/fiber/v2" 11 | gomock "go.uber.org/mock/gomock" 12 | ) 13 | 14 | // MockUserHandlers is a mock of UserHandlers interface. 15 | type MockUserHandlers struct { 16 | ctrl *gomock.Controller 17 | recorder *MockUserHandlersMockRecorder 18 | } 19 | 20 | // MockUserHandlersMockRecorder is the mock recorder for MockUserHandlers. 21 | type MockUserHandlersMockRecorder struct { 22 | mock *MockUserHandlers 23 | } 24 | 25 | // NewMockUserHandlers creates a new mock instance. 26 | func NewMockUserHandlers(ctrl *gomock.Controller) *MockUserHandlers { 27 | mock := &MockUserHandlers{ctrl: ctrl} 28 | mock.recorder = &MockUserHandlersMockRecorder{mock} 29 | return mock 30 | } 31 | 32 | // EXPECT returns an object that allows the caller to indicate expected use. 33 | func (m *MockUserHandlers) EXPECT() *MockUserHandlersMockRecorder { 34 | return m.recorder 35 | } 36 | 37 | // Login mocks base method. 38 | func (m *MockUserHandlers) Login(arg0 *fiber.Ctx) error { 39 | m.ctrl.T.Helper() 40 | ret := m.ctrl.Call(m, "Login", arg0) 41 | ret0, _ := ret[0].(error) 42 | return ret0 43 | } 44 | 45 | // Login indicates an expected call of Login. 46 | func (mr *MockUserHandlersMockRecorder) Login(arg0 interface{}) *gomock.Call { 47 | mr.mock.ctrl.T.Helper() 48 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockUserHandlers)(nil).Login), arg0) 49 | } 50 | 51 | // Register mocks base method. 52 | func (m *MockUserHandlers) Register(arg0 *fiber.Ctx) error { 53 | m.ctrl.T.Helper() 54 | ret := m.ctrl.Call(m, "Register", arg0) 55 | ret0, _ := ret[0].(error) 56 | return ret0 57 | } 58 | 59 | // Register indicates an expected call of Register. 60 | func (mr *MockUserHandlersMockRecorder) Register(arg0 interface{}) *gomock.Call { 61 | mr.mock.ctrl.T.Helper() 62 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Register", reflect.TypeOf((*MockUserHandlers)(nil).Register), arg0) 63 | } 64 | -------------------------------------------------------------------------------- /pkg/mocks/mock_user_repository.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports (interfaces: UserRepository) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | models "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 11 | gomock "go.uber.org/mock/gomock" 12 | ) 13 | 14 | // MockUserRepository is a mock of UserRepository interface. 15 | type MockUserRepository struct { 16 | ctrl *gomock.Controller 17 | recorder *MockUserRepositoryMockRecorder 18 | } 19 | 20 | // MockUserRepositoryMockRecorder is the mock recorder for MockUserRepository. 21 | type MockUserRepositoryMockRecorder struct { 22 | mock *MockUserRepository 23 | } 24 | 25 | // NewMockUserRepository creates a new mock instance. 26 | func NewMockUserRepository(ctrl *gomock.Controller) *MockUserRepository { 27 | mock := &MockUserRepository{ctrl: ctrl} 28 | mock.recorder = &MockUserRepositoryMockRecorder{mock} 29 | return mock 30 | } 31 | 32 | // EXPECT returns an object that allows the caller to indicate expected use. 33 | func (m *MockUserRepository) EXPECT() *MockUserRepositoryMockRecorder { 34 | return m.recorder 35 | } 36 | 37 | // FindByCredentials mocks base method. 38 | func (m *MockUserRepository) FindByCredentials(arg0 *models.AuthRequest) (*models.User, error) { 39 | m.ctrl.T.Helper() 40 | ret := m.ctrl.Call(m, "FindByCredentials", arg0) 41 | ret0, _ := ret[0].(*models.User) 42 | ret1, _ := ret[1].(error) 43 | return ret0, ret1 44 | } 45 | 46 | // FindByCredentials indicates an expected call of FindByCredentials. 47 | func (mr *MockUserRepositoryMockRecorder) FindByCredentials(arg0 interface{}) *gomock.Call { 48 | mr.mock.ctrl.T.Helper() 49 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindByCredentials", reflect.TypeOf((*MockUserRepository)(nil).FindByCredentials), arg0) 50 | } 51 | 52 | // Save mocks base method. 53 | func (m *MockUserRepository) Save(arg0 *models.User) error { 54 | m.ctrl.T.Helper() 55 | ret := m.ctrl.Call(m, "Save", arg0) 56 | ret0, _ := ret[0].(error) 57 | return ret0 58 | } 59 | 60 | // Save indicates an expected call of Save. 61 | func (mr *MockUserRepositoryMockRecorder) Save(arg0 interface{}) *gomock.Call { 62 | mr.mock.ctrl.T.Helper() 63 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Save", reflect.TypeOf((*MockUserRepository)(nil).Save), arg0) 64 | } 65 | -------------------------------------------------------------------------------- /pkg/mocks/mock_validator_application.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports (interfaces: ValidatorApplication) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | ports "github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports" 11 | gomock "go.uber.org/mock/gomock" 12 | ) 13 | 14 | // MockValidatorApplication is a mock of ValidatorApplication interface. 15 | type MockValidatorApplication struct { 16 | ctrl *gomock.Controller 17 | recorder *MockValidatorApplicationMockRecorder 18 | } 19 | 20 | // MockValidatorApplicationMockRecorder is the mock recorder for MockValidatorApplication. 21 | type MockValidatorApplicationMockRecorder struct { 22 | mock *MockValidatorApplication 23 | } 24 | 25 | // NewMockValidatorApplication creates a new mock instance. 26 | func NewMockValidatorApplication(ctrl *gomock.Controller) *MockValidatorApplication { 27 | mock := &MockValidatorApplication{ctrl: ctrl} 28 | mock.recorder = &MockValidatorApplicationMockRecorder{mock} 29 | return mock 30 | } 31 | 32 | // EXPECT returns an object that allows the caller to indicate expected use. 33 | func (m *MockValidatorApplication) EXPECT() *MockValidatorApplicationMockRecorder { 34 | return m.recorder 35 | } 36 | 37 | // Struct mocks base method. 38 | func (m *MockValidatorApplication) Struct(arg0 ports.Evaluable) error { 39 | m.ctrl.T.Helper() 40 | ret := m.ctrl.Call(m, "Struct", arg0) 41 | ret0, _ := ret[0].(error) 42 | return ret0 43 | } 44 | 45 | // Struct indicates an expected call of Struct. 46 | func (mr *MockValidatorApplicationMockRecorder) Struct(arg0 interface{}) *gomock.Call { 47 | mr.mock.ctrl.T.Helper() 48 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Struct", reflect.TypeOf((*MockValidatorApplication)(nil).Struct), arg0) 49 | } 50 | -------------------------------------------------------------------------------- /pkg/mocks/mock_validator_evaluable.go: -------------------------------------------------------------------------------- 1 | // Code generated by MockGen. DO NOT EDIT. 2 | // Source: github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports (interfaces: Evaluable) 3 | 4 | // Package mocks is a generated GoMock package. 5 | package mocks 6 | 7 | import ( 8 | reflect "reflect" 9 | 10 | gomock "go.uber.org/mock/gomock" 11 | ) 12 | 13 | // MockEvaluable is a mock of Evaluable interface. 14 | type MockEvaluable struct { 15 | ctrl *gomock.Controller 16 | recorder *MockEvaluableMockRecorder 17 | } 18 | 19 | // MockEvaluableMockRecorder is the mock recorder for MockEvaluable. 20 | type MockEvaluableMockRecorder struct { 21 | mock *MockEvaluable 22 | } 23 | 24 | // NewMockEvaluable creates a new mock instance. 25 | func NewMockEvaluable(ctrl *gomock.Controller) *MockEvaluable { 26 | mock := &MockEvaluable{ctrl: ctrl} 27 | mock.recorder = &MockEvaluableMockRecorder{mock} 28 | return mock 29 | } 30 | 31 | // EXPECT returns an object that allows the caller to indicate expected use. 32 | func (m *MockEvaluable) EXPECT() *MockEvaluableMockRecorder { 33 | return m.recorder 34 | } 35 | 36 | // Validate mocks base method. 37 | func (m *MockEvaluable) Validate() error { 38 | m.ctrl.T.Helper() 39 | ret := m.ctrl.Call(m, "Validate") 40 | ret0, _ := ret[0].(error) 41 | return ret0 42 | } 43 | 44 | // Validate indicates an expected call of Validate. 45 | func (mr *MockEvaluableMockRecorder) Validate() *gomock.Call { 46 | mr.mock.ctrl.T.Helper() 47 | return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Validate", reflect.TypeOf((*MockEvaluable)(nil).Validate)) 48 | } 49 | -------------------------------------------------------------------------------- /pkg/server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "github.com/gofiber/fiber/v2/middleware/cors" 5 | "github.com/gofiber/fiber/v2/middleware/logger" 6 | "github.com/gofiber/fiber/v2/middleware/monitor" 7 | 8 | "github.com/gofiber/fiber/v2" 9 | "github.com/gofiber/swagger" 10 | "github.com/golang-jwt/jwt/v4" 11 | userHdl "github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports" 12 | configurator "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports" 13 | mdl "github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports" 14 | ) 15 | 16 | type Server struct { 17 | app *fiber.App 18 | mdl mdl.MiddlewareHandlers 19 | user userHdl.UserHandlers 20 | configurator configurator.ConfigApplication 21 | } 22 | 23 | func NewServer(mdl mdl.MiddlewareHandlers, user userHdl.UserHandlers, config configurator.ConfigApplication) *Server { 24 | return &Server{mdl: mdl, user: user, configurator: config} 25 | } 26 | 27 | func (s *Server) Run(port string) error { 28 | cfg, err := s.configurator.GetConfig() 29 | if err != nil { 30 | return err 31 | } 32 | app := fiber.New() 33 | app.Use(cors.New()) 34 | app.Get("/metrics", monitor.New(monitor.Config{Title: cfg.App.Name + " Metrics"})) 35 | app.Use(logger.New()) 36 | app.Get("/docs/*", swagger.New()) 37 | 38 | v1Public := app.Group("/api/v1") 39 | 40 | // User Endpoints 41 | v1Public.Post("/register", s.user.Register) 42 | v1Public.Post("/login", s.user.Login) 43 | 44 | v1Private := app.Group("/api/v1").Use(s.mdl.Authenticate()) 45 | // Test Endpoint 46 | v1Private.Get("/secret", func(c *fiber.Ctx) error { 47 | user := c.Locals("user").(*jwt.Token) 48 | claims := user.Claims.(jwt.MapClaims) 49 | email := claims["email"].(string) 50 | return c.SendString("Welcome 👋" + email) 51 | 52 | }) 53 | s.app = app 54 | err = app.Listen(":" + port) 55 | if err != nil { 56 | return err 57 | } 58 | return nil 59 | } 60 | 61 | func (s *Server) Stop() error { 62 | err := s.app.Shutdown() 63 | if err != nil { 64 | return err 65 | } 66 | return nil 67 | } 68 | -------------------------------------------------------------------------------- /pkg/server/server_test.go: -------------------------------------------------------------------------------- 1 | package server_test 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | 7 | "github.com/solrac97gr/go-jwt-auth/pkg/config/domain/models" 8 | "github.com/solrac97gr/go-jwt-auth/pkg/mocks" 9 | "github.com/solrac97gr/go-jwt-auth/pkg/server" 10 | "go.uber.org/mock/gomock" 11 | ) 12 | 13 | func TestServer_Run(t *testing.T) { 14 | 15 | tests := map[string]struct { 16 | port string 17 | setup func(handlers *mocks.MockMiddlewareHandlers, user *mocks.MockUserHandlers, config *mocks.MockConfigApplication) 18 | assert func(t *testing.T, err error) 19 | }{ 20 | "should return error if config is not found": { 21 | port: "8080", 22 | setup: func(handlers *mocks.MockMiddlewareHandlers, user *mocks.MockUserHandlers, config *mocks.MockConfigApplication) { 23 | config.EXPECT().GetConfig().Return(nil, errors.New("error")) 24 | }, 25 | assert: func(t *testing.T, err error) { 26 | if err == nil { 27 | t.Error("expected error, got nil") 28 | } 29 | }, 30 | }, 31 | "should return success if server not fails": { 32 | port: "8080", 33 | setup: func(handlers *mocks.MockMiddlewareHandlers, user *mocks.MockUserHandlers, config *mocks.MockConfigApplication) { 34 | config.EXPECT().GetConfig().Return(&models.Config{ 35 | App: &models.App{Name: "test"}, 36 | Database: &models.Database{}, 37 | Server: &models.Server{Port: "8080"}, 38 | JWT: &models.JWT{}, 39 | }, nil) 40 | 41 | handlers.EXPECT().Authenticate().Return(nil) 42 | }, 43 | assert: func(t *testing.T, err error) { 44 | if err != nil { 45 | t.Error("expected error, got nil") 46 | } 47 | }, 48 | }, 49 | } 50 | 51 | for name, tt := range tests { 52 | t.Run(name, func(t *testing.T) { 53 | ctrl := gomock.NewController(t) 54 | handlers := mocks.NewMockMiddlewareHandlers(ctrl) 55 | user := mocks.NewMockUserHandlers(ctrl) 56 | config := mocks.NewMockConfigApplication(ctrl) 57 | if tt.setup != nil { 58 | tt.setup(handlers, user, config) 59 | } 60 | s := server.NewServer(handlers, user, config) 61 | err := s.Run(tt.port) 62 | if tt.assert != nil { 63 | tt.assert(t, err) 64 | } 65 | err = s.Stop() 66 | if err != nil { 67 | t.Fatal(err) 68 | } 69 | }) 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /pkg/utils/password_validation.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import "unicode" 4 | 5 | func IsValid(s string) bool { 6 | var ( 7 | hasMinLen = false 8 | hasUpper = false 9 | hasLower = false 10 | hasNumber = false 11 | hasSpecial = false 12 | ) 13 | if len(s) >= 7 { 14 | hasMinLen = true 15 | } 16 | for _, char := range s { 17 | switch { 18 | case unicode.IsUpper(char): 19 | hasUpper = true 20 | case unicode.IsLower(char): 21 | hasLower = true 22 | case unicode.IsNumber(char): 23 | hasNumber = true 24 | case unicode.IsPunct(char) || unicode.IsSymbol(char): 25 | hasSpecial = true 26 | } 27 | } 28 | return hasMinLen && hasUpper && hasLower && hasNumber && hasSpecial 29 | } 30 | -------------------------------------------------------------------------------- /pkg/utils/password_validation_test.go: -------------------------------------------------------------------------------- 1 | package utils_test 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/pkg/utils" 5 | "testing" 6 | ) 7 | 8 | func TestIsValid(t *testing.T) { 9 | tests := map[string]struct { 10 | password string 11 | want bool 12 | }{ 13 | "valid": { 14 | password: "Aa1!Password", 15 | want: true, 16 | }, 17 | "invalid": { 18 | password: "Aa1Password", 19 | want: false, 20 | }, 21 | } 22 | for name, tt := range tests { 23 | t.Run(name, func(t *testing.T) { 24 | if got := utils.IsValid(tt.password); got != tt.want { 25 | t.Errorf("IsValid() = %v, want %v", got, tt.want) 26 | } 27 | }) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /pkg/validator/application/validator.go: -------------------------------------------------------------------------------- 1 | package application 2 | 3 | import "github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports" 4 | 5 | type Validator struct { 6 | } 7 | 8 | func NewValidator() *Validator { 9 | return &Validator{} 10 | } 11 | 12 | func (v *Validator) Struct(s ports.Evaluable) error { 13 | return s.Validate() 14 | } -------------------------------------------------------------------------------- /pkg/validator/application/validator_test.go: -------------------------------------------------------------------------------- 1 | package application_test 2 | 3 | import ( 4 | "github.com/solrac97gr/go-jwt-auth/internal/user/domain/models" 5 | "github.com/solrac97gr/go-jwt-auth/pkg/validator/application" 6 | "github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports" 7 | "go.mongodb.org/mongo-driver/bson/primitive" 8 | "testing" 9 | "time" 10 | ) 11 | 12 | func TestValidator(t *testing.T) { 13 | test := map[string]struct { 14 | input ports.Evaluable 15 | want error 16 | }{ 17 | "Valid": { 18 | input: &models.User{ 19 | ID: primitive.ObjectID{}, 20 | Email: "test@gmail.com", 21 | Password: "test12345@P", 22 | CreatedAt: time.Now(), 23 | }, 24 | }, 25 | } 26 | 27 | for name, tc := range test { 28 | t.Run(name, func(t *testing.T) { 29 | validator := application.NewValidator() 30 | err := validator.Struct(tc.input) 31 | if err != nil { 32 | t.Errorf("got %v, want %v", err, tc.want) 33 | } 34 | }) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /pkg/validator/domain/ports/validator_ports.go: -------------------------------------------------------------------------------- 1 | package ports 2 | 3 | type ValidatorApplication interface { 4 | Struct(s Evaluable) error 5 | } 6 | 7 | type Evaluable interface { 8 | Validate() error 9 | } 10 | -------------------------------------------------------------------------------- /scripts/build-container.sh: -------------------------------------------------------------------------------- 1 | docker build --tag go-jwt-app -f ./deployment/Dockerfile . 2 | -------------------------------------------------------------------------------- /scripts/generate-coverage-report.sh: -------------------------------------------------------------------------------- 1 | go test -coverprofile=coverage.out ./... && go tool cover -html=coverage.out && rm coverage.out -------------------------------------------------------------------------------- /scripts/generate-docs.sh: -------------------------------------------------------------------------------- 1 | swag init --parseDependency=true -g cmd/http/main.go -------------------------------------------------------------------------------- /scripts/generate-mocks.sh: -------------------------------------------------------------------------------- 1 | mockgen -destination=pkg/mocks/mock_user_application.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports UserApplication && 2 | mockgen -destination=pkg/mocks/mock_user_repository.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports UserRepository && 3 | mockgen -destination=pkg/mocks/mock_user_handlers.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/internal/user/domain/ports UserHandlers && 4 | mockgen -destination=pkg/mocks/mock_mdl_application.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports MiddlewareApplication && 5 | mockgen -destination=pkg/mocks/mock_mdl_handlers.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports MiddlewareHandlers && 6 | mockgen -destination=pkg/mocks/mock_mdl_repository.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/pkg/middleware/domain/ports MiddlewareRepository && 7 | mockgen -destination=pkg/mocks/mock_validator_application.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports ValidatorApplication && 8 | mockgen -destination=pkg/mocks/mock_validator_evaluable.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/pkg/validator/domain/ports Evaluable && 9 | mockgen -destination=pkg/mocks/mock_config_application.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports ConfigApplication && 10 | mockgen -destination=pkg/mocks/mock_config_repository.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/pkg/config/domain/ports ConfigRepository && 11 | mockgen -destination=pkg/mocks/mock_logger_application.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports LoggerApplication && 12 | mockgen -destination=pkg/mocks/mock_logger_repository.go -package=mocks --build_flags=--mod=mod github.com/solrac97gr/go-jwt-auth/pkg/logger/domain/ports LoggerRepository && 13 | echo "Mocks generated successfully 🚀[pkg/mocks]" -------------------------------------------------------------------------------- /scripts/run-container.sh: -------------------------------------------------------------------------------- 1 | docker run --rm go-jwt-app -------------------------------------------------------------------------------- /scripts/run-lint.sh: -------------------------------------------------------------------------------- 1 | golangci-lint run -------------------------------------------------------------------------------- /scripts/run-test.sh: -------------------------------------------------------------------------------- 1 | go test ./... -------------------------------------------------------------------------------- /scripts/run.sh: -------------------------------------------------------------------------------- 1 | go run cmd/http/main.go --------------------------------------------------------------------------------