From 2f9803548f3c2152fec180860b34af82ff44b7ed Mon Sep 17 00:00:00 2001 From: Aliaksandr Pliutau Date: Wed, 20 Jan 2016 11:17:19 +0700 Subject: [PATCH] GET /v1/identity/openidconnect/userinfo/?schema=openid --- README.md | 8 ++++++++ client_test.go | 1 + examples/main.go | 7 +++++++ identity.go | 24 +++++++++++++++++++++++- identity_test.go | 22 ++++++++++++++++------ types.go | 20 ++++++++++++++++++++ 6 files changed, 75 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b6931c0..17ea383 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ * POST /v1/payments/orders/**ID**/authorize * POST /v1/payments/orders/**ID**/capture * POST /v1/payments/orders/**ID**/do-void + * POST /v1/identity/openidconnect/tokenservice + * GET /v1/identity/openidconnect/userinfo/?schema=**SCHEMA** #### Missing endpoints It is possible that some endpoints are missing in this SDK Client, but you can use built-in **paypalsdk** functions to perform a request: **NewClient -> NewRequest -> SendWithAuth** @@ -198,6 +200,12 @@ token, err := c.GrantNewAccessTokenFromAuthCode("", "http:// token, err := c.GrantNewAccessTokenFromRefreshToken("") ``` +#### Retreive user information + +```go +userInfo, err := c.GetUserInfo("openid") +``` + #### How to Contribute * Fork a repository diff --git a/client_test.go b/client_test.go index 918f9e1..eb37983 100644 --- a/client_test.go +++ b/client_test.go @@ -15,6 +15,7 @@ var testFakeOrderID = "FAKE-O-4J082351X3132253H" var testSaleID = "4CF18861HF410323U" var testPaymentID = "PAY-5YK922393D847794YKER7MUI" var testPayerID = "CR87QHB7JTRSC" +var testUserID = "https://www.paypal.com/webapps/auth/identity/user/WEssgRpQij92sE99_F9MImvQ8FPYgUEjrvCja2qH2H8" func TestNewClient(t *testing.T) { _, err := NewClient("", "", "") diff --git a/examples/main.go b/examples/main.go index bc90fe0..794a905 100644 --- a/examples/main.go +++ b/examples/main.go @@ -137,4 +137,11 @@ func main() { } else { fmt.Println("ERROR: " + err.Error()) } + + u, err := c.GetUserInfo("openid") + if err == nil { + fmt.Println("DEBUG: UserID=" + u.ID) + } else { + fmt.Println("ERROR: " + err.Error()) + } } diff --git a/identity.go b/identity.go index c7b4c34..9d21885 100644 --- a/identity.go +++ b/identity.go @@ -1,6 +1,9 @@ package paypalsdk -import "fmt" +import ( + "fmt" + "net/http" +) // GrantNewAccessTokenFromAuthCode - Use this call to grant a new access token, using the previously obtained authorization code. // Endpoint: POST /v1/identity/openidconnect/tokenservice @@ -48,3 +51,22 @@ func (c *Client) GrantNewAccessTokenFromRefreshToken(refreshToken string) (*Toke return token, nil } + +// GetUserInfo - Use this call to retrieve user profile attributes. +// Endpoint: GET /v1/identity/openidconnect/userinfo/?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) { + u := UserInfo{} + + req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/identity/openidconnect/userinfo/?schema=", schema), nil) + if err != nil { + return &u, err + } + + err = c.SendWithAuth(req, &u) + if err != nil { + return &u, err + } + + return &u, nil +} diff --git a/identity_test.go b/identity_test.go index 6c3da93..6336e9b 100644 --- a/identity_test.go +++ b/identity_test.go @@ -5,17 +5,27 @@ import "testing" func TestGrantNewAccessTokenFromAuthCode(t *testing.T) { c, _ := NewClient(testClientID, testSecret, APIBaseSandBox) - token, _ := c.GrantNewAccessTokenFromAuthCode("123", "http://example.com/myapp/return.php") - if token.Token != "" { - t.Errorf("Empty token must be returned for invalid code") + _, err := c.GrantNewAccessTokenFromAuthCode("123", "http://example.com/myapp/return.php") + if err == nil { + t.Errorf("GrantNewAccessTokenFromAuthCode must return error for invalid code") } } func TestGrantNewAccessTokenFromRefreshToken(t *testing.T) { c, _ := NewClient(testClientID, testSecret, APIBaseSandBox) - token, _ := c.GrantNewAccessTokenFromRefreshToken("123") - if token.Token != "" { - t.Errorf("Empty token must be returned for invalid refresh token") + _, err := c.GrantNewAccessTokenFromRefreshToken("123") + if err == nil { + t.Errorf("GrantNewAccessTokenFromRefreshToken must return error for invalid refresh token") + } +} + +func TestGetUserInfo(t *testing.T) { + c, _ := NewClient(testClientID, testSecret, APIBaseSandBox) + c.GetAccessToken() + + u, err := c.GetUserInfo("openid") + if u.ID != testUserID || err != nil { + t.Errorf("GetUserInfo must return valid test ID=" + testUserID) } } diff --git a/types.go b/types.go index 212683f..ae7d30d 100644 --- a/types.go +++ b/types.go @@ -275,6 +275,26 @@ type ( Custom string `json:"custom,omitempty"` SoftDescriptor string `json:"soft_descriptor,omitempty"` } + + // UserInfo https://developer.paypal.com/webapps/developer/docs/api/#userinfo-object + UserInfo struct { + ID string `json:"user_id"` + Name string `json:"name"` + GivenName string `json:"given_name"` + FamilyName string `json:"family_name"` + Email string `json:"email"` + Verified bool `json:"verified,omitempty"` + Gender string `json:"gender,omitempty"` + BirthDate string `json:"birthdate,omitempty"` + ZoneInfo string `json:"zoneinfo,omitempty"` + Locale string `json:"locale,omitempty"` + Phone string `json:"phone_number,omitempty"` + Address *Address `json:"address,omitempty"` + VerifiedAccount bool `json:"verified_account,omitempty"` + AccountType string `json:"account_type,omitempty"` + AgeRange string `json:"age_range,omitempty"` + PayerID string `json:"payer_id,omitempty"` + } ) // Error method implementation for ErrorResponse struct