Feature/6207 Endpoint api/v1/sign_in now outputs randomized tokens in response upon sign-in#6216
Conversation
|
looks good to me so far 🔥 I think instead of storing the token and refresh token as plaintext in the db, we can store it as a hash Then the client would be provided the plaintext versions of the tokens and whenever they call the api, the hash versions will be compared |
| if @user | ||
|
|
||
| @user.regenerate_session_token! | ||
| @user.regenerate_refresh_token! |
There was a problem hiding this comment.
It is actually for two separate tokens. The plan is the session_token expires quickly and is repeatedly replaced for as long as the refresh_token is valid to maintain access.
There was a problem hiding this comment.
Token regeneration is now handled in the blueprint before being passed to the session controller.
| # receive_email_notifications :boolean default(TRUE) | ||
| # receive_reimbursement_email :boolean default(FALSE) | ||
| # receive_sms_notifications :boolean default(FALSE), not null | ||
| # refresh_token :string |
There was a problem hiding this comment.
maybe make a new type of user, ApiUser?
There was a problem hiding this comment.
Looking into this, may complicate authorization. I'll just create a completely new table for api/v1 credentials so the user model and roles inheriting from it aren't overloaded.
Additionally, will attempt to do more compartmentalizing this PR through a concern :)
There was a problem hiding this comment.
Created the concern and imported into models/user.rb
| after_create :create_preference_set | ||
| before_update :record_previous_email | ||
| has_secure_token :token, length: 36 | ||
| has_secure_token :refresh_token, length: 36 |
There was a problem hiding this comment.
36 what? seconds, minutes, days, weeks?
There was a problem hiding this comment.
Vague syntax refers to token length, in this case. The token is 36 characters long.
…or greater separation of concerns (SoC)
…ctions (randomization)
…ement and test BaseController for signed-in user endpoints)
| before_save :generate_refresh_token | ||
|
|
||
| # Securely confirm/deny that Hash in db is same as current users token Hash | ||
| def authenticate_api_token(api_token) |
There was a problem hiding this comment.
does these functions return boolean value?
if so, the naming should be consistent with other similar functions starting with the is_....
There was a problem hiding this comment.
Yes, returns a boolean. How's Something like "is_api_token_valid"?
There was a problem hiding this comment.
yes that would be perfect
also follows ruby conventions too
| def change | ||
| create_table :api_credentials do |t| | ||
| t.references :user, null: false, foreign_key: true | ||
| t.string :api_token |
There was a problem hiding this comment.
you're also storing the plain text tokens in the table?
There was a problem hiding this comment.
nvm I think you created a migration to remove them right?
There was a problem hiding this comment.
Yeah, originally added 2 unneeded token columns then created a migration to remove them.
|
|
||
| # Put your task implementation HERE. | ||
| User.where(token: nil).each do |user| | ||
| User.find_each do |user| |
There was a problem hiding this comment.
is it possible to create a separate deployment task for populating the api tokens?
so we don't touch the sms stuff
There was a problem hiding this comment.
There was a problem hiding this comment.
I see I see
I forgot I originally created this
yea, just leave it then
What github issue is this PR for, if any?
Resolves #6207
What changed, and why?
Volunteers api/v1/sign_in endpoint now outputs secured randomized tokens upon sign-in.
tokencolumn fromuserstableapi_credentialstable to be used forusersmodel for greaterseparation of concerns(SoC)SHA-256hash to ensureraw tokensare never stored indb(api_token_digest&refresh_token_digest)models/api_credentialsandconcern/api.rbfor furtherapiconcern separation andutilityWhy?: To ensure compliance with apples review process, and enhance security with an applicable industry standard.
How is this tested? (please write tests!) 💖💪
New tests:
Authentication Helper Function tests (14 in total)→ spec/models/api_credential_spec.rbCredentials Factory Definition→ spec/factories/api_credential.rbUpdated tests ( no more token column ):
BaseController test→ spec/requests/api/v1/base_spec.rbSessionController test→ spec/requests/api/v1/users/sessions_spec.rbUsers Factory Definition→ spec/factories/users.rbDeployment & Documentation:
Deployment task→ lib/tasks/deployment/20230822145532_populate_api_tokens.rakeSwagger Documentation→ swagger/v1/swagger.yamlMore Swagger→ spec/swagger_helper.rbScreenshots please :)
Example output with updated api/v1/sign_in endpoint blueprint
Feelings gif (optional)