oauth

package module
Version: v0.0.0-...-126b352 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2019 License: MIT Imports: 20 Imported by: 568

README

OAuth 1.0 Library for Go

GoDoc

CircleCI

(If you need an OAuth 2.0 library, check out: https://godoc.org/golang.org/x/oauth2)

Developing your own apps, with this library

  • First, install the library

      go get github.com/mrjones/oauth
    
  • Then, check out the comments in oauth.go

  • Or, have a look at the examples:

    • Netflix

        go run examples/netflix/netflix.go --consumerkey [key] --consumersecret [secret] --appname [appname]
      
    • Twitter

      Command line:

        go run examples/twitter/twitter.go --consumerkey [key] --consumersecret [secret]
      

      Or, in the browser (using an HTTP server):

        go run examples/twitterserver/twitterserver.go --consumerkey [key] --consumersecret [secret] --port 8888        
      
    • The Google Latitude example is broken, now that Google uses OAuth 2.0

Contributing to this library

  • Please install the pre-commit hook, which will run tests, and go-fmt before committing.

      ln -s $PWD/pre-commit.sh .git/hooks/pre-commit
    
  • Running tests and building is as you'd expect:

      go test *.go
      go build *.go
    

Documentation

Overview

OAuth 1.0 consumer implementation. See http://www.oauth.net and RFC 5849

There are typically three parties involved in an OAuth exchange:

(1) The "Service Provider" (e.g. Google, Twitter, NetFlix) who operates the
    service where the data resides.
(2) The "End User" who owns that data, and wants to grant access to a third-party.
(3) That third-party who wants access to the data (after first being authorized by
    the user). This third-party is referred to as the "Consumer" in OAuth
    terminology.

This library is designed to help implement the third-party consumer by handling the low-level authentication tasks, and allowing for authenticated requests to the service provider on behalf of the user.

Caveats:

- Currently only supports HMAC and RSA signatures.
- Currently only supports SHA1 and SHA256 hashes.
- Currently only supports OAuth 1.0

Overview of how to use this library:

(1) First create a new Consumer instance with the NewConsumer function
(2) Get a RequestToken, and "authorization url" from GetRequestTokenAndUrl()
(3) Save the RequestToken, you will need it again in step 6.
(4) Redirect the user to the "authorization url" from step 2, where they will
    authorize your access to the service provider.
(5) Wait. You will be called back on the CallbackUrl that you provide, and you
    will recieve a "verification code".
(6) Call AuthorizeToken() with the RequestToken from step 2 and the
    "verification code" from step 5.
(7) You will get back an AccessToken.  Save this for as long as you need access
    to the user's data, and treat it like a password; it is a secret.
(8) You can now throw away the RequestToken from step 2, it is no longer
    necessary.
(9) Call "MakeHttpClient" using the AccessToken from step 7 to get an
    HTTP client which can access protected resources.

Index

Constants

View Source
const (
	OAUTH_VERSION         = "1.0"
	SIGNATURE_METHOD_HMAC = "HMAC-"
	SIGNATURE_METHOD_RSA  = "RSA-"

	HTTP_AUTH_HEADER       = "Authorization"
	OAUTH_HEADER           = "OAuth "
	BODY_HASH_PARAM        = "oauth_body_hash"
	CALLBACK_PARAM         = "oauth_callback"
	CONSUMER_KEY_PARAM     = "oauth_consumer_key"
	NONCE_PARAM            = "oauth_nonce"
	SESSION_HANDLE_PARAM   = "oauth_session_handle"
	SIGNATURE_METHOD_PARAM = "oauth_signature_method"
	SIGNATURE_PARAM        = "oauth_signature"
	TIMESTAMP_PARAM        = "oauth_timestamp"
	TOKEN_PARAM            = "oauth_token"
	TOKEN_SECRET_PARAM     = "oauth_token_secret"
	VERIFIER_PARAM         = "oauth_verifier"
	VERSION_PARAM          = "oauth_version"
)

Variables

View Source
var HASH_METHOD_MAP = map[crypto.Hash]string{
	crypto.SHA1:   "SHA1",
	crypto.SHA256: "SHA256",
}

Functions

This section is empty.

Types

type AccessToken

type AccessToken struct {
	Token          string
	Secret         string
	AdditionalData map[string]string
}

type ByValue

type ByValue []string

func (ByValue) Len

func (a ByValue) Len() int

func (ByValue) Less

func (a ByValue) Less(i, j int) bool

func (ByValue) Swap

func (a ByValue) Swap(i, j int)

type Consumer

type Consumer struct {
	// Some ServiceProviders require extra parameters to be passed for various reasons.
	// For example Google APIs require you to set a scope= parameter to specify how much
	// access is being granted.  The proper values for scope= depend on the service:
	// For more, see: http://code.google.com/apis/accounts/docs/OAuth.html#prepScope
	AdditionalParams map[string]string

	// Some APIs (e.g. Netflix) aren't quite standard OAuth, and require passing
	// additional parameters when authorizing the request token. For most APIs
	// this field can be ignored.  For Netflix, do something like:
	// 	consumer.AdditionalAuthorizationUrlParams = map[string]string{
	// 		"application_name":   "YourAppName",
	// 		"oauth_consumer_key": "YourConsumerKey",
	// 	}
	AdditionalAuthorizationUrlParams map[string]string

	// Defaults to http.Client{}, can be overridden (e.g. for testing) as necessary
	HttpClient HttpClient

	// Some APIs (e.g. Intuit/Quickbooks) require sending additional headers along with
	// requests. (like "Accept" to specify the response type as XML or JSON) Note that this
	// will only *add* headers, not set existing ones.
	AdditionalHeaders map[string][]string
	// contains filtered or unexported fields
}

Consumers are stateless, you can call the various methods (GetRequestTokenAndUrl, AuthorizeToken, and Get) on various different instances of Consumers *as long as they were set up in the same way.* It is up to you, as the caller to persist the necessary state (RequestTokens and AccessTokens).

func NewConsumer

func NewConsumer(consumerKey string, consumerSecret string,
	serviceProvider ServiceProvider) *Consumer

Creates a new Consumer instance, with a HMAC-SHA1 signer

- consumerKey and consumerSecret:
  values you should obtain from the ServiceProvider when you register your
  application.

- serviceProvider:
  see the documentation for ServiceProvider for how to create this.

func NewCustomConsumer

func NewCustomConsumer(consumerKey string, consumerSecret string,
	hashFunc crypto.Hash, serviceProvider ServiceProvider,
	httpClient *http.Client) *Consumer

Creates a new Consumer instance, with a HMAC signer

- consumerKey and consumerSecret:
  values you should obtain from the ServiceProvider when you register your
  application.

- hashFunc:
  the crypto.Hash to use for signatures

- serviceProvider:
  see the documentation for ServiceProvider for how to create this.

- httpClient:
  Provides a custom implementation of the httpClient used under the hood
  to make the request.  This is especially useful if you want to use
  Google App Engine. Can be nil for default.

func NewCustomHttpClientConsumer

func NewCustomHttpClientConsumer(consumerKey string, consumerSecret string,
	serviceProvider ServiceProvider, httpClient *http.Client) *Consumer

Creates a new Consumer instance, with a HMAC-SHA1 signer

     - consumerKey and consumerSecret:
       values you should obtain from the ServiceProvider when you register your
       application.

     - serviceProvider:
       see the documentation for ServiceProvider for how to create this.

		- httpClient:
		  Provides a custom implementation of the httpClient used under the hood
		  to make the request.  This is especially useful if you want to use
		  Google App Engine.

func NewCustomRSAConsumer

func NewCustomRSAConsumer(consumerKey string, privateKey *rsa.PrivateKey,
	hashFunc crypto.Hash, serviceProvider ServiceProvider,
	httpClient *http.Client) *Consumer

Creates a new Consumer instance, with a RSA signer

- consumerKey:
  value you should obtain from the ServiceProvider when you register your
  application.

- privateKey:
  the private key to use for signatures

- hashFunc:
  the crypto.Hash to use for signatures

- serviceProvider:
  see the documentation for ServiceProvider for how to create this.

- httpClient:
  Provides a custom implementation of the httpClient used under the hood
  to make the request.  This is especially useful if you want to use
  Google App Engine. Can be nil for default.

func NewRSAConsumer

func NewRSAConsumer(consumerKey string, privateKey *rsa.PrivateKey,
	serviceProvider ServiceProvider) *Consumer

Creates a new Consumer instance, with a RSA-SHA1 signer

- consumerKey:
  value you should obtain from the ServiceProvider when you register your
  application.

- privateKey:
  the private key to use for signatures

- serviceProvider:
  see the documentation for ServiceProvider for how to create this.

func (*Consumer) AuthorizeToken

func (c *Consumer) AuthorizeToken(rtoken *RequestToken, verificationCode string) (atoken *AccessToken, err error)

After the user has authorized you to the service provider, use this method to turn your temporary RequestToken into a permanent AccessToken. You must pass in two values:

- rtoken:
  The RequestToken returned from GetRequestTokenAndUrl()

- verificationCode:
  The string which passed back from the server, either as the oauth_verifier
  query param appended to callbackUrl *OR* a string manually entered by the user
  if callbackUrl is "oob"

It will return:

- atoken:
  A permanent AccessToken which can be used to access the user's data (until it is
  revoked by the user or the service provider).

