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

Strip Bearer From Token #5

Merged
merged 4 commits into from
Jan 9, 2019
Merged

Strip Bearer From Token #5

merged 4 commits into from
Jan 9, 2019

Conversation

dpbackes
Copy link
Contributor

@dpbackes dpbackes commented Jan 7, 2019

  • Some libraries use the "Bearer" prefix when doing token
    authentication. If we encounter a token with a "Bearer"
    prefix then just strip it off and proceed as normal.

@dpbackes
Copy link
Contributor Author

dpbackes commented Jan 7, 2019

The C# library I'm using does this and it'll be easier to deal with it here rather than doing the stripping in all our services. cc @tylerodonnell @eesmith from our discussion earlier today.

@dpbackes dpbackes force-pushed the StripBearer branch 2 times, most recently from a63aadd to c0aff33 Compare January 7, 2019 22:15
index.js Outdated
@@ -26,6 +26,9 @@ const chooseKey = key =>

const decode = partialRight(jwt.decode, [{ complete: true }])

const stripBearer = token =>
token ? token.replace(/^Bearer /, '') : null
Copy link
Member

Choose a reason for hiding this comment

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

I think we should also strip the lowercase version. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't see any reason not to

@tylerodonnell
Copy link
Member

So most of our services are already stripping bearer from the token - but I like having the option of Authentic stripping "Bearer" if it exists.

- Some libraries use the "Bearer" prefix when doing token
  authentication. If we encounter a token with a "Bearer"
  prefix then just strip it off and proceed as normal.
@flintinatux
Copy link
Collaborator

👀

Copy link
Collaborator

@flintinatux flintinatux left a comment

Choose a reason for hiding this comment

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

See comments below.

One general comment on workflow: please no more squashing and force-pushing. Just make separate commits for your changes. Force-pushing is problematic for others wishing to checkout locally and contribute.

When we merge it, we'll select the Squash and Merge option, which will rollup the whole PR into a single commit for us. It'll keep the commit history nice and pretty.

index.js Outdated
@@ -26,6 +26,9 @@ const chooseKey = key =>

const decode = partialRight(jwt.decode, [{ complete: true }])

const stripBearer = token =>
token ? token.replace(/^[B|b]earer /, '') : null
Copy link
Collaborator

Choose a reason for hiding this comment

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

The idiomatic way to do this in js is with the case-insensitive i flag. Also, Ramda has a point-free replace function:

const stripBearer =
  replace(/^Bearer /i, '')

The null-check is best left to the existing enforce function. I'll make a separate comment explaining that.

package.json Outdated
@@ -1,6 +1,6 @@
{
"name": "@articulate/authentic",
"version": "0.1.2",
"version": "0.1.3",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bumping the version isn't done until after the updates have been merged into #master. After the PR is merged, a new version is created and published with the following flow:

> yarn version  # bumps the version and creates a matching git tag
> git push --tags origin master
> npm publish --access=public

Just move this back to 0.1.2 for now.

index.js Outdated

const authentic = token =>
Promise.resolve(token)
.then(tapP(enforce))
.then(stripBearer)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The need to strip and null-check in multiple places is a 👃 that suggests we organize things a little better with some composition:

// use unauthorized in enforce
const enforce = token =>
  token || Promise.reject(unauthorized('null token not allowed'))

const stripBearer = // <<-- move here to maintain alphebetic order
  replace(/^Bearer /i, '')

const unauthorized = err =>
  Promise.reject(Boom.wrap(err, 401))

const factory = opts => {
  // omiting other bits here...

  // remove stripBearer from verify
  const verify = curryN(2, partialRight(promisify(jwt.verify), [ verifyOpts ]))

  const authentic = token =>
    Promise.resolve(token)
      // remove tapP(enforce) here
      // remove stripBearer
      .then(decode)
      .then(tapP(checkIss))
      .then(getSigningKey)
      .then(chooseKey)
      .then(verify(token))
      .catch(unauthorized)

  // composeP to null-check and strip all in one place
  return composeP(authentic, stripBearer, tapP(enforce))
}

@dpbackes
Copy link
Contributor Author

dpbackes commented Jan 8, 2019

@flintinatux updated. Rambda doesn't support catches in composeP so I just did a normal promise chain.

@flintinatux
Copy link
Collaborator

@dpbackes, that's why I suggested swapping the new Error in enforce with unauthorized, so that the error would be correctly formatted without catching, and the .catch could be left in the authentic function.

@flintinatux
Copy link
Collaborator

Hm, I'm noticing now that it should be Boom.unauthorized in enforce, since the custom unauthorized function wraps a native Error object.

@flintinatux
Copy link
Collaborator

squirrel

@flintinatux flintinatux merged commit ec63514 into master Jan 9, 2019
@flintinatux flintinatux deleted the StripBearer branch January 9, 2019 14:16
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

Successfully merging this pull request may close these issues.

3 participants