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

Feature/MZ-559 #97

Merged
merged 10 commits into from
Oct 14, 2024
10 changes: 10 additions & 0 deletions common/net/http/httputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,13 @@ func GetFileFromHeader(ctx *fiber.Ctx) (string, error) {

return fileString, nil
}

// GetTokenHeader func that get token from header
func GetTokenHeader(c *fiber.Ctx) string {
splitToken := strings.Split(c.Get(fiber.HeaderAuthorization), "Bearer")
if len(splitToken) == 2 {
return strings.TrimSpace(splitToken[1])
}

return ""
}
15 changes: 3 additions & 12 deletions common/net/http/withJWT.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,6 @@ func convertGroups(groups []any) []string {
return newGroups
}

func getTokenHeader(c *fiber.Ctx) string {
splitToken := strings.Split(c.Get(fiber.HeaderAuthorization), "Bearer")
if len(splitToken) == 2 {
return strings.TrimSpace(splitToken[1])
}

return ""
}

func getTokenHeaderFromContext(ctx context.Context) string {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
Expand Down Expand Up @@ -234,9 +225,9 @@ func (jwtm *JWTMiddleware) ProtectHTTP() fiber.Handler {

l.Debug("Read token from header")

tokenString := getTokenHeader(c)
tokenString := GetTokenHeader(c)

if len(tokenString) == 0 {
if common.IsNilOrEmpty(&tokenString) {
msg := errors.Wrap(errors.New("token not found in context"), "No token found in context")
l.Error(msg.Error())

Expand All @@ -245,7 +236,7 @@ func (jwtm *JWTMiddleware) ProtectHTTP() fiber.Handler {

l.Debugf("Get JWK keys using %s", jwtm.JWK.URI)

keySet, err := jwtm.JWK.Fetch(context.Background())
keySet, err := jwtm.JWK.Fetch(c.Context())
if err != nil {
msg := errors.Wrap(err, "Couldn't load JWK keys from source")
l.Error(msg.Error())
Expand Down
10 changes: 5 additions & 5 deletions components/auth/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ x-postgres-common:
env_file:
- .env
networks:
- app-tier
- midaz_network

services:
casdoor:
Expand All @@ -21,7 +21,7 @@ services:
casdoor-db:
condition: service_healthy
networks:
- app-tier
- midaz_network

casdoor-db:
<<: *postgres-common
Expand All @@ -43,6 +43,6 @@ services:
retries: 5

networks:
app-tier:
driver: bridge
name: app-tier
midaz_network:
name: midaz_network
driver: bridge
14 changes: 7 additions & 7 deletions components/ledger/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ x-postgres-ledger-common:
timeout: 5s
retries: 5
networks:
- app-tier
- midaz_network

x-mongodb-common:
&mongodb-common
Expand All @@ -21,15 +21,15 @@ x-mongodb-common:
timeout: 5s
retries: 5
networks:
- app-tier
- midaz_network

x-redis-common:
&redis-common
image: redis:latest
env_file:
- .env
networks:
- app-tier
- midaz_network

services:
ledger:
Expand Down Expand Up @@ -59,7 +59,7 @@ services:
replica-ledger:
condition: service_healthy
networks:
- app-tier
- midaz_network

mongodb:
<<: *mongodb-common
Expand Down Expand Up @@ -135,6 +135,6 @@ volumes:
mongodb_data_container:

networks:
app-tier:
driver: bridge
name: app-tier
midaz_network:
name: midaz_network
driver: bridge
2 changes: 1 addition & 1 deletion components/ledger/internal/gen/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package gen

import (
"fmt"
"github.com/LerianStudio/midaz/common/mcasdoor"
"sync"

"github.com/LerianStudio/midaz/common"
"github.com/LerianStudio/midaz/common/mcasdoor"
"github.com/LerianStudio/midaz/common/mmongo"
"github.com/LerianStudio/midaz/common/mpostgres"
"github.com/LerianStudio/midaz/common/mzap"
Expand Down
12 changes: 6 additions & 6 deletions components/transaction/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ x-postgres-transaction-common:
timeout: 5s
retries: 5
networks:
- app-tier
- midaz_network

x-mongodb-common:
&mongodb-common
Expand All @@ -21,7 +21,7 @@ x-mongodb-common:
timeout: 5s
retries: 5
networks:
- app-tier
- midaz_network

services:
transaction:
Expand All @@ -47,7 +47,7 @@ services:
replica-transaction:
condition: service_healthy
networks:
- app-tier
- midaz_network

mongodb:
<<: *mongodb-common
Expand Down Expand Up @@ -175,6 +175,6 @@ volumes:
mongodb_data_container:

networks:
app-tier:
driver: bridge
name: app-tier
midaz_network:
name: midaz_network
driver: bridge
16 changes: 13 additions & 3 deletions components/transaction/internal/adapters/grpc/account.grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/LerianStudio/midaz/common/mgrpc"
proto "github.com/LerianStudio/midaz/common/mgrpc/account"
gmtdt "google.golang.org/grpc/metadata"
)

// AccountGRPCRepository is a gRPC implementation of the account.proto
Expand All @@ -27,7 +28,7 @@ func NewAccountGRPC(c *mgrpc.GRPCConnection) *AccountGRPCRepository {
}

// GetAccountsByIds returns a grpc accounts on ledger bi given ids.
func (a *AccountGRPCRepository) GetAccountsByIds(ctx context.Context, ids []string) (*proto.AccountsResponse, error) {
func (a *AccountGRPCRepository) GetAccountsByIds(ctx context.Context, token string, ids []string) (*proto.AccountsResponse, error) {
conn, err := a.conn.GetNewClient()
if err != nil {
return nil, err
Expand All @@ -39,6 +40,9 @@ func (a *AccountGRPCRepository) GetAccountsByIds(ctx context.Context, ids []stri
Ids: ids,
}

md := gmtdt.Pairs("authorization", "Bearer "+token)
ctx = gmtdt.NewOutgoingContext(ctx, md)

accountsResponse, err := client.GetAccountsByIds(ctx, accountsID)
if err != nil {
return nil, err
Expand All @@ -48,7 +52,7 @@ func (a *AccountGRPCRepository) GetAccountsByIds(ctx context.Context, ids []stri
}

// GetAccountsByAlias returns a grpc accounts on ledger bi given aliases.
func (a *AccountGRPCRepository) GetAccountsByAlias(ctx context.Context, aliases []string) (*proto.AccountsResponse, error) {
func (a *AccountGRPCRepository) GetAccountsByAlias(ctx context.Context, token string, aliases []string) (*proto.AccountsResponse, error) {
conn, err := a.conn.GetNewClient()
if err != nil {
return nil, err
Expand All @@ -60,6 +64,9 @@ func (a *AccountGRPCRepository) GetAccountsByAlias(ctx context.Context, aliases
Aliases: aliases,
}

md := gmtdt.Pairs("authorization", "Bearer "+token)
ctx = gmtdt.NewOutgoingContext(ctx, md)

accountsResponse, err := client.GetAccountsByAliases(ctx, accountsAlias)
if err != nil {
return nil, err
Expand All @@ -69,7 +76,7 @@ func (a *AccountGRPCRepository) GetAccountsByAlias(ctx context.Context, aliases
}

// UpdateAccounts update a grpc accounts on ledger.
func (a *AccountGRPCRepository) UpdateAccounts(ctx context.Context, accounts []*proto.Account) (*proto.AccountsResponse, error) {
func (a *AccountGRPCRepository) UpdateAccounts(ctx context.Context, token string, accounts []*proto.Account) (*proto.AccountsResponse, error) {
conn, err := a.conn.GetNewClient()
if err != nil {
return nil, err
Expand All @@ -81,6 +88,9 @@ func (a *AccountGRPCRepository) UpdateAccounts(ctx context.Context, accounts []*
Accounts: accounts,
}

md := gmtdt.Pairs("authorization", "Bearer "+token)
ctx = gmtdt.NewOutgoingContext(ctx, md)

accountsResponse, err := client.UpdateAccounts(ctx, accountsRequest)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
//
//go:generate mockgen --destination=../../gen/mock/account/account_mock.go --package=mock . Repository
type Repository interface {
GetAccountsByIds(ctx context.Context, ids []string) (*proto.AccountsResponse, error)
GetAccountsByAlias(ctx context.Context, aliases []string) (*proto.AccountsResponse, error)
UpdateAccounts(ctx context.Context, accounts []*proto.Account) (*proto.AccountsResponse, error)
GetAccountsByIds(ctx context.Context, token string, ids []string) (*proto.AccountsResponse, error)
GetAccountsByAlias(ctx context.Context, token string, aliases []string) (*proto.AccountsResponse, error)
UpdateAccounts(ctx context.Context, token string, accounts []*proto.Account) (*proto.AccountsResponse, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Responses struct {
// ValidateAccounts function with some validates in accounts and DSL operations
func ValidateAccounts(validate Responses, accounts []*a.Account) error {
for _, acc := range accounts {
if acc.Balance.Available == 0 {
if acc.Balance.Available <= 0 && !strings.Contains(acc.Alias, "@external") {
return common.ValidationError{
Code: "0025",
Title: "Insuficient balance",
Expand Down
21 changes: 18 additions & 3 deletions components/transaction/internal/gen/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package gen

import (
"fmt"
"github.com/LerianStudio/midaz/components/transaction/internal/ports"
"sync"

"github.com/LerianStudio/midaz/common"
"github.com/LerianStudio/midaz/common/mcasdoor"
"github.com/LerianStudio/midaz/common/mgrpc"
"github.com/LerianStudio/midaz/common/mmongo"
"github.com/LerianStudio/midaz/common/mpostgres"
Expand Down Expand Up @@ -58,6 +58,20 @@ func setupMongoDBConnection(cfg *service.Config) *mmongo.MongoConnection {
}
}

func setupCasdoorConnection(cfg *service.Config) *mcasdoor.CasdoorConnection {
casdoor := &mcasdoor.CasdoorConnection{
JWKUri: cfg.JWKAddress,
Endpoint: cfg.CasdoorAddress,
ClientID: cfg.CasdoorClientID,
ClientSecret: cfg.CasdoorClientSecret,
OrganizationName: cfg.CasdoorOrganizationName,
ApplicationName: cfg.CasdoorApplicationName,
EnforcerName: cfg.CasdoorEnforcerName,
}

return casdoor
}

func setupGRPCConnection(cfg *service.Config) *mgrpc.GRPCConnection {
addr := fmt.Sprintf("%s:%s", cfg.LedgerGRPCAddr, cfg.LedgerGRPCPort)

Expand All @@ -72,6 +86,7 @@ var (
mzap.InitializeLogger,
setupPostgreSQLConnection,
setupMongoDBConnection,
setupCasdoorConnection,
setupGRPCConnection,
service.NewConfig,
httpHandler.NewRouter,
Expand All @@ -80,8 +95,8 @@ var (
postgres.NewOperationPostgreSQLRepository,
mongodb.NewMetadataMongoDBRepository,
grpc.NewAccountGRPC,
wire.Struct(new(ports.TransactionHandler), "*"),
wire.Struct(new(ports.OperationHandler), "*"),
wire.Struct(new(httpHandler.TransactionHandler), "*"),
wire.Struct(new(httpHandler.OperationHandler), "*"),
wire.Struct(new(command.UseCase), "*"),
wire.Struct(new(query.UseCase), "*"),
wire.Bind(new(t.Repository), new(*postgres.TransactionPostgreSQLRepository)),
Expand Down
24 changes: 12 additions & 12 deletions components/transaction/internal/gen/mock/account/account_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading