Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/oauth implementation #14

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading