which will be inherited by all controllers that sets currentuser accordingly so

Which will be inherited by all controllers that sets

This preview shows page 183 - 185 out of 517 pages.

(which will be inherited by all controllers) that sets @current_user accordingly, so that controller methods or views can just look at @current_user without being coupled to the details of how the user was authenticated. The third aspect is linking our own representation of a user’s identity—that is, her primary key in the moviegoers table—with the auth provider’s representation, such as the uid in the case of Twitter. Since we may want to expand which auth providers our customers can use in the future, the migration in Figure 5.9 (a) that creates the Moviegoer model specifies both a uid field and a provider field. What happens the very first time Alice logs into RottenPotatoes with her Twitter ID? The find_by_provider_and_uid query in line 6 of the sessions controller (Figure 5.10 (b)) will return nil , so Moviegoer.create_with_omniauth (Figure 5.9 (b), lines 5–10) will be called to create a new record for this user. Note that “Alice as authenticated by Twitter” would therefore be a different user from our point of view than “Alice as authenticated by Facebook,” because we have no way of knowing that those represent the same person. That’s why some sites that support multiple third-party auth providers give users a way to “link” two accounts to indicate that they identify the same person. This may seem like a lot of moving parts, but compared to accomplishing the same task without an abstraction such as OmniAuth, this is very clean code: we added fewer than two dozen lines, and by incorporating more OmniAuth strategies, we could support additional third-party auth providers with essentially no new work. Screencast 5.2.1 shows the user experience associated with this code.
Image of page 183
Screencast 5.2.1: Logging into RottenPotatoes with Twitter This version of RottenPotatoes, modified to use the OmniAuth gem as described in the text, allows moviegoers to login using their existing Twitter IDs. However, we have just created a security vulnerability. So far we’ve exploited the convenience of “mass assignment” from the params[] hash to an ActiveRecord object, as when we write @movie.update_attributes(params[:movie]) in MoviesController#update . But what if a malicious attacker crafts a form submission that tries to modify params[:moviegoer][:uid] or params[:moviegoer][:provider] —fields that should only be modified by the authentication logic— by posting hidden form fields named params[moviegoer][uid] and so on? The attr_accessible command in line 3 of Figure 5.9 (b) is what allows us to mass-assign specific attributes. If we wanted to “protect” sensitive attributes from being mass-assigned from params we could use attr_protected . The more restrictive attr_accessible arranges for only the named attributes to be modifiable through mass assignment from params[] (or for that matter from any hash). This more restrictive mechanism is recommended because it follows the principle of least privilege in computer security, a topic to which we return in Section 12.9 when discussing how to defend customer data.
Image of page 184
Image of page 185

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors