<commit_before>// tweetlib - A fully oauth-authenticated Go Twitter library
//
// Copyright 2011 The Tweetlib Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tweetlib

import (
	"bytes"
	"crypto/hmac"
	"encoding/base64"
	"fmt"
	"http"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"
	"url"
	"io"
	"io/ioutil"
)

const (
	tokenRequestURL = "https://api.twitter.com/oauth/request_token" // request token endpoint
	authURL         = "https://api.twitter.com/oauth/authorize"     // user authorization endpoint
	accessTokenURL  = "https://api.twitter.com/oauth/access_token"  // access token endpoint
)

type Config struct {
	ConsumerKey    string
	ConsumerSecret string
	Callback       string
}

type Token struct {
	OAuthSecret string
	OAuthToken  string
}
type TempToken struct {
	Token  string
	Secret string
}

func (tt *TempToken) AuthURL() string {
	return fmt.Sprintf("%s?oauth_token=%s", authURL, tt.Token)
}

func (t *Transport) nonce() string {
	s := time.Nanoseconds()
	return strconv.Itoa64(s)
}

func (c *Config) callback() string {
	if c.Callback != "" {
		return c.Callback
	}
	return "oob"
}

type Transport struct {
	*Config
	*Token

	// Transport is the HTTP transport to use when making requests.
	// It will default to http.DefaultTransport if nil.
	// (It should never be an oauth.Transport.)
	Transport http.RoundTripper
}

// Client returns an *http.Client that makes OAuth-authenticated requests.
func (t *Transport) Client() *http.Client {
	return &http.Client{Transport: t}
}

func (t *Transport) transport() http.RoundTripper {
	if t.Transport != nil {
		return t.Transport
	}
	return http.DefaultTransport
}

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, os.Error) {
	if t.Config == nil {
		return nil, os.NewError("no Config supplied")
	}
	if t.Token == nil {
		return nil, os.NewError("no Token supplied")
	}

	// Refresh the Token if it has expired.
	//if t.Expired() {
	//	if err := t.Refresh(); err != nil {
	//		return nil, err
	//	}
	//}
	// Make the HTTP request.
	t.sign(req)
	return t.transport().RoundTrip(req)
}

// Twitter requires that all authenticated requests be
// signed with HMAC-SHA1
//
// https://dev.twitter.com/docs/auth/oauth
//
// The base string is a special combination
// of parameters:
//
//      httpMethod + "&" +
//      url_encode(  base_uri ) + "&" +
//      sorted_query_params.each  { | k, v |
//          url_encode ( k ) + "%3D" +
//          url_encode ( v )
//      }.join("%26")
//
// And then you sign this with HMAC-SHA1 with the key:
//
//    consumer_secret&oauth_token_secret
//
func (t *Transport) sign(req *http.Request) os.Error {
	u, _ := url.ParseQuery(req.URL.RawQuery) //"status": {"testing..."}}
	//u := url.Values{"status": {"testing..."}}
	u.Set("oauth_signature_method", "HMAC-SHA1")
	u.Set("oauth_timestamp", strconv.Itoa64(time.Seconds()))
	u.Set("oauth_nonce", t.nonce())
	u.Set("oauth_version", "1.0")
	u.Set("oauth_consumer_key", t.ConsumerKey)
	if t.OAuthToken != "" {
		u.Set("oauth_token", t.OAuthToken)
	}

	// url-encode and sort all parameters as required by twitter
	var pairs, oauthPairs []string
	for k, v := range u {
		pairs = append(pairs, t.percentEncode(k)+"="+t.percentEncode((v[0])))
		if len(k) > 4 && k[:5] == "oauth" {
			oauthPairs = append(oauthPairs, t.percentEncode((k))+"=\""+t.percentEncode((v[0]))+"\"")
		}
	}
	sort.Strings(pairs)

	// create the base string
	parameters := strings.Join(pairs, "&")
	urlForBase := strings.Split(req.URL.String(), "?")[0]
	if req.Method == "POST" {
		req.URL, _ = url.Parse(urlForBase)
	}
	base := req.Method + "&" +
		t.percentEncode(urlForBase) + "&" + t.percentEncode((parameters))
	fmt.Printf("base: %s\n", base)
	// sign the base string with the consumer secret and aouth token string
	signature := t.createSignature(base)

	// Create the Authentication header
	authHeader := "OAuth " + strings.Join(oauthPairs, ", ")
	authHeader += ", oauth_signature=\"" + t.percentEncode((signature)) + "\""
	fmt.Printf("Signature: %s\n", authHeader)
	req.Header.Set("Authorization", authHeader)
	return nil
}

