Skip to content

Commit

Permalink
Add state param to AuthCodeURL method
Browse files Browse the repository at this point in the history
  • Loading branch information
printesoi committed Apr 9, 2024
1 parent 244f678 commit c07f4d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
40 changes: 25 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,39 @@ API via the Client object and for generating an Invoice UBL XML.
import "github.com/printesoi/e-factura-go"
```

Construct a new client:
Construct the required OAuth2 config needed for the Client:

```go
client, err := efactura.NewClient(
context.Background(),
efactura.ClientOAuth2Config(oauth2Cfg),
efactura.ClientOAuth2InitialToken(initialToken),
efactura.ClientProductionEnvironment(false),
oauth2Cfg, err := efactura.MakeOAuth2Config(
efactura.OAuth2ConfigCredentials(anafAppClientID, anafApplientSecret),
efactura.OAuth2ConfigRedirectURL(anafAppRedirectURL),
)
if err != nil {
// Handle error
}
```

Construct the required OAuth2 config needed for the Client:
Generate an authorization link for certificate authorization:

```go
oauth2Cfg, err := efactura.MakeOAuth2Config(
efactura.OAuth2ConfigCredentials(anafAppClientID, anafApplientSecret),
efactura.OAuth2ConfigRedirectURL(anafAppRedirectURL),
)
authorizeURL := oauth2Cfg.AuthCodeURL(state)
// Redirect the user to authorizeURL
```

Getting a token from an authorization code (the parameter `code` sent via GET
to the redirect URL):

```go
// Assuming the oauth2Cfg is built as above
initialToken, err := oauth2Cfg.Exchange(ctx, authorizationCode)
if err != nil {
// Handle error
}
```

If you specified a non-empty state when building the authorization URL, you
will also receive the `state` parameter with `code`.

Parse the initial token from JSON:

```go
Expand All @@ -80,12 +87,15 @@ if err != nil {
}
```

Getting a token from an authorization code (the parameter `code` sent via GET
to the redirect URL):
Construct a new client:

```go
// Assuming the oauth2Cfg is built as above
initialToken, err := oauth2Cfg.Exchange(ctx, authorizationCode)
client, err := efactura.NewClient(
context.Background(),
efactura.ClientOAuth2Config(oauth2Cfg),
efactura.ClientOAuth2InitialToken(initialToken),
efactura.ClientProductionEnvironment(false), // false for test, true for production mode
)
if err != nil {
// Handle error
}
Expand Down
4 changes: 2 additions & 2 deletions oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (c *OAuth2Config) Valid() bool {
}

// AuthCodeURL generates the code authorization URL.
func (c OAuth2Config) AuthCodeURL() string {
return c.Config.AuthCodeURL("",
func (c OAuth2Config) AuthCodeURL(state string) string {
return c.Config.AuthCodeURL(state,
oauth2.SetAuthURLParam("token_content_type", "jwt"))
}

Expand Down

0 comments on commit c07f4d4

Please sign in to comment.