- err:
  Set only if there was an error, nil otherwise.

func (*Consumer) AuthorizeTokenWithParams

func (c *Consumer) AuthorizeTokenWithParams(rtoken *RequestToken, verificationCode string, additionalParams map[string]string) (atoken *AccessToken, err error)

func (*Consumer) Debug

func (c *Consumer) Debug(enabled bool)

func (*Consumer) Delete

func (c *Consumer) Delete(url string, userParams map[string]string, token *AccessToken) (resp *http.Response, err error)

** DEPRECATED ** Please call "Delete" on the http client returned by MakeHttpClient instead

func (*Consumer) Get

func (c *Consumer) Get(url string, userParams map[string]string, token *AccessToken) (resp *http.Response, err error)

** DEPRECATED ** Please call Get on the http client returned by MakeHttpClient instead!

Executes an HTTP Get, authorized via the AccessToken.

- url:
  The base url, without any query params, which is being accessed

- userParams:
  Any key=value params to be included in the query string

- token:
  The AccessToken returned by AuthorizeToken()

This method returns:

- resp:
  The HTTP Response resulting from making this request.

- err:
  Set only if there was an error, nil otherwise.

func (*Consumer) GetRequestTokenAndUrl

func (c *Consumer) GetRequestTokenAndUrl(callbackUrl string) (rtoken *RequestToken, loginUrl string, err error)

