This episode is for members only

Sign up to access "Build An Uptime Monitor with Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
29. Authorizing the endpoint details

Episodes

0%
Your progress
  • Total: 4h 59m
  • Played: 0m
  • Remaining: 4h 59m
Join or sign in to track your progress

Transcript

00:00
A really quick fix before we finish up on our endpoint index just here, this one, we didn't authorize this request. Now we know that over on our endpoint policy, we have the ability to update, destroy or check against this. We want to add one in here to say
00:18
can we view this or show this. So I'm going to go ahead and call this show, I think that makes sense, or list or whatever you want to call it. You could even create a generic touch to see if a user just has permission in general. So let's call this show and from the endpoint that we get passed in, of course the check is exactly the same here, we just want to make sure that the user has access
00:41
to this via the site that they own. So now that that's in there, we can go ahead and authorize this. Now of course you can authorize this directly within the controller, so we could say can we show this endpoint and that would work as normal. So we see that we've got permission to do this and if the id differs then it's not going to work. But again across our app we've been using
01:03
form requests, so I always like to keep things as consistent as possible and even if we're not posting through here, we can still create a form request to at least authorize this. So let's go ahead and do that now. Let's create out a form request here and keep the naming convention the same. If we look at the form request that we've got already, it's endpoint destroy controller,
01:22
endpoint store controller update. So we're gonna name this, if we just go ahead and make out a request here, endpoint and we could say endpoint index request. I think that actually makes a little bit more sense because technically this is an index, but then again we're showing this via root model binding. So yeah let's keep this to show, endpoint show controller request. I think
01:48
that makes a lot more sense. Okay so we can swap this out straight away. Endpoint show request and we're done. So we can go into here and the rules obviously we don't care about because we're just getting this, but from here we can do exactly the same thing. So this user can show this endpoint. So now everything is nicely tucked away in a form request. Our controller here is nice and clean.
02:12
I'll get rid of the eager loading note because we're going to come back to that later and there we go. So if we come over, sure enough it still works. So a quick fix up there. Of course that's really important because otherwise anyone would be able to type in any endpoint id and see information about that, which is not good.

Episode summary

In this episode, we notice a missing authorization check on our endpoint's index (or "show") action. This is a pretty important security detail—without it, anyone could access the details of any endpoint just by guessing or supplying an ID!

So, we quickly remedy this by adding a new method to our Endpoint Policy, specifically for checking if a user can 'show' or view an endpoint. The logic stays similar to our other policy methods: making sure the current user owns the resource they're trying to access.

To keep our codebase consistent, we wrap this check in a form request, even though this is a GET operation. We create a new EndpointShowRequest for authorization, swap it into the controller, and keep everything nice and tidy. As a result, our controller stays clean, all our authorization logic is centralized, and we remain consistent with our previous approach for other actions like update and destroy.

At the end, we test things out and confirm it's all working correctly! With this fix in place, our app's endpoint details are locked down and only visible to the right users.

Episode discussion

No comments, yet. Be the first!