Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ func (c *ApiClient) buildBaseRequest(method, endpoint string, body io.Reader, co
return nil, xurlErrors.NewHTTPError(err)
}

// Send the query string in the encoding the OAuth 1.0a signature covers. X
// rejects a signed request whose query carries a raw reserved character,
// such as the colon in query=from:XDevelopers.
if req.URL.RawQuery != "" {
req.URL.RawQuery = strings.ReplaceAll(req.URL.Query().Encode(), "+", "%20")
}

// Add headers
for _, header := range headers {
parts := strings.SplitN(header, ":", 2)
Expand All @@ -320,7 +327,7 @@ func (c *ApiClient) buildBaseRequest(method, endpoint string, body io.Reader, co
// opts into unauthenticated requests (allowUnauthenticated, set only by
// library/test constructors), where we proceed and let the server decide.
if req.Header.Get("Authorization") == "" {
authHeader, err := c.getAuthHeader(httpMethod, url, authType, username)
authHeader, err := c.getAuthHeader(httpMethod, req.URL.String(), authType, username)
if err != nil {
if !c.allowUnauthenticated {
return nil, err
Expand Down
8 changes: 8 additions & 0 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ func TestBuildRequest(t *testing.T) {
wantURL: "https://api.x.com/2/tweets",
wantErr: false,
},
{
name: "Query is sent percent-encoded as it is signed",
method: "GET",
endpoint: "/2/tweets/search/recent?query=from:XDevelopers+-is:retweet&max_results=10",
authType: "app",
wantMethod: "GET",
wantURL: "https://api.x.com/2/tweets/search/recent?max_results=10&query=from%3AXDevelopers%20-is%3Aretweet",
},
{
name: "Absolute URL",
method: "GET",
Expand Down
4 changes: 3 additions & 1 deletion auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,10 @@ func generateTimestamp() string {
return fmt.Sprintf("%d", time.Now().Unix())
}

// encode percent-encodes s as OAuth 1.0a requires (RFC 5849, section 3.6):
// every byte except the unreserved characters, with a space as %20, not +.
func encode(s string) string {
return url.QueryEscape(s)
return strings.ReplaceAll(url.QueryEscape(s), "+", "%20")
}

func generateCodeVerifierAndChallenge() (string, string, error) {
Expand Down
24 changes: 23 additions & 1 deletion auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func TestEncode(t *testing.T) {
expected string
}{
{"abc", "abc"},
{"a b c", "a+b+c"},
{"a b c", "a%20b%20c"},
{"from:XDevelopers", "from%3AXDevelopers"},
{"a+b+c", "a%2Bb%2Bc"},
{"a/b/c", "a%2Fb%2Fc"},
{"a?b=c", "a%3Fb%3Dc"},
Expand All @@ -139,6 +140,27 @@ func TestEncode(t *testing.T) {
}
}

// TestGenerateSignature checks the worked example in X's "Creating a
// signature" guide, whose status contains spaces.
func TestGenerateSignature(t *testing.T) {
params := map[string]string{
"status": "Hello Ladies + Gentlemen, a signed OAuth request!",
"include_entities": "true",
"oauth_consumer_key": "xvz1evFS4wEEPTGEFPHBog",
"oauth_nonce": "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg",
"oauth_signature_method": "HMAC-SHA1",
"oauth_timestamp": "1318622958",
"oauth_token": "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb",
"oauth_version": "1.0",
}

signature, err := generateSignature("POST", "https://api.twitter.com/1.1/statuses/update.json", params,
"kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw", "LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE")

require.NoError(t, err)
assert.Equal(t, "hCtSmYh+iHYCEqBWrE7C7hYmtUk=", signature)
}

func TestGenerateCodeVerifierAndChallenge(t *testing.T) {
verifier, challenge, err := generateCodeVerifierAndChallenge()

Expand Down