Skip to content

Commit

Permalink
refact: BasicAuth structをconfigから移動した
Browse files Browse the repository at this point in the history
  • Loading branch information
Sardonyx001 committed Feb 29, 2024
1 parent b6f0694 commit b20fadf
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 42 deletions.
21 changes: 0 additions & 21 deletions backend/config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package config

import (
"os"

validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/golang-jwt/jwt/v4"
)

type AuthConfig struct {
Expand All @@ -18,21 +15,3 @@ func LoadAuthConfig() AuthConfig {
RefreshSecret: os.Getenv("REFRESH_SECRET"),
}
}

type BasicAuth struct {
Username string `json:"username" validate:"required" example:"test_username"`
Password string `json:"password" validate:"required" example:"test_password"`
}

func (ba BasicAuth) Validate() error {
return validation.ValidateStruct(&ba,
validation.Field(&ba.Username, validation.Length(8, 255)),
validation.Field(&ba.Password, validation.Length(8, 255)),
)
}

type JwtCustomClaims struct {
ID string `json:"id"`
Admin bool `json:"admin"`
jwt.RegisteredClaims
}
4 changes: 2 additions & 2 deletions backend/middlewares/auth_middleware.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package middlewares

import (
"backend/config"
"backend/stores"
"backend/utils"
"net/http"

"github.com/golang-jwt/jwt/v4"
Expand All @@ -25,7 +25,7 @@ func (m *AuthMiddleware) RestaurantAccess() echo.MiddlewareFunc {
restaurant_id := c.Param("restaurant_id")

token := c.Get("user").(*jwt.Token)
claims := token.Claims.(*config.JwtCustomClaims)
claims := token.Claims.(*utils.JwtCustomClaims)

user, err := m.store.User.GetById(claims.ID)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions backend/server/handlers/admin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handlers

import (
"backend/config"
"backend/services"
"backend/utils"
"net/http"
Expand Down Expand Up @@ -33,7 +32,7 @@ func (h *adminHandler) GetAdminById(c echo.Context) error {
}

func (h *adminHandler) CreateAdmin(c echo.Context) error {
userAuth := new(config.BasicAuth)
userAuth := new(utils.BasicAuth)

if err := c.Bind(userAuth); err != nil {
return c.JSON(http.StatusBadRequest, "Request doesn't match schema")
Expand Down
6 changes: 3 additions & 3 deletions backend/server/handlers/auth.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package handlers

import (
"backend/config"
"backend/logger"
"backend/services"
"backend/utils"
"net/http"

"github.com/labstack/echo/v4"
Expand All @@ -25,7 +25,7 @@ type (
)

func (h *authHandler) LoginForUser(c echo.Context) error {
userAuth := new(config.BasicAuth)
userAuth := new(utils.BasicAuth)

if err := c.Bind(userAuth); err != nil {
return c.JSON(http.StatusBadRequest, "Request doesn't match schema")
Expand All @@ -52,7 +52,7 @@ func (h *authHandler) LoginForUser(c echo.Context) error {
}

func (h *authHandler) LoginForAdmin(c echo.Context) error {
userAuth := new(config.BasicAuth)
userAuth := new(utils.BasicAuth)

if err := c.Bind(userAuth); err != nil {
return c.JSON(http.StatusBadRequest, "Request doesn't match schema")
Expand Down
5 changes: 2 additions & 3 deletions backend/server/handlers/restaurant.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handlers

import (
"backend/config"
"backend/logger"
"backend/models"
"backend/services"
Expand Down Expand Up @@ -30,7 +29,7 @@ type (

func (h *restaurantHandler) GetRestaurants(c echo.Context) error {
token := c.Get("user").(*jwt.Token)
claims := token.Claims.(*config.JwtCustomClaims)
claims := token.Claims.(*utils.JwtCustomClaims)
user, err := h.u.GetUserById(claims.ID)
if err != nil {
logger.Error(err.Error())
Expand All @@ -53,7 +52,7 @@ func (h *restaurantHandler) GetRestaurantById(c echo.Context) error {

func (h *restaurantHandler) CreateRestaurant(c echo.Context) error {
token := c.Get("user").(*jwt.Token)
claims := token.Claims.(*config.JwtCustomClaims)
claims := token.Claims.(*utils.JwtCustomClaims)
user, err := h.u.GetUserById(claims.ID)
if err != nil {
return c.JSON(http.StatusForbidden, "Invalid credentials")
Expand Down
3 changes: 1 addition & 2 deletions backend/server/handlers/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handlers

import (
"backend/config"
"backend/services"
"backend/utils"
"net/http"
Expand Down Expand Up @@ -33,7 +32,7 @@ func (h *userHandler) GetUserById(c echo.Context) error {
}

func (h *userHandler) CreateUser(c echo.Context) error {
userAuth := new(config.BasicAuth)
userAuth := new(utils.BasicAuth)

if err := c.Bind(userAuth); err != nil {
return c.JSON(http.StatusBadRequest, "Request doesn't match schema")
Expand Down
4 changes: 2 additions & 2 deletions backend/server/routes.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package server

import (
"backend/config"
"backend/middlewares"
"backend/server/handlers"
"backend/services"
"backend/stores"
"backend/utils"
"net/http"

"github.com/golang-jwt/jwt/v4"
Expand All @@ -30,7 +30,7 @@ func ConfigureRoutes(server *Server) {
g := server.Echo.Group("api/v1")
jwtConfig := echojwt.Config{
NewClaimsFunc: func(c echo.Context) jwt.Claims {
return new(config.JwtCustomClaims)
return new(utils.JwtCustomClaims)
},
SigningKey: []byte(server.Config.Auth.AccessSecret),
}
Expand Down
6 changes: 3 additions & 3 deletions backend/services/admin.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package services

import (
"backend/config"
"backend/models"
"backend/stores"
"backend/utils"

"golang.org/x/crypto/bcrypt"
)
Expand All @@ -12,7 +12,7 @@ type (
AdminService interface {
GetAdminById(id string) (*models.Admin, error)
GetAdminByUsername(username string) (*models.Admin, error)
CreateAdmin(creds *config.BasicAuth) (string, error)
CreateAdmin(creds *utils.BasicAuth) (string, error)
// UpdateAdminById(user *models.User) (string, error)
// DeleteAdmin(id string) error
}
Expand All @@ -22,7 +22,7 @@ type (
}
)

func (s *adminService) CreateAdmin(creds *config.BasicAuth) (string, error) {
func (s *adminService) CreateAdmin(creds *utils.BasicAuth) (string, error) {
encryptedPassword, err := bcrypt.GenerateFromPassword(
[]byte(creds.Password),
bcrypt.DefaultCost,
Expand Down
3 changes: 2 additions & 1 deletion backend/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"backend/config"
"backend/stores"
"backend/utils"
"time"

"github.com/golang-jwt/jwt/v4"
Expand All @@ -22,7 +23,7 @@ type (
func (s *authService) GenerateAccessToken(id string, admin bool) (accessToken string, exp int64, err error) {
expired := time.Now().Add(time.Hour * 72)

claims := &config.JwtCustomClaims{
claims := &utils.JwtCustomClaims{
ID: id,
Admin: admin,
RegisteredClaims: jwt.RegisteredClaims{
Expand Down
6 changes: 3 additions & 3 deletions backend/services/user.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package services

import (
"backend/config"
"backend/models"
"backend/stores"
"backend/utils"

"golang.org/x/crypto/bcrypt"
)
Expand All @@ -12,7 +12,7 @@ type (
UserService interface {
GetUserById(id string) (*models.User, error)
GetUserByUsername(username string) (*models.User, error)
CreateUser(creds *config.BasicAuth) (string, error)
CreateUser(creds *utils.BasicAuth) (string, error)
DeleteUser(id string) error
}

Expand All @@ -25,7 +25,7 @@ func NewUserSevice(stores *stores.Stores) *userService {
return &userService{stores: stores}
}

func (s *userService) CreateUser(creds *config.BasicAuth) (string, error) {
func (s *userService) CreateUser(creds *utils.BasicAuth) (string, error) {
encryptedPassword, err := bcrypt.GenerateFromPassword(
[]byte(creds.Password),
bcrypt.DefaultCost,
Expand Down
24 changes: 24 additions & 0 deletions backend/utils/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/golang-jwt/jwt/v4"
)

type BasicAuth struct {
Username string `json:"username" validate:"required" example:"test_username"`
Password string `json:"password" validate:"required" example:"test_password"`
}

func (ba BasicAuth) Validate() error {
return validation.ValidateStruct(&ba,
validation.Field(&ba.Username, validation.Length(8, 255)),
validation.Field(&ba.Password, validation.Length(8, 255)),
)
}

type JwtCustomClaims struct {
ID string `json:"id"`
Admin bool `json:"admin"`
jwt.RegisteredClaims
}

0 comments on commit b20fadf

Please sign in to comment.