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

Create express middleware function to verify API keys #163

Closed
mwcz opened this issue Feb 12, 2020 · 0 comments · Fixed by #218
Closed

Create express middleware function to verify API keys #163

mwcz opened this issue Feb 12, 2020 · 0 comments · Fixed by #218
Assignees
Labels
api SPAship API

Comments

@mwcz
Copy link
Contributor

mwcz commented Feb 12, 2020

Create an express middleware function that verifies API keys, and apply that function to every express endpoint that needs auth.

Note, https://github.com/spaship/spaship/blob/master/packages/sync-service/lib/db.apikey.js already provides functions for creating, storing, and verifying API keys. This issue is asking for an express middleware function that uses db.apikey.js to enforce API key auth on certain endpoints. The function getUserByKey is the best one to use for validating that an incoming API key is valid.

It should work something like this:

  1. HTTP request comes in
  2. If no Authorization header, return 401
  3. If Authorization header exists and is of the form Authorization: APIKey MY_API_KEY then get the value of MY_API_KEY and pass it into db.apikey.getUserByKey("MY_API_KEY") to determine if it's a valid key. Proceed to step 6.
  4. If Authorization header exists and is of the form Authorization: Bearer MY_TOKEN then get the value of MY_TOKEN and validate it with a JWT validation library.
    1. If the token is valid, take the sub property (we treat this property as a UUID for users) and pass it into db.apikey.getKeysByUser(sub) and proceed to step 6.
  5. If Authorization header exists but is not of the form Authorization: APIKey MY_KEY or Authorization: Bearer MY_TOKEN, then return a 403
  6. If the function returns a non-empty array, allow the request to proceed (by passing through to the next middleware function). If it returns an empty array, return a 403.

Todo: determine which endpoints need auth.

@mwcz mwcz added the help wanted Extra attention is needed label Feb 12, 2020
@mwcz mwcz removed the help wanted Extra attention is needed label Feb 18, 2020
@npatil9 npatil9 added this to Development In progress in MVP Mar 5, 2020
@mwcz mwcz assigned mwcz and unassigned sayak-sarkar Mar 9, 2020
@npatil9 npatil9 added the api SPAship API label Mar 17, 2020
@npatil9 npatil9 removed this from Development In progress in MVP Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api SPAship API
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants