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

fix: request verification example code in comments not being usable #15

Merged
merged 5 commits into from
Nov 5, 2020

Conversation

VaguelySerious
Copy link

@VaguelySerious VaguelySerious commented Nov 4, 2020

  • Fix error from missing number casting of timestamp header
  • Fix code in comments/documentation not being usable due to wrong imports
  • Some beauty changes

src/requests/sign-request.ts Outdated Show resolved Hide resolved
* const {pick} = require('lodash')
* const {server} = require('./imaginary-server')
*
* const SECRET = process.env.SECRET
*
* server.post('/api/my-resources', (req, res) => {
* const incomingSignature = req.headers['x-contentful-signature']
* const incomingTimestamp = req.headers['x-contentful-timestamp']
* const incomingTimestamp = Number(req.headers['x-contentful-timestamp'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really depends on the imaginary framework, I don't it's a mistake. If we want to enforce type, then we should use Number.parseInt

Copy link
Author

@VaguelySerious VaguelySerious Nov 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headers are always strings, and this code will, as far as I can see, fail 100% of the time, because we pass incomingTimestamp to TimestampValidator internally which will throw if it is a string.
I will use Number.parseInt, that's fine

return headers
}

export const pickHeaders = (headers: Record<string, string>, keys: string[]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headers are generally speaking not mandatory in our model. This should stay optional for consistency

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These if statements are unused, even if the user passes no headers, because we already validate it beforehand and pass an empty object here

src/requests/sign-request.ts Outdated Show resolved Hide resolved
src/index.ts Outdated
@@ -1,3 +1,3 @@
export { getManagementToken } from './keys'
export { signRequest, verifyRequest, ContentfulHeader } from './requests'
export { Secret, CanonicalRequest, Timestamp, SignedRequestHeaders } from './requests/typings'
export { CanonicalRequest, SignedRequestHeaders } from './requests/typings'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus, this should be moved into the ./requests/index.ts and exported from there

Copy link
Author

@VaguelySerious VaguelySerious Nov 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secret and Timestamp are just string and number types, and they are not otherwise relevant to the user, so it make little sense to export them.
I agree that the other types should be exported through requests/index

@shikaan
Copy link
Contributor

shikaan commented Nov 4, 2020

Forgot to mention, the commit type should be docs as we're not fixing anything and we're not providing features. We cannot mess up with commit types, since we have automated releases

@VaguelySerious
Copy link
Author

Forgot to mention, the commit type should be docs as we're not fixing anything and we're not providing features. We cannot mess up with commit types, since we have automated releases

How do I change the commit type after the fact? Force push? And since we are exporting more types, a fix version seems appropriate

@VaguelySerious VaguelySerious merged commit 4cef437 into master Nov 5, 2020
@VaguelySerious VaguelySerious deleted the fix/request-verification-doc branch November 5, 2020 09:36
@ghost
Copy link

ghost commented Nov 5, 2020

🎉 This PR is included in version 1.3.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ghost ghost added the released label Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants