Skip to content

Commit

Permalink
refactor: Refactor authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
meysamhadeli committed Apr 5, 2024
1 parent 40c2ba4 commit 4f21c74
Show file tree
Hide file tree
Showing 25 changed files with 102 additions and 247 deletions.
111 changes: 0 additions & 111 deletions .github/release-drafter.yml

This file was deleted.

53 changes: 0 additions & 53 deletions .github/workflows/ci.yml

This file was deleted.

21 changes: 0 additions & 21 deletions .github/workflows/release-drafter-labeler.yml

This file was deleted.

19 changes: 0 additions & 19 deletions .github/workflows/release-drafter.yml

This file was deleted.

8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.PHONY:

## choco install make
# ==============================================================================
# Run Services
run_products_service:
Expand All @@ -18,11 +19,11 @@ docker-compose_infra_down:
@echo Stoping infrastructure docker-compose down
docker-compose -f deployments/docker-compose/infrastructure.yaml down

## choco install protoc
## go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
# ==============================================================================
# Proto Identity Service

## go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

## grpc-server
proto_identities_get_user_by_id_service:
@echo Generating identity_service proto
Expand All @@ -34,11 +35,10 @@ proto_identities_get_user_by_id_service:
@echo Generating identity_service proto
protoc --go_out=./internal/services/product_service/product/grpc_client/protos --go-grpc_opt=require_unimplemented_servers=false --go-grpc_out=./internal/services/product_service/product/grpc_client/protos ./internal/services/product_service/product/grpc_client/protos/*.proto

## go install github.com/swaggo/swag/cmd/swag@latest
# ==============================================================================
# Swagger products Service #https://github.com/swaggo/swag/issues/817

## go install github.com/swaggo/swag/cmd/swag@latest

swagger_products:
@echo Starting swagger generating
swag init -g ./internal/services/product_service/cmd/main.go -o ./internal/services/product_service/docs
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/meysamhadeli/shop-golang-microservices/internal/pkg

go 1.21
go 1.22

require (
github.com/ahmetb/go-linq/v3 v3.2.0
Expand Down
33 changes: 30 additions & 3 deletions internal/pkg/oauth2/password_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import (
"github.com/go-oauth2/oauth2/v4/store"
"github.com/golang-jwt/jwt"
"github.com/labstack/echo/v4"
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/utils"
uuid "github.com/satori/go.uuid"
"gorm.io/gorm"
"log"
"net/http"
"sync"
"time"
)

var (
Expand All @@ -24,6 +28,18 @@ var (
clients = []*models.Client{{ID: "clientId", Secret: "clientSecret"}, {ID: "clientId2", Secret: "clientSecret2"}}
)

// User model
type User struct {
UserId uuid.UUID `json:"userId" gorm:"primaryKey"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
UserName string `json:"userName"`
Email string `json:"email"`
Password string `json:"password"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

func init() {
manager = manage.NewDefaultManager()
manager.MapAccessGenerate(generates.NewJWTAccessGenerate("", privateKey, jwt.SigningMethodHS512))
Expand Down Expand Up @@ -52,13 +68,24 @@ func clientStore(clients ...*models.Client) oauth2.ClientStore {
}

// ref: https://github.com/go-oauth2/oauth2
func RunOauthServer(e *echo.Echo) {
func RunOauthServer(e *echo.Echo, gorm *gorm.DB) {

manager.MapClientStorage(clientStore(clients...))

srv.SetPasswordAuthorizationHandler(func(ctx context.Context, clientID, username, password string) (userID string, err error) {
if username == "admin" && password == "admin" {
userID = "1"

u := User{}
gorm.Where("user_name = ?", username).First(&u)

// now use p
isMatch, err := utils.ComparePasswords(u.Password, password)

if err != nil {
return "", err
}

if isMatch {
return u.UserId.String(), nil
}
return
})
Expand Down
7 changes: 6 additions & 1 deletion internal/services/identity_service/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity_service/config"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity_service/identity/configurations"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity_service/identity/data/repositories"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity_service/identity/data/seeds"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity_service/identity/mappings"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity_service/identity/models"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity_service/server"
Expand Down Expand Up @@ -45,7 +46,11 @@ func main() {
fx.Invoke(configurations.ConfigMiddlewares),
fx.Invoke(configurations.ConfigSwagger),
fx.Invoke(func(gorm *gorm.DB) error {
return gormpgsql.Migrate(gorm, &models.User{})
err := gormpgsql.Migrate(gorm, &models.User{})
if err != nil {
return err
}
return seeds.DataSeeder(gorm)
}),
fx.Invoke(mappings.ConfigureMappings),
fx.Invoke(configurations.ConfigEndpoints),
Expand Down
2 changes: 1 addition & 1 deletion internal/services/identity_service/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const docTemplate = `{
"password": {
"type": "string"
},
"userId": {
"id": {
"type": "string"
},
"userName": {
Expand Down
2 changes: 1 addition & 1 deletion internal/services/identity_service/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/meysamhadeli/shop-golang-microservices/internal/services/identity_service

go 1.21
go 1.22

replace github.com/meysamhadeli/shop-golang-microservices/internal/pkg => ../../pkg

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seeds

import (
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/utils"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity_service/identity/models"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"gorm.io/gorm"
"time"
)

func DataSeeder(gorm *gorm.DB) error {
return seedUser(gorm)
}

func seedUser(gorm *gorm.DB) error {
if (gorm.Find(&models.User{}).RowsAffected <= 0) {
pass, err := utils.HashPassword("Admin@12345")
if err != nil {
return err
}
user := &models.User{UserId: uuid.NewV4(), UserName: "admin_user", FirstName: "admin", LastName: "admin", CreatedAt: time.Now(), Email: "[email protected]", Password: pass}

if err := gorm.Create(user).Error; err != nil {
return errors.Wrap(err, "error in the inserting user into the database.")
}
}
return nil
}

This file was deleted.

Loading

0 comments on commit 4f21c74

Please sign in to comment.