Kicks off the OAuth authorization process.

- callbackUrl:
  Authorizing a token *requires* redirecting to the service provider. This is the
  URL which the service provider will redirect the user back to after that
  authorization is completed. The service provider will pass back a verification
  code which is necessary to complete the rest of the process (in AuthorizeToken).
  Notes on callbackUrl:
    - Some (all?) service providers allow for setting "oob" (for out-of-band) as a
      callback url.  If this is set the service provider will present the
      verification code directly to the user, and you must provide a place for
      them to copy-and-paste it into.
    - Otherwise, the user will be redirected to callbackUrl in the browser, and
      will append a "oauth_verifier=<verifier>" parameter.

This function returns:

- rtoken:
  A temporary RequestToken, used during the authorization process. You must save
  this since it will be necessary later in the process when calling
  AuthorizeToken().

- url:
  A URL that you should redirect the user to in order that they may authorize you
  to the service provider.

- err:
  Set only if there was an error, nil otherwise.

func (*Consumer) GetRequestTokenAndUrlWithParams

func (c *Consumer) GetRequestTokenAndUrlWithParams(callbackUrl string, additionalParams map[string]string) (rtoken *RequestToken, loginUrl string, err error)

func (*Consumer) MakeHttpClient

func (c *Consumer) MakeHttpClient(token *AccessToken) (*http.Client, error)

func (*Consumer) MakeRoundTripper

func (c *Consumer) MakeRoundTripper(token *AccessToken) (*RoundTripper, error)

func (*Consumer) Post

func (c *Consumer) Post(url string, userParams map[string]string, token *AccessToken) (resp *http.Response, err error)

** DEPRECATED ** Please call "Post" on the http client returned by MakeHttpClient instead

func (*Consumer) PostForm

func (c *Consumer) PostForm(url string, userParams map[string]string, token *AccessToken) (resp *http.Response, err error)

** DEPRECATED ** Please call "Post" on the http client returned by MakeHttpClient instead

func (*Consumer) PostJson

func (c *Consumer) PostJson(url string, body string, token *AccessToken) (resp *http.Response, err error)

** DEPRECATED ** Please call "Do" on the http client returned by MakeHttpClient instead (and set the "Content-Type" header explicitly in the http.Request)

func (*Consumer) PostMultipart

func (c *Consumer) PostMultipart(url, multipartName string, multipartData io.ReadCloser, userParams map[string]string, token *AccessToken) (resp *http.Response, err error)

** DEPRECATED ** Please call "Do" on the http client returned by MakeHttpClient instead (and setup the multipart data explicitly in the http.Request)

func (*Consumer) PostWithBody

func (c *Consumer) PostWithBody(url string, body string, userParams map[string]string, token *AccessToken) (resp *http.Response, err error)

** DEPRECATED ** Please call "Post" on the http client returned by MakeHttpClient instead

func (*Consumer) PostXML

func (c *Consumer) PostXML(url string, body string, token *AccessToken) (resp *http.Response, err error)

** DEPRECATED ** Please call "Do" on the http client returned by MakeHttpClient instead (and set the "Content-Type" header explicitly in the http.Request)

func (*Consumer) Put

func (c *Consumer) Put(url string, body string, userParams map[string]string, token *AccessToken) (resp *http.Response, err error)

** DEPRECATED ** Please call "Put" on the http client returned by MakeHttpClient instead

func (*Consumer) RefreshToken

func (c *Consumer) RefreshToken(accessToken *AccessToken) (atoken *AccessToken, err error)

Use the service provider to refresh the AccessToken for a given session. Note that this is only supported for service providers that manage an authorization session (e.g. Yahoo).

Most providers do not return the SESSION_HANDLE_PARAM needed to refresh the token.

See http://oauth.googlecode.com/svn/spec/ext/session/1.0/drafts/1/spec.html for more information.

- accessToken:
  The AccessToken returned from AuthorizeToken()

It will return:

- atoken:
  An AccessToken which can be used to access the user's data (until it is
  revoked by the user or the service provider).

- err:
  Set if accessToken does not contain the SESSION_HANDLE_PARAM needed to
  refresh the token, or if an error occurred when making the request.

type ConsumerGetter

type ConsumerGetter func(key string, header map[string]string) (*Consumer, error)

type DataLocation

type DataLocation int
const (
	LOC_BODY DataLocation = iota + 1
	LOC_URL
	LOC_MULTIPART
	LOC_JSON
	LOC_XML
)

type HMACSigner

type HMACSigner struct {
	// contains filtered or unexported fields
}

func (*HMACSigner) Debug

func (s *HMACSigner) Debug(enabled bool)

func (*HMACSigner) HashFunc

func (s *HMACSigner) HashFunc() crypto.Hash

func (*HMACSigner) Sign

func (s *HMACSigner) Sign(message string, tokenSecret string) (string, error)

func (*HMACSigner) SignatureMethod

func (s *HMACSigner) SignatureMethod() string

func (*HMACSigner) Verify

func (s *HMACSigner) Verify(message string, signature string) error

type HTTPExecuteError

type HTTPExecuteError struct {
	// RequestHeaders provides a stringified listing of request headers.
	RequestHeaders string
	// ResponseBodyBytes is the response read into a byte slice.
	ResponseBodyBytes []byte
	// Status is the status code string response.
	Status string
	// StatusCode is the parsed status code.
	StatusCode int
}

HTTPExecuteError signals that a call to httpExecute failed.

func (HTTPExecuteError) Error

func (e HTTPExecuteError) Error() string

Error provides a printable string description of an HTTPExecuteError.

type HttpClient

type HttpClient interface {
	Do(req *http.Request) (resp *http.Response, err error)
}

type OrderedParams

type OrderedParams struct {
	// contains filtered or unexported fields
}

func NewOrderedParams

func NewOrderedParams() *OrderedParams

func (*OrderedParams) Add

func (o *OrderedParams) Add(key, value string)

func (*OrderedParams) AddUnescaped

func (o *OrderedParams) AddUnescaped(key, value string)

func (*OrderedParams) Clone

func (o *OrderedParams) Clone() *OrderedParams

func (*OrderedParams) Get

func (o *OrderedParams) Get(key string) []string

func (*OrderedParams) Keys

func (o *OrderedParams) Keys() []string

func (*OrderedParams) Len

func (o *OrderedParams) Len() int

func (*OrderedParams) Less

func (o *OrderedParams) Less(i int, j int) bool

func (*OrderedParams) Swap

