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

Improve the Auth generated by amber g auth #1322

Open
crimson-knight opened this issue Jun 13, 2023 · 2 comments
Open

Improve the Auth generated by amber g auth #1322

crimson-knight opened this issue Jun 13, 2023 · 2 comments

Comments

@crimson-knight
Copy link
Member

The current authentication provided by the generator is too basic. The current auth uses the user_id as the session ID. Let's improve the generator to do the following:

  • If a "user" model already exists, append the method information to that model instead of overwriting it
  • If a "user" model already exists, make the generated migration an "alter table" instead of "create table" migration
  • Add two new columns: session_random_token with type VARCHAR & session_expiration_date with type TIMESTAMP
  • Make the session_token = used_id + session_random_token
  • Change the sessions controller to set the session to use session_token and set the token into an in-memory data store
  • Change the auth pipe from using a DB query to check for the existing user_id, check the in-memory data store for the session token that is provided.

The in-memory data store is probably best represented as a Hash(String, User)

{
  "kjlh01nv081304813408934": User
}

The session_token is going to require some parsing in order to do a database lookup in the event that the session token is not found. The idea here is that the session_token should be "#{session_random_token}:#{random_string}" where random_string is a randomly generated 64 character long string of mixed uppercase, lowercase and numbers.

@robacarp
Copy link
Member

robacarp commented Jun 14, 2023

Good changes. You'll certainly want to make the relationship between Users and Session tokens a "has many" however you'd like to go about that. I don't really think there's a need to make the session token anything but a secure random string.

@crimson-knight
Copy link
Member Author

@robacarp that's a good point of refinement, I was shooting for a single session allowed at a time, but that doesn't make sense even for my own use case, ha!

Since I'm building a mobile app, I want multiple sessions up to the device limit. So that's definitely a has_many kind of relationship.

So far I have the single token & single session working and using an in-memory cache that is managed by the Amber::Server class, but it's a basic Hash(String, User) setup. Which will probably get too inefficient at volume. because the keys are not sorted at first.

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

No branches or pull requests

2 participants