func (t *Transport) createSignature(base string) string {
	key := t.percentEncode((t.ConsumerSecret)) + "&" + t.percentEncode((t.OAuthSecret))
	hash := hmac.NewSHA1([]byte(key))
	hash.Write([]byte(base))
	var sha1Hash bytes.Buffer
	encoder := base64.NewEncoder(base64.StdEncoding, &sha1Hash)
	encoder.Write(hash.Sum())
	encoder.Close()
	return sha1Hash.String()
}

func (t *Transport) AccessToken(tempToken *TempToken, oauthVerifier string) os.Error {

	u := &url.Values{"oauth_token": {tempToken.Token},
		"oauth_verifier": {oauthVerifier}}
	var body io.Reader
	body = bytes.NewBuffer([]byte(""))
	urls := fmt.Sprintf("%s?%s", accessTokenURL, u.Encode())
	req, err := http.NewRequest("POST", urls, body)
	if err != nil {
		return err
	}
	resp, err := t.RoundTrip(req)
	if err != nil {
		return err
	}
	//if resp.StatusCode != 200 {
	//	return os.NewError("Authentication Error")
	//}
	defer resp.Body.Close()

	var respBody []byte
	respBody, _ = ioutil.ReadAll(resp.Body)
	fmt.Printf("Body: %s\n", respBody)
	data, err := url.ParseQuery(string(respBody))
	if err != nil {
		return err
	}
	t.OAuthToken = data.Get("oauth_token")
	t.OAuthSecret = data.Get("oauth_token_secret")
	return nil
}
<commit_msg>Removing debugging printfs<commit_after>// tweetlib - A fully oauth-authenticated Go Twitter library
//
// Copyright 2011 The Tweetlib Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tweetlib

import (
	"bytes"
	"crypto/hmac"
	"encoding/base64"
	"fmt"
	"http"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"
	"url"
	"io"
	"io/ioutil"
)

const (
	tokenRequestURL = "https://api.twitter.com/oauth/request_token" // request token endpoint
	authURL         = "https://api.twitter.com/oauth/authorize"     // user authorization endpoint
	accessTokenURL  = "https://api.twitter.com/oauth/access_token"  // access token endpoint
)

type Config struct {
	ConsumerKey    string
	ConsumerSecret string
	Callback       string
}

type Token struct {
	OAuthSecret string
	OAuthToken  string
}
type TempToken struct {
	Token  string
	Secret string
}

func (tt *TempToken) AuthURL() string {
	return fmt.Sprintf("%s?oauth_token=%s", authURL, tt.Token)
}

func (t *Transport) nonce() string {
	s := time.Nanoseconds()
	return strconv.Itoa64(s)
}

func (c *Config) callback() string {
	if c.Callback != "" {
		return c.Callback
	}
	return "oob"
}

type Transport struct {
	*Config
	*Token

	// Transport is the HTTP transport to use when making requests.
	// It will default to http.DefaultTransport if nil.
	// (It should never be an oauth.Transport.)
	Transport http.RoundTripper
}

// Client returns an *http.Client that makes OAuth-authenticated requests.
func (t *Transport) Client() *http.Client {
	return &http.Client{Transport: t}
}

func (t *Transport) transport() http.RoundTripper {
	if t.Transport != nil {
		return t.Transport
	}
	return http.DefaultTransport
}

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, os.Error) {
	if t.Config == nil {
		return nil, os.NewError("no Config supplied")
	}
	if t.Token == nil {
		return nil, os.NewError("no Token supplied")
	}

	// Refresh the Token if it has expired.
	//if t.Expired() {
	//	if err := t.Refresh(); err != nil {
	//		return nil, err
	//	}
	//}
	// Make the HTTP request.
	t.sign(req)
	return t.transport().RoundTrip(req)
}

