Skip to content

Instantly share code, notes, and snippets.

@JCNapier
Last active January 28, 2022 18:28
Show Gist options
  • Select an option

  • Save JCNapier/3c3fdec849fef4e3d50b5d322d4eb541 to your computer and use it in GitHub Desktop.

Select an option

Save JCNapier/3c3fdec849fef4e3d50b5d322d4eb541 to your computer and use it in GitHub Desktop.

Mod 3 Prework Notes

You need to store a user in a database table; what do we need to know about them? (ie, email, password)

  • To store a user in a databse we would need to know first and last name, email, passoword, and maybe a phone number.
  • Depening on the application using the database it might require a mailing address, age, or other important attributes.

How can we store a password in a secure way? (ie, if our database is compromised, how can we protect their passwords from prying eyes)

  • One way is by encrypting stored passwords with a nonreversible cryptographic function so an attacker could not solve the encryprion logic(if it were reversible).
  • The use of salt, which generates two random hashes for a password, and increases the chances of the generated hash being unique to itslef. Salt forces password breaking software to restart everytime (for every password) a code is broken.
  • Pepper, Hashes, and Rainbow tables are other forms of password protection.

How would we store the idea of what a user is allowed to do on a web site?

  • I think we can do this by storing client data through the use of cookies, and caches.

How would we build a safe login page?

  • Through a series of checks and balances with Authenticaiton through the JSON respones.
  • Validate username
  • Validate password
  • Encode all output.
  • Keep password obscured/doens't show on the page. Maintain password security
  • Don't be too specific in your error messages, so attackers aren't able to sort out what is correct/incorrect.
  • Hash and/or Salt your passwords. Combination of Password protection tools.

How could we tell if a user is logged in since HTTP is “stateless”?

  • I think you could tell if a user is logged in by requiring pages to check the login status from the response on every page as a conditional before loading the page.

How could we allow a user to “stay logged in for 7 days” even if your Rails app is restarted?

  • I think you would have to store the user data somehow, and have a way to reference it later.
  • Through the use of cookies, and encrypting what data is stored in that cookie. Store the cookie itself for a time that has a maximum limit, and during that time frame if the user logs in again the cookie will automatically be loaded, and pass the encrypted log in information through all of the same checks and balances of the login process to make sure it matches up with the login credentials in the database. If it does match then it would automatically open/load the application again-otherwise it would have the user sign in again.

Screen Shot 2022-01-27 at 1 35 09 PM

Screen Shot 2022-01-27 at 2 06 58 PM\

Screen Shot 2022-01-27 at 1 57 04 PM

Rails Params Magic

  • Two kinds of params are possible in a web application:
  1. String Query Params which are passed in the URL after the "?" (Query Tag)
  2. POST Data which is passed in through the HTML and has been passed in by the user. Can only be used with an HTTP POST request.

Rails does not make any distinction between the two and both are available in the params hash in your controller.

  • The params hash is noot limited to single keys and values. It can also contain nested hashes and arrays as the values. Rails will automatically decode the HTML encoded array brackets using modulos, and 5b/5d.
  • Symbols and Strings are interchangeable.
  • JSON Parameters can also be accepted. You Can wrap paramters to omit the outer hash layer and access the params hash a normal in the controller.
  • Using private params allows us to return a 400 Bad Request Error if not all of the attributes are included in the params. This also allows us to sanitize data for each individual user checking for permissable attributes in the params.
  • This method can also be considered "Strong Params".
  • We can use permit, as a means to check for the scalar value of a params key. We can check if params[:id] is a value type like a hash or an array by using params.permit(id: [])
  • We can also permit nested params keys, when there are nested params being passed in.
  • We have the ability to permit entire param hash objects that are stored or nested with permit! however we need to take caution when using this because it permits everything and can permit arbitrary information, if not damaging information to be permitted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment