4 ms·
Interesting idea. Some notes / comments / questions on the posted code (in no particular order): * setting up a separate routing namespace for the API would cl
by al2o3cr 13y ago
Interesting idea. Some notes / comments / questions on the posted code (in no particular order):
* setting up a separate routing namespace for the API would clean up a number of things. In particular, it would make the web-only actions much clearer; calling respond_with with a JSON error payload and then relying on it to render the template (as done in OnionsController#index, for instance) is terribly confusing on a first read.
* does logging in actually set session[:UserKey]? It doesn't appear to, but that would make returning users unable to decrypt their existing onions.
* I really, really hope that isn't your actual session secret in config/initializers/secret_token.rb. More info on why this is bad and how to fix it here: http://blog.phusion.nl/2013/01/04/securing-the-rails-session-secret/ http://blog.phusion.nl/2013/01/04/securing-the-rails-session...
* The encryption code in OnionsController would be better suited in the Onion model, especially since the decryption code is already there.
* the API for adding onions (OnionsController#addOnion) does not encrypt the data before storing it. Is this intentional?
* every action that modifies onions creates a new session. From the way that new_session works, I'm guessing this is to provide "session expiration" that's reset every time a user changes something. Would updating the timestamp on the user's existing Session accomplish the same thing, or have I missed something?
Some more code-review oriented notes:
* Some of your class methods in the models would be clearer as instance methods - for instance, both account_exists and pass_is_good take the hashed username as an argument and find the corresponding Account object. pass_is_good could be an instance method on Account. As another example, the code inside the loop in decrypted_onions_with_key could be an Onion instance method (telling an Onion object to decrypt itself vs. doing it externally).
* the comment character in Ruby is #, not surrounding things in / symbols (which creates a Regexp literal). In most cases (config/routes.rb, for instance) this happens to not cause problems - but it's still not a good habit to get into.
* some style notes: none of these affect the operation of your program, but they will affect how efficiently other developers read your code and work with it.
First, pick a tab setting in your editor of choice and stick with it. The "Ruby standard" is 2 spaces, but if you prefer something else at least be consistent.
Second, check out some of the Ruby style guides regarding naming. CapitalizedCamelCase is makes for somewhat odd-looking database column names.
- bennyg 13y agoThanks for all the input, Ruby is basically the equivalent of ESL to me. I'm an Objective-C programmer by nature (self-taught at that, I have a Bachelor's in Graphic Design as it stands). These are all really good points, as I get better in this language. Logging in does generate and set the UserKey. I'm making a rolling, resetting SessionKey, so as not to keep one session alive forever. Every action makes a new one (also hence the primary key resetting that happens when it approaches the limit of a 32bit signed int). Also, regarding tabs, I just can't get a damn editor to actually do these correctly. I've been using Sublime and then switched to RubyMine recently, but both are awfully annoying. Clicking in a new line doesn't automatically start the cursor at where the next tab should be, it starts it exactly in the line where I clicked - making me have to hit return again to get a new line in the appropriate place. Sometimes I just go on because I'm trying to put the idea down, resulting in shitty looking code. The only IDE I've seen that does this right is Xcode, and I really wish RubyMine could do it, because it's also been very nice so far. But poor UI in IDEs is neither here nor there for this project. Thanks again for the input.