Skip to content

Commit 1930a7c

Browse files
committed
Improve README summary and usage docs
1 parent 5256a13 commit 1930a7c

File tree

4 files changed

+123
-28
lines changed

4 files changed

+123
-28
lines changed

README.md

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11

22
# OAuth1 [![Build Status](https://travis-ci.org/dghubble/oauth1.png)](https://travis-ci.org/dghubble/oauth1) [![GoDoc](http://godoc.org/github.com/dghubble/oauth1?status.png)](http://godoc.org/github.com/dghubble/oauth1)
33

4-
OAauth1 is a Go client implementation of the OAuth1 spec. It supports authorizing HTTP requests.
4+
OAauth1 is a Go implementation of the [OAuth 1 spec](https://tools.ietf.org/html/rfc5849).
55

6-
The OAuth1 package takes design cues from the [golang.org/x/oauth2](https://godoc.org/golang.org/x/oauth2), providing an http.Client which handles signing requests and authorization via a custom Transport.
7-
8-
If an official oauth1 package were to be developed by the Go authors, I'd recommend you use that implementation instead. However, at this time, no official implementation exists.
9-
10-
## Note
11-
12-
This library is currently under development. It provides a signing http.Client, but does not yet completely implement the spec or handle credential retrieval from a provider backend.
6+
It takes design cues from [golang.org/x/oauth2](https://godoc.org/golang.org/x/oauth2), providing an `http.Client` which handles authorization and signing.
137

148
## Install
159

@@ -19,11 +13,39 @@ This library is currently under development. It provides a signing http.Client,
1913

2014
Read [GoDoc](https://godoc.org/github.com/dghubble/oauth1)
2115

22-
## Usage
16+
### Components
17+
18+
An `Endpoint` groups an OAuth provider's URLs for getting a request token, allowing users to authorize applications, and getting access tokens. Endpoints for common providers like [twitter](twitter) and [dropbox](dropbox) are provided in subpackages.
19+
20+
A `Config` stores a consumer application's consumer key and secret, the callback URL, and the Endpoint to which the consumer is registered. It provides OAuth 1 authorization flow methods and a `Client(token *Token)` method which returns an `http.Client` which will transparently authorize requests.
21+
22+
An OAuth1 `Token` is an access token which allows requests to be made as a particular user. It has fields `Token` and `TokenSecret`. If you already have an access token, skip to [Authorized Requests](#Authorized Requests).
23+
24+
If you've used [golang.org/x/oauth2](https://godoc.org/golang.org/x/oauth2) before, this organization is similar.
2325

24-
Create an application `Config` with a `ConsumerKey` and `ConsumerSecret`. Obtain a token credential in some way (many providers offer a web interface or command line tool for this) and create a `Token`.
26+
### Authorization Flow
27+
28+
The OAuth 1 authorization flow to request that a user grant an application access to his/her account (via an access token) typically looks like:
29+
30+
* User visits Consumer's "/login" route (via "Login with Provider" button)
31+
* Login handler calls `config.GetRequestToken()`
32+
* Login handler redirects user to `config.AuthorizationURL(rt *RequestToken)`
33+
* Provider calls Consumer's CallbackURL with a `verifier`
34+
* `config.GetAccessToken(rt *RequestToken, verifier string)`
35+
* Consumer application stores access token. Optionally creates some form of unforgeable session state.
36+
37+
For more details, see the Twitter PIN-based login [example](examples) or the
38+
[go-twitter](https://github.com/dghubble/go-twitter) `login` package.
39+
40+
### Authorized Requests
41+
42+
After an access `Token` has been obtained, authorized requests can be made on behalf of the user.
2543

2644
```go
45+
import (
46+
"github.com/dghubble/oauth1"
47+
)
48+
2749
func main() {
2850
config := oauth1.NewConfig("consumerKey", "consumerSecret")
2951
token := oauth1.NewToken("token", "tokenSecret")
@@ -40,4 +62,6 @@ func main() {
4062
}
4163
```
4264

65+
See more request [examples](examples).
66+
4367

config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type RequestToken struct {
5959
// RequestTokenURL. The request is signed by the consumer secret and an empty
6060
// token secret. The response body form is validated to ensure
6161
// oauth_callback_confirmed is true. Returns a new RequestToken with the
62-
// oauth_token and oauth_token_secret in the body.
62+
// oauth_token and oauth_token_secret from the body.
6363
// See RFC 5849 2.1 Temporary Credentials.
6464
func (c *Config) GetRequestToken() (*RequestToken, error) {
6565
req, err := http.NewRequest("POST", c.Endpoint.RequestTokenURL, nil)
@@ -115,7 +115,7 @@ func (c *Config) AuthorizationURL(rt *RequestToken) (*url.URL, error) {
115115
// and oauth_verifier are parsed and returned. The oauth_token (temporary
116116
// credential) identifies the RequestToken pair obtained from GetRequestToken
117117
// previously.
118-
// See RFC 2.2 Resource Owner Authorization.
118+
// See RFC 5849 2.2 Resource Owner Authorization.
119119
func (c *Config) HandleAuthorizationCallback(req *http.Request) (tokenKey, verifier string, err error) {
120120
// parse the raw query from the URL into req.Form
121121
err = req.ParseForm()
@@ -135,7 +135,7 @@ func (c *Config) HandleAuthorizationCallback(req *http.Request) (tokenKey, verif
135135
// Endpoint AccessTokenURL. The request is signed by the consumer secret and
136136
// request token secret pair. The access oauth_token and oauth_secret are
137137
// read from the response body form to return an AccessToken.
138-
// See RFC 2.3 Token Credentials.
138+
// See RFC 5849 2.3 Token Credentials.
139139
func (c *Config) GetAccessToken(requestToken *RequestToken, verifier string) (*Token, error) {
140140
req, err := http.NewRequest("POST", c.Endpoint.AccessTokenURL, nil)
141141
if err != nil {

doc.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Package oauth1 is a Go implementation of the OAuth1 spec.
3+
4+
Components
5+
6+
An Endpoint groups an OAuth provider's URLs for getting a request token,
7+
allowing users to authorize applications, and getting access tokens. Endpoints
8+
for common providers like Twitter and Dropbox are provided in subpackages.
9+
10+
A Config stores a consumer application's consumer key and secret, the
11+
callback URL, and the Endpoint to which the consumer is registered. It
12+
provides OAuth 1 authorization flow methods and a Client(token *Token)
13+
method which returns an http.Client which will transparently authorize
14+
requests.
15+
16+
An OAuth1 Token is an access token which allows requests to be made as a
17+
particular user. It has fields Token and TokenSecret. If you already have
18+
an access token, skip to Authorized Requests.
19+
20+
If you've used https://godoc.org/golang.org/x/oauth2 before, this organization
21+
is similar.
22+
23+
Authorization Flow
24+
25+
The OAuth 1 authorization flow to request that a user grant an application
26+
access to his/her account (via an access token) typically looks like:
27+
28+
* User visits Consumer's "/login" route (via "Login with Provider"
29+
button)
30+
* Login handler calls config.GetRequestToken()
31+
* Login handler redirects user to config.AuthorizationURL(rt *RequestToken)
32+
* Provider calls Consumer's CallbackURL with a verifier
33+
* config.GetAccessToken(rt *RequestToken, verifier string)
34+
* Consumer application stores access token. Optionally creates some form of
35+
unforgeable session state.
36+
37+
For more details, see the Twitter PIN-based login example or the
38+
https://github.com/dghubble/go-twitter login package.
39+
40+
Authorized Requests
41+
42+
After an access Token has been obtained, authorized requests can be made on
43+
behalf of the user.
44+
45+
import (
46+
"github.com/dghubble/oauth1"
47+
)
48+
49+
func main() {
50+
config := oauth1.NewConfig("consumerKey", "consumerSecret")
51+
token := oauth1.NewToken("token", "tokenSecret")
52+
53+
// httpClient will automatically authorize http.Request's
54+
httpClient := config.Client(token)
55+
56+
// example Twitter API request
57+
path := "https://api.twitter.com/1.1/statuses/home_timeline.json?count=2"
58+
resp, _ := httpClient.Get(path)
59+
defer resp.Body.Close()
60+
body, _ := ioutil.ReadAll(resp.Body)
61+
fmt.Printf("Raw Response Body:\n%v\n", string(body))
62+
}
63+
64+
See more request examples in the examples subpackage.
65+
*/
66+
package oauth1

examples/README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@
33

44
## Twitter
55

6-
### Login Flow (PIN)
7-
8-
A consumer application can obtain a Twitter Access Token for a user by requesting the user grant access via [3-legged](https://dev.twitter.com/oauth/3-legged) or [PIN-based](https://dev.twitter.com/oauth/pin-based) OAuth 1.
9-
10-
export TWITTER_CONSUMER_KEY=xxx
11-
export TWITTER_CONSUMER_SECRET=yyy
12-
13-
go run twitter-login.go
14-
15-
Open this URL in your browser:
16-
https://api.twitter.com/oauth/authenticate?oauth_token=xxx
17-
Paste your PIN here: ddddddd
18-
Consumer was granted an access token to act on behalf of a user.
19-
token: ddddd-xxxxx
20-
secret: yyyyyy
6+
### Login Flow (PIN-based)
7+
8+
An application can obtain a Twitter Access Token for a user by requesting the user grant access via [3-legged](https://dev.twitter.com/oauth/3-legged) or [PIN-based](https://dev.twitter.com/oauth/pin-based) OAuth 1.
9+
10+
```
11+
export TWITTER_CONSUMER_KEY=xxx
12+
export TWITTER_CONSUMER_SECRET=yyy
13+
```
14+
15+
Run `twitter-login.go` to authorize the consumer application to a Twitter account.
16+
17+
```
18+
go run twitter-login.go
19+
Open this URL in your browser:
20+
https://api.twitter.com/oauth/authenticate?oauth_token=xxx
21+
Paste your PIN here: ddddddd
22+
Consumer was granted an access token to act on behalf of a user.
23+
token: ddddd-xxxxx
24+
secret: yyyyyy
25+
```
2126

2227
Note that website backends should define a CallbackURL which can receive a verifier string and request an access token, "oob" is for PIN-based agents such as the command line.
2328

0 commit comments

Comments
 (0)