GetAuthorizationCodeURL

This commit is contained in:
Aliaksandr Pliutau 2015-10-15 15:47:36 +07:00
parent edbe51d686
commit c0396a16be
3 changed files with 50 additions and 2 deletions

23
auth.go Normal file
View File

@ -0,0 +1,23 @@
package paypalsdk
import (
"strings"
"net/url"
"errors"
)
// GetAuthorizationCodeURL returns URL where we need to redirect user
// After signin in PayPal get authorization_code on redirectURI
func (c *Client) GetAuthorizationCodeURL(redirectURI string, scopes []string) (string, error) {
if redirectURI == "" {
return "", errors.New("redirectURI cannot be empty")
}
if len(scopes) == 0 {
scopes = []string{"profile", "email"}
}
return c.APIBase + "/webapps/auth/protocol/openidconnect/v1/authorize?client_id=" +
url.QueryEscape(c.ClientID) + "&response_type=code&scope=" + strings.Join(scopes, "+") +
"&redirect_uri=" + url.QueryEscape(redirectURI), nil
}

24
auth_test.go Normal file
View File

@ -0,0 +1,24 @@
package paypalsdk
import (
"testing"
)
func TestGetAuthorizationCodeURL(t *testing.T) {
c, _ := NewClient("clid", "secret", APIBaseSandBox)
_, err := c.GetAuthorizationCodeURL("", []string{})
if err == nil {
t.Errorf("redirectURI is required in GetAuthorizationCodeURL")
}
uri, err := c.GetAuthorizationCodeURL("test", []string{})
if uri != "https://api.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id=clid&response_type=code&scope=profile+email&redirect_uri=test" {
t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test")
}
uri, err = c.GetAuthorizationCodeURL("test", []string{"address"})
if uri != "https://api.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id=clid&response_type=code&scope=address&redirect_uri=test" {
t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test and scope=address")
}
}

View File

@ -6,12 +6,13 @@ import (
"fmt" "fmt"
) )
const ( const (
// APIBaseSandBox points to the sandbox (for testing) version of the API // APIBaseSandBox points to the sandbox (for testing) version of the API
APIBaseSandBox = "https://api.sandbox.paypal.com/v1" APIBaseSandBox = "https://api.sandbox.paypal.com"
// APIBaseLive points to the live version of the API // APIBaseLive points to the live version of the API
APIBaseLive = "https://api.paypal.com/v1" APIBaseLive = "https://api.paypal.com"
) )
type ( type (