Skip to content

Commit

Permalink
Merge pull request #14 from mercadopago/feature/oauth-implementation
Browse files Browse the repository at this point in the history
Feature/oauth implementation
  • Loading branch information
edmarSoaress authored Feb 19, 2024
2 parents 43d195f + 645c804 commit d732a0e
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/apis/oauth/create/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"context"
"fmt"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/oauth"
)

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

client := oauth.NewClient(cfg)
authorizationCode := "{{AUTHORIZATION_CODE}}"
redirectURI := "{{REDIRECT_URI}}"

cred, err := client.Create(context.Background(), authorizationCode, redirectURI)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(cred)
}
28 changes: 28 additions & 0 deletions examples/apis/oauth/getauthorizationurl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/oauth"
)

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

client := oauth.NewClient(cfg)

clientID := "{{CLIENT_ID}}"
redirectURI := "{{REDIRECT_URI}}"
state := "state"

url := client.GetAuthorizationURL(clientID, redirectURI, state)

fmt.Println(url)
}
38 changes: 38 additions & 0 deletions examples/apis/oauth/refresh/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"fmt"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/oauth"
)

func main() {
accessToken := "{{ACCESS_TOKEN}}"

cfg, err := config.New(accessToken)
if err != nil {
fmt.Println(err)
return
}

client := oauth.NewClient(cfg)
authorizationCode := "{{AUTHORIZATION_CODE}}"
redirectURI := "{{REDIRECT_URI}}"

cred, err := client.Create(context.Background(), authorizationCode, redirectURI)
if err != nil {
fmt.Println(err)
return
}

refreshToken := cred.RefreshToken
cred, err = client.Refresh(context.Background(), refreshToken)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(cred)
}
98 changes: 98 additions & 0 deletions pkg/oauth/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package oauth

import (
"context"
"net/url"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/baseclient"
)

const (
urlBase = "https://api.mercadopago.com/oauth/token"
urlAuth = "https://auth.mercadopago.com/authorization"
)

// Client contains the method to interact with the Oauth API.
type Client interface {

// Create oauth credentials to operate on behalf of a seller
// It is a post request to the endpoint: "https://api.mercadopago.com/oauth/token"
// Reference: https://www.mercadopago.com/developers/en/reference/oauth/_oauth_token/post
Create(ctx context.Context, authorizationCode, redirectURI string) (*Response, error)

// Get url for oauth authorization.
GetAuthorizationURL(clientID, redirectURI, state string) string

// Refresh token received when you create credentials.
// It is a post request to the endpoint: "https://api.mercadopago.com/oauth/token"
// Reference: https://www.mercadopago.com/developers/en/reference/oauth/_oauth_token/post
Refresh(ctx context.Context, refreshToken string) (*Response, error)
}

// client is the implementation of Client.
type client struct {
cfg *config.Config
}

// NewClient returns a new Oauth API Client.
func NewClient(c *config.Config) Client {
return &client{
cfg: c,
}
}

func (c *client) Create(ctx context.Context, authorizationCode, redirectURI string) (*Response, error) {
request := &Request{
ClientSecret: c.cfg.AccessToken,
Code: authorizationCode,
RedirectURI: redirectURI,
GrantType: "authorization_code",
}

res, err := baseclient.Post[*Response](ctx, c.cfg, urlBase, request)
if err != nil {
return nil, err
}

return res, nil
}

func (c *client) GetAuthorizationURL(clientID, redirectURI, state string) string {
params := map[string]string{
"client_id": clientID,
"response_type": "code",
"platform_id": "mp",
"redirect_uri": redirectURI,
}

parsedURL, err := url.Parse(urlAuth)
if err != nil {
return ""
}

queryParams := url.Values{}

for k, v := range params {
queryParams.Add(k, v)
}

parsedURL.RawQuery = queryParams.Encode()

return parsedURL.String()
}

func (c *client) Refresh(ctx context.Context, refreshToken string) (*Response, error) {
request := &RefreshTokenRequest{
ClientSecret: c.cfg.AccessToken,
RefreshToken: refreshToken,
GrantType: "refresh_token",
}

res, err := baseclient.Post[*Response](ctx, c.cfg, urlBase, request)
if err != nil {
return nil, err
}

return res, nil
}
Loading

0 comments on commit d732a0e

Please sign in to comment.