From c07f4d4a59babef70b8e960286326008aa438a2c Mon Sep 17 00:00:00 2001 From: Victor Dodon Date: Tue, 9 Apr 2024 09:18:38 +0300 Subject: [PATCH] Add state param to AuthCodeURL method --- README.md | 40 +++++++++++++++++++++++++--------------- oauth2.go | 4 ++-- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e5fb4bd..0cf76c7 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 } diff --git a/oauth2.go b/oauth2.go index fd863aa..36c9e1a 100644 --- a/oauth2.go +++ b/oauth2.go @@ -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")) }