// Twitter requires that all authenticated requests be
// signed with HMAC-SHA1
//
// https://dev.twitter.com/docs/auth/oauth
//
// The base string is a special combination
// of parameters:
//
//      httpMethod + "&" +
//      url_encode(  base_uri ) + "&" +
//      sorted_query_params.each  { | k, v |
//          url_encode ( k ) + "%3D" +
//          url_encode ( v )
//      }.join("%26")
//
// And then you sign this with HMAC-SHA1 with the key:
//
//    consumer_secret&oauth_token_secret
//
func (t *Transport) sign(req *http.Request) os.Error {
	u, _ := url.ParseQuery(req.URL.RawQuery) //"status": {"testing..."}}
	u.Set("oauth_signature_method", "HMAC-SHA1")
	u.Set("oauth_timestamp", strconv.Itoa64(time.Seconds()))
	u.Set("oauth_nonce", t.nonce())
	u.Set("oauth_version", "1.0")
	u.Set("oauth_consumer_key", t.ConsumerKey)
	if t.OAuthToken != "" {
		u.Set("oauth_token", t.OAuthToken)
	}

	// url-encode and sort all parameters as required by twitter
	var pairs, oauthPairs []string
	for k, v := range u {
		pairs = append(pairs, t.percentEncode(k)+"="+t.percentEncode((v[0])))
		if len(k) > 4 && k[:5] == "oauth" {
			oauthPairs = append(oauthPairs, t.percentEncode((k))+"=\""+t.percentEncode((v[0]))+"\"")
		}
	}
	sort.Strings(pairs)

	// create the base string
	parameters := strings.Join(pairs, "&")
	urlForBase := strings.Split(req.URL.String(), "?")[0]
	if req.Method == "POST" {
		req.URL, _ = url.Parse(urlForBase)
	}
	base := req.Method + "&" +
		t.percentEncode(urlForBase) + "&" + t.percentEncode((parameters))
	// sign the base string with the consumer secret and aouth token string
	signature := t.createSignature(base)

	// Create the Authentication header
	authHeader := "OAuth " + strings.Join(oauthPairs, ", ")
	authHeader += ", oauth_signature=\"" + t.percentEncode((signature)) + "\""
	req.Header.Set("Authorization", authHeader)
	return nil
}

func (t *Transport) createSignature(base string) string {
	key := t.percentEncode((t.ConsumerSecret)) + "&" + t.percentEncode((t.OAuthSecret))
	hash := hmac.NewSHA1([]byte(key))
	hash.Write([]byte(base))
	var sha1Hash bytes.Buffer
	encoder := base64.NewEncoder(base64.StdEncoding, &sha1Hash)
	encoder.Write(hash.Sum())
	encoder.Close()
	return sha1Hash.String()
}

func (t *Transport) AccessToken(tempToken *TempToken, oauthVerifier string) os.Error {

	u := &url.Values{"oauth_token": {tempToken.Token},
		"oauth_verifier": {oauthVerifier}}
	var body io.Reader
	body = bytes.NewBuffer([]byte(""))
	urls := fmt.Sprintf("%s?%s", accessTokenURL, u.Encode())
	req, err := http.NewRequest("POST", urls, body)
	if err != nil {
		return err
	}
	resp, err := t.RoundTrip(req)
	if err != nil {
		return err
	}
	//if resp.StatusCode != 200 {
	//	return os.NewError("Authentication Error")
	//}
	defer resp.Body.Close()

	var respBody []byte
	respBody, _ = ioutil.ReadAll(resp.Body)
	data, err := url.ParseQuery(string(respBody))
	if err != nil {
		return err
	}
	t.OAuthToken = data.Get("oauth_token")
	t.OAuthSecret = data.Get("oauth_token_secret")
	return nil
}

{'fraction_non_alphanumeric': 0.16060462919225318, 'fraction_numerical': 0.006424185167690127, 'mean_word_length': 3.6923758865248226, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '460425', 'n_tokens_mistral': 3852, 'n_tokens_neox': 3761, 'n_words': 926}