Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add authentication middleware #31

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions application/services/login_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package services

import (
"context"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -39,8 +38,6 @@ func (ls *LoginServiceImpl) Login(context context.Context, login *dtos.LoginDTO)
}

func (ls *LoginServiceImpl) GenerateToken(login *dtos.LoginDTO) error {
fmt.Println(ls.config.PrivateKeyPath)
fmt.Println("Llego aquí")
encodedKey, error := os.ReadFile(ls.config.PrivateKeyPath)
if error != nil {
return errors.Errors{}.CannotReadFileError("OPENSSH private key")
Expand Down
7 changes: 1 addition & 6 deletions configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package configuration

import (
"encoding/json"
"fmt"
"log"
"os"

Expand All @@ -15,9 +14,5 @@ func LoadCfg(config *models.Config) error {
log.Fatal("Configuration could not be loaded.")
}

error = json.Unmarshal(configFile, config)
fmt.Println("Estoy en la config")
fmt.Println(config)

return error
return json.Unmarshal(configFile, config)
}
29 changes: 25 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package main
import (
"encoding/json"
"log"
"os"

jwtware "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/golang-jwt/jwt/v5"
"todoapp.com/application/services"
"todoapp.com/configuration"
"todoapp.com/domain/models"
Expand All @@ -27,10 +30,19 @@ func main() {
log.Fatal("Error decoding loaded configuration file.")
}

db := connectors.Postgre{}.Connect()
encodedKey, error := os.ReadFile(config.PrivateKeyPath)
if error != nil {
log.Fatal("Error accessing OPENSSH public key.")
}

privateKey, error := jwt.ParseRSAPrivateKeyFromPEM(encodedKey)
if error != nil {
log.Fatal("Error parsing OPENSSH public key.")
}

app := fiber.New(fiber.Config{JSONEncoder: json.Marshal, JSONDecoder: json.Unmarshal})
api := app.Group("/api", logger.New())

db := connectors.Postgre{}.Connect()

// repositories
todosRepository := repositories.NewTodosRepository(db)
Expand All @@ -46,10 +58,19 @@ func main() {
usersController := controllers.NewUsersController(usersService)
loginController := controllers.NewLoginController(loginService)

// routes
// unsecured routes
loginController.Route(app.Group("/api", logger.New()))

// secured routes
app.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{
JWTAlg: jwtware.RS256,
Key: privateKey.Public(),
},
}))
api := app.Group("/api", logger.New())
todosController.Route(api)
usersController.Route(api)
loginController.Route(api)

app.Listen(":3000")
}
4 changes: 4 additions & 0 deletions presentation/controllers/users_controller.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package controllers

import (
// "fmt"
"strconv"

"github.com/gofiber/fiber/v2"
// "github.com/golang-jwt/jwt/v5"
"todoapp.com/application/dtos"
"todoapp.com/domain/interfaces"
customErrors "todoapp.com/presentation/errors"
Expand All @@ -29,6 +31,8 @@ func (uc *UsersController) Route(router fiber.Router) {
}

func (uc *UsersController) GetAll(context *fiber.Ctx) error {
// fmt.Println(context.Locals("email"))

userDTOs := uc.usersService.GetAll(context.Context())

if len(userDTOs) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion presentation/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (_ Messages) CollectionEmptyMessage(itemName string) string { return fmt.Sp
func (_ Messages) IdConflictErrorMessage(itemName string) string { return fmt.Sprintf("The route id and the %s's id are not equal.", itemName) }
func (_ Messages) ItemCreatedSuccessfullyMessage(item interface{}) string { return fmt.Sprintf("%v was created successfully.", item) }
func (_ Messages) ItemDeletedSuccessfullyMessage(itemName string, id uint) string { return fmt.Sprintf("%s with id %d was deleted successfully.", itemName , id) }
func (_ Messages) LoggedInSuccessfullyMessage() string { return fmt.Sprintln("Logged in successfully.") }
func (_ Messages) LoggedInSuccessfullyMessage() string { return fmt.Sprint("Logged in successfully.") }
func (_ Messages) ParsingErrorMessage(itemName string) string { return fmt.Sprintf("The provided %s could not be parsed.", itemName) }
func (_ Messages) ReturningItemsSuccessfullyMessage(length int, itemName string) string { return fmt.Sprintf("Returning %d %s(s).", length, itemName) }
func (_ Messages) RouteFormatErrorMessage(parameter string) string { return fmt.Sprintf("Incorrect format in route's '%s' parameter.", parameter) }
Expand Down