From c4d44ca3c921aef57756105fd5fe0a2194a1f495 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Tue, 24 Jun 2014 12:44:20 -0700 Subject: [PATCH] Add examples for regular and JWT configs and transport init. --- example_test.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ oauth2.go | 26 ------------- 2 files changed, 97 insertions(+), 26 deletions(-) create mode 100644 example_test.go diff --git a/example_test.go b/example_test.go new file mode 100644 index 00000000..bcd87132 --- /dev/null +++ b/example_test.go @@ -0,0 +1,97 @@ +package oauth2 + +import ( + "fmt" + "log" + "net/http" +) + +func Example_config() { + conf, err := NewConfig(&Options{ + ClientID: "YOUR_CLIENT_ID", + ClientSecret: "YOUR_CLIENT_SECRET", + RedirectURL: "YOUR_REDIRECT_URL", + Scopes: []string{"SCOPE1", "SCOPE2"}, + }, + "https://provider.com/o/oauth2/auth", + "https://provider.com/o/oauth2/token") + if err != nil { + log.Fatal(err) + } + + // Redirect user to consent page to ask for permission + // for the scopes specified above. + url, _ := conf.AuthCodeURL("") + fmt.Printf("Visit the URL for the auth dialog: %v", url) + + // Use the exchange code that is handled by the redirect URL. + // NewTransportWithCode will do the handshake to retrieve + // an access token and iniate a Transport that is + // authorized and authenticated the retrieved token. + var exchangeCode string + fmt.Scanln(&exchangeCode) + t, err := conf.NewTransportWithCode(exchangeCode) + if err != nil { + log.Fatal(err) + } + + // You can use t to initiate a new http.Client and + // start making authenticated requests. + client := http.Client{Transport: t} + client.Get("https://host/path") + + // Alternatively, you can initiate a new transport + // with tokens from a cache. + cache := NewFileCache("/path/to/file") + // NewTransportWithCache will try to read the cached + // token, if any error occurs, it returns the error. + // If a token is available at the cache, initiates + // a new transport authorized and authenticated with + // the read token. If token expires, and a new access + // token is retrieved, it writes the newly fetched + // token to the cache. + t, err = conf.NewTransportWithCache(cache) + if err != nil { + log.Fatal(err) + } + client = http.Client{Transport: t} + client.Get("https://host/path") +} + +func Example_jWTConfig() { + conf, _ := NewJWTConfig(&JWTOptions{ + Email: "xxx@developer.gserviceaccount.com", + // The path to the pem file. If you have a p12 file instead, you + // can use `openssl` to export the private key into a pem file. + // $ openssl pkcs12 -in key.p12 -out key.pem -nodes + PemFilename: "/path/to/pem/file.pem", + Scopes: []string{"SCOPE1", "SCOPE2"}, + }, + "https://provider.com/o/oauth2/token") + + // Initiate an http.Client, the following GET request will be + // authorized and authenticated on the behalf of + // xxx@developer.gserviceaccount.com. + client := http.Client{Transport: conf.NewTransport()} + client.Get("https://host/path") + + // If you would like to impersonate a user, you can + // create a transport with a subject. The following GET + // request will be made on the behalf of user@example.com. + client = http.Client{Transport: conf.NewTransportWithUser("user@example.com")} + client.Get("https://host/path") + + // Alternatively you can iniate a transport with + // a token read from the cache. + // If the existing access token expires, and a new access token is + // retrieved, the newly fetched token will be written to the cache. + cache := NewFileCache("/path/to/file") + t, err := conf.NewTransportWithCache(cache) + if err != nil { + log.Fatal(err) + } + client = http.Client{Transport: t} + // The following request will be authorized by the token + // retrieved from the cache. + client.Get("https://host/path") +} diff --git a/oauth2.go b/oauth2.go index b87de7d6..27882ac9 100644 --- a/oauth2.go +++ b/oauth2.go @@ -5,32 +5,6 @@ // Package oauth2 provides support for making // OAuth2 authorized and authenticated HTTP requests. // It can additionally grant authorization with Bearer JWT. -// -// Example usage: -// -// // Specify your configuration. (typically as a global variable) -// config := oauth2.NewConfig(&oauth2.Options{ -// ClientID: YOUR_CLIENT_ID, -// ClientSecret: YOUR_CLIENT_SECRET, -// RedirectURL: "http://you.example.org/handler", -// Scopes: []string{ "scope1", "scope2" }, -// }, OAUTH2_PROVIDER_AUTH_URL, OAUTH2_PROVIDER_TOKEN_URL) -// -// // A landing page redirects to the OAuth provider to get the auth code. -// func landing(w http.ResponseWriter, r *http.Request) { -// http.Redirect(w, r, config.AuthCodeURL("foo"), http.StatusFound) -// } -// -// // The user will be redirected back to this handler, that takes the -// // "code" query parameter and Exchanges it for an access token. -// func handler(w http.ResponseWriter, r *http.Request) { -// t, err := config.NewTransportWithCode(r.FormValue("code")) -// // The Transport now has a valid Token. Create an *http.Client -// // with which we can make authenticated API requests. -// c := t.Client() -// c.Post(...) -// } -// package oauth2 import (