Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request body empty after reading it #9

Closed
4 tasks done
kaischwanke opened this issue Sep 21, 2023 · 1 comment
Closed
4 tasks done

Request body empty after reading it #9

kaischwanke opened this issue Sep 21, 2023 · 1 comment

Comments

@kaischwanke
Copy link
Contributor

kaischwanke commented Sep 21, 2023

Bug Report Checklist

  • Have you provided a code sample to reproduce the issue?
  • Have you tested with the latest release to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?

Description
Reading the request body in the sign function leaves it empty

To Reproduce
Test to illustrate the behaviour:

func TestBodyRestore(t *testing.T) {
	originalBody := "{\"foo\": \"bar\"}"
	getRequest, _ := http.NewRequest("GET", "", strings.NewReader(originalBody))
	bodyHash := getSignedBodyHash(getRequest)
	bodyHash2 := getSignedBodyHash(getRequest)
	if bodyHash != bodyHash2 {
		t.Errorf("Body hashes sould be the same")
	}
}

func getSignedBodyHash(r *http.Request) string {
	signer := &oauth.Signer{ConsumerKey: consumerKey, SigningKey: signingKey}
	_ = signer.Sign(r)
	authHeader := r.Header["Authorization"][0]
	return extractBodyHash(authHeader)
}

func extractBodyHash(input string) string {
	regexPattern := `oauth_body_hash="([^"]+)"`
	re := regexp.MustCompile(regexPattern)

	match := re.FindStringSubmatch(input)
	if len(match) == 0 {
		panic("value not found")
	}
	return match[1]
}

Expected behavior
The request body should remain intact after reading it

Additional context
The request body in Go is a ReadCloser interface. Once you read the content, the buffer will be empty.
I had forgotten about that behaviour in my previous PR

Related issues/PRs
PR #10 created

@kaischwanke kaischwanke changed the title [BUG] Description Request body empty after reading it Sep 21, 2023
@danny-gallagher
Copy link
Contributor

Resolved in #10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants