2019-08-21 15:50:20 +02:00
|
|
|
package paypal
|
2016-01-18 09:42:42 +01:00
|
|
|
|
2016-01-20 05:17:19 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-10-21 15:17:58 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-01-20 05:17:19 +01:00
|
|
|
)
|
2016-01-18 09:42:42 +01:00
|
|
|
|
|
|
|
// GrantNewAccessTokenFromAuthCode - Use this call to grant a new access token, using the previously obtained authorization code.
|
|
|
|
// Endpoint: POST /v1/identity/openidconnect/tokenservice
|
2019-06-16 04:39:08 +02:00
|
|
|
func (c *Client) GrantNewAccessTokenFromAuthCode(code, redirectURI string) (*TokenResponse, error) {
|
2016-01-18 09:42:42 +01:00
|
|
|
token := &TokenResponse{}
|
|
|
|
|
2017-10-21 15:17:58 +02:00
|
|
|
q := url.Values{}
|
|
|
|
q.Set("grant_type", "authorization_code")
|
|
|
|
q.Set("code", code)
|
|
|
|
q.Set("redirect_uri", redirectURI)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/identity/openidconnect/tokenservice"), strings.NewReader(q.Encode()))
|
2016-01-18 09:42:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
2017-10-21 15:17:58 +02:00
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
2017-11-23 03:15:11 +01:00
|
|
|
if err = c.SendWithBasicAuth(req, token); err != nil {
|
2016-01-18 09:42:42 +01:00
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GrantNewAccessTokenFromRefreshToken - Use this call to grant a new access token, using a refresh token.
|
|
|
|
// Endpoint: POST /v1/identity/openidconnect/tokenservice
|
|
|
|
func (c *Client) GrantNewAccessTokenFromRefreshToken(refreshToken string) (*TokenResponse, error) {
|
|
|
|
type request struct {
|
|
|
|
GrantType string `json:"grant_type"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
token := &TokenResponse{}
|
|
|
|
|
|
|
|
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/identity/openidconnect/tokenservice"), request{GrantType: "refresh_token", RefreshToken: refreshToken})
|
|
|
|
if err != nil {
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
2017-11-23 03:15:11 +01:00
|
|
|
if err = c.SendWithAuth(req, token); err != nil {
|
2016-01-18 09:42:42 +01:00
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
2016-01-20 05:17:19 +01:00
|
|
|
|
|
|
|
// GetUserInfo - Use this call to retrieve user profile attributes.
|
|
|
|
// Endpoint: GET /v1/identity/openidconnect/userinfo/?schema=<Schema>
|
|
|
|
// Pass the schema that is used to return as per openidconnect protocol. The only supported schema value is openid.
|
|
|
|
func (c *Client) GetUserInfo(schema string) (*UserInfo, error) {
|
2019-06-16 04:39:08 +02:00
|
|
|
u := &UserInfo{}
|
2016-01-20 05:17:19 +01:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/identity/openidconnect/userinfo/?schema=", schema), nil)
|
|
|
|
if err != nil {
|
2019-06-16 04:39:08 +02:00
|
|
|
return u, err
|
2016-01-20 05:17:19 +01:00
|
|
|
}
|
|
|
|
|
2019-06-16 04:39:08 +02:00
|
|
|
if err = c.SendWithAuth(req, u); err != nil {
|
|
|
|
return u, err
|
2016-01-20 05:17:19 +01:00
|
|
|
}
|
|
|
|
|
2019-06-16 04:39:08 +02:00
|
|
|
return u, nil
|
2016-01-20 05:17:19 +01:00
|
|
|
}
|