00:02
Now, currently the token that we have been working with thus far actually has unfettered access to all of our API operations.
00:12
If we were to dig back into our organization access tokens panel, this right here is the one that we're working with, which has permissions to read, create, update, and delete.
00:21
However, as we can see below, we have the ability to create just tokens that can read, just tokens that can create, so on and so forth.
00:28
And one thing we've yet to account for inside of our API is ensuring that the token that is making the request actually has the permission to perform the operation,
00:38
AKA authorizing the actual request. Now, in our setup stage, we actually created an ability to authorize our token.
00:47
And within here, we have read, create, update, and delete, which checks to see whether or not the current access token for the request allows that particular action.
00:56
If it doesn't, then we throw a forbidden exception, preventing the operation from taking place. So what we need to do is implement this inside of our API controllers.
01:05
And we actually have something similar implemented inside of some of our web-based controllers as well. So if we dig into the organizations controller, directly nested within our controllers directory,
01:15
so the main organizations controller, and scroll on down, we should see that we have an update method within here that gets the user's role within the organization,
01:24
and then checks to see whether or not that role can perform an edit operation, and then forbids the operation if it cannot, prior to the operation actually taking place.
01:34
So that's the same premise that we need to take with our API, the only difference is, we'll use the API token to determine whether or not the operation is authorized rather than the role of the user.
01:44
Since we're working at the organization level within our API rather than the user level. So let's go ahead and dig into our organizations controller first, because what this does is reads.
01:53
So we need to authorize reading from this endpoint. So let's go ahead and give ourselves a couple of new lines. And since our authorized token action
02:01
directly throws a forbidden exception within here, we don't wanna do an if check, we just merely want to call the appropriate action to determine whether or not it's authorized.
02:10
And if it's not, then we'll throw an exception, which will be caught by our exception handler, and will prevent further operations within our controller from actually executing.
02:19
So all's we need to do is authorize token and import that action, call the read method, since we're trying to read within this controller, and pass our organization in.
02:28
That will ensure that the organization token that we're working with has the ability to read from our API. And if it does, it will continue onward and populate our difficulty statuses
02:38
and access levels onto our organization, then kick back the organization itself. And if it doesn't, as we saw, we'll get that forbidden exception. Now, we don't wanna perform this authorization check inside of our middleware
02:48
that's actually querying the organization, because that organization is also needed, as we saw, for our update, create, and delete operations as well.
02:56
So we do need it if the token is able to do any one of those, so we're not gonna prevent that with our read here. We'll specifically prevent it just within the controller handlers.
03:06
So we can give that a save, and let's continue on through before we test any of these, and populate all of the others. So we also have our difficulties controller, which has various operations.
03:15
So we'll just walk through it and do the exact same thing. So we'll authorize token, and this one is reading, and pass it in the organization. Then we have our store.
03:24
So we'll authorize token dot create, and pass the organization in there as well. Let's give ourselves a line break for that one. Then we have show, which again is going to read.
03:34
So authorize token, read organization, and update. So authorize token, and we should have an update one. There we go.
03:43
Organization, and give ourselves a line break. And then finally we have delete. So authorize token dot delete, and pass the organization in there as well. Cool.
03:52
I'm going to go ahead and just drill through the access levels and status controllers for this as well. So authorization token read, organization,
04:01
give that a copy, paste it in the show, door, authorization token create, organization, key. Have our updates just like so,
04:09
and delete, authorization token delete, organization, and good. Then finally our statuses. So let's scroll on up to the top again. And I believe I still have the read copied.
04:19
So we'll get that. Command dot to import that, paste that into show as well. And then we have authorization token create again,
04:27
the organization, and update. There we go. And finally delete.
04:33
So authorization token delete, organization, and good. Perfect. So now that we have all of the authorization checks added into all of our API controllers,
04:43
let's go ahead and dig back into our browser and let's create a new development read, development create, development update, development delete token.
04:51
So we'll add an access token, development read. I'm just going to go ahead and give development there a copy, let's create that one. Oh, right, nevermind. We need to copy our token as well.
05:01
Jump back into Hopscotch and let's go into our environments, our development environments, and over to our secrets. And let's add a new one for token read
05:11
and then paste that secret value in. Let's see, I think we also appended bearer onto that as well. So let's make sure that we have bearer appended onto that too. Okay, before we go ahead and save that,
05:21
let's run through all of the others as well. So let's dig back into our browser. We have our read one, do update or do create next,
05:30
untick read and create gets a tick there. Let's create that one, copy the token, back over to Hopscotch, add another token,
05:38
underscore create bearer and paste in the token. Let me go ahead and show it so that I can confirm that I got that right. All right, then we need updates.
05:47
So untick create, tick update, create that, copy it, back over to Hopscotch, token, underscore updates, bearer, space, paste. Let's show it to make sure I got that right again.
05:57
Okay, close that. And finally, we need one for deleting. So development, delete, and tick delete there, copy our token.
06:05
And one more time, token, underscore, delete, bearer, space, and paste the token in. Okay, perfect. Let's give all of those a save and let's test them out.
06:15
So let's jump back over into our collections. Now, remember, we set up the accept and authorization headers within our API collection and it cascades through to all of these sub collections within there.
06:25
However, a nice thing that we can do is override that by adding the header directly into the header list for the specific request.
06:33
So for example, if we were to add an authorization into this one, the authorization header goes ahead and disappears from this particular request
06:43
from our API collection. Meaning that we can now dig into the value and reach for say our token and let's do read, which certainly does not have the ability to delete.
06:52
Test this out, send that off. And sure enough, we get a forbidden exception. The provided token does not have the permission to delete in this organization.
07:01
So that's working perfectly fine. And then if we were to go ahead and switch that back to the one that does have specific abilities to delete, which is our token delete variable, send that off.
07:11
We're gonna get a row not found, which is a different error, but it means that our request was indeed authorized. Of course, we're getting the 404 there because we've already deleted the ID of 11.
07:20
So awesome, everything's working good for our delete. We can go ahead and remove that authorization token and the one from our API collection comes back. Let's dig back into our status collections.
07:30
We've tested our destroy. Let's walk through the full CRUD flow here just to make sure everything with these is working okay. So we'll add an authorization header in here.
07:40
Let's reach for our token. This is update that we're on. So let's do maybe create, there we go. Should again get a forbidden exception, which we do.
07:50
And then let's jump back over to here and do updates. We've already got our body payload in there. So that should send off a okay, and it does, perfect. Let's get rid of that header.
07:59
Jump now to our get a specific status. So this one is the read. So let's add in our authorization header and then token underscore,
08:09
maybe let's try delete for this one. If we send that off, we get a forbidden exception as expected. If we switch that to read now, send that off,
08:17
we get back our record and a 200 response. Awesome, remove that token yet again and jump now over to our create, which is our store.
08:25
And again, add in the authorization header, token, let's try update for this one. This one only has the ability to update. So it should not be able to create,
08:34
send that off and there's our forbidden exception again, switch that back to create, send that off and we get back a brand new record with an ID of 13.
08:43
Awesome, lastly, we have our getting a list. So this one is again, a get. This one we did not, looks like we still already have a specific token within here.
08:53
So let's just go ahead and add create onto that token, send it off, there's our forbidden exception
09:00
and let's now go ahead and test just the one that can read, send that off and perfect, we get back our list of statuses. We can go ahead and remove the token from that one
09:10
and the one from the collection will pop up there. Cool, so now we have authorization out of the way and we're done with our organization's resources, which is our difficulty statuses and access levels,
09:19
which gives us play to now move on to more interesting things like our courses, modules and lessons.