func (o *OrderedParams) Swap(i int, j int)

type Provider

type Provider struct {
	ConsumerGetter ConsumerGetter
	// contains filtered or unexported fields
}

Provider provides methods for a 2-legged Oauth1 provider

func NewProvider

func NewProvider(secretGetter ConsumerGetter) *Provider

NewProvider takes a function to get the consumer secret from a datastore. Returns a Provider

func (*Provider) IsAuthorized

func (provider *Provider) IsAuthorized(request *http.Request) (*string, error)

IsAuthorized takes an *http.Request and returns a pointer to a string containing the consumer key, or nil if not authorized

type RSASigner

type RSASigner struct {
	// contains filtered or unexported fields
}

func (*RSASigner) Debug

func (s *RSASigner) Debug(enabled bool)

func (*RSASigner) HashFunc

func (s *RSASigner) HashFunc() crypto.Hash

func (*RSASigner) Sign

func (s *RSASigner) Sign(message string, tokenSecret string) (string, error)

func (*RSASigner) SignatureMethod

func (s *RSASigner) SignatureMethod() string

func (*RSASigner) Verify

func (s *RSASigner) Verify(message string, base64signature string) error

type RequestToken

type RequestToken struct {
	Token  string
	Secret string
}

TODO(mrjones) Do we definitely want separate "Request" and "Access" token classes? They're identical structurally, but used for different purposes.

type RoundTripper

type RoundTripper struct {
	// contains filtered or unexported fields
}

func (*RoundTripper) RoundTrip

func (rt *RoundTripper) RoundTrip(userRequest *http.Request) (*http.Response, error)

type ServiceProvider

type ServiceProvider struct {
	RequestTokenUrl   string
	AuthorizeTokenUrl string
	AccessTokenUrl    string
	HttpMethod        string
	BodyHash          bool
	IgnoreTimestamp   bool

	// Enables non spec-compliant behavior:
	// Allow parameters to be passed in the query string rather
	// than the body.
	// See https://github.com/mrjones/oauth/pull/63
	SignQueryParams bool
}

Information about how to contact the service provider (see #1 above). You usually find all of these URLs by reading the documentation for the service that you're trying to connect to. Some common examples are:

(1) Google, standard APIs:
    http://code.google.com/apis/accounts/docs/OAuth_ref.html
    - RequestTokenUrl:   https://www.google.com/accounts/OAuthGetRequestToken
    - AuthorizeTokenUrl: https://www.google.com/accounts/OAuthAuthorizeToken
    - AccessTokenUrl:    https://www.google.com/accounts/OAuthGetAccessToken
    Note: Some Google APIs (for example, Google Latitude) use different values for
    one or more of those URLs.
(2) Twitter API:
    http://dev.twitter.com/pages/auth
    - RequestTokenUrl:   http://api.twitter.com/oauth/request_token
    - AuthorizeTokenUrl: https://api.twitter.com/oauth/authorize
    - AccessTokenUrl:    https://api.twitter.com/oauth/access_token
(3) NetFlix API:
    http://developer.netflix.com/docs/Security
    - RequestTokenUrl:   http://api.netflix.com/oauth/request_token
    - AuthroizeTokenUrl: https://api-user.netflix.com/oauth/login
    - AccessTokenUrl:    http://api.netflix.com/oauth/access_token

Set HttpMethod if the service provider requires a different HTTP method to be used for OAuth token requests

Directories

Path Synopsis
examples
jira
Note: I haven't had a chance to test that this works.
Note: I haven't had a chance to test that this works.
latitude
THIS NO LONGER WORKS!! Latitude is using OAuth 2.0 now.
THIS NO LONGER WORKS!! Latitude is using OAuth 2.0 now.
netflix
NOTE: Netflix shut down its API in 2014.
NOTE: Netflix shut down its API in 2014.
trello
Check Trello OAuth Detail At: https://trello.com/docs/gettingstarted/oauth.html
Check Trello OAuth Detail At: https://trello.com/docs/gettingstarted/oauth.html
trelloserver
Check Trello OAuth Detail At: https://trello.com/docs/gettingstarted/oauth.html
Check Trello OAuth Detail At: https://trello.com/docs/gettingstarted/oauth.html
twitterserver
Similar to the twitter example, but using an HTTP server instead of the command line.
Similar to the twitter example, but using an HTTP server instead of the command line.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL