diff --git a/.github/workflows/build-deploy.yml b/.github/workflows/build-deploy.yml index e66b61e..071f6ea 100644 --- a/.github/workflows/build-deploy.yml +++ b/.github/workflows/build-deploy.yml @@ -32,7 +32,7 @@ jobs: - name: Test run: | - go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/... + go test -v -coverpkg ./internal/... -coverprofile coverage.out -covermode count ./internal/... go tool cover -func="./coverage.out" build: diff --git a/.github/workflows/run-unit-test.yml b/.github/workflows/run-unit-test.yml index 4953875..e64fa2e 100644 --- a/.github/workflows/run-unit-test.yml +++ b/.github/workflows/run-unit-test.yml @@ -32,5 +32,5 @@ jobs: - name: Test run: | - go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/... + go test -v -coverpkg ./internal/... -coverprofile coverage.out -covermode count ./internal/... go tool cover -func="./coverage.out" diff --git a/.gitignore b/.gitignore index 3aa49c7..4c726ea 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,4 @@ coverage.html .DS_store -src/internal/repository/user/memory: +internal/repository/user/memory: diff --git a/Dockerfile b/Dockerfile index 4fc6518..707c676 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,10 +11,10 @@ COPY go.mod go.sum ./ RUN go mod download # Copy the source code -COPY ./src ./src +COPY . . # Build the application -RUN go build -o server ./src/. +RUN go build -o server ./cmd/main.go # Copy config files COPY ./config ./config diff --git a/Makefile b/Makefile index 4bd008c..4f6ced1 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,13 @@ server: - go run ./src/. + go run ./cmd/. mock-gen: - mockgen -source ./src/pkg/repository/cache/cache.repository.go -destination ./src/mocks/repository/cache/cache.mock.go - mockgen -source ./src/pkg/repository/auth/auth.repository.go -destination ./src/mocks/repository/auth/auth.mock.go + mockgen -source ./pkg/repository/cache/cache.repository.go -destination ./mocks/repository/cache/cache.mock.go + mockgen -source ./pkg/repository/auth/auth.repository.go -destination ./mocks/repository/auth/auth.mock.go test: go vet ./... - go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/... + go test -v -coverpkg ./internal/... -coverprofile coverage.out -covermode count ./internal/... go tool cover -func=coverage.out go tool cover -html=coverage.out -o coverage.html diff --git a/src/config/config.go b/cfgldr/config.go similarity index 99% rename from src/config/config.go rename to cfgldr/config.go index 1e011ef..2983ef4 100644 --- a/src/config/config.go +++ b/cfgldr/config.go @@ -1,4 +1,4 @@ -package config +package cfgldr import ( "github.com/pkg/errors" diff --git a/src/main.go b/cmd/main.go similarity index 85% rename from src/main.go rename to cmd/main.go index 3fe8b03..bc0ad6d 100644 --- a/src/main.go +++ b/cmd/main.go @@ -3,6 +3,15 @@ package main import ( "context" "fmt" + "github.com/isd-sgcu/johnjud-auth/cfgldr" + "github.com/isd-sgcu/johnjud-auth/database" + authRp "github.com/isd-sgcu/johnjud-auth/internal/repository/auth" + cacheRp "github.com/isd-sgcu/johnjud-auth/internal/repository/cache" + userRp "github.com/isd-sgcu/johnjud-auth/internal/repository/user" + authSvc "github.com/isd-sgcu/johnjud-auth/internal/service/auth" + jwtSvc "github.com/isd-sgcu/johnjud-auth/internal/service/jwt" + tokenSvc "github.com/isd-sgcu/johnjud-auth/internal/service/token" + userSvc "github.com/isd-sgcu/johnjud-auth/internal/service/user" "net" "os" "os/signal" @@ -10,17 +19,8 @@ import ( "syscall" "time" - "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/database" - "github.com/isd-sgcu/johnjud-auth/src/internal/strategy" - "github.com/isd-sgcu/johnjud-auth/src/internal/utils" - authRp "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/auth" - cacheRp "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/cache" - userRp "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user" - authSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/auth" - jwtSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/jwt" - tokenSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/token" - userSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/user" + "github.com/isd-sgcu/johnjud-auth/internal/strategy" + "github.com/isd-sgcu/johnjud-auth/internal/utils" authPb "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" userPb "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" "github.com/rs/zerolog/log" @@ -87,7 +87,7 @@ func gracefulShutdown(ctx context.Context, timeout time.Duration, ops map[string } func main() { - conf, err := config.LoadConfig() + conf, err := cfgldr.LoadConfig() if err != nil { log.Fatal(). Err(err). diff --git a/src/database/postgresql.connection.go b/database/postgresql.connection.go similarity index 79% rename from src/database/postgresql.connection.go rename to database/postgresql.connection.go index b8d14ce..60b6a5b 100644 --- a/src/database/postgresql.connection.go +++ b/database/postgresql.connection.go @@ -2,15 +2,15 @@ package database import ( "fmt" - "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/cfgldr" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" "gorm.io/driver/postgres" "gorm.io/gorm" gormLogger "gorm.io/gorm/logger" "strconv" ) -func InitPostgresDatabase(conf *config.Database, isDebug bool) (db *gorm.DB, err error) { +func InitPostgresDatabase(conf *cfgldr.Database, isDebug bool) (db *gorm.DB, err error) { dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", conf.Host, strconv.Itoa(conf.Port), conf.Username, conf.Password, conf.Name, conf.SSL) gormConf := &gorm.Config{TranslateError: true} diff --git a/src/database/redis.connection.go b/database/redis.connection.go similarity index 77% rename from src/database/redis.connection.go rename to database/redis.connection.go index eab03f0..6e10cf0 100644 --- a/src/database/redis.connection.go +++ b/database/redis.connection.go @@ -2,12 +2,12 @@ package database import ( "fmt" - "github.com/isd-sgcu/johnjud-auth/src/config" + "github.com/isd-sgcu/johnjud-auth/cfgldr" "github.com/pkg/errors" "github.com/redis/go-redis/v9" ) -func InitRedisConnection(conf *config.Redis) (*redis.Client, error) { +func InitRedisConnection(conf *cfgldr.Redis) (*redis.Client, error) { addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port) cache := redis.NewClient(&redis.Options{ diff --git a/src/internal/constant/error.constant.go b/internal/constant/error.constant.go similarity index 100% rename from src/internal/constant/error.constant.go rename to internal/constant/error.constant.go diff --git a/src/internal/constant/user.constant.go b/internal/constant/user.constant.go similarity index 100% rename from src/internal/constant/user.constant.go rename to internal/constant/user.constant.go diff --git a/src/internal/domain/dto/token/token.dto.go b/internal/domain/dto/token/token.dto.go similarity index 92% rename from src/internal/domain/dto/token/token.dto.go rename to internal/domain/dto/token/token.dto.go index b538444..e9450f0 100644 --- a/src/internal/domain/dto/token/token.dto.go +++ b/internal/domain/dto/token/token.dto.go @@ -2,7 +2,7 @@ package token import ( "github.com/golang-jwt/jwt/v4" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" + "github.com/isd-sgcu/johnjud-auth/internal/constant" ) type UserCredential struct { diff --git a/src/internal/domain/model/auth.model.go b/internal/domain/model/auth.model.go similarity index 100% rename from src/internal/domain/model/auth.model.go rename to internal/domain/model/auth.model.go diff --git a/src/internal/domain/model/common.model.go b/internal/domain/model/common.model.go similarity index 100% rename from src/internal/domain/model/common.model.go rename to internal/domain/model/common.model.go diff --git a/src/internal/domain/model/user.model.go b/internal/domain/model/user.model.go similarity index 83% rename from src/internal/domain/model/user.model.go rename to internal/domain/model/user.model.go index 48560f3..bac59f1 100644 --- a/src/internal/domain/model/user.model.go +++ b/internal/domain/model/user.model.go @@ -1,6 +1,8 @@ package model -import "github.com/isd-sgcu/johnjud-auth/src/internal/constant" +import ( + "github.com/isd-sgcu/johnjud-auth/internal/constant" +) type User struct { Base diff --git a/src/internal/repository/auth/auth.repository.go b/internal/repository/auth/auth.repository.go similarity index 67% rename from src/internal/repository/auth/auth.repository.go rename to internal/repository/auth/auth.repository.go index 26998ae..18d63d8 100644 --- a/src/internal/repository/auth/auth.repository.go +++ b/internal/repository/auth/auth.repository.go @@ -1,7 +1,8 @@ package auth import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/pkg/repository/auth" "gorm.io/gorm" ) @@ -9,7 +10,7 @@ type repositoryImpl struct { Db *gorm.DB } -func NewRepository(db *gorm.DB) *repositoryImpl { +func NewRepository(db *gorm.DB) auth.Repository { return &repositoryImpl{Db: db} } diff --git a/src/internal/repository/auth/auth.repository_test.go b/internal/repository/auth/auth.repository_test.go similarity index 90% rename from src/internal/repository/auth/auth.repository_test.go rename to internal/repository/auth/auth.repository_test.go index c6f7d04..ff1e245 100644 --- a/src/internal/repository/auth/auth.repository_test.go +++ b/internal/repository/auth/auth.repository_test.go @@ -3,7 +3,8 @@ package auth import ( "fmt" "github.com/google/uuid" - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/pkg/repository/auth" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "gorm.io/driver/postgres" @@ -14,7 +15,7 @@ import ( type AuthRepositoryTest struct { suite.Suite db *gorm.DB - authRepo *repositoryImpl + authRepo auth.Repository } func TestAuthRepository(t *testing.T) { diff --git a/src/internal/repository/cache/cache.repository.go b/internal/repository/cache/cache.repository.go similarity index 89% rename from src/internal/repository/cache/cache.repository.go rename to internal/repository/cache/cache.repository.go index d564876..c6e2cd5 100644 --- a/src/internal/repository/cache/cache.repository.go +++ b/internal/repository/cache/cache.repository.go @@ -3,6 +3,7 @@ package cache import ( "context" "encoding/json" + "github.com/isd-sgcu/johnjud-auth/pkg/repository/cache" "github.com/redis/go-redis/v9" "time" ) @@ -11,7 +12,7 @@ type repositoryImpl struct { client *redis.Client } -func NewRepository(client *redis.Client) *repositoryImpl { +func NewRepository(client *redis.Client) cache.Repository { return &repositoryImpl{client: client} } diff --git a/src/internal/repository/cache/cache.repository_test.go b/internal/repository/cache/cache.repository_test.go similarity index 89% rename from src/internal/repository/cache/cache.repository_test.go rename to internal/repository/cache/cache.repository_test.go index fbdaf05..af4b7f6 100644 --- a/src/internal/repository/cache/cache.repository_test.go +++ b/internal/repository/cache/cache.repository_test.go @@ -4,7 +4,8 @@ import ( "context" "fmt" "github.com/go-faker/faker/v4" - tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token" + tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token" + "github.com/isd-sgcu/johnjud-auth/pkg/repository/cache" "github.com/redis/go-redis/v9" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" @@ -15,7 +16,7 @@ import ( type CacheRepositoryTest struct { suite.Suite cacheDb *redis.Client - cacheRepo *repositoryImpl + cacheRepo cache.Repository key string value *tokenDto.AccessTokenCache } @@ -41,7 +42,7 @@ func (t *CacheRepositoryTest) SetupTest() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - err := cacheRepo.client.FlushDB(ctx).Err() + err := cacheDb.FlushDB(ctx).Err() assert.Nil(t.T(), err) err = cacheRepo.SetValue(key, value, 60) diff --git a/src/internal/repository/user/user.repository.go b/internal/repository/user/user.repository.go similarity index 83% rename from src/internal/repository/user/user.repository.go rename to internal/repository/user/user.repository.go index b325b56..bb84065 100644 --- a/src/internal/repository/user/user.repository.go +++ b/internal/repository/user/user.repository.go @@ -1,7 +1,8 @@ package user import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/pkg/repository/user" "gorm.io/gorm" ) @@ -9,7 +10,7 @@ type repositoryImpl struct { Db *gorm.DB } -func NewRepository(db *gorm.DB) *repositoryImpl { +func NewRepository(db *gorm.DB) user.Repository { return &repositoryImpl{Db: db} } diff --git a/src/internal/repository/user/user.repository_test.go b/internal/repository/user/user.repository_test.go similarity index 96% rename from src/internal/repository/user/user.repository_test.go rename to internal/repository/user/user.repository_test.go index 2b858e7..6499103 100644 --- a/src/internal/repository/user/user.repository_test.go +++ b/internal/repository/user/user.repository_test.go @@ -3,8 +3,9 @@ package user import ( "fmt" "github.com/go-faker/faker/v4" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/internal/constant" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/pkg/repository/user" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "gorm.io/driver/postgres" @@ -15,7 +16,7 @@ import ( type UserRepositoryTest struct { suite.Suite db *gorm.DB - userRepo *repositoryImpl + userRepo user.Repository initialUser *model.User } diff --git a/src/internal/service/auth/auth.service.go b/internal/service/auth/auth.service.go similarity index 89% rename from src/internal/service/auth/auth.service.go rename to internal/service/auth/auth.service.go index f2c5064..2467af7 100644 --- a/src/internal/service/auth/auth.service.go +++ b/internal/service/auth/auth.service.go @@ -2,14 +2,14 @@ package auth import ( "context" + "github.com/isd-sgcu/johnjud-auth/internal/constant" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/internal/utils" + "github.com/isd-sgcu/johnjud-auth/pkg/repository/auth" + "github.com/isd-sgcu/johnjud-auth/pkg/repository/user" + "github.com/isd-sgcu/johnjud-auth/pkg/service/token" "github.com/google/uuid" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" - "github.com/isd-sgcu/johnjud-auth/src/internal/utils" - "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/auth" - "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user" - "github.com/isd-sgcu/johnjud-auth/src/pkg/service/token" authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" "github.com/pkg/errors" "google.golang.org/grpc/codes" @@ -25,7 +25,7 @@ type serviceImpl struct { bcryptUtil utils.IBcryptUtil } -func NewService(authRepo auth.Repository, userRepo user.Repository, tokenService token.Service, bcryptUtil utils.IBcryptUtil) *serviceImpl { +func NewService(authRepo auth.Repository, userRepo user.Repository, tokenService token.Service, bcryptUtil utils.IBcryptUtil) authProto.AuthServiceServer { return &serviceImpl{ authRepo: authRepo, userRepo: userRepo, diff --git a/src/internal/service/auth/auth.service_test.go b/internal/service/auth/auth.service_test.go similarity index 97% rename from src/internal/service/auth/auth.service_test.go rename to internal/service/auth/auth.service_test.go index 5035a43..48d5589 100644 --- a/src/internal/service/auth/auth.service_test.go +++ b/internal/service/auth/auth.service_test.go @@ -2,19 +2,19 @@ package auth import ( "context" + "github.com/isd-sgcu/johnjud-auth/internal/constant" + tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/mocks/repository/auth" + "github.com/isd-sgcu/johnjud-auth/mocks/repository/user" + "github.com/isd-sgcu/johnjud-auth/mocks/service/token" + "github.com/isd-sgcu/johnjud-auth/mocks/utils" "testing" "time" "github.com/go-faker/faker/v4" "github.com/golang/mock/gomock" "github.com/google/uuid" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token" - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" - mock_auth "github.com/isd-sgcu/johnjud-auth/src/mocks/repository/auth" - "github.com/isd-sgcu/johnjud-auth/src/mocks/repository/user" - "github.com/isd-sgcu/johnjud-auth/src/mocks/service/token" - "github.com/isd-sgcu/johnjud-auth/src/mocks/utils" authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" "github.com/pkg/errors" "github.com/stretchr/testify/assert" diff --git a/src/internal/service/jwt/jwt.service.go b/internal/service/jwt/jwt.service.go similarity index 69% rename from src/internal/service/jwt/jwt.service.go rename to internal/service/jwt/jwt.service.go index 7731531..1b691a5 100644 --- a/src/internal/service/jwt/jwt.service.go +++ b/internal/service/jwt/jwt.service.go @@ -3,22 +3,23 @@ package jwt import ( "fmt" _jwt "github.com/golang-jwt/jwt/v4" - "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token" - "github.com/isd-sgcu/johnjud-auth/src/internal/utils" - "github.com/isd-sgcu/johnjud-auth/src/pkg/strategy" + "github.com/isd-sgcu/johnjud-auth/cfgldr" + "github.com/isd-sgcu/johnjud-auth/internal/constant" + tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token" + "github.com/isd-sgcu/johnjud-auth/internal/utils" + "github.com/isd-sgcu/johnjud-auth/pkg/service/jwt" + "github.com/isd-sgcu/johnjud-auth/pkg/strategy" "github.com/pkg/errors" "time" ) type serviceImpl struct { - config config.Jwt + config cfgldr.Jwt strategy strategy.JwtStrategy jwtUtil utils.IJwtUtil } -func NewService(config config.Jwt, strategy strategy.JwtStrategy, jwtUtil utils.IJwtUtil) *serviceImpl { +func NewService(config cfgldr.Jwt, strategy strategy.JwtStrategy, jwtUtil utils.IJwtUtil) jwt.Service { return &serviceImpl{config: config, strategy: strategy, jwtUtil: jwtUtil} } @@ -47,6 +48,6 @@ func (s *serviceImpl) VerifyAuth(token string) (*_jwt.Token, error) { return s.jwtUtil.ParseToken(token, s.strategy.AuthDecode) } -func (s *serviceImpl) GetConfig() *config.Jwt { +func (s *serviceImpl) GetConfig() *cfgldr.Jwt { return &s.config } diff --git a/src/internal/service/jwt/jwt.service_test.go b/internal/service/jwt/jwt.service_test.go similarity index 92% rename from src/internal/service/jwt/jwt.service_test.go rename to internal/service/jwt/jwt.service_test.go index a1b6230..ffddc76 100644 --- a/src/internal/service/jwt/jwt.service_test.go +++ b/internal/service/jwt/jwt.service_test.go @@ -4,11 +4,11 @@ import ( "fmt" "github.com/go-faker/faker/v4" "github.com/golang-jwt/jwt/v4" - "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token" - "github.com/isd-sgcu/johnjud-auth/src/mocks/strategy" - "github.com/isd-sgcu/johnjud-auth/src/mocks/utils" + "github.com/isd-sgcu/johnjud-auth/cfgldr" + "github.com/isd-sgcu/johnjud-auth/internal/constant" + tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token" + "github.com/isd-sgcu/johnjud-auth/mocks/strategy" + "github.com/isd-sgcu/johnjud-auth/mocks/utils" "github.com/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -19,7 +19,7 @@ import ( type JwtServiceTest struct { suite.Suite - config config.Jwt + config cfgldr.Jwt userId string role constant.Role authSessionId string @@ -33,7 +33,7 @@ func TestJwtService(t *testing.T) { } func (t *JwtServiceTest) SetupTest() { - config := config.Jwt{ + config := cfgldr.Jwt{ Secret: "testSecret", ExpiresIn: 3600, Issuer: "testIssuer", diff --git a/src/internal/service/token/token.service.go b/internal/service/token/token.service.go similarity index 90% rename from src/internal/service/token/token.service.go rename to internal/service/token/token.service.go index 383f9aa..bd6a016 100644 --- a/src/internal/service/token/token.service.go +++ b/internal/service/token/token.service.go @@ -2,11 +2,12 @@ package token import ( _jwt "github.com/golang-jwt/jwt/v4" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token" - "github.com/isd-sgcu/johnjud-auth/src/internal/utils" - "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/cache" - "github.com/isd-sgcu/johnjud-auth/src/pkg/service/jwt" + "github.com/isd-sgcu/johnjud-auth/internal/constant" + tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token" + "github.com/isd-sgcu/johnjud-auth/internal/utils" + "github.com/isd-sgcu/johnjud-auth/pkg/repository/cache" + "github.com/isd-sgcu/johnjud-auth/pkg/service/jwt" + "github.com/isd-sgcu/johnjud-auth/pkg/service/token" authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" "github.com/pkg/errors" "github.com/redis/go-redis/v9" @@ -20,7 +21,7 @@ type serviceImpl struct { uuidUtil utils.IUuidUtil } -func NewService(jwtService jwt.Service, accessTokenCache cache.Repository, refreshTokenCache cache.Repository, uuidUtil utils.IUuidUtil) *serviceImpl { +func NewService(jwtService jwt.Service, accessTokenCache cache.Repository, refreshTokenCache cache.Repository, uuidUtil utils.IUuidUtil) token.Service { return &serviceImpl{ jwtService: jwtService, accessTokenCache: accessTokenCache, diff --git a/src/internal/service/token/token.service_test.go b/internal/service/token/token.service_test.go similarity index 97% rename from src/internal/service/token/token.service_test.go rename to internal/service/token/token.service_test.go index ad1cd71..d16cae0 100644 --- a/src/internal/service/token/token.service_test.go +++ b/internal/service/token/token.service_test.go @@ -5,12 +5,12 @@ import ( _jwt "github.com/golang-jwt/jwt/v4" "github.com/golang/mock/gomock" "github.com/google/uuid" - "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token" - mock_cache "github.com/isd-sgcu/johnjud-auth/src/mocks/repository/cache" - "github.com/isd-sgcu/johnjud-auth/src/mocks/service/jwt" - "github.com/isd-sgcu/johnjud-auth/src/mocks/utils" + "github.com/isd-sgcu/johnjud-auth/cfgldr" + "github.com/isd-sgcu/johnjud-auth/internal/constant" + tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token" + "github.com/isd-sgcu/johnjud-auth/mocks/repository/cache" + "github.com/isd-sgcu/johnjud-auth/mocks/service/jwt" + "github.com/isd-sgcu/johnjud-auth/mocks/utils" authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" "github.com/pkg/errors" "github.com/redis/go-redis/v9" @@ -27,7 +27,7 @@ type TokenServiceTest struct { authSessionId string accessToken string refreshToken *uuid.UUID - jwtConfig *config.Jwt + jwtConfig *cfgldr.Jwt validateToken string } @@ -41,7 +41,7 @@ func (t *TokenServiceTest) SetupTest() { authSessionId := faker.UUIDDigit() accessToken := "testAccessToken" refreshToken := uuid.New() - jwtConfig := &config.Jwt{ + jwtConfig := &cfgldr.Jwt{ Secret: "testSecret", ExpiresIn: 3600, RefreshTokenTTL: 604800, diff --git a/src/internal/service/user/user.service.go b/internal/service/user/user.service.go similarity index 88% rename from src/internal/service/user/user.service.go rename to internal/service/user/user.service.go index ab31db0..fe9036e 100644 --- a/src/internal/service/user/user.service.go +++ b/internal/service/user/user.service.go @@ -3,11 +3,11 @@ package user import ( "context" "errors" + "github.com/isd-sgcu/johnjud-auth/internal/constant" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/internal/utils" + userRepo "github.com/isd-sgcu/johnjud-auth/pkg/repository/user" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" - "github.com/isd-sgcu/johnjud-auth/src/internal/utils" - userRepo "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user" proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -20,7 +20,7 @@ type serviceImpl struct { bcryptUtil utils.IBcryptUtil } -func NewService(repo userRepo.Repository, bcryptUtil utils.IBcryptUtil) *serviceImpl { +func NewService(repo userRepo.Repository, bcryptUtil utils.IBcryptUtil) proto.UserServiceServer { return &serviceImpl{repo: repo, bcryptUtil: bcryptUtil} } diff --git a/src/internal/service/user/user.service_test.go b/internal/service/user/user.service_test.go similarity index 94% rename from src/internal/service/user/user.service_test.go rename to internal/service/user/user.service_test.go index 5069207..70dd9ac 100644 --- a/src/internal/service/user/user.service_test.go +++ b/internal/service/user/user.service_test.go @@ -3,15 +3,15 @@ package user import ( "context" "errors" + "github.com/isd-sgcu/johnjud-auth/cfgldr" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" + mock "github.com/isd-sgcu/johnjud-auth/mocks/repository/user" + "github.com/isd-sgcu/johnjud-auth/mocks/utils" "testing" "time" "github.com/go-faker/faker/v4" "github.com/google/uuid" - "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" - mock "github.com/isd-sgcu/johnjud-auth/src/mocks/repository/user" - "github.com/isd-sgcu/johnjud-auth/src/mocks/utils" proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" @@ -22,7 +22,7 @@ import ( type UserServiceTest struct { suite.Suite - config *config.App + config *cfgldr.App User *model.User UpdateUser *model.User UserDto *proto.User diff --git a/src/internal/strategy/jwt.strategy.go b/internal/strategy/jwt.strategy.go similarity index 80% rename from src/internal/strategy/jwt.strategy.go rename to internal/strategy/jwt.strategy.go index a7454da..82e341e 100644 --- a/src/internal/strategy/jwt.strategy.go +++ b/internal/strategy/jwt.strategy.go @@ -3,6 +3,7 @@ package strategy import ( "fmt" "github.com/golang-jwt/jwt/v4" + "github.com/isd-sgcu/johnjud-auth/pkg/strategy" "github.com/pkg/errors" ) @@ -10,7 +11,7 @@ type jwtStrategyImpl struct { secret string } -func NewJwtStrategy(secret string) *jwtStrategyImpl { +func NewJwtStrategy(secret string) strategy.JwtStrategy { return &jwtStrategyImpl{secret: secret} } diff --git a/src/internal/strategy/jwt.strategy_test.go b/internal/strategy/jwt.strategy_test.go similarity index 100% rename from src/internal/strategy/jwt.strategy_test.go rename to internal/strategy/jwt.strategy_test.go diff --git a/src/internal/utils/bcrypt.utils.go b/internal/utils/bcrypt.utils.go similarity index 94% rename from src/internal/utils/bcrypt.utils.go rename to internal/utils/bcrypt.utils.go index 16e74a1..6d74c78 100644 --- a/src/internal/utils/bcrypt.utils.go +++ b/internal/utils/bcrypt.utils.go @@ -9,7 +9,7 @@ type IBcryptUtil interface { type bcryptUtil struct{} -func NewBcryptUtil() *bcryptUtil { +func NewBcryptUtil() IBcryptUtil { return &bcryptUtil{} } diff --git a/src/internal/utils/jwt.utils.go b/internal/utils/jwt.utils.go similarity index 96% rename from src/internal/utils/jwt.utils.go rename to internal/utils/jwt.utils.go index 3a4694e..0a8c786 100644 --- a/src/internal/utils/jwt.utils.go +++ b/internal/utils/jwt.utils.go @@ -14,7 +14,7 @@ type IJwtUtil interface { type jwtUtilImpl struct{} -func NewJwtUtil() *jwtUtilImpl { +func NewJwtUtil() IJwtUtil { return &jwtUtilImpl{} } diff --git a/src/internal/utils/uuid.utils.go b/internal/utils/uuid.utils.go similarity index 88% rename from src/internal/utils/uuid.utils.go rename to internal/utils/uuid.utils.go index 11d8004..2480403 100644 --- a/src/internal/utils/uuid.utils.go +++ b/internal/utils/uuid.utils.go @@ -8,7 +8,7 @@ type IUuidUtil interface { type uuidUtil struct{} -func NewUuidUtil() *uuidUtil { +func NewUuidUtil() IUuidUtil { return &uuidUtil{} } diff --git a/src/mocks/repository/auth/auth.mock.go b/mocks/repository/auth/auth.mock.go similarity index 93% rename from src/mocks/repository/auth/auth.mock.go rename to mocks/repository/auth/auth.mock.go index e80ea9a..8b0970d 100644 --- a/src/mocks/repository/auth/auth.mock.go +++ b/mocks/repository/auth/auth.mock.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: ./src/pkg/repository/auth/auth.repository.go +// Source: ./pkg/repository/auth/auth.repository.go // Package mock_auth is a generated GoMock package. package mock_auth @@ -8,7 +8,7 @@ import ( reflect "reflect" gomock "github.com/golang/mock/gomock" - model "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" + model "github.com/isd-sgcu/johnjud-auth/internal/domain/model" ) // MockRepository is a mock of Repository interface. diff --git a/src/mocks/repository/cache/cache.mock.go b/mocks/repository/cache/cache.mock.go similarity index 97% rename from src/mocks/repository/cache/cache.mock.go rename to mocks/repository/cache/cache.mock.go index 429c53a..7102795 100644 --- a/src/mocks/repository/cache/cache.mock.go +++ b/mocks/repository/cache/cache.mock.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: ./src/pkg/repository/cache/cache.repository.go +// Source: ./pkg/repository/cache/cache.repository.go // Package mock_cache is a generated GoMock package. package mock_cache diff --git a/src/mocks/repository/user/user.mock.go b/mocks/repository/user/user.mock.go similarity index 95% rename from src/mocks/repository/user/user.mock.go rename to mocks/repository/user/user.mock.go index 031ddf2..77b06ce 100644 --- a/src/mocks/repository/user/user.mock.go +++ b/mocks/repository/user/user.mock.go @@ -1,7 +1,7 @@ package user import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" "github.com/stretchr/testify/mock" ) diff --git a/src/mocks/service/jwt/jwt.mock.go b/mocks/service/jwt/jwt.mock.go similarity index 75% rename from src/mocks/service/jwt/jwt.mock.go rename to mocks/service/jwt/jwt.mock.go index 437dd8b..b4fb76f 100644 --- a/src/mocks/service/jwt/jwt.mock.go +++ b/mocks/service/jwt/jwt.mock.go @@ -2,8 +2,8 @@ package jwt import ( "github.com/golang-jwt/jwt/v4" - "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" + "github.com/isd-sgcu/johnjud-auth/cfgldr" + "github.com/isd-sgcu/johnjud-auth/internal/constant" "github.com/stretchr/testify/mock" ) @@ -29,7 +29,7 @@ func (m *JwtServiceMock) VerifyAuth(token string) (*jwt.Token, error) { return nil, args.Error(1) } -func (m *JwtServiceMock) GetConfig() *config.Jwt { +func (m *JwtServiceMock) GetConfig() *cfgldr.Jwt { args := m.Called() - return args.Get(0).(*config.Jwt) + return args.Get(0).(*cfgldr.Jwt) } diff --git a/src/mocks/service/token/token.mock.go b/mocks/service/token/token.mock.go similarity index 87% rename from src/mocks/service/token/token.mock.go rename to mocks/service/token/token.mock.go index 619e829..0be8264 100644 --- a/src/mocks/service/token/token.mock.go +++ b/mocks/service/token/token.mock.go @@ -1,8 +1,8 @@ package token import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token" + "github.com/isd-sgcu/johnjud-auth/internal/constant" + tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token" authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" "github.com/stretchr/testify/mock" ) diff --git a/src/mocks/strategy/strategy.mock.go b/mocks/strategy/strategy.mock.go similarity index 100% rename from src/mocks/strategy/strategy.mock.go rename to mocks/strategy/strategy.mock.go diff --git a/src/mocks/utils/bcrypt.mock.go b/mocks/utils/bcrypt.mock.go similarity index 100% rename from src/mocks/utils/bcrypt.mock.go rename to mocks/utils/bcrypt.mock.go diff --git a/src/mocks/utils/jwt.mock.go b/mocks/utils/jwt.mock.go similarity index 100% rename from src/mocks/utils/jwt.mock.go rename to mocks/utils/jwt.mock.go diff --git a/src/mocks/utils/uuid.mock.go b/mocks/utils/uuid.mock.go similarity index 100% rename from src/mocks/utils/uuid.mock.go rename to mocks/utils/uuid.mock.go diff --git a/pkg/repository/auth/auth.repository.go b/pkg/repository/auth/auth.repository.go new file mode 100644 index 0000000..67f89be --- /dev/null +++ b/pkg/repository/auth/auth.repository.go @@ -0,0 +1,10 @@ +package auth + +import ( + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" +) + +type Repository interface { + Create(auth *model.AuthSession) error + Delete(id string) error +} diff --git a/pkg/repository/cache/cache.repository.go b/pkg/repository/cache/cache.repository.go new file mode 100644 index 0000000..a786144 --- /dev/null +++ b/pkg/repository/cache/cache.repository.go @@ -0,0 +1,7 @@ +package cache + +type Repository interface { + SetValue(key string, value interface{}, ttl int) error + GetValue(key string, value interface{}) error + DeleteValue(key string) error +} diff --git a/src/pkg/repository/user/user.repository.go b/pkg/repository/user/user.repository.go similarity index 56% rename from src/pkg/repository/user/user.repository.go rename to pkg/repository/user/user.repository.go index 583a5b3..16db2cb 100644 --- a/src/pkg/repository/user/user.repository.go +++ b/pkg/repository/user/user.repository.go @@ -1,9 +1,7 @@ package user import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" - "github.com/isd-sgcu/johnjud-auth/src/internal/repository/user" - "gorm.io/gorm" + "github.com/isd-sgcu/johnjud-auth/internal/domain/model" ) type Repository interface { @@ -14,7 +12,3 @@ type Repository interface { Update(id string, user *model.User) error Delete(id string) error } - -func NewRepository(db *gorm.DB) Repository { - return user.NewRepository(db) -} diff --git a/pkg/service/jwt/jwt.service.go b/pkg/service/jwt/jwt.service.go new file mode 100644 index 0000000..e69b109 --- /dev/null +++ b/pkg/service/jwt/jwt.service.go @@ -0,0 +1,13 @@ +package jwt + +import ( + _jwt "github.com/golang-jwt/jwt/v4" + "github.com/isd-sgcu/johnjud-auth/cfgldr" + "github.com/isd-sgcu/johnjud-auth/internal/constant" +) + +type Service interface { + SignAuth(userId string, role constant.Role, authSessionId string) (string, error) + VerifyAuth(token string) (*_jwt.Token, error) + GetConfig() *cfgldr.Jwt +} diff --git a/pkg/service/token/token.service.go b/pkg/service/token/token.service.go new file mode 100644 index 0000000..9e2921a --- /dev/null +++ b/pkg/service/token/token.service.go @@ -0,0 +1,14 @@ +package token + +import ( + "github.com/isd-sgcu/johnjud-auth/internal/constant" + tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token" + authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" +) + +type Service interface { + CreateCredential(userId string, role constant.Role, authSessionId string) (*authProto.Credential, error) + Validate(token string) (*tokenDto.UserCredential, error) + CreateRefreshToken() string + RemoveTokenCache(refreshToken string) error +} diff --git a/pkg/strategy/jwt.strategy.go b/pkg/strategy/jwt.strategy.go new file mode 100644 index 0000000..e4df013 --- /dev/null +++ b/pkg/strategy/jwt.strategy.go @@ -0,0 +1,9 @@ +package strategy + +import ( + "github.com/golang-jwt/jwt/v4" +) + +type JwtStrategy interface { + AuthDecode(token *jwt.Token) (interface{}, error) +} diff --git a/src/pkg/repository/auth/auth.repository.go b/src/pkg/repository/auth/auth.repository.go deleted file mode 100644 index 5960187..0000000 --- a/src/pkg/repository/auth/auth.repository.go +++ /dev/null @@ -1,16 +0,0 @@ -package auth - -import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/domain/model" - "github.com/isd-sgcu/johnjud-auth/src/internal/repository/auth" - "gorm.io/gorm" -) - -type Repository interface { - Create(auth *model.AuthSession) error - Delete(id string) error -} - -func NewRepository(db *gorm.DB) Repository { - return auth.NewRepository(db) -} diff --git a/src/pkg/repository/cache/cache.repository.go b/src/pkg/repository/cache/cache.repository.go deleted file mode 100644 index d5f4cd5..0000000 --- a/src/pkg/repository/cache/cache.repository.go +++ /dev/null @@ -1,16 +0,0 @@ -package cache - -import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/repository/cache" - "github.com/redis/go-redis/v9" -) - -type Repository interface { - SetValue(key string, value interface{}, ttl int) error - GetValue(key string, value interface{}) error - DeleteValue(key string) error -} - -func NewRepository(client *redis.Client) Repository { - return cache.NewRepository(client) -} diff --git a/src/pkg/service/auth/auth.service.go b/src/pkg/service/auth/auth.service.go deleted file mode 100644 index 712113e..0000000 --- a/src/pkg/service/auth/auth.service.go +++ /dev/null @@ -1,14 +0,0 @@ -package auth - -import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/service/auth" - "github.com/isd-sgcu/johnjud-auth/src/internal/utils" - authRp "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/auth" - userRp "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user" - tokenSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/token" - authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" -) - -func NewService(authRepo authRp.Repository, userRepo userRp.Repository, tokenService tokenSvc.Service, bcryptUtil utils.IBcryptUtil) authProto.AuthServiceServer { - return auth.NewService(authRepo, userRepo, tokenService, bcryptUtil) -} diff --git a/src/pkg/service/jwt/jwt.service.go b/src/pkg/service/jwt/jwt.service.go deleted file mode 100644 index 2f3ff25..0000000 --- a/src/pkg/service/jwt/jwt.service.go +++ /dev/null @@ -1,20 +0,0 @@ -package jwt - -import ( - _jwt "github.com/golang-jwt/jwt/v4" - "github.com/isd-sgcu/johnjud-auth/src/config" - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - "github.com/isd-sgcu/johnjud-auth/src/internal/service/jwt" - "github.com/isd-sgcu/johnjud-auth/src/internal/utils" - jwtStg "github.com/isd-sgcu/johnjud-auth/src/pkg/strategy" -) - -type Service interface { - SignAuth(userId string, role constant.Role, authSessionId string) (string, error) - VerifyAuth(token string) (*_jwt.Token, error) - GetConfig() *config.Jwt -} - -func NewService(config config.Jwt, strategy jwtStg.JwtStrategy, jwtUtil utils.IJwtUtil) Service { - return jwt.NewService(config, strategy, jwtUtil) -} diff --git a/src/pkg/service/token/token.service.go b/src/pkg/service/token/token.service.go deleted file mode 100644 index b41376b..0000000 --- a/src/pkg/service/token/token.service.go +++ /dev/null @@ -1,22 +0,0 @@ -package token - -import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/constant" - tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token" - "github.com/isd-sgcu/johnjud-auth/src/internal/service/token" - "github.com/isd-sgcu/johnjud-auth/src/internal/utils" - "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/cache" - jwtSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/jwt" - authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1" -) - -type Service interface { - CreateCredential(userId string, role constant.Role, authSessionId string) (*authProto.Credential, error) - Validate(token string) (*tokenDto.UserCredential, error) - CreateRefreshToken() string - RemoveTokenCache(refreshToken string) error -} - -func NewService(jwtService jwtSvc.Service, accessTokenCache cache.Repository, refreshTokenCache cache.Repository, uuidUtil utils.IUuidUtil) Service { - return token.NewService(jwtService, accessTokenCache, refreshTokenCache, uuidUtil) -} diff --git a/src/pkg/service/user/user.service.go b/src/pkg/service/user/user.service.go deleted file mode 100644 index fba3fdc..0000000 --- a/src/pkg/service/user/user.service.go +++ /dev/null @@ -1,12 +0,0 @@ -package user - -import ( - "github.com/isd-sgcu/johnjud-auth/src/internal/service/user" - "github.com/isd-sgcu/johnjud-auth/src/internal/utils" - userRepo "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user" - userProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1" -) - -func NewService(userRepository userRepo.Repository, bcryptUtil utils.IBcryptUtil) userProto.UserServiceServer { - return user.NewService(userRepository, bcryptUtil) -} diff --git a/src/pkg/strategy/jwt.strategy.go b/src/pkg/strategy/jwt.strategy.go deleted file mode 100644 index 68109c5..0000000 --- a/src/pkg/strategy/jwt.strategy.go +++ /dev/null @@ -1,14 +0,0 @@ -package strategy - -import ( - "github.com/golang-jwt/jwt/v4" - "github.com/isd-sgcu/johnjud-auth/src/internal/strategy" -) - -type JwtStrategy interface { - AuthDecode(token *jwt.Token) (interface{}, error) -} - -func NewJwtStrategy(secret string) JwtStrategy { - return strategy.NewJwtStrategy(secret) -}