YouTubevideo· Jun 13, 2024· 8:19
so I just published a video on the official Lille Channel talking about authentication versus authorization how do you authorize your users to actually view or create or edit things that you want them to do while disallowing them to do the things that you don't want them to do and one of the things that I forgot to touch on in that video is the difference within a multi-tenant app where you're using a query to gather
information from the user let's say they have to-dos that are associated with that particular user is there a good reason to use authorization in addition to that and I think yes so let's check it out okay so I have an application with a bunch of different to-dos created by multiple users I I seeded this within
my database Cedar and this basically just shows that when you are logged in I want to only view the to-dos that I have created I don't want to see anyone else's because let's imagine you have more sensitive information maybe you know it's it's Hippa information or maybe it's specific to your application
on you don't want that information to be seen by multiple different users and that's typical within most applications you only want their data to be seen so how do you stop that from being leaked across your application as you can see here I can see all the to-dos created across the whole application because we haven't actually narrowed this down just yet but I was also I would also be able to delete other people's to and that
isn't necessarily a good thing if I was the great one hello there you can see um it's down here of course you can see how this would be a great way of now I just want to see ones created by me or maybe if you were using
teams within an application you want to see all the ones created by your your family your household your team your organization well how do we make this happen so that we aren't breaking any security rules okay the first thing that we need to change is actually calling the to-dos and and showing the to-dos that are associated with a given person you can see here in our user model we have this public function to-do so we can call the to-dos of an authenticated
user like this why don't we do that so in our to-do manager in our with function instead of just returning all the to-dos which is what we're doing right here why don't we go ahead and say maybe off um user and then we'll go ahead and just call the to-dos and why don't we just do that as well so we're sorting them by created ads so let's see what this does so now we just have the to-dos created by us great that's awesome but we shouldn't stop there when
we're building our applications with this in mind because what happens if for example I in another part of the application I forget to add this information to link it only to this particular authenticated user well as
you can see if that was uh you know if that was just to do all we're able to still see that information we're able to still delete that information of course just with the query we can automatically Rectify this because now it's a little bit harder to delete it to do if you can't see it but within
Livewire you're able to modify any of these public methods so I could pass an integer of the ID to find this to-do and then delete it and I'm not actually authorized or able to delete that even though I can't see it so we could run
that method on the back end and say hey delete this even if I'm not actually seeing that to do in the first place so how can we fix this this delete method let's go ahead and inspect this and take a look you can see here that we're calling this delete do 87 but if I was to modify this because
this is all client side if I was to modify this and say let's delete to do um let's delete to do f live and before I click that button let me go and pull open database viewer so here we go we have to-dos here and we have a to-do of five this is the second one you can see here it's not associated with my user because I'm not seeing this so this idea five hello there but now that I modified
this let me go ahead and click delete oh what happened let's see let me go refresh this and now we don't have ID number five you can see how this could potentially be a big security concern for example if we had a view page for this to do and we wanted to say um maybe it was just in the URL of maybe it's to-dos SL 34 for example well I could just
automatically put in a two and then I might if I'm putting all that information in the query and saying only grab the to-do if the user is associated with it well that's good what if I forget that well this is where policies come into play so in our to-do policy what we can do is on that view we could say only return that if the user is
associated to that to do but what about the delete one well I'm glad you're asked so delete let's do the same thing return user ID if the user is associated to that to-do so now if I was to go into our to-do manager again it's pretty simple in delete Todo I can go ahead and
say authorize delete Todo so this authorize is a method that's being called in our Livewire class function that just says okay we have the to-do is this user in this case this authenticated user authorized to run that delete method for that to do why
don't we go back and when we go ahead and edit this again and we'll say instead delete to do 87 we'll delete to do six okay let's run delete and now this action isn't authorized that too is not associated with us again we can kind of
double time this and we can say we're going to make this extra secure because just like we're doing right here we only want to show the user the to-dos that they're able to view now we could add extra security measures in that policy that they can only view the single parameter the single to-do that's associated with them we do the same thing in this delete too instead of to do find we could say maybe off user Tod
do find or fail ID so that way even if we were to remove this let's see what happens if we were to go back in here we'll refresh and then in our delete to do instead of 87 we'll say six again and delete now it's just four or four not found that's not really helpful for the user because if they're trying to do something you know malicious perhaps they're saying that they there is a security concern because I'm not unauthorized to do that but this is
where we can kind of double down again we can add multiple security measures with policies to say not only are we going to ensure that they're only deleting or even viewing things that they're associated with I also want to make sure that they're actually allowed to do that too and it's again it's just a nice fail safe if I forget get to do this and I just say to do find ID then
we're still taken care of we're still good to go so hopefully that gives you some clarity into why authorization in the case of a policy is still important within Lille within Live Wire especially because you're able to modify some of those methods on the front end you want to make sure that the user is actually allowed to modify it it's a security concern but it's also just a double measure to make sure you're only giving your users the proper methods the proper
safety the proper ability to do what you actually want them to do keep building keep creating \[Music\]