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

Add token support #738

Merged
merged 27 commits into from Jan 25, 2021
Merged

Add token support #738

merged 27 commits into from Jan 25, 2021

Conversation

DerMolly
Copy link
Member

Component/Part

private api

Description

This PR adds the private api and the token routes.

This can be merged after #733 was merged.

Steps

@codecov
Copy link

codecov bot commented Jan 17, 2021

Codecov Report

Merging #738 (2c38bd5) into develop (b586b9f) will decrease coverage by 0.33%.
The diff coverage is 82.24%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #738      +/-   ##
===========================================
- Coverage    82.30%   81.96%   -0.34%     
===========================================
  Files           58       65       +7     
  Lines         1000     1181     +181     
  Branches        57       74      +17     
===========================================
+ Hits           823      968     +145     
- Misses         175      211      +36     
  Partials         2        2              
Flag Coverage Δ
e2e-tests 72.26% <50.46%> (-5.40%) ⬇️
integration-tests 58.51% <77.38%> (+3.89%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/revisions/authorship.entity.ts 100.00% <ø> (ø)
src/users/identity.entity.ts 100.00% <ø> (ø)
src/users/users.module.ts 100.00% <ø> (ø)
src/errors/errors.ts 60.00% <50.00%> (-6.67%) ⬇️
src/auth/token.strategy.ts 58.33% <58.33%> (ø)
src/api/private/tokens/tokens.controller.ts 66.66% <66.66%> (ø)
src/api/public/me/me.controller.ts 71.42% <75.00%> (+2.19%) ⬆️
src/auth/auth.service.ts 75.25% <75.25%> (ø)
src/api/private/private-api.module.ts 100.00% <100.00%> (ø)
src/api/public/media/media.controller.ts 73.33% <100.00%> (+1.90%) ⬆️
... and 22 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b586b9f...2c38bd5. Read the comment docs.

Copy link
Member

@InnayTool InnayTool left a comment

Choose a reason for hiding this comment

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

This PR adds some database fields, we have to update the db-schema.plantuml.

src/api/private/tokens/tokens.controller.ts Outdated Show resolved Hide resolved
@DerMolly
Copy link
Member Author

This PR adds some database fields, we have to update the db-schema.plantuml.

This PR is still a Draft 😉

@InnayTool
Copy link
Member

We should hash the auth token in database because it's working like a password.

@InnayTool
Copy link
Member

InnayTool commented Jan 17, 2021

This PR adds some database fields, we have to update the db-schema.plantuml.

This PR is still a Draft wink

yes I only want to mention it because it can easiely be forgotten.

@DerMolly DerMolly force-pushed the private/tokens branch 2 times, most recently from 1ffef32 to 1c6dd14 Compare January 17, 2021 13:52
docs/content/dev/db-schema.plantuml Outdated Show resolved Hide resolved
src/users/users.service.ts Outdated Show resolved Hide resolved
src/users/users.service.ts Outdated Show resolved Hide resolved
src/users/users.service.ts Outdated Show resolved Hide resolved
src/users/users.service.ts Outdated Show resolved Hide resolved
src/users/users.service.ts Outdated Show resolved Hide resolved
InnayTool
InnayTool previously approved these changes Jan 17, 2021
Copy link
Contributor

@SISheogorath SISheogorath left a comment

Choose a reason for hiding this comment

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

A minor and a major remark that we should get sorted.

src/users/users.service.ts Outdated Show resolved Hide resolved
src/users/auth-token.entity.ts Outdated Show resolved Hide resolved
@DerMolly DerMolly self-assigned this Jan 19, 2021
@DerMolly DerMolly changed the base branch from develop to config/split January 19, 2021 10:10
Base automatically changed from config/split to develop January 19, 2021 11:58
@davidmehren
Copy link
Member

I feel using a JWT is a bit overcomplicated here.
Why not:

  • generate some random bytes
  • create a string using base64url -> the secret
  • hash the string using bcrypt2 and save to the database
  • hand the token to the user as base64url(username).secret

When we get a token from the user, we can then get the username and only check the database for tokens that correspond to the user.

The only downside of this approach is that check-time scales linearly with the amount of API tokens a user has, but we could just limit that to a reasonable number (20?).

@ErikMichelson
Copy link
Member

ErikMichelson commented Jan 20, 2021

Inspired by @davidmehren's idea: What about having a key-secret pair?

I could imagine the following process:

  • generate two random strings using base64url
  • one of the strings will be inserted into the database directly as keyid, the other one will be hashed and inserted into the database as corresponding secret
  • the token consists of KEYID.SECRET

With that approach you won't have a linear scale on the process to find the user for a token.

@DerMolly
Copy link
Member Author

If we use the id of the token anyway (for deletion purposes), we could just use that instead of a new second random string.

@SISheogorath
Copy link
Contributor

Let's not invent an own session management. The OWASP has some quite clear guidelines: https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html

We should also see if there might be a library which just takes care of that, as it appears to be a quite common use case.

@DerMolly
Copy link
Member Author

Let's not invent an own session management. The OWASP has some quite clear guidelines: cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html

Your linking to something talking about sessions, when we don't implement any sessions (in this PR). I'm a bit confused what we should take from that post?

@InnayTool InnayTool changed the title private: adds tokens controller Add token support Jan 21, 2021
@DerMolly
Copy link
Member Author

If we use the id of the token anyway (for deletion purposes), we could just use that instead of a new second random string.

A point against this made by @InnayTool is that an incrementing id would tell every user exactly how many tokens were at one point issued on the instance. An information we may want to not disclose.

@ErikMichelson
Copy link
Member

To make it clear:
I had the following data-structure in my mind:

  • id (autogenerated, may be counted up, is always non-public, needs to be unique)
  • user-id (relation to users table)
  • keyid (A, needs to be unique)
  • secret (hash of B)

The token is made from A.B.
A and B are both each a base64url version of random bytes.

An API user authenticates via the A.B token. The database lookup for the user of the token is O(1) as the keyid can be matched directly to the user.

This doesn't involve any session logic. It's just about the tokens for the API. A user has a list of their API keyids to delete them any time. After generation a user can not reclaim the secret as it's only stored hashed.

In some terms this would be similar to GitHub's PATs.

adds identifier and createdAt to AuthToken
renames authToken in User to authTokens

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Philip Molares <philip.molares@udo.edu>
this seems very unnecessary as the chance of this is 1 / 2^512

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Philip Molares <philip.molares@udo.edu>
adds auth service
adds auth module
adds token-auth strategy
adds token-auth to all public api calls

Signed-off-by: Philip Molares <philip.molares@udo.edu>
See:
https://docs.nestjs.com/openapi/security
Signed-off-by: Philip Molares <philip.molares@udo.edu>
adds MockAuthGuard which always return user 'hardcoded'

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Fix token deletion
Update plantuml docs
Add token validUntil and lastUsed fields

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Move AuthTokens to auth folder

Signed-off-by: Philip Molares <philip.molares@udo.edu>
add test for BufferToBase64Url and toAuthTokenDto

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Add number type alias TimestampMillis
Remove solved ToDos
Change AuthToken and AuthTokenDto to use Date
Rename authService unit tests

Signed-off-by: Philip Molares <philip.molares@udo.edu>
As suggested by @InnayTool

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Rename AuthToken.identifier to label

Signed-off-by: Philip Molares <philip.molares@udo.edu>
adds auth service
adds auth module
adds token-auth strategy
adds token-auth to all public api calls

Signed-off-by: Philip Molares <philip.molares@udo.edu>
adds MockAuthGuard which always return user 'hardcoded'

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Fix token deletion
Update plantuml docs
Add token validUntil and lastUsed fields

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This is a very high ceiling unlikely to hinder legitimate usage, but should prevent possible attack vectors

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This should prevent problem with the AuthToken purge on Sundays, as the service is either running on sunday or will be restarted there after.

Also move base64url comment to right function

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This should prevent problem with the AuthToken purge on Sundays, as the service is either running on sunday or will be restarted there after.

Also move base64url comment to right function

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Philip Molares <philip.molares@udo.edu>
@InnayTool InnayTool merged commit ca04856 into develop Jan 25, 2021
@InnayTool InnayTool deleted the private/tokens branch January 25, 2021 20:38
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.

None yet

5 participants