From 87cea6c59067d8dac8cd252c8818345ee85f1677 Mon Sep 17 00:00:00 2001 From: Jumpei Tsuji Date: Tue, 26 May 2015 17:29:34 +0900 Subject: [PATCH] Update oauth library --- README.md | 17 ++++-- playstore/validator.go | 58 ++++++++++----------- playstore/validator_test.go | 100 +++++++++++++++++++----------------- 3 files changed, 93 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 28f4c9f..2da2de5 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,13 @@ Current API Documents: * AppStore: [![GoDoc](https://godoc.org/github.com/dogenzaka/go-iap/appstore?status.svg)](https://godoc.org/github.com/dogenzaka/go-iap/appstore) * GooglePlay: [![GoDoc](https://godoc.org/github.com/dogenzaka/go-iap/playstore?status.svg)](https://godoc.org/github.com/dogenzaka/go-iap/playstore) +# Dependencies +``` +go get github.com/parnurzeal/gorequest +go get golang.org/x/net/context +go get golang.org/x/oauth2 +go get google.golang.org/api/androidpublisher/v2 +``` # Installation ``` @@ -41,19 +48,19 @@ func main() { ``` import( - "code.google.com/p/goauth2/oauth" + "golang.org/x/oauth2" "github.com/dogenzaka/go-iap/playstore" ) func main() { - // You need to prepare an authorization code or a refresh token - // If you have a refresh token, you can generate an oauth token like this - oauth := &oauth.Token{ + // You need to prepare an authorization code or a refresh token. + // If you have a refresh token, you can generate an oauth token like this. + token := &oauth2.Token{ RefreshToken: "your refresh token", } - client := playstore.New(oauth) + client := playstore.New(token) resp, err := client.VerifySubscription("package", "subscriptionID", "purchaseToken") } ``` diff --git a/playstore/validator.go b/playstore/validator.go index f90b64a..3a9eb1a 100644 --- a/playstore/validator.go +++ b/playstore/validator.go @@ -2,13 +2,13 @@ package playstore import ( "errors" - "net" "net/http" "os" "time" - "code.google.com/p/goauth2/oauth" - "code.google.com/p/google-api-go-client/androidpublisher/v2" + "golang.org/x/net/context" + "golang.org/x/oauth2" + androidpublisher "google.golang.org/api/androidpublisher/v2" ) const ( @@ -19,22 +19,24 @@ const ( timeout = time.Second * 5 ) -var defaultConfig *oauth.Config +var defaultConfig *oauth2.Config var defaultTimeout = timeout // Init initializes the global configuration func Init() error { - defaultConfig = &oauth.Config{ - Scope: scope, - AuthURL: authURL, - TokenURL: tokenURL, + defaultConfig = &oauth2.Config{ + Scopes: []string{scope}, + Endpoint: oauth2.Endpoint{ + AuthURL: authURL, + TokenURL: tokenURL, + }, } clientID := os.Getenv("IAB_CLIENT_ID") if clientID != "" { - defaultConfig.ClientId = clientID + defaultConfig.ClientID = clientID } - if defaultConfig.ClientId == "" { + if defaultConfig.ClientID == "" { return errors.New("Client ID is required") } @@ -50,8 +52,8 @@ func Init() error { } // InitWithConfig initializes the global configuration with parameters -func InitWithConfig(config *oauth.Config) error { - if config.ClientId == "" { +func InitWithConfig(config *oauth2.Config) error { + if config.ClientID == "" { return errors.New("Client ID is required") } @@ -59,16 +61,16 @@ func InitWithConfig(config *oauth.Config) error { return errors.New("Client Secret Key is required") } - if config.Scope == "" { - config.Scope = scope + if len(config.Scopes) == 0 { + config.Scopes = []string{scope} } - if config.AuthURL == "" { - config.AuthURL = authURL + if config.Endpoint.AuthURL == "" { + config.Endpoint.AuthURL = authURL } - if config.TokenURL == "" { - config.TokenURL = tokenURL + if config.Endpoint.TokenURL == "" { + config.Endpoint.TokenURL = tokenURL } defaultConfig = config @@ -84,6 +86,7 @@ func SetTimeout(t time.Duration) { // The IABClient type is an interface to verify purchase token type IABClient interface { VerifySubscription(string, string, string) (*androidpublisher.SubscriptionPurchase, error) + VerifyProduct(string, string, string) (*androidpublisher.ProductPurchase, error) } // The Client type implements VerifySubscription method @@ -92,16 +95,13 @@ type Client struct { } // New returns http client which has oauth token -func New(token *oauth.Token) Client { - t := &oauth.Transport{ - Token: token, - Config: defaultConfig, - Transport: &http.Transport{ - Dial: dialTimeout, - }, - } +func New(token *oauth2.Token) Client { + ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, &http.Client{ + Timeout: defaultTimeout, + }) + + httpClient := defaultConfig.Client(ctx, token) - httpClient := t.Client() return Client{httpClient} } @@ -138,7 +138,3 @@ func (c *Client) VerifyProduct( return result, err } - -func dialTimeout(network, addr string) (net.Conn, error) { - return net.DialTimeout(network, addr, defaultTimeout) -} diff --git a/playstore/validator_test.go b/playstore/validator_test.go index 248944e..84a318b 100644 --- a/playstore/validator_test.go +++ b/playstore/validator_test.go @@ -7,16 +7,18 @@ import ( "testing" "time" - "code.google.com/p/goauth2/oauth" + "golang.org/x/oauth2" ) func TestInit(t *testing.T) { - expected := &oauth.Config{ - ClientId: "dummyId", + expected := &oauth2.Config{ + ClientID: "dummyId", ClientSecret: "dummySecret", - Scope: "https://www.googleapis.com/auth/androidpublisher", - AuthURL: "https://accounts.google.com/o/oauth2/auth", - TokenURL: "https://accounts.google.com/o/oauth2/token", + Scopes: []string{"https://www.googleapis.com/auth/androidpublisher"}, + Endpoint: oauth2.Endpoint{ + AuthURL: "https://accounts.google.com/o/oauth2/auth", + TokenURL: "https://accounts.google.com/o/oauth2/token", + }, } os.Setenv("IAB_CLIENT_ID", "dummyId") os.Setenv("IAB_CLIENT_SECRET", "dummySecret") @@ -41,21 +43,21 @@ func TestInitWithoutClientSecret(t *testing.T) { } func TestInitWithConfig(t *testing.T) { - expected := &oauth.Config{ - ClientId: "dummyId", + expected := &oauth2.Config{ + ClientID: "dummyId", ClientSecret: "dummySecret", - Scope: "https://www.googleapis.com/auth/androidpublisher", - AuthURL: "https://accounts.google.com/o/oauth2/auth", - TokenURL: "https://accounts.google.com/o/oauth2/token", + Scopes: []string{"https://www.googleapis.com/auth/androidpublisher"}, + Endpoint: oauth2.Endpoint{ + AuthURL: "https://accounts.google.com/o/oauth2/auth", + TokenURL: "https://accounts.google.com/o/oauth2/token", + }, } - config := &oauth.Config{ - ClientId: "dummyId", + config := &oauth2.Config{ + ClientID: "dummyId", ClientSecret: "dummySecret", - Scope: "https://www.googleapis.com/auth/androidpublisher", - AuthURL: "https://accounts.google.com/o/oauth2/auth", - TokenURL: "https://accounts.google.com/o/oauth2/token", } + InitWithConfig(config) actual := defaultConfig if !reflect.DeepEqual(actual, expected) { @@ -66,10 +68,12 @@ func TestInitWithConfig(t *testing.T) { func TestInitWithConfigErrors(t *testing.T) { expected := errors.New("Client ID is required") - config := &oauth.Config{ - Scope: "https://www.googleapis.com/auth/androidpublisher", - AuthURL: "https://accounts.google.com/o/oauth2/auth", - TokenURL: "https://accounts.google.com/o/oauth2/token", + config := &oauth2.Config{ + Scopes: []string{"https://www.googleapis.com/auth/androidpublisher"}, + Endpoint: oauth2.Endpoint{ + AuthURL: "https://accounts.google.com/o/oauth2/auth", + TokenURL: "https://accounts.google.com/o/oauth2/token", + }, } actual := InitWithConfig(config) @@ -78,11 +82,13 @@ func TestInitWithConfigErrors(t *testing.T) { } expected = errors.New("Client Secret Key is required") - config = &oauth.Config{ - ClientId: "dummyId", - Scope: "https://www.googleapis.com/auth/androidpublisher", - AuthURL: "https://accounts.google.com/o/oauth2/auth", - TokenURL: "https://accounts.google.com/o/oauth2/token", + config = &oauth2.Config{ + ClientID: "dummyId", + Scopes: []string{"https://www.googleapis.com/auth/androidpublisher"}, + Endpoint: oauth2.Endpoint{ + AuthURL: "https://accounts.google.com/o/oauth2/auth", + TokenURL: "https://accounts.google.com/o/oauth2/token", + }, } actual = InitWithConfig(config) @@ -93,27 +99,29 @@ func TestInitWithConfigErrors(t *testing.T) { func TestNew(t *testing.T) { // Initialize config - _config := &oauth.Config{ - ClientId: "dummyId", + _config := &oauth2.Config{ + ClientID: "dummyId", ClientSecret: "dummySecret", + RedirectURL: "REDIRECT_URL", + Scopes: []string{"scope1", "scope2"}, + Endpoint: oauth2.Endpoint{ + AuthURL: "http://example.com/auth", + TokenURL: "http://example.com/token", + }, } InitWithConfig(_config) - token := &oauth.Token{ + _token := &oauth2.Token{ AccessToken: "accessToken", RefreshToken: "refreshToken", - Expiry: time.Unix(1234567890, 0).UTC(), + Expiry: time.Unix(2234567890, 0).UTC(), } - actual := New(token) - val, _ := actual.httpClient.Transport.(*oauth.Transport) - - if !reflect.DeepEqual(val.Config, _config) { - t.Errorf("got %v\nwant %v", val.Config, _config) - } - - if !reflect.DeepEqual(val.Token, token) { - t.Errorf("got %v\nwant %v", val.Token, token) + actual := New(_token) + val := actual.httpClient.Transport.(*oauth2.Transport) + token, _ := val.Source.Token() + if !reflect.DeepEqual(token, _token) { + t.Errorf("got %v\nwant %v", token, _token) } } @@ -130,21 +138,21 @@ func TestVerifySubscription(t *testing.T) { Init() // Exception scenario - token := &oauth.Token{ + token := &oauth2.Token{ AccessToken: "accessToken", RefreshToken: "refreshToken", - Expiry: time.Unix(1234567890, 0).UTC(), + Expiry: time.Unix(2234567890, 0).UTC(), } client := New(token) - expected := "Get https://www.googleapis.com/androidpublisher/v2/applications/package/purchases/subscriptions/subscriptionID/tokens/purchaseToken?alt=json: OAuthError: updateToken: Unexpected HTTP status 400 Bad Request" + expected := "googleapi: Error 401: Invalid Credentials, authError" _, err := client.VerifySubscription("package", "subscriptionID", "purchaseToken") if err.Error() != expected { t.Errorf("got %v", err) } - // TODO Nomal scenario + // TODO Normal scenario } func TestVerifySubscriptionAndroidPublisherError(t *testing.T) { @@ -161,21 +169,21 @@ func TestVerifyProduct(t *testing.T) { Init() // Exception scenario - token := &oauth.Token{ + token := &oauth2.Token{ AccessToken: "accessToken", RefreshToken: "refreshToken", - Expiry: time.Unix(1234567890, 0).UTC(), + Expiry: time.Unix(2234567890, 0).UTC(), } client := New(token) - expected := "Get https://www.googleapis.com/androidpublisher/v2/applications/package/purchases/products/productID/tokens/purchaseToken?alt=json: OAuthError: updateToken: Unexpected HTTP status 400 Bad Request" + expected := "googleapi: Error 401: Invalid Credentials, authError" _, err := client.VerifyProduct("package", "productID", "purchaseToken") if err.Error() != expected { t.Errorf("got %v", err) } - // TODO Nomal scenario + // TODO Normal scenario } func TestVerifyProductAndroidPublisherError(t *testing.T) {