Skip to content

Commit

Permalink
feat: add authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 28, 2024
1 parent 0554fdb commit 552df47
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Base URLs:

- Flow: clientCredentials

- Token URL = [/api/auth/oauth/token](/api/auth/oauth/token)
- Token URL = [/oauth/token](/oauth/token)

|Scope|Scope Description|
|---|---|
Expand Down
4 changes: 2 additions & 2 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3738,6 +3738,6 @@ components:
type: oauth2
flows:
clientCredentials:
tokenUrl: /api/auth/oauth/token
refreshUrl: /api/auth/oauth/token
tokenUrl: /oauth/token
refreshUrl: /oauth/token
scopes: {}
4 changes: 2 additions & 2 deletions openapi/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1300,8 +1300,8 @@ components:
type: oauth2
flows:
clientCredentials:
tokenUrl: '/api/auth/oauth/token'
refreshUrl: '/api/auth/oauth/token'
tokenUrl: '/oauth/token'
refreshUrl: '/oauth/token'
scopes: { }

schemas:
Expand Down
4 changes: 2 additions & 2 deletions openapi/v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,8 @@ components:
type: oauth2
flows:
clientCredentials:
tokenUrl: "/api/auth/oauth/token"
refreshUrl: "/api/auth/oauth/token"
tokenUrl: "/oauth/token"
refreshUrl: "/oauth/token"
scopes: {}

schemas:
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/.speakeasy/gen.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
lockVersion: 2.0.0
id: a9ac79e1-e429-4ee3-96c4-ec973f19bec3
management:
docChecksum: 169efa4fe3c5d6561f06920598d20df4
docChecksum: 46d538f65c61649e934b6991843b1e67
docVersion: v1
speakeasyVersion: 1.351.0
generationVersion: 2.384.1
releaseVersion: 0.4.20
configChecksum: e66e70c75590218ba585d230919d03e3
releaseVersion: 0.4.21
configChecksum: 2dae5ea4cfeda429fc8fea96da1ab7b6
features:
go:
additionalDependencies: 0.1.0
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/.speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ generation:
auth:
oAuth2ClientCredentialsEnabled: true
go:
version: 0.4.20
version: 0.4.21
additionalDependencies: {}
allowUnknownFieldsInWeakUnions: false
clientServerStatusCodesAsErrors: true
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/formance.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ func New(opts ...SDKOption) *Formance {
sdkConfiguration: sdkConfiguration{
Language: "go",
OpenAPIDocVersion: "v1",
SDKVersion: "0.4.20",
SDKVersion: "0.4.21",
GenVersion: "2.384.1",
UserAgent: "speakeasy-sdk/go 0.4.20 2.384.1 v1 github.com/formancehq/ledger/pkg/client",
UserAgent: "speakeasy-sdk/go 0.4.21 2.384.1 v1 github.com/formancehq/ledger/pkg/client",
Hooks: hooks.New(),
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/models/components/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Security struct {
ClientID string `security:"scheme,type=oauth2,subtype=client_credentials,name=clientID"`
ClientSecret string `security:"scheme,type=oauth2,subtype=client_credentials,name=clientSecret"`
tokenURL string `const:"/api/auth/oauth/token"`
tokenURL string `const:"/oauth/token"`
}

func (s Security) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -38,5 +38,5 @@ func (o *Security) GetClientSecret() string {
}

func (o *Security) GetTokenURL() string {
return "/api/auth/oauth/token"
return "/oauth/token"
}
61 changes: 59 additions & 2 deletions tools/generator/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"context"
"crypto/tls"
"errors"
"fmt"
"github.com/formancehq/go-libs/v2/logging"
Expand All @@ -9,6 +11,9 @@ import (
"github.com/formancehq/ledger/pkg/client/models/operations"
"github.com/formancehq/ledger/pkg/client/models/sdkerrors"
"github.com/formancehq/ledger/pkg/generate"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
"net/http"
"os"
"sync"

Expand All @@ -25,6 +30,10 @@ var (
parallelFlag = "parallel"
ledgerFlag = "ledger"
untilTransactionIDFlag = "until-transaction-id"
clientIDFlag = "client-id"
clientSecretFlag = "client-secret"
authUrlFlag = "auth-url"
insecureSkipVerifyFlag = "insecure-skip-verify"
)

func run(cmd *cobra.Command, args []string) error {
Expand All @@ -51,14 +60,58 @@ func run(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to get untilTransactionID: %w", err)
}

client := ledgerclient.New(ledgerclient.WithServerURL(ledgerUrl))
insecureSkipVerify, err := cmd.Flags().GetBool(insecureSkipVerifyFlag)
if err != nil {
return fmt.Errorf("failed to get insecureSkipVerify: %w", err)
}

httpClient := &http.Client{
Transport: &http.Transport{
MaxIdleConns: vus,
MaxConnsPerHost: vus,
MaxIdleConnsPerHost: vus,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
},
},
}

clientID, err := cmd.Flags().GetString(clientIDFlag)
if err != nil {
return fmt.Errorf("failed to get client id: %w", err)
}
if clientID != "" {
clientSecret, err := cmd.Flags().GetString(clientSecretFlag)
if err != nil {
return fmt.Errorf("failed to get client secret: %w", err)
}

authUrl, err := cmd.Flags().GetString(authUrlFlag)
if err != nil {
return fmt.Errorf("failed to get auth url: %w", err)
}

httpClient = (&clientcredentials.Config{
ClientID: clientID,
ClientSecret: clientSecret,
TokenURL: authUrl + "/oauth/token",
Scopes: []string{"ledger:read", "ledger:write"},
}).
Client(context.WithValue(cmd.Context(), oauth2.HTTPClient, httpClient))
}

client := ledgerclient.New(
ledgerclient.WithServerURL(ledgerUrl),
ledgerclient.WithClient(httpClient),
)

_, err = client.Ledger.V2.CreateLedger(cmd.Context(), operations.V2CreateLedgerRequest{
Ledger: ledger,
})
if err != nil {
sdkError := &sdkerrors.V2ErrorResponse{}
if !errors.As(err, &sdkError) || sdkError.ErrorCode != components.V2ErrorsEnumLedgerAlreadyExists {
if !errors.As(err, &sdkError) || (sdkError.ErrorCode != components.V2ErrorsEnumLedgerAlreadyExists &&
sdkError.ErrorCode != components.V2ErrorsEnumValidation) {
return fmt.Errorf("failed to create ledger: %w", err)
}
}
Expand Down Expand Up @@ -112,6 +165,10 @@ func Execute() {
}

func init() {
rootCmd.Flags().String(clientIDFlag, "", "Client ID")
rootCmd.Flags().String(clientSecretFlag, "", "Client Secret")
rootCmd.Flags().String(authUrlFlag, "", "Auth URL")
rootCmd.Flags().Bool(insecureSkipVerifyFlag, false, "Skip TLS verification")
rootCmd.Flags().IntP(parallelFlag, "p", 1, "Number of parallel users")
rootCmd.Flags().StringP(ledgerFlag, "l", "default", "Ledger to feed")
rootCmd.Flags().Int64P(untilTransactionIDFlag, "u", 0, "Stop after this transaction ID")
Expand Down

0 comments on commit 552df47

Please sign in to comment.