Skip to content

Commit

Permalink
feat: add scopes current regitered client
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ragot committed Dec 22, 2023
1 parent 25bdee7 commit 44369cd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ee/auth/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
golang.org/x/text v0.12.0
gopkg.in/square/go-jose.v2 v2.6.0
gorm.io/driver/postgres v1.4.8
gorm.io/gorm v1.24.5
gorm.io/gorm v1.25.4
)

require (
Expand All @@ -40,6 +40,7 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-gormigrate/gormigrate/v2 v2.1.1
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions ee/auth/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gormigrate/gormigrate/v2 v2.1.1 h1:eGS0WTFRV30r103lU8JNXY27KbviRnqqIDobW3EV3iY=
github.com/go-gormigrate/gormigrate/v2 v2.1.1/go.mod h1:L7nJ620PFDKei9QOhJzqA8kRCk+E3UbV2f5gv+1ndLc=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -752,6 +754,8 @@ gorm.io/driver/postgres v1.4.8/go.mod h1:O9MruWGNLUBUWVYfWuBClpf3HeGjOoybY0SNmCs
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.24.5 h1:g6OPREKqqlWq4kh/3MCQbZKImeB9e6Xgc4zD+JgNZGE=
gorm.io/gorm v1.24.5/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.2.0 h1:I0DwBVMGAx26dttAj1BtJLAkVGncrkkUXfJLC4Flt/I=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
64 changes: 63 additions & 1 deletion ee/auth/pkg/storage/sqlstorage/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

auth "github.com/formancehq/auth/pkg"
"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/go-gormigrate/gormigrate/v2"
"go.uber.org/fx"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand Down Expand Up @@ -60,6 +61,61 @@ func LoadGorm(d gorm.Dialector, gormConfig *gorm.Config) (*gorm.DB, error) {
return db, nil
}

const (
Wallets = "wallets"
Orchestration = "orchestration"
Ledger = "ledger"
Payments = "payments"
Webhooks = "webhooks"
Auth = "auth"
Reconciliation = "reconciliation"
Search = "search"
)

type Services []string

var AllServices = Services{
Wallets,
Orchestration,
Ledger,
Payments,
Webhooks,
Auth,
Reconciliation,
Search,
}

func MigrateData(ctx context.Context, db *gorm.DB) error {

gormigrate := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
{
ID: "202312221800",
Migrate: func(tx *gorm.DB) error {
scopes := auth.Array[string]{"openid"}
for _, service := range AllServices {
scopes = append(scopes, service+":read", service+":write")
}
return tx.Exec(
`
UPDATE clients
SET scopes = ?;
`, scopes).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec(
`
UPDATE clients
SET scopes = NULL;
`,
).Error
},
},
})

return gormigrate.Migrate()
}

func MigrateTables(ctx context.Context, db *gorm.DB) error {
return db.WithContext(ctx).AutoMigrate(
&auth.Client{},
Expand Down Expand Up @@ -91,7 +147,13 @@ func gormModule(kind, uri string) fx.Option {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
logging.FromContext(ctx).Info("Migrate tables")
return MigrateTables(ctx, db)

err := MigrateTables(ctx, db)
if err != nil {
return err
}

return MigrateData(ctx, db)
},
OnStop: func(ctx context.Context) error {
logging.FromContext(ctx).Info("Closing database...")
Expand Down

0 comments on commit 44369cd

Please sign